Skip to content

Commit 05be222

Browse files
committed
Add support to read COMMIT and VERSION files
Signed-off-by: Tobias Wolf <[email protected]>
1 parent 471c9fc commit 05be222

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/gardenlinux/features/__main__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from .parser import Parser
55

66
from functools import reduce
7-
from os.path import basename, dirname
8-
7+
from os import path
98
import argparse
9+
import os
1010
import re
1111
import sys
1212

@@ -44,6 +44,7 @@ def main():
4444
arch = None
4545
cname_base = None
4646
commit_id = None
47+
gardenlinux_root = path.dirname(args.feature_dir)
4748
version = None
4849

4950
if args.cname:
@@ -85,12 +86,14 @@ def main():
8586
assert args.default_arch, "Architecture could not be determined and no default architecture set"
8687
arch = args.default_arch
8788

89+
if not commit_id or not version:
90+
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
91+
8892
if not version and (args.type in ("cname", "version" )):
8993
assert args.default_version, "version not specified and no default version set"
9094
version = args.default_version
9195

92-
gardenlinux_root = dirname(args.feature_dir)
93-
feature_dir_name = basename(args.feature_dir)
96+
feature_dir_name = path.basename(args.feature_dir)
9497

9598
if gardenlinux_root == "":
9699
gardenlinux_root = "."
@@ -149,6 +152,20 @@ def get_cname_base(sorted_features):
149152
lambda a, b : a + ("-" if not b.startswith("_") else "") + b, sorted_features
150153
)
151154

155+
def get_version_and_commit_id_from_files(gardenlinux_root):
156+
commit_id = None
157+
version = None
158+
159+
if os.access(path.join(gardenlinux_root, "COMMIT"), os.R_OK):
160+
with open(path.join(gardenlinux_root, "COMMIT"), "r") as fp:
161+
commit_id = fp.read().strip()
162+
163+
if os.access(path.join(gardenlinux_root, "VERSION"), os.R_OK):
164+
with open(path.join(gardenlinux_root, "VERSION"), "r") as fp:
165+
version = fp.read().strip()
166+
167+
return (version, commit_id)
168+
152169
def get_minimal_feature_set(graph):
153170
return set([node for (node, degree) in graph.in_degree() if degree == 0])
154171

src/gardenlinux/features/cname_main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import argparse
77
import re
88

9-
from .__main__ import get_cname_base, get_minimal_feature_set, sort_subset
9+
from .__main__ import get_cname_base, get_minimal_feature_set, get_version_and_commit_id_from_files, sort_subset
1010
from .parser import Parser
1111

1212

@@ -29,6 +29,7 @@ def main():
2929

3030
arch = None
3131
commit_id = None
32+
gardenlinux_root = dirname(args.feature_dir)
3233
version = None
3334

3435
if re_match.lastindex == 1:
@@ -49,14 +50,16 @@ def main():
4950

5051
assert arch is None or arch == "", "Architecture could not be determined"
5152

53+
if not commit_id or not version:
54+
version, commit_id = get_version_and_commit_id_from_files(gardenlinux_root)
55+
5256
if args.version is not None:
5357
re_match = re.match("([a-z0-9.]+)(-([a-z0-9]+))?$", args.version)
5458
assert re_match, f"Not a valid version {args.version}"
5559

5660
commit_id = re_match[3]
5761
version = re_match[1]
5862

59-
gardenlinux_root = dirname(args.feature_dir)
6063
feature_dir_name = basename(args.feature_dir)
6164

6265
if gardenlinux_root == "":

0 commit comments

Comments
 (0)