Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ boto3 = "*"
black = "^24.8.0"

[tool.poetry.scripts]
flavors-parse = "src.python_gardenlinux_lib.flavors.parse_flavors:main"
gl-cname = "python_gardenlinux_lib.cname:main"
gl-flavors-parse = "python_gardenlinux_lib.flavors.parse_flavors:main"
flavors-parse = "python_gardenlinux_lib.flavors.parse_flavors:main"

[tool.pytest.ini_options]
pythonpath = [
Expand Down
85 changes: 85 additions & 0 deletions src/python_gardenlinux_lib/cname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python3

from .features import parse_features

from functools import reduce
from os.path import basename, dirname

import argparse
import re


def main():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for your reference, we also have the python-gardenlinux-cli, which is used as a CLI wrapper for this library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... at least this was the initial intention. :D

not sure how to design this now, since python-gardenlinux-cli looks more like a tool for OCI only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if adding a separate CLI repository may not be counter productive as reusability of code between "lib" and "cli" parts are at least more complicated in that case. Naming of both repositories may be changed to reflect there content.

Furthermore GardenLinux does not have a Python code base that allows "lib" code to be used without a "cli" implementation. Therefore I would suggest to unify the code paths in place.

parser = argparse.ArgumentParser()

parser.add_argument("--arch", dest="arch")
parser.add_argument("--feature-dir", default="features")
parser.add_argument("--version", dest="version")
parser.add_argument("cname")

args = parser.parse_args()

re_match = re.match(
"([a-zA-Z0-9]+(-[a-zA-Z0-9\\_\\-]*?)?)(-([a-z0-9]+)(-([a-z0-9.]+)-([a-z0-9]+))*)?$",
args.cname
)

assert re_match, f"not a valid cname {args.cname}"

if re_match.lastindex == 1:
cname_base, arch = re_match[1].split("-", 1)
commit_id = None
version = None
else:
arch = re_match[4]
cname_base = re_match[1]
commit_id = re_match[7]
version = re_match[6]

if args.arch is not None:
arch = args.arch

if args.version is not None:
re_match = re.match("([a-z0-9.]+)(-([a-z0-9]+))?$", args.cname)
assert re_match, f"not a valid version {args.version}"

commit_id = re_match[3]
version = re_match[1]

gardenlinux_root = dirname(args.feature_dir)
feature_dir_name = basename(args.feature_dir)

if gardenlinux_root == "":
gardenlinux_root = "."

graph = parse_features.get_features_graph(
cname_base, gardenlinux_root, feature_dir_name
)

sorted_features = parse_features.sort_nodes(graph)

minimal_feature_set = get_minimal_feature_set(graph)

sorted_minimal_features = parse_features.sort_set(
minimal_feature_set, sorted_features
)

cname_base = get_cname_base(sorted_minimal_features)

cname = f"{cname_base}-{arch}"
if commit_id is not None:
cname += f"-{version}-{commit_id}"

print(cname)

def get_cname_base(sorted_features):
return reduce(
lambda a, b : a + ("-" if not b.startswith("_") else "") + b, sorted_features
)

def get_minimal_feature_set(graph):
return set([node for (node, degree) in graph.in_degree() if degree == 0])


if __name__ == "__main__":
main()
80 changes: 80 additions & 0 deletions src/python_gardenlinux_lib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3

# It is important that this list is sorted in descending length of the entries
GL_MEDIA_TYPES = [
"gcpimage.tar.gz.log",
"firecracker.tar.gz",
"platform.test.log",
"platform.test.xml",
"gcpimage.tar.gz",
"chroot.test.log",
"chroot.test.xml",
"pxe.tar.gz.log",
"root.squashfs",
"manifest.log",
"release.log",
"pxe.tar.gz",
"qcow2.log",
"test-log",
"boot.efi",
"manifest",
"vmdk.log",
"tar.log",
"uki.log",
"vmlinuz",
"release",
"vhd.log",
"ova.log",
"raw.log",
"oci.log",
"initrd",
"tar.gz",
"qcow2",
"tar",
"iso",
"oci",
"vhd",
"vmdk",
"ova",
"uki",
"raw",
]

GL_MEDIA_TYPE_LOOKUP = {
"tar": "application/io.gardenlinux.image.archive.format.tar",
"tar.gz": "application/io.gardenlinux.image.archive.format.tar.gz",
"pxe.tar.gz": "application/io.gardenlinux.image.archive.format.pxe.tar.gz",
"iso": "application/io.gardenlinux.image.archive.format.iso",
"oci": "application/io.gardenlinux.image.archive.format.oci",
"firecracker.tar.gz": "application/io.gardenlinux.image.archive.format.firecracker.tar.gz",
"qcow2": "application/io.gardenlinux.image.format.qcow2",
"vhd": "application/io.gardenlinux.image.format.vhd",
"gcpimage.tar.gz": "application/io.gardenlinux.image.format.gcpimage.tar.gz",
"vmdk": "application/io.gardenlinux.image.format.vmdk",
"ova": "application/io.gardenlinux.image.format.ova",
"uki": "application/io.gardenlinux.uki",
"uki.log": "application/io.gardenlinux.log",
"raw": "application/io.gardenlinux.image.archive.format.raw",
"manifest.log": "application/io.gardenlinux.log",
"release.log": "application/io.gardenlinux.log",
"test-log": "application/io.gardenlinux.test-log",
"manifest": "application/io.gardenlinux.manifest",
"tar.log": "application/io.gardenlinux.log",
"release": "application/io.gardenlinux.release",
"raw.log": "application/io.gardenlinux.log",
"qcow2.log": "application/io.gardenlinux.log",
"pxe.tar.gz.log": "application/io.gardenlinux.log",
"gcpimage.tar.gz.log": "application/io.gardenlinux.log",
"vmdk.log": "application/io.gardenlinux.log",
"vhd.log": "application/io.gardenlinux.log",
"ova.log": "application/io.gardenlinux.log",
"vmlinuz": "application/io.gardenlinux.kernel",
"initrd": "application/io.gardenlinux.initrd",
"root.squashfs": "application/io.gardenlinux.squashfs",
"boot.efi": "application/io.gardenlinux.efi",
"platform.test.log": "application/io.gardenlinux.io.platform.test.log",
"platform.test.xml": "application/io.gardenlinux.io.platform.test.xml",
"chroot.test.log": "application/io.gardenlinux.io.chroot.test.log",
"chroot.test.xml": "application/io.gardenlinux.io.chroot.test.xml",
"oci.log": "application/io.gardenlinux.log",
}
Loading
Loading