forked from dberzano/cern-alice-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildstring.py
executable file
·264 lines (207 loc) · 7.01 KB
/
buildstring.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
import re
import os, subprocess
import sys, getopt
# Regular expressions
re_sanitize = re.compile( r'[^A-Za-z0-9._]' )
re_majorminorpatches = re.compile( r'^(([0-9]+)\.([0-9]+))\.([0-9]+)$' )
re_gccver = re.compile( r'version\s+((([0-9]+)\.([0-9]+))\.([0-9]+))' )
re_llvmver = re.compile( r'LLVM\s+((([0-9]+)\.([0-9]+))([^)]*))' )
re_pyver = re.compile( r'Python\s+((([0-9]+)\.([0-9]+))\.([0-9]+))' )
re_tag = re.compile( r'%([a-z]+)(\*?)%' )
# Exception thrown if something goes wrong while getting system information
class SysInfoError(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
def sanitize(s):
"""Returns the sanitized version of the current string: it will be with only
letters, numbers and the underscore. All invalid characters are replaced with
the underscore.
"""
# http://stackoverflow.com/questions/4260280/python-if-else-in-list-comprehension
gen = ( '_' if re_sanitize.match(x) else x for x in s )
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^
# conditional expr (3-way op) ^^^^^^^^^^
# iterator
return ''.join(gen)
def get_os():
"""Returns a string identifying the operating system. The string is guaranteed
being all lowercase and containing only letters, numbers and the underscore.
Raises SysInfoError in case of problems.
"""
try:
with open(os.devnull, 'w') as dn:
sp = subprocess.Popen(['uname', '-s'], stdout=subprocess.PIPE, stderr=dn)
sp.wait()
if sp.returncode != 0:
raise SysInfoError('Cannot get Operating System')
out = sanitize( sp.communicate()[0].strip() ).lower()
return out
except OSError as e:
raise SysInfoError('While getting Operating System: ' + str(e))
def get_arch():
"""Returns a string identifying the architecture. Raises SysInfoError in case
of problems.
"""
try:
with open(os.devnull, 'w') as dn:
sp = subprocess.Popen(['uname', '-m'], stdout=subprocess.PIPE, stderr=dn)
sp.wait()
if sp.returncode != 0:
raise SysInfoError('Cannot get Architecture')
out = sanitize( sp.communicate()[0].strip() ).lower()
return out
except OSError as e:
raise SysInfoError('While getting Architecture: ' + str(e))
def get_python(command=None):
"""Returns the Python version. Raises SysInfoError in case of problems."""
if command is None:
name = 'python'
command = 'python'
else:
name = os.path.basename(command)
try:
sp = subprocess.Popen([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
sp.wait()
if sp.returncode != 0:
vers_raw = None
else:
vers_raw = sp.communicate()[0]
except OSError as e:
raise SysInfoError('Error getting Python version: %s' % e)
try:
m = re_pyver.search( vers_raw )
except TypeError:
m = None
if m is None:
raise SysInfoError('Cannot get Python version')
return {
'vers_full': m.group(1),
'vers_short': m.group(2),
}
def get_compiler(command=None):
"""Returns a dictionary with the compiler's name and versions (short and
full). Raises SysInfoError in case of problems.
"""
if command is None:
name = 'gcc'
command = 'gcc'
else:
name = os.path.basename(command)
vers_full = None
vers_short = None
try:
with open(os.devnull, 'w') as dn:
if name.startswith('cc') or name.startswith('gcc'):
# First try with -dumpversion
sp = subprocess.Popen([command, '-dumpversion'], stdout=subprocess.PIPE, stderr=dn)
sp.wait()
if sp.returncode != 0:
vers_full = None
else:
vers_full = sp.communicate()[0].strip()
# It might not return a properly formatted version (MAJ.MIN.PATCHES)
try:
m = re_majorminorpatches.match( vers_full )
except TypeError:
m = None
if m is not None:
vers_short = m.group(1)
else:
sp = subprocess.Popen([command, '-v'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
sp.wait()
for l in sp.stdout:
m = re_gccver.search(l)
if m:
vers_full = m.group(1)
vers_short = m.group(2)
if vers_full is None:
raise SysInfoError('Cannot get Compiler Info for GCC-like output (%s)' % name)
elif name == 'clang':
sp = subprocess.Popen([command, '-v'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
sp.wait()
for l in sp.stdout:
m = re_llvmver.search(l)
if m:
vers_full = m.group(1)
vers_short = m.group(2)
if vers_full is None:
raise SysInfoError('Cannot get Compiler Info for clang-like output (%s)' % name)
else:
raise SysInfoError('While getting compiler: unsupported compiler %s' % name)
except OSError as e:
raise SysInfoError('While getting Compiler Info: ' + str(e))
return {
'name': name,
'vers_full': vers_full,
'vers_short': vers_short
}
def get_build_tag(format='%os%', compiler=None, python=None):
"""Returns a formatted build tag. Format specifiers:
- %os%: the operating system
- %arch%: the architecture
- %compiler%: the compiler name
- %compilerverfull%: the compiler version (full)
- %compilerver%: the compiler version (major and minor)
- %pythonverfull%: Python version (full)
- %pythonver%: Python version (major and minor)
"""
# Cache
os = None
arch = None
comp = None
py = None
# Find all tags
dest = format
for m in re_tag.finditer(format):
tag = m.group(1)
value = None
if tag == 'os':
if os is None: os = get_os()
value = os
elif tag == 'arch':
if arch is None: arch = get_arch()
value = arch
elif tag.startswith('compiler'):
if comp is None: comp = get_compiler(compiler)
if tag == 'compiler':
value = comp['name']
elif tag == 'compilerver':
value = comp['vers_short']
elif tag == 'compilerverfull':
value = comp['vers_full']
elif tag.startswith('pyver'):
if py is None: py = get_python(python)
if tag == 'pyver':
value = py['vers_short']
elif tag == 'pyverfull':
value = py['vers_full']
if value == None:
value = '<tag_%s_unknown>' % tag
if m.group(2) == '*':
value = value.replace('.', '')
dest = dest.replace( m.group(0), value, 1 )
return dest
def main(argv):
"""Tries to generate an architecture string for the current build
environment."""
compiler = None
python = None
format = "%os%-%arch%-%compiler%%compilerver*%"
try:
opts, args = getopt.getopt(argv, '', [ 'compiler=', 'python=', 'format=' ])
for o, a in opts:
if o == '--compiler':
compiler = a
elif o == '--python':
python = a
elif o == '--format':
format = a
except getopt.GetoptError as e:
print "buildstring: %s" % e
return 1
print get_build_tag(format, compiler=compiler, python=python)
return 0
# Entry point
if __name__ == '__main__':
sys.exit( main(sys.argv[1:]) )