Improve handling common parser arguments (#2221)

This commit is contained in:
Ayaz Salikhov
2025-02-18 11:06:10 +00:00
committed by GitHub
parent 35b440186e
commit e57047801c
12 changed files with 149 additions and 159 deletions

View File

@@ -1,12 +1,12 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import logging
from pathlib import Path
import plumbum
from tagging.common_arguments import common_arguments_parser
from tagging.get_platform import unify_aarch64
from tagging.get_prefix import get_file_prefix_for_platform
@@ -16,12 +16,13 @@ LOGGER = logging.getLogger(__name__)
def apply_tags(
short_image_name: str,
*,
registry: str,
owner: str,
tags_dir: Path,
platform: str,
short_image_name: str,
variant: str,
platform: str,
tags_dir: Path,
) -> None:
"""
Tags <registry>/<owner>/<short_image_name>:latest with the tags reported by all taggers for this image
@@ -41,18 +42,7 @@ def apply_tags(
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
arg_parser.add_argument(
"--tags-dir",
required=True,
type=Path,
help="Directory with saved tags file",
)
arg_parser = common_arguments_parser()
arg_parser.add_argument(
"--platform",
required=True,
@@ -61,30 +51,12 @@ if __name__ == "__main__":
help="Image platform",
)
arg_parser.add_argument(
"--registry",
"--tags-dir",
required=True,
type=str,
choices=["docker.io", "quay.io"],
help="Image registry",
)
arg_parser.add_argument(
"--owner",
required=True,
help="Owner of the image",
)
arg_parser.add_argument(
"--variant",
required=True,
help="Variant tag prefix",
type=Path,
help="Directory with saved tags file",
)
args = arg_parser.parse_args()
args.platform = unify_aarch64(args.platform)
apply_tags(
args.short_image_name,
args.registry,
args.owner,
args.tags_dir,
args.platform,
args.variant,
)
apply_tags(**vars(args))

View File

@@ -0,0 +1,39 @@
import argparse
def common_arguments_parser(
registry: bool = True,
owner: bool = True,
short_image_name: bool = True,
variant: bool = True,
) -> argparse.ArgumentParser:
"""Add common CLI arguments to parser"""
parser = argparse.ArgumentParser()
if registry:
parser.add_argument(
"--registry",
required=True,
choices=["docker.io", "quay.io"],
help="Image registry",
)
if owner:
parser.add_argument(
"--owner",
required=True,
help="Owner of the image",
)
if short_image_name:
parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
if variant:
parser.add_argument(
"--variant",
required=True,
help="Variant tag prefix",
)
return parser

View File

@@ -1,12 +1,12 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import logging
from pathlib import Path
import plumbum
from tagging.common_arguments import common_arguments_parser
from tagging.get_platform import ALL_PLATFORMS
from tagging.get_prefix import get_file_prefix_for_platform
@@ -16,6 +16,7 @@ LOGGER = logging.getLogger(__name__)
def merge_tags(
*,
short_image_name: str,
variant: str,
tags_dir: Path,
@@ -60,23 +61,13 @@ def merge_tags(
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
arg_parser = common_arguments_parser(registry=False, owner=False)
arg_parser.add_argument(
"--tags-dir",
required=True,
type=Path,
help="Directory with saved tags file",
)
arg_parser.add_argument(
"--variant",
required=True,
help="Variant tag prefix",
)
args = arg_parser.parse_args()
merge_tags(args.short_image_name, args.variant, args.tags_dir)
merge_tags(**vars(args))

View File

@@ -155,7 +155,11 @@ def remove_old_manifests(wiki_dir: Path) -> None:
def update_wiki(
wiki_dir: Path, hist_lines_dir: Path, manifests_dir: Path, allow_no_files: bool
*,
wiki_dir: Path,
hist_lines_dir: Path,
manifests_dir: Path,
allow_no_files: bool,
) -> None:
LOGGER.info("Updating wiki")
@@ -214,6 +218,4 @@ if __name__ == "__main__":
)
args = arg_parser.parse_args()
update_wiki(
args.wiki_dir, args.hist_lines_dir, args.manifests_dir, args.allow_no_files
)
update_wiki(**vars(args))

View File

@@ -1,13 +1,13 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import datetime
import logging
from pathlib import Path
from docker.models.containers import Container
from tagging.common_arguments import common_arguments_parser
from tagging.docker_runner import DockerRunner
from tagging.get_prefix import get_file_prefix, get_tag_prefix
from tagging.get_taggers_and_manifests import get_taggers_and_manifests
@@ -22,9 +22,10 @@ MARKDOWN_LINE_BREAK = "<br />"
def write_build_history_line(
short_image_name: str,
*,
registry: str,
owner: str,
short_image_name: str,
hist_lines_dir: Path,
filename: str,
all_tags: list[str],
@@ -49,9 +50,10 @@ def write_build_history_line(
def write_manifest_file(
short_image_name: str,
*,
registry: str,
owner: str,
short_image_name: str,
manifests_dir: Path,
filename: str,
manifests: list[ManifestInterface],
@@ -70,9 +72,10 @@ def write_manifest_file(
def write_manifest(
short_image_name: str,
*,
registry: str,
owner: str,
short_image_name: str,
variant: str,
hist_lines_dir: Path,
manifests_dir: Path,
@@ -92,28 +95,28 @@ def write_manifest(
tags_prefix + "-" + tagger.tag_value(container) for tagger in taggers
]
write_build_history_line(
short_image_name, registry, owner, hist_lines_dir, filename, all_tags
registry=registry,
owner=owner,
short_image_name=short_image_name,
hist_lines_dir=hist_lines_dir,
filename=filename,
all_tags=all_tags,
)
write_manifest_file(
short_image_name,
registry,
owner,
manifests_dir,
filename,
manifests,
container,
registry=registry,
owner=owner,
short_image_name=short_image_name,
manifests_dir=manifests_dir,
filename=filename,
manifests=manifests,
container=container,
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
arg_parser = common_arguments_parser()
arg_parser.add_argument(
"--hist-lines-dir",
required=True,
@@ -126,32 +129,8 @@ if __name__ == "__main__":
type=Path,
help="Directory to save manifest file",
)
arg_parser.add_argument(
"--registry",
required=True,
type=str,
choices=["docker.io", "quay.io"],
help="Image registry",
)
arg_parser.add_argument(
"--owner",
required=True,
help="Owner of the image",
)
arg_parser.add_argument(
"--variant",
required=True,
help="Variant tag prefix",
)
args = arg_parser.parse_args()
LOGGER.info(f"Current build timestamp: {BUILD_TIMESTAMP}")
write_manifest(
args.short_image_name,
args.registry,
args.owner,
args.variant,
args.hist_lines_dir,
args.manifests_dir,
)
write_manifest(**vars(args))

View File

@@ -1,10 +1,10 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import logging
from pathlib import Path
from tagging.common_arguments import common_arguments_parser
from tagging.docker_runner import DockerRunner
from tagging.get_prefix import get_file_prefix, get_tag_prefix
from tagging.get_taggers_and_manifests import get_taggers_and_manifests
@@ -13,9 +13,10 @@ LOGGER = logging.getLogger(__name__)
def write_tags_file(
short_image_name: str,
*,
registry: str,
owner: str,
short_image_name: str,
variant: str,
tags_dir: Path,
) -> None:
@@ -48,41 +49,13 @@ def write_tags_file(
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
arg_parser = common_arguments_parser()
arg_parser.add_argument(
"--tags-dir",
required=True,
type=Path,
help="Directory to save tags file",
)
arg_parser.add_argument(
"--registry",
required=True,
type=str,
choices=["docker.io", "quay.io"],
help="Image registry",
)
arg_parser.add_argument(
"--owner",
required=True,
help="Owner of the image",
)
arg_parser.add_argument(
"--variant",
required=True,
help="Variant tag prefix",
)
args = arg_parser.parse_args()
write_tags_file(
args.short_image_name,
args.registry,
args.owner,
args.variant,
args.tags_dir,
)
write_tags_file(**vars(args))