forked from stromerbike/meta-medusa-dist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
52 lines (45 loc) · 2.24 KB
/
version.py
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
import os
import subprocess
def get_medusa_codename():
try:
codename = os.environ['MEDUSA_CODENAME']
except KeyError:
codename = '4.255.255.255'
return codename
def get_medusa_variant():
if subprocess.check_output(['git', 'status', '--porcelain'], cwd=os.path.dirname(os.path.realpath(__file__)) + '/..'):
try:
variant = os.environ['MEDUSA_VARIANT'] + '-DIRTY'
except KeyError:
variant = '-DIRTY'
else:
try:
variant = os.environ['MEDUSA_VARIANT']
except KeyError:
variant = ''
return variant
def isFileContentChanged(filePath, newContent):
if os.path.isfile(filePath):
with open(filePath, 'rt') as file:
existingContent = file.read()
if existingContent == newContent:
return False
return True
if __name__ == '__main__':
codename = get_medusa_codename()
git_date_time = subprocess.check_output(['git', 'log', '-1', '--format=%cd', '--date=format:%Y-%m-%d-%H%M%S'], cwd=os.path.dirname(os.path.realpath(__file__)) + '/..').strip()
git_hash_short = subprocess.check_output(['git', 'rev-parse', '--short=8', 'HEAD'], cwd=os.path.dirname(os.path.realpath(__file__)) + '/..').strip()[:8]
git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=os.path.dirname(os.path.realpath(__file__)) + '/..').strip()
variant = get_medusa_variant()
content = ''
with open(os.path.dirname(os.path.realpath(__file__)) + '/conf/distro/medusa.conftemplate', 'rt') as recipe_template:
for line in recipe_template:
line = line.replace('${MEDUSA_METADATA_CODENAME}', codename)
line = line.replace('${MEDUSA_METADATA_GIT_DATE_TIME}', git_date_time)
line = line.replace('${MEDUSA_METADATA_GIT_HASH_SHORT}', git_hash_short)
line = line.replace('${MEDUSA_METADATA_GIT_HASH}', git_hash)
line = line.replace('${MEDUSA_METADATA_VARIANT}', variant)
content += line
if isFileContentChanged(os.path.dirname(os.path.realpath(__file__)) + '/conf/distro/medusa.conf', content):
with open(os.path.dirname(os.path.realpath(__file__)) + '/conf/distro/medusa.conf', 'wt') as recipe:
recipe.write(content)