Skip to content

Commit 123d215

Browse files
authored
Refactor tagging: create functions, better logs and names, textwrap.dedent (#2239)
1 parent 6c47a89 commit 123d215

File tree

8 files changed

+128
-91
lines changed

8 files changed

+128
-91
lines changed

tagging/apps/apply_tags.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616

1717
def apply_tags(config: Config) -> None:
18-
"""
19-
Tags <config.full_image()> with the tags reported by all taggers for this image
20-
"""
2118
LOGGER.info(f"Tagging image: {config.image}")
2219

2320
file_prefix = get_file_prefix_for_platform(config.platform, config.variant)
@@ -28,6 +25,8 @@ def apply_tags(config: Config) -> None:
2825
LOGGER.info(f"Applying tag: {tag}")
2926
docker["tag", config.full_image(), tag] & plumbum.FG
3027

28+
LOGGER.info(f"All tags applied to image: {config.image}")
29+
3130

3231
if __name__ == "__main__":
3332
logging.basicConfig(level=logging.INFO)

tagging/apps/merge_tags.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@
1515
LOGGER = logging.getLogger(__name__)
1616

1717

18-
def merge_tags(config: Config) -> None:
19-
"""
20-
Merge tags for x86_64 and aarch64 images when possible.
21-
"""
22-
LOGGER.info(f"Merging tags for image: {config.image}")
23-
24-
all_tags: set[str] = set()
18+
def read_tags_from_files(config: Config) -> set[str]:
19+
LOGGER.info(f"Read tags from file(s) for image: {config.image}")
2520

21+
tags: set[str] = set()
2622
for platform in ALL_PLATFORMS:
23+
LOGGER.info(f"Reading tags for platform: {platform}")
24+
2725
file_prefix = get_file_prefix_for_platform(platform, config.variant)
2826
filename = f"{file_prefix}-{config.image}.txt"
29-
file_path = config.tags_dir / filename
30-
if file_path.exists():
31-
tags = file_path.read_text().splitlines()
32-
all_tags.update(tag.replace(platform + "-", "") for tag in tags)
27+
path = config.tags_dir / filename
28+
if path.exists():
29+
LOGGER.info(f"Tag file: {path} found")
30+
lines = path.read_text().splitlines()
31+
tags.update(tag.replace(platform + "-", "") for tag in lines)
32+
else:
33+
LOGGER.info(f"Tag file: {path} doesn't exist")
34+
35+
LOGGER.info(f"Tags read for image: {config.image}")
36+
return tags
3337

34-
LOGGER.info(f"Got tags: {all_tags}")
3538

39+
def merge_tags(config: Config) -> None:
40+
LOGGER.info(f"Merging tags for image: {config.image}")
41+
42+
all_tags = read_tags_from_files(config)
3643
for tag in all_tags:
3744
LOGGER.info(f"Trying to merge tag: {tag}")
3845
existing_images = []
@@ -51,8 +58,11 @@ def merge_tags(config: Config) -> None:
5158
LOGGER.info(f"Found images: {existing_images}")
5259
docker["manifest", "create", tag][existing_images] & plumbum.FG
5360
docker["manifest", "push", tag] & plumbum.FG
61+
5462
LOGGER.info(f"Successfully merged and pushed tag: {tag}")
5563

64+
LOGGER.info(f"All tags merged for image: {config.image}")
65+
5666

5767
if __name__ == "__main__":
5868
logging.basicConfig(level=logging.INFO)

tagging/apps/write_manifest.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
from docker.models.containers import Container
88

99
from tagging.apps.common_cli_arguments import common_arguments_parser
10-
from tagging.hierarchy.get_taggers_and_manifests import (
11-
get_taggers_and_manifests,
12-
)
10+
from tagging.hierarchy.get_manifests import get_manifests
11+
from tagging.hierarchy.get_taggers import get_taggers
1312
from tagging.manifests.build_info import BuildInfo
14-
from tagging.manifests.manifest_interface import ManifestInterface
1513
from tagging.utils.config import Config
1614
from tagging.utils.docker_runner import DockerRunner
1715
from tagging.utils.get_prefix import get_file_prefix, get_tag_prefix
@@ -24,10 +22,12 @@
2422
MARKDOWN_LINE_BREAK = "<br />"
2523

2624

27-
def write_build_history_line(
28-
config: Config, filename: str, all_tags: list[str]
29-
) -> None:
30-
LOGGER.info("Appending build history line")
25+
def get_build_history_line(config: Config, filename: str, container: Container) -> str:
26+
LOGGER.info(f"Calculating build history line for image: {config.image}")
27+
28+
taggers = get_taggers(config.image)
29+
tags_prefix = get_tag_prefix(config.variant)
30+
all_tags = [tags_prefix + "-" + tagger.tag_value(container) for tagger in taggers]
3131

3232
date_column = f"`{BUILD_TIMESTAMP}`"
3333
image_column = MARKDOWN_LINE_BREAK.join(
@@ -42,19 +42,28 @@ def write_build_history_line(
4242
]
4343
)
4444
build_history_line = f"| {date_column} | {image_column} | {links_column} |"
45-
config.hist_lines_dir.mkdir(parents=True, exist_ok=True)
46-
file = config.hist_lines_dir / f"{filename}.txt"
47-
file.write_text(build_history_line)
48-
LOGGER.info(f"Build history line written to: {file}")
49-
50-
51-
def write_manifest_file(
52-
config: Config,
53-
filename: str,
54-
commit_hash_tag: str,
55-
manifests: list[ManifestInterface],
56-
container: Container,
45+
46+
LOGGER.info(f"Build history line calculated for image: {config.image}")
47+
return build_history_line
48+
49+
50+
def write_build_history_line(
51+
config: Config, filename: str, container: Container
5752
) -> None:
53+
LOGGER.info(f"Writing tags for image: {config.image}")
54+
55+
path = config.hist_lines_dir / f"{filename}.txt"
56+
path.parent.mkdir(parents=True, exist_ok=True)
57+
build_history_line = get_build_history_line(config, filename, container)
58+
path.write_text(build_history_line)
59+
60+
LOGGER.info(f"Build history line written to: {path}")
61+
62+
63+
def get_manifest(config: Config, commit_hash_tag: str, container: Container) -> str:
64+
LOGGER.info(f"Calculating manifest file for image: {config.image}")
65+
66+
manifests = get_manifests(config.image)
5867
manifest_names = [manifest.__class__.__name__ for manifest in manifests]
5968
LOGGER.info(f"Using manifests: {manifest_names}")
6069

@@ -65,27 +74,35 @@ def write_manifest_file(
6574
]
6675
markdown_content = "\n\n".join(markdown_pieces) + "\n"
6776

68-
config.manifests_dir.mkdir(parents=True, exist_ok=True)
69-
file = config.manifests_dir / f"{filename}.md"
70-
file.write_text(markdown_content)
71-
LOGGER.info(f"Manifest file written to: {file}")
77+
LOGGER.info(f"Manifest file calculated for image: {config.image}")
78+
return markdown_content
79+
80+
81+
def write_manifest(
82+
config: Config, filename: str, commit_hash_tag: str, container: Container
83+
) -> None:
84+
LOGGER.info(f"Writing manifest file for image: {config.image}")
7285

86+
path = config.manifests_dir / f"{filename}.md"
87+
path.parent.mkdir(parents=True, exist_ok=True)
88+
manifest = get_manifest(config, commit_hash_tag, container)
89+
path.write_text(manifest)
7390

74-
def write_manifest(config: Config) -> None:
75-
LOGGER.info(f"Creating manifests for image: {config.image}")
76-
taggers, manifests = get_taggers_and_manifests(config.image)
91+
LOGGER.info(f"Manifest file wrtitten to: {path}")
92+
93+
94+
def write_all(config: Config) -> None:
95+
LOGGER.info(f"Writing all files for image: {config.image}")
7796

7897
file_prefix = get_file_prefix(config.variant)
7998
commit_hash_tag = GitHelper.commit_hash_tag()
8099
filename = f"{file_prefix}-{config.image}-{commit_hash_tag}"
81100

82101
with DockerRunner(config.full_image()) as container:
83-
tags_prefix = get_tag_prefix(config.variant)
84-
all_tags = [
85-
tags_prefix + "-" + tagger.tag_value(container) for tagger in taggers
86-
]
87-
write_build_history_line(config, filename, all_tags)
88-
write_manifest_file(config, filename, commit_hash_tag, manifests, container)
102+
write_build_history_line(config, filename, container)
103+
write_manifest(config, filename, commit_hash_tag, container)
104+
105+
LOGGER.info(f"All files written for image: {config.image}")
89106

90107

91108
if __name__ == "__main__":
@@ -101,4 +118,4 @@ def write_manifest(config: Config) -> None:
101118
manifests_dir=True,
102119
repository=True,
103120
)
104-
write_manifest(config)
121+
write_all(config)

tagging/apps/write_tags_file.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,18 @@
44
import logging
55

66
from tagging.apps.common_cli_arguments import common_arguments_parser
7-
from tagging.hierarchy.get_taggers_and_manifests import (
8-
get_taggers_and_manifests,
9-
)
7+
from tagging.hierarchy.get_taggers import get_taggers
108
from tagging.utils.config import Config
119
from tagging.utils.docker_runner import DockerRunner
1210
from tagging.utils.get_prefix import get_file_prefix, get_tag_prefix
1311

1412
LOGGER = logging.getLogger(__name__)
1513

1614

17-
def write_tags_file(config: Config) -> None:
18-
"""
19-
Writes tags file for the image {config.full_image()}
20-
"""
21-
LOGGER.info(f"Tagging image: {config.image}")
22-
taggers, _ = get_taggers_and_manifests(config.image)
23-
24-
file_prefix = get_file_prefix(config.variant)
25-
filename = f"{file_prefix}-{config.image}.txt"
15+
def get_tags(config: Config) -> list[str]:
16+
LOGGER.info(f"Calculating tags for image: {config.image}")
2617

18+
taggers = get_taggers(config.image)
2719
tags_prefix = get_tag_prefix(config.variant)
2820
tags = [f"{config.full_image()}:{tags_prefix}-latest"]
2921
with DockerRunner(config.full_image()) as container:
@@ -34,10 +26,22 @@ def write_tags_file(config: Config) -> None:
3426
f"Calculated tag, tagger_name: {tagger_name} tag_value: {tag_value}"
3527
)
3628
tags.append(f"{config.full_image()}:{tags_prefix}-{tag_value}")
37-
config.tags_dir.mkdir(parents=True, exist_ok=True)
38-
file = config.tags_dir / filename
39-
file.write_text("\n".join(tags))
40-
LOGGER.info(f"Tags file written to: {file}")
29+
30+
LOGGER.info(f"Tags calculated for image: {config.image}")
31+
return tags
32+
33+
34+
def write_tags_file(config: Config) -> None:
35+
LOGGER.info(f"Writing tags for image: {config.image}")
36+
37+
file_prefix = get_file_prefix(config.variant)
38+
filename = f"{file_prefix}-{config.image}.txt"
39+
path = config.tags_dir / filename
40+
path.parent.mkdir(parents=True, exist_ok=True)
41+
tags = get_tags(config)
42+
path.write_text("\n".join(tags))
43+
44+
LOGGER.info(f"Tags wrtitten to: {path}")
4145

4246

4347
if __name__ == "__main__":

tagging/hierarchy/get_manifests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
from tagging.hierarchy.images_hierarchy import ALL_IMAGES
4+
from tagging.manifests.manifest_interface import ManifestInterface
5+
6+
7+
def get_manifests(image: str | None) -> list[ManifestInterface]:
8+
if image is None:
9+
return []
10+
image_description = ALL_IMAGES[image]
11+
parent_manifests = get_manifests(image_description.parent_image)
12+
return parent_manifests + image_description.manifests

tagging/hierarchy/get_taggers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
from tagging.hierarchy.images_hierarchy import ALL_IMAGES
4+
from tagging.taggers.tagger_interface import TaggerInterface
5+
6+
7+
def get_taggers(image: str | None) -> list[TaggerInterface]:
8+
if image is None:
9+
return []
10+
image_description = ALL_IMAGES[image]
11+
parent_taggers = get_taggers(image_description.parent_image)
12+
return parent_taggers + image_description.taggers

tagging/hierarchy/get_taggers_and_manifests.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

tagging/utils/quoted_output.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
3+
import textwrap
4+
35
from docker.models.containers import Container
46

57
from tagging.utils.docker_runner import DockerRunner
@@ -14,9 +16,11 @@ def quoted_output(container: Container, cmd: str) -> str:
1416

1517
assert cmd_output, f"Command `{cmd}` returned empty output"
1618

17-
return f"""\
18-
`{cmd}`:
19+
return textwrap.dedent(
20+
f"""\
21+
`{cmd}`:
1922
20-
```text
21-
{cmd_output}
22-
```"""
23+
```text
24+
{cmd_output}
25+
```"""
26+
)

0 commit comments

Comments
 (0)