Skip to content

Commit e570478

Browse files
authored
Improve handling common parser arguments (#2221)
1 parent 35b4401 commit e570478

12 files changed

+148
-158
lines changed

.github/workflows/docker-build-test-upload.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ jobs:
6666
shell: bash
6767

6868
- name: Write tags file 🏷
69-
run: |
70-
python3 -m tagging.write_tags_file --short-image-name ${{ inputs.image }} --tags-dir /tmp/jupyter/tags/ --registry ${{ env.REGISTRY }} --owner ${{ env.OWNER }} --variant ${{ inputs.variant }}
69+
run: >
70+
python3 -m tagging.write_tags_file
71+
--registry ${{ env.REGISTRY }}
72+
--owner ${{ env.OWNER }}
73+
--short-image-name ${{ inputs.image }}
74+
--variant ${{ inputs.variant }}
75+
--tags-dir /tmp/jupyter/tags/
7176
shell: bash
7277
- name: Upload tags file 💾
7378
uses: actions/upload-artifact@v4
@@ -77,7 +82,14 @@ jobs:
7782
retention-days: 3
7883

7984
- name: Write manifest and build history file 🏷
80-
run: python3 -m tagging.write_manifest --short-image-name ${{ inputs.image }} --hist-lines-dir /tmp/jupyter/hist_lines/ --manifests-dir /tmp/jupyter/manifests/ --registry ${{ env.REGISTRY }} --owner ${{ env.OWNER }} --variant ${{ inputs.variant }}
85+
run: >
86+
python3 -m tagging.write_manifest
87+
--registry ${{ env.REGISTRY }}
88+
--owner ${{ env.OWNER }}
89+
--short-image-name ${{ inputs.image }}
90+
--variant ${{ inputs.variant }}
91+
--hist-lines-dir /tmp/jupyter/hist_lines/
92+
--manifests-dir /tmp/jupyter/manifests/
8193
shell: bash
8294
- name: Upload manifest file 💾
8395
uses: actions/upload-artifact@v4
@@ -105,5 +117,9 @@ jobs:
105117
retention-days: 3
106118

107119
- name: Run tests ✅
108-
run: python3 -m tests.run_tests --short-image-name ${{ inputs.image }} --registry ${{ env.REGISTRY }} --owner ${{ env.OWNER }}
120+
run: >
121+
python3 -m tests.run_tests
122+
--registry ${{ env.REGISTRY }}
123+
--owner ${{ env.OWNER }}
124+
--short-image-name ${{ inputs.image }}
109125
shell: bash

.github/workflows/docker-merge-tags.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ jobs:
6262

6363
- name: Merge tags for the images 🔀
6464
if: env.PUSH_TO_REGISTRY == 'true'
65-
run: python3 -m tagging.merge_tags --short-image-name ${{ inputs.image }} --tags-dir /tmp/jupyter/tags/ --variant ${{ inputs.variant }}
65+
run: >
66+
python3 -m tagging.merge_tags
67+
--short-image-name ${{ inputs.image }}
68+
--variant ${{ inputs.variant }}
69+
--tags-dir /tmp/jupyter/tags/
6670
shell: bash

.github/workflows/docker-tag-push.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ jobs:
5656
name: ${{ inputs.image }}-${{ inputs.platform }}-${{ inputs.variant }}-tags
5757
path: /tmp/jupyter/tags/
5858
- name: Apply tags to the loaded image 🏷
59-
run: python3 -m tagging.apply_tags --short-image-name ${{ inputs.image }} --tags-dir /tmp/jupyter/tags/ --platform ${{ inputs.platform }} --registry ${{ env.REGISTRY }} --owner ${{ env.OWNER }} --variant ${{ inputs.variant }}
59+
run: >
60+
python3 -m tagging.apply_tags
61+
--registry ${{ env.REGISTRY }}
62+
--owner ${{ env.OWNER }}
63+
--short-image-name ${{ inputs.image }}
64+
--variant ${{ inputs.variant }}
65+
--platform ${{ inputs.platform }}
66+
--tags-dir /tmp/jupyter/tags/
6067
# This step is needed to prevent pushing non-multi-arch "latest" tag
6168
- name: Remove the "latest" tag from the image 🗑️
6269
run: docker image rmi ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ inputs.image }}:latest

.github/workflows/docker-wiki-update.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ jobs:
3939
path: wiki/
4040

4141
- name: Update wiki 🏷
42-
run: python3 -m tagging.update_wiki --wiki-dir wiki/ --hist-lines-dir /tmp/jupyter/hist_lines/ --manifests-dir /tmp/jupyter/manifests/
42+
run: >
43+
python3 -m tagging.update_wiki
44+
--wiki-dir wiki/
45+
--hist-lines-dir /tmp/jupyter/hist_lines/
46+
--manifests-dir /tmp/jupyter/manifests/
4347
shell: bash
4448

4549
- name: Push Uncyclo to GitHub 📤

Makefile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,25 @@ linkcheck-docs: ## check broken links
7979
hook/%: VARIANT?=default
8080
hook/%: ## run post-build hooks for an image
8181
python3 -m tagging.write_tags_file \
82-
--short-image-name "$(notdir $@)" \
83-
--tags-dir /tmp/jupyter/tags/ \
8482
--registry "$(REGISTRY)" \
8583
--owner "$(OWNER)" \
86-
--variant "$(VARIANT)"
84+
--short-image-name "$(notdir $@)" \
85+
--variant "$(VARIANT)" \
86+
--tags-dir /tmp/jupyter/tags/
8787
python3 -m tagging.write_manifest \
88+
--registry "$(REGISTRY)" \
89+
--owner "$(OWNER)" \
8890
--short-image-name "$(notdir $@)" \
91+
--variant "$(VARIANT)" \
8992
--hist-lines-dir /tmp/jupyter/hist_lines/ \
90-
--manifests-dir /tmp/jupyter/manifests/ \
93+
--manifests-dir /tmp/jupyter/manifests/
94+
python3 -m tagging.apply_tags \
9195
--registry "$(REGISTRY)" \
9296
--owner "$(OWNER)" \
93-
--variant "$(VARIANT)"
94-
python3 -m tagging.apply_tags \
9597
--short-image-name "$(notdir $@)" \
96-
--tags-dir /tmp/jupyter/tags/ \
98+
--variant "$(VARIANT)" \
9799
--platform "$(shell uname -m)" \
98-
--registry "$(REGISTRY)" \
99-
--owner "$(OWNER)" \
100-
--variant "$(VARIANT)"
100+
--tags-dir /tmp/jupyter/tags/
101101
hook-all: $(foreach I, $(ALL_IMAGES), hook/$(I)) ## run post-build hooks for all images
102102

103103

@@ -135,7 +135,7 @@ run-sudo-shell/%: ## run bash in interactive mode as root in a stack
135135

136136
test/%: ## run tests against a stack
137137
python3 -m tests.run_tests \
138-
--short-image-name "$(notdir $@)" \
139138
--registry "$(REGISTRY)" \
140-
--owner "$(OWNER)"
139+
--owner "$(OWNER)" \
140+
--short-image-name "$(notdir $@)"
141141
test-all: $(foreach I, $(ALL_IMAGES), test/$(I)) ## test all stacks

tagging/apply_tags.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
4-
import argparse
54
import logging
65
from pathlib import Path
76

87
import plumbum
98

9+
from tagging.common_arguments import common_arguments_parser
1010
from tagging.get_platform import unify_aarch64
1111
from tagging.get_prefix import get_file_prefix_for_platform
1212

@@ -16,12 +16,13 @@
1616

1717

1818
def apply_tags(
19-
short_image_name: str,
19+
*,
2020
registry: str,
2121
owner: str,
22-
tags_dir: Path,
23-
platform: str,
22+
short_image_name: str,
2423
variant: str,
24+
platform: str,
25+
tags_dir: Path,
2526
) -> None:
2627
"""
2728
Tags <registry>/<owner>/<short_image_name>:latest with the tags reported by all taggers for this image
@@ -41,18 +42,7 @@ def apply_tags(
4142
if __name__ == "__main__":
4243
logging.basicConfig(level=logging.INFO)
4344

44-
arg_parser = argparse.ArgumentParser()
45-
arg_parser.add_argument(
46-
"--short-image-name",
47-
required=True,
48-
help="Short image name",
49-
)
50-
arg_parser.add_argument(
51-
"--tags-dir",
52-
required=True,
53-
type=Path,
54-
help="Directory with saved tags file",
55-
)
45+
arg_parser = common_arguments_parser()
5646
arg_parser.add_argument(
5747
"--platform",
5848
required=True,
@@ -61,30 +51,12 @@ def apply_tags(
6151
help="Image platform",
6252
)
6353
arg_parser.add_argument(
64-
"--registry",
65-
required=True,
66-
type=str,
67-
choices=["docker.io", "quay.io"],
68-
help="Image registry",
69-
)
70-
arg_parser.add_argument(
71-
"--owner",
72-
required=True,
73-
help="Owner of the image",
74-
)
75-
arg_parser.add_argument(
76-
"--variant",
54+
"--tags-dir",
7755
required=True,
78-
help="Variant tag prefix",
56+
type=Path,
57+
help="Directory with saved tags file",
7958
)
8059
args = arg_parser.parse_args()
8160
args.platform = unify_aarch64(args.platform)
8261

83-
apply_tags(
84-
args.short_image_name,
85-
args.registry,
86-
args.owner,
87-
args.tags_dir,
88-
args.platform,
89-
args.variant,
90-
)
62+
apply_tags(**vars(args))

tagging/common_arguments.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import argparse
2+
3+
4+
def common_arguments_parser(
5+
registry: bool = True,
6+
owner: bool = True,
7+
short_image_name: bool = True,
8+
variant: bool = True,
9+
) -> argparse.ArgumentParser:
10+
"""Add common CLI arguments to parser"""
11+
12+
parser = argparse.ArgumentParser()
13+
if registry:
14+
parser.add_argument(
15+
"--registry",
16+
required=True,
17+
choices=["docker.io", "quay.io"],
18+
help="Image registry",
19+
)
20+
if owner:
21+
parser.add_argument(
22+
"--owner",
23+
required=True,
24+
help="Owner of the image",
25+
)
26+
if short_image_name:
27+
parser.add_argument(
28+
"--short-image-name",
29+
required=True,
30+
help="Short image name",
31+
)
32+
if variant:
33+
parser.add_argument(
34+
"--variant",
35+
required=True,
36+
help="Variant tag prefix",
37+
)
38+
39+
return parser

tagging/merge_tags.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
4-
import argparse
54
import logging
65
from pathlib import Path
76

87
import plumbum
98

9+
from tagging.common_arguments import common_arguments_parser
1010
from tagging.get_platform import ALL_PLATFORMS
1111
from tagging.get_prefix import get_file_prefix_for_platform
1212

@@ -16,6 +16,7 @@
1616

1717

1818
def merge_tags(
19+
*,
1920
short_image_name: str,
2021
variant: str,
2122
tags_dir: Path,
@@ -60,23 +61,13 @@ def merge_tags(
6061
if __name__ == "__main__":
6162
logging.basicConfig(level=logging.INFO)
6263

63-
arg_parser = argparse.ArgumentParser()
64-
arg_parser.add_argument(
65-
"--short-image-name",
66-
required=True,
67-
help="Short image name",
68-
)
64+
arg_parser = common_arguments_parser(registry=False, owner=False)
6965
arg_parser.add_argument(
7066
"--tags-dir",
7167
required=True,
7268
type=Path,
7369
help="Directory with saved tags file",
7470
)
75-
arg_parser.add_argument(
76-
"--variant",
77-
required=True,
78-
help="Variant tag prefix",
79-
)
8071
args = arg_parser.parse_args()
8172

82-
merge_tags(args.short_image_name, args.variant, args.tags_dir)
73+
merge_tags(**vars(args))

tagging/update_wiki.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ def remove_old_manifests(wiki_dir: Path) -> None:
155155

156156

157157
def update_wiki(
158-
wiki_dir: Path, hist_lines_dir: Path, manifests_dir: Path, allow_no_files: bool
158+
*,
159+
wiki_dir: Path,
160+
hist_lines_dir: Path,
161+
manifests_dir: Path,
162+
allow_no_files: bool,
159163
) -> None:
160164
LOGGER.info("Updating wiki")
161165

@@ -214,6 +218,4 @@ def update_wiki(
214218
)
215219
args = arg_parser.parse_args()
216220

217-
update_wiki(
218-
args.wiki_dir, args.hist_lines_dir, args.manifests_dir, args.allow_no_files
219-
)
221+
update_wiki(**vars(args))

0 commit comments

Comments
 (0)