From 2401290ab3bc6606ed4805e8d06947d32af24e29 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sun, 5 Nov 2023 20:30:41 +0100 Subject: [PATCH] Make manifests and manifests.py itself more beautiful --- tagging/manifests.py | 140 ++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 82 deletions(-) diff --git a/tagging/manifests.py b/tagging/manifests.py index 6c4531e7..200cab22 100644 --- a/tagging/manifests.py +++ b/tagging/manifests.py @@ -10,15 +10,15 @@ docker = plumbum.local["docker"] def quoted_output(container: Container, cmd: str) -> str: - return "\n".join( - [ - "```text", - DockerRunner.run_simple_command(container, cmd, print_result=False).strip( - "\n" - ), - "```", - ] - ) + cmd_output = DockerRunner.run_simple_command(container, cmd, print_result=False) + # For example, `mamba info --quiet` adds redundant empty lines + cmd_output = cmd_output.strip("\n") + # For example, R packages list contains trailing backspaces + cmd_output = "\n".join(line.rstrip() for line in cmd_output.split("\n")) + return f"""\ +```text +{cmd_output} +```""" class ManifestHeader: @@ -32,7 +32,7 @@ class ManifestHeader: commit_hash_tag = GitHelper.commit_hash_tag() commit_message = GitHelper.commit_message() - # Unfortunately, docker images doesn't work when specifying `docker.io` as registry + # Unfortunately, `docker images` doesn't work when specifying `docker.io` as registry fixed_registry = registry + "/" if registry != "docker.io" else "" image_size = docker[ @@ -42,23 +42,20 @@ class ManifestHeader: "{{.Size}}", ]().rstrip() - return "\n".join( - [ - f"# Build manifest for image: {short_image_name}:{commit_hash_tag}", - "", - "## Build Info", - "", - f"- Build datetime: {build_timestamp}", - f"- Docker image: `{registry}/{owner}/{short_image_name}:{commit_hash_tag}`", - f"- Docker image size: {image_size}", - f"- Git commit SHA: [{commit_hash}](https://github.com/jupyter/docker-stacks/commit/{commit_hash})", - "- Git commit message:", - "", - "```text", - f"{commit_message}", - "```", - ] - ) + return f"""\ +# Build manifest for image: {short_image_name}:{commit_hash_tag} + +## Build Info + +- Build datetime: {build_timestamp} +- Docker image: `{registry}/{owner}/{short_image_name}:{commit_hash_tag}` +- Docker image size: {image_size} +- Git commit SHA: [{commit_hash}](https://github.com/jupyter/docker-stacks/commit/{commit_hash}) +- Git commit message: + +```text +{commit_message} +```""" class ManifestInterface: @@ -72,78 +69,57 @@ class ManifestInterface: class CondaEnvironmentManifest(ManifestInterface): @staticmethod def markdown_piece(container: Container) -> str: - return "\n".join( - [ - "## Python Packages", - "", - DockerRunner.run_simple_command(container, "python --version"), - "", - "`mamba info --quiet`:", - "", - quoted_output(container, "mamba info --quiet"), - "", - "`mamba list`:", - "", - quoted_output(container, "mamba list"), - ] - ) + return f"""\ +## Python Packages + +{DockerRunner.run_simple_command(container, "python --version")} + +`mamba info --quiet`: + +{quoted_output(container, "mamba info --quiet")} + +`mamba list`: + +{quoted_output(container, "mamba list")}""" class AptPackagesManifest(ManifestInterface): @staticmethod def markdown_piece(container: Container) -> str: - return "\n".join( - [ - "## Apt Packages", - "", - "`apt list --installed`:", - "", - quoted_output(container, "apt list --installed"), - ] - ) + return f"""\ +## Apt Packages + +`apt list --installed`: + +{quoted_output(container, "apt list --installed")}""" class RPackagesManifest(ManifestInterface): @staticmethod def markdown_piece(container: Container) -> str: - return "\n".join( - [ - "## R Packages", - "", - quoted_output(container, "R --version"), - "", - quoted_output( - container, - "R --silent -e 'installed.packages(.Library)[, c(1,3)]'", - ), - ] - ) + return f"""\ +## R Packages + +{quoted_output(container, "R --version")} + +{quoted_output(container, "R --silent -e 'installed.packages(.Library)[, c(1,3)]'")}""" class JuliaPackagesManifest(ManifestInterface): @staticmethod def markdown_piece(container: Container) -> str: - return "\n".join( - [ - "## Julia Packages", - "", - quoted_output( - container, - "julia -E 'using InteractiveUtils; versioninfo()'", - ), - "", - quoted_output(container, "julia -E 'import Pkg; Pkg.status()'"), - ] - ) + return f"""\ +## Julia Packages + +{quoted_output(container, "julia -E 'using InteractiveUtils; versioninfo()'")} + +{quoted_output(container, "julia -E 'import Pkg; Pkg.status()'")}""" class SparkInfoManifest(ManifestInterface): @staticmethod def markdown_piece(container: Container) -> str: - return "\n".join( - [ - "## Apache Spark", - "", - quoted_output(container, "/usr/local/spark/bin/spark-submit --version"), - ] - ) + return f"""\ +## Apache Spark + +{quoted_output(container, "/usr/local/spark/bin/spark-submit --version")}"""