-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcname.py
More file actions
85 lines (59 loc) · 2.12 KB
/
cname.py
File metadata and controls
85 lines (59 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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():
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()