Add some typing

This commit is contained in:
Ayaz Salikhov
2021-05-03 00:54:52 +03:00
parent 7d7bbba09f
commit 2172a9ab16
7 changed files with 40 additions and 30 deletions

View File

@@ -5,10 +5,11 @@ import argparse
import datetime import datetime
import logging import logging
import os import os
from typing import List
from .docker_runner import DockerRunner from .docker_runner import DockerRunner
from .get_taggers_and_manifests import get_taggers_and_manifests from .get_taggers_and_manifests import get_taggers_and_manifests
from .git_helper import GitHelper from .git_helper import GitHelper
from .manifests import ManifestHeader from .manifests import ManifestHeader, ManifestInterface
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -18,7 +19,7 @@ BUILD_TIMESTAMP = datetime.datetime.utcnow().isoformat()[:-7] + "Z"
MARKDOWN_LINE_BREAK = "<br />" MARKDOWN_LINE_BREAK = "<br />"
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") logger.info("Appending build history line")
date_column = f"`{BUILD_TIMESTAMP}`" 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) 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] manifest_names = [manifest.__name__ for manifest in manifests]
logger.info(f"Using manifests: {manifest_names}") 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) 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}") logger.info(f"Creating manifests for image: {short_image_name}")
taggers, manifests = get_taggers_and_manifests(short_image_name) taggers, manifests = get_taggers_and_manifests(short_image_name)

View File

@@ -8,7 +8,7 @@ logger = logging.getLogger(__name__)
class DockerRunner: 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.container = None
self.image_name = image_name self.image_name = image_name
self.command = command self.command = command
@@ -29,7 +29,7 @@ class DockerRunner:
logger.info(f"Container {self.container.name} removed") logger.info(f"Container {self.container.name} removed")
@staticmethod @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}") logger.info(f"Running cmd: '{cmd}' on container: {container}")
out = container.exec_run(cmd) out = container.exec_run(cmd)
assert out.exit_code == 0, f"Command: {cmd} failed" assert out.exit_code == 0, f"Command: {cmd} failed"

View File

@@ -1,11 +1,14 @@
# Copyright (c) Jupyter Development Team. # Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License. # Distributed under the terms of the Modified BSD License.
from typing import List, Tuple
from .images_hierarchy import ALL_IMAGES from .images_hierarchy import ALL_IMAGES
from .manifests import ManifestInterface
from .taggers import TaggerInterface
def get_taggers_and_manifests(short_image_name): def get_taggers_and_manifests(short_image_name: str) -> Tuple[List[TaggerInterface], List[ManifestInterface]]:
taggers = [] taggers: List[TaggerInterface] = []
manifests = [] manifests: List[ManifestInterface] = []
while short_image_name is not None: while short_image_name is not None:
image_description = ALL_IMAGES[short_image_name] image_description = ALL_IMAGES[short_image_name]

View File

@@ -6,15 +6,15 @@ from plumbum.cmd import git
class GitHelper: class GitHelper:
@staticmethod @staticmethod
def commit_hash(): def commit_hash() -> str:
return git["rev-parse", "HEAD"]().strip() return git["rev-parse", "HEAD"]().strip()
@staticmethod @staticmethod
def commit_hash_tag(): def commit_hash_tag() -> str:
return GitHelper.commit_hash()[:12] return GitHelper.commit_hash()[:12]
@staticmethod @staticmethod
def commit_message(): def commit_message() -> str:
return git["log", -1, "--pretty=%B"]().strip() return git["log", -1, "--pretty=%B"]().strip()

View File

@@ -20,7 +20,7 @@ def quoted_output(container, cmd: str) -> str:
class ManifestHeader: class ManifestHeader:
"""ManifestHeader doesn't fall under common interface and we run it separately""" """ManifestHeader doesn't fall under common interface and we run it separately"""
@staticmethod @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 = GitHelper.commit_hash()
commit_hash_tag = GitHelper.commit_hash_tag() commit_hash_tag = GitHelper.commit_hash_tag()
commit_message = GitHelper.commit_message() commit_message = GitHelper.commit_message()

View File

@@ -11,7 +11,7 @@ from .get_taggers_and_manifests import get_taggers_and_manifests
logger = logging.getLogger(__name__) 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}") logger.info(f"Tagging image: {short_image_name}")
taggers, _ = get_taggers_and_manifests(short_image_name) taggers, _ = get_taggers_and_manifests(short_image_name)

View File

@@ -8,11 +8,11 @@ from .docker_runner import DockerRunner
logger = logging.getLogger(__name__) 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") 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( env = DockerRunner.run_simple_command(
container, container,
cmd="env", cmd="env",
@@ -24,7 +24,7 @@ def _get_env_variable(container, variable):
raise KeyError(variable) raise KeyError(variable)
def _get_pip_package_version(container, package): def _get_pip_package_version(container, package: str) -> str:
VERSION_PREFIX = "Version: " VERSION_PREFIX = "Version: "
package_info = DockerRunner.run_simple_command( package_info = DockerRunner.run_simple_command(
container, container,
@@ -39,19 +39,19 @@ def _get_pip_package_version(container, package):
class TaggerInterface: class TaggerInterface:
"""Common interface for all taggers""" """Common interface for all taggers"""
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
raise NotImplementedError raise NotImplementedError
class SHATagger(TaggerInterface): class SHATagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return GitHelper.commit_hash_tag() return GitHelper.commit_hash_tag()
class UbuntuVersionTagger(TaggerInterface): class UbuntuVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
os_release = DockerRunner.run_simple_command(container, "cat /etc/os-release").split("\n") os_release = DockerRunner.run_simple_command(container, "cat /etc/os-release").split("\n")
for line in os_release: for line in os_release:
if line.startswith("VERSION_ID"): if line.startswith("VERSION_ID"):
@@ -60,59 +60,59 @@ class UbuntuVersionTagger(TaggerInterface):
class PythonVersionTagger(TaggerInterface): class PythonVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "python-" + _get_program_version(container, "python").split()[1] return "python-" + _get_program_version(container, "python").split()[1]
class JupyterNotebookVersionTagger(TaggerInterface): class JupyterNotebookVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "notebook-" + _get_program_version(container, "jupyter-notebook") return "notebook-" + _get_program_version(container, "jupyter-notebook")
class JupyterLabVersionTagger(TaggerInterface): class JupyterLabVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "lab-" + _get_program_version(container, "jupyter-lab") return "lab-" + _get_program_version(container, "jupyter-lab")
class JupyterHubVersionTagger(TaggerInterface): class JupyterHubVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "hub-" + _get_program_version(container, "jupyterhub") return "hub-" + _get_program_version(container, "jupyterhub")
class RVersionTagger(TaggerInterface): class RVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "r-" + _get_program_version(container, "R").split()[2] return "r-" + _get_program_version(container, "R").split()[2]
class TensorflowVersionTagger(TaggerInterface): class TensorflowVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "tensorflow-" + _get_pip_package_version(container, "tensorflow") return "tensorflow-" + _get_pip_package_version(container, "tensorflow")
class JuliaVersionTagger(TaggerInterface): class JuliaVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "julia-" + _get_program_version(container, "julia").split()[2] return "julia-" + _get_program_version(container, "julia").split()[2]
class SparkVersionTagger(TaggerInterface): class SparkVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "spark-" + _get_env_variable(container, "APACHE_SPARK_VERSION") return "spark-" + _get_env_variable(container, "APACHE_SPARK_VERSION")
class HadoopVersionTagger(TaggerInterface): class HadoopVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "hadoop-" + _get_env_variable(container, "HADOOP_VERSION") return "hadoop-" + _get_env_variable(container, "HADOOP_VERSION")
class JavaVersionTagger(TaggerInterface): class JavaVersionTagger(TaggerInterface):
@staticmethod @staticmethod
def tag_value(container): def tag_value(container) -> str:
return "java-" + _get_program_version(container, "java").split()[1] return "java-" + _get_program_version(container, "java").split()[1]