diff --git a/tagging/create_manifests.py b/tagging/create_manifests.py index 9963b20a..88da30b4 100755 --- a/tagging/create_manifests.py +++ b/tagging/create_manifests.py @@ -5,10 +5,11 @@ import argparse import datetime import logging import os +from typing import List from .docker_runner import DockerRunner from .get_taggers_and_manifests import get_taggers_and_manifests from .git_helper import GitHelper -from .manifests import ManifestHeader +from .manifests import ManifestHeader, ManifestInterface logger = logging.getLogger(__name__) @@ -18,7 +19,7 @@ BUILD_TIMESTAMP = datetime.datetime.utcnow().isoformat()[:-7] + "Z" MARKDOWN_LINE_BREAK = "
" -def append_build_history_line(short_image_name, owner, wiki_path, all_tags): +def append_build_history_line(short_image_name: str, owner: str, wiki_path: str, all_tags: List[str]) -> None: logger.info("Appending build history line") date_column = f"`{BUILD_TIMESTAMP}`" @@ -43,7 +44,13 @@ def append_build_history_line(short_image_name, owner, wiki_path, all_tags): f.write(file) -def create_manifest_file(short_image_name, owner, wiki_path, manifests, container): +def create_manifest_file( + short_image_name: str, + owner: str, + wiki_path: str, + manifests: List[ManifestInterface], + container +) -> None: manifest_names = [manifest.__name__ for manifest in manifests] logger.info(f"Using manifests: {manifest_names}") @@ -58,7 +65,7 @@ def create_manifest_file(short_image_name, owner, wiki_path, manifests, containe f.write(markdown_content) -def create_manifests(short_image_name, owner, wiki_path): +def create_manifests(short_image_name: str, owner: str, wiki_path: str) -> None: logger.info(f"Creating manifests for image: {short_image_name}") taggers, manifests = get_taggers_and_manifests(short_image_name) diff --git a/tagging/docker_runner.py b/tagging/docker_runner.py index bac940da..ce87df51 100644 --- a/tagging/docker_runner.py +++ b/tagging/docker_runner.py @@ -8,7 +8,7 @@ logger = logging.getLogger(__name__) class DockerRunner: - def __init__(self, image_name, docker_client=docker.from_env(), command="sleep infinity"): + def __init__(self, image_name: str, docker_client=docker.from_env(), command: str = "sleep infinity"): self.container = None self.image_name = image_name self.command = command @@ -29,7 +29,7 @@ class DockerRunner: logger.info(f"Container {self.container.name} removed") @staticmethod - def run_simple_command(container, cmd, print_result=True): + def run_simple_command(container, cmd: str, print_result: bool = True): logger.info(f"Running cmd: '{cmd}' on container: {container}") out = container.exec_run(cmd) assert out.exit_code == 0, f"Command: {cmd} failed" diff --git a/tagging/get_taggers_and_manifests.py b/tagging/get_taggers_and_manifests.py index 02eeb516..49c90160 100644 --- a/tagging/get_taggers_and_manifests.py +++ b/tagging/get_taggers_and_manifests.py @@ -1,11 +1,14 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +from typing import List, Tuple from .images_hierarchy import ALL_IMAGES +from .manifests import ManifestInterface +from .taggers import TaggerInterface -def get_taggers_and_manifests(short_image_name): - taggers = [] - manifests = [] +def get_taggers_and_manifests(short_image_name: str) -> Tuple[List[TaggerInterface], List[ManifestInterface]]: + taggers: List[TaggerInterface] = [] + manifests: List[ManifestInterface] = [] while short_image_name is not None: image_description = ALL_IMAGES[short_image_name] diff --git a/tagging/git_helper.py b/tagging/git_helper.py index ed1af56c..8ce8ff38 100755 --- a/tagging/git_helper.py +++ b/tagging/git_helper.py @@ -6,15 +6,15 @@ from plumbum.cmd import git class GitHelper: @staticmethod - def commit_hash(): + def commit_hash() -> str: return git["rev-parse", "HEAD"]().strip() @staticmethod - def commit_hash_tag(): + def commit_hash_tag() -> str: return GitHelper.commit_hash()[:12] @staticmethod - def commit_message(): + def commit_message() -> str: return git["log", -1, "--pretty=%B"]().strip() diff --git a/tagging/manifests.py b/tagging/manifests.py index 8bf898bc..c704dd01 100644 --- a/tagging/manifests.py +++ b/tagging/manifests.py @@ -20,7 +20,7 @@ def quoted_output(container, cmd: str) -> str: class ManifestHeader: """ManifestHeader doesn't fall under common interface and we run it separately""" @staticmethod - def create_header(short_image_name, owner, build_timestamp) -> str: + def create_header(short_image_name: str, owner: str, build_timestamp: str) -> str: commit_hash = GitHelper.commit_hash() commit_hash_tag = GitHelper.commit_hash_tag() commit_message = GitHelper.commit_message() diff --git a/tagging/tag_image.py b/tagging/tag_image.py index 0726deac..beb92d12 100755 --- a/tagging/tag_image.py +++ b/tagging/tag_image.py @@ -11,7 +11,7 @@ from .get_taggers_and_manifests import get_taggers_and_manifests logger = logging.getLogger(__name__) -def tag_image(short_image_name, owner): +def tag_image(short_image_name: str, owner: str) -> None: logger.info(f"Tagging image: {short_image_name}") taggers, _ = get_taggers_and_manifests(short_image_name) diff --git a/tagging/taggers.py b/tagging/taggers.py index c363cd1d..d2c99148 100644 --- a/tagging/taggers.py +++ b/tagging/taggers.py @@ -8,11 +8,11 @@ from .docker_runner import DockerRunner logger = logging.getLogger(__name__) -def _get_program_version(container, program): +def _get_program_version(container, program: str) -> str: return DockerRunner.run_simple_command(container, cmd=f"{program} --version") -def _get_env_variable(container, variable): +def _get_env_variable(container, variable: str) -> str: env = DockerRunner.run_simple_command( container, cmd="env", @@ -24,7 +24,7 @@ def _get_env_variable(container, variable): raise KeyError(variable) -def _get_pip_package_version(container, package): +def _get_pip_package_version(container, package: str) -> str: VERSION_PREFIX = "Version: " package_info = DockerRunner.run_simple_command( container, @@ -39,19 +39,19 @@ def _get_pip_package_version(container, package): class TaggerInterface: """Common interface for all taggers""" @staticmethod - def tag_value(container): + def tag_value(container) -> str: raise NotImplementedError class SHATagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return GitHelper.commit_hash_tag() class UbuntuVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: os_release = DockerRunner.run_simple_command(container, "cat /etc/os-release").split("\n") for line in os_release: if line.startswith("VERSION_ID"): @@ -60,59 +60,59 @@ class UbuntuVersionTagger(TaggerInterface): class PythonVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "python-" + _get_program_version(container, "python").split()[1] class JupyterNotebookVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "notebook-" + _get_program_version(container, "jupyter-notebook") class JupyterLabVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "lab-" + _get_program_version(container, "jupyter-lab") class JupyterHubVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "hub-" + _get_program_version(container, "jupyterhub") class RVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "r-" + _get_program_version(container, "R").split()[2] class TensorflowVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "tensorflow-" + _get_pip_package_version(container, "tensorflow") class JuliaVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "julia-" + _get_program_version(container, "julia").split()[2] class SparkVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "spark-" + _get_env_variable(container, "APACHE_SPARK_VERSION") class HadoopVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "hadoop-" + _get_env_variable(container, "HADOOP_VERSION") class JavaVersionTagger(TaggerInterface): @staticmethod - def tag_value(container): + def tag_value(container) -> str: return "java-" + _get_program_version(container, "java").split()[1]