-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversioninfo.py
94 lines (76 loc) · 2.11 KB
/
versioninfo.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
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
86
87
88
89
90
91
92
93
94
#@ {
#@ "targets":
#@ [{
#@ "name":"versioninfo.txt"
#@ ,"status_check":"dynamic"
#@ ,"dependencies":[{"ref":"maike","rel":"tool"}]
#@ }]
#@ }
import sys
import subprocess
import shutil
import os
def modified_time(filename):
try:
return (os.path.getmtime(filename),True)
except (KeyboardInterrupt, SystemExit):
raise
except:
return (0,False)
def newer(file_a,file_b):
mod_a=modified_time(file_a)
mod_b=modified_time(file_b)
if mod_a[1]==False and mod_b[1]==False:
raise OSError('Error: None of the files %s, and %s are accessible.'%(file_a,file_b))
if not mod_a[1]:
return False
if not mod_b[1]:
return True
return mod_a[0] > mod_b[0]
def newer_than_all(file_a, files):
for file in files:
if newer(file,file_a):
return False
return True
def git_changes():
with subprocess.Popen(('git', 'status','--porcelain'),stdout=subprocess.PIPE) \
as git:
result=[];
for k in filter(None,git.stdout.read().decode().split('\n')):
result.append( k[3:].split(' ')[0] )
return result
def get_revision():
if shutil.which('git')==None:
with open('versioninfo-in.txt') as versionfile:
return versionfile.read().strip()
else:
with subprocess.Popen(('git', 'describe','--tags','--dirty','--always') \
,stdout=subprocess.PIPE) as git:
result=git.stdout.read().decode().strip()
git.wait()
status=git.returncode
if status:
with open('versioninfo-in.txt') as versionfile:
return versionfile.read().strip()
else:
return result
def write_error(*args, **kwargs):
print(*args,file=sys.stderr,**kwargs)
def get_rev_old():
with os.fdopen(os.open(target_dir + '/versioninfo.txt',os.O_RDONLY|os.O_CREAT),'r+') \
as verfile:
return verfile.read().strip()
try:
target_dir=sys.argv[1]
in_dir=sys.argv[2]
revision=get_revision()
rev_old=get_rev_old()
if rev_old!=revision:
with open(target_dir + '/versioninfo.txt','w') as verfile:
verfile.write(revision)
with open('versioninfo-in.txt','w') as verfile:
verfile.write(revision)
sys.exit(0)
except Exception:
write_error('%s:%d: error: %s\n'%(sys.argv[0],sys.exc_info()[2].tb_lineno,sys.exc_info()[1]))
sys.exit(-1)