Skip to content

Commit 781d60c

Browse files
committed
pyHDLC: add support for saving and using version
1 parent ef1c166 commit 781d60c

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

Diff for: utils/pyHDLC/__init__.py

+58-25
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Any, Dict, List, Optional, Tuple, Union
2121
from pathlib import Path
2222
from string import Template
23+
from re import search as re_search, IGNORECASE as re_IGNORECASE
2324

2425
from dataclasses import dataclass
2526
from yamldataclassconfig.config import YamlDataClassConfig
@@ -424,23 +425,41 @@ def BuildImage(
424425
if target not in [None, '']:
425426
cmd += [f"--target={target}"]
426427

427-
cpath = Path(collection.replace("/", "-"))
428-
dpath = cpath / dockerfile
428+
def _getCollectionAndDockerfilePaths(collection, dockerfile):
429+
collectionPath = Path(collection.replace("/", "-"))
430+
dockerfilePath = collectionPath / dockerfile
429431

430-
if dpath.is_dir():
431-
cpath = dpath
432-
dpath = dpath / 'Dockerfile'
433-
else:
434-
dpath = cpath / f"{dockerfile}.dockerfile"
435-
cmd += ["-f", str(dpath)]
432+
if dockerfilePath.is_dir():
433+
contextPath = dockerfilePath
434+
dockerfilePath = contextPath / 'Dockerfile'
435+
else:
436+
contextPath = collectionPath
437+
dockerfilePath = contextPath / f"{dockerfile}.dockerfile"
438+
439+
if not dockerfilePath.exists():
440+
raise Exception(f"Dockerfile <{dockerfilePath}> does not exist!")
441+
442+
return (contextPath, dockerfilePath)
443+
444+
(contextPath, dockerfilePath) = _getCollectionAndDockerfilePaths(collection, dockerfile)
436445

437-
if not dpath.exists():
438-
raise Exception(f"Dockerfile <{dpath}> does not exist!")
446+
if len(dockerfilePath.suffix) != 0:
447+
cmd += ["-f", str(dockerfilePath)]
439448

440-
cmd += [str(cpath)]
449+
cmd += [str(contextPath)]
441450

442451
_exec(args=cmd, dry=dry, collapse=f"[Build] Build {imageName}")
443452

453+
with dockerfilePath.open('r') as rfptr:
454+
for line in rfptr:
455+
if re_search('FROM scratch AS version', line, re_IGNORECASE):
456+
_exec(
457+
args=['docker', 'build', '--target', 'version', '-o', 'dist', str(contextPath)],
458+
dry=dry,
459+
collapse=f"[Build] Version {imageName}"
460+
)
461+
break
462+
444463
if test:
445464
TestImage(
446465
f"{img}{(f'#{withDir}' if withDir is not None else '')}",
@@ -578,8 +597,16 @@ def PushImage(
578597
* ``#A``: architecture
579598
* ``#C``: collection
580599
"""
581-
def dpush(imgName):
582-
_exec(args=["docker", "push", imgName], dry=dry, collapse=f"Push {imgName}")
600+
def dpush(args: List[str]):
601+
_exec(args=["docker", "push"]+args, dry=dry, collapse=f"Push {' '.join(args)}")
602+
603+
def dtag(imgName: str, tags: List[str]):
604+
for tag in tags:
605+
_exec(
606+
args=["docker", "tag", imgName, tag],
607+
dry=dry,
608+
collapse=f"Tag {tag}",
609+
)
583610

584611
mirrors = [] if mirror is None else [mirror] if isinstance(mirror, str) else mirror
585612

@@ -589,17 +616,23 @@ def dpush(imgName):
589616
# There, it denotes keywords for replacement.
590617
img = rimg.split('#')[0]
591618
imageName = f"{registry}/{architecture}/{collection}/{img}"
592-
dpush(imageName)
619+
dpush([imageName])
620+
621+
print('\nChecking version file...')
622+
versionString = None
623+
versionFile = Path('dist') / f'hdlc.{rimg if rimg[0:4] != "pkg/" else rimg[4:]}.version'
624+
if versionFile.exists():
625+
with versionFile.open('r') as rfptr:
626+
versionString = rfptr.read().strip()
627+
print(f'{rimg}: {versionString}')
628+
593629
for mirror in mirrors:
594-
mimg = (
595-
img.replace("/", ":", 1).replace("/", "--")
596-
if mirror.startswith("docker.io")
597-
else img
598-
)
630+
isDocker = mirror.startswith("docker.io")
631+
mimg = img.replace("/", ":", 1).replace("/", "--") if isDocker else img
599632
mirrorName = f"{mirror.replace('#A', architecture).replace('#C', collection)}/{mimg}"
600-
_exec(
601-
args=["docker", "tag", imageName, mirrorName],
602-
dry=dry,
603-
collapse=f"Tag {imageName} {mirrorName}",
604-
)
605-
dpush(mirrorName)
633+
if isDocker or (versionString is None):
634+
dtag(imageName, [mirrorName])
635+
dpush([mirrorName])
636+
continue
637+
dtag(imageName, [mirrorName, f'{mirrorName}:{versionString}'])
638+
dpush(['--all-tags', mirrorName])

0 commit comments

Comments
 (0)