forked from xenserver/win-xenvif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
397 lines (281 loc) · 10.1 KB
/
build.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!python -u
import os, sys
import datetime
import re
import glob
import tarfile
import subprocess
import shutil
import time
def next_build_number():
try:
file = open('.build_number', 'r')
build_number = file.read()
file.close()
except IOError:
build_number = '0'
file = open('.build_number', 'w')
file.write(str(int(build_number) + 1))
file.close()
return build_number
def make_header():
now = datetime.datetime.now()
file = open('include\\version.h', 'w')
file.write('#define MAJOR_VERSION\t' + os.environ['MAJOR_VERSION'] + '\n')
file.write('#define MAJOR_VERSION_STR\t"' + os.environ['MAJOR_VERSION'] + '"\n')
file.write('\n')
file.write('#define MINOR_VERSION\t' + os.environ['MINOR_VERSION'] + '\n')
file.write('#define MINOR_VERSION_STR\t"' + os.environ['MINOR_VERSION'] + '"\n')
file.write('\n')
file.write('#define MICRO_VERSION\t' + os.environ['MICRO_VERSION'] + '\n')
file.write('#define MICRO_VERSION_STR\t"' + os.environ['MICRO_VERSION'] + '"\n')
file.write('\n')
file.write('#define BUILD_NUMBER\t' + os.environ['BUILD_NUMBER'] + '\n')
file.write('#define BUILD_NUMBER_STR\t"' + os.environ['BUILD_NUMBER'] + '"\n')
file.write('\n')
file.write('#define YEAR\t' + str(now.year) + '\n')
file.write('#define YEAR_STR\t"' + str(now.year) + '"\n')
file.write('#define MONTH\t' + str(now.month) + '\n')
file.write('#define MONTH_STR\t"' + str(now.month) + '"\n')
file.write('#define DAY\t' + str(now.day) + '\n')
file.write('#define DAY_STR\t"' + str(now.day) + '"\n')
file.close()
def copy_inf(name):
src = open('src\\%s.inf' % name, 'r')
dst = open('proj\\%s.inf' % name, 'w')
for line in src:
line = re.sub('@MAJOR_VERSION@', os.environ['MAJOR_VERSION'], line)
line = re.sub('@MINOR_VERSION@', os.environ['MINOR_VERSION'], line)
line = re.sub('@MICRO_VERSION@', os.environ['MICRO_VERSION'], line)
line = re.sub('@BUILD_NUMBER@', os.environ['BUILD_NUMBER'], line)
dst.write(line)
dst.close()
src.close()
def get_expired_symbols(name, age = 30):
path = os.path.join(os.environ['SYMBOL_SERVER'], '000Admin\\history.txt')
try:
file = open(path, 'r')
except IOError:
return []
threshold = datetime.datetime.utcnow() - datetime.timedelta(days = age)
expired = []
for line in file:
item = line.split(',')
if (re.match('add', item[1])):
id = item[0]
date = item[3].split('/')
time = item[4].split(':')
tag = item[5].strip('"')
age = datetime.datetime(year = int(date[2]),
month = int(date[0]),
day = int(date[1]),
hour = int(time[0]),
minute = int(time[1]),
second = int(time[2]))
if (tag == name and age < threshold):
expired.append(id)
elif (re.match('del', item[1])):
id = item[2].rstrip()
try:
expired.remove(id)
except ValueError:
pass
file.close()
return expired
def get_configuration(release, debug):
configuration = release
if debug:
configuration += ' Debug'
else:
configuration += ' Release'
return configuration
def get_target_path(release, arch, debug):
configuration = get_configuration(release, debug)
name = ''.join(configuration.split(' '))
target = { 'x86': os.sep.join([name, 'Win32']), 'x64': os.sep.join([name, 'x64']) }
target_path = os.sep.join(['proj', target[arch]])
return target_path
def shell(command, dir):
print(dir)
print(command)
sys.stdout.flush()
sub = subprocess.Popen(' '.join(command), cwd=dir,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
for line in sub.stdout:
print(line.rstrip())
sub.wait()
return sub.returncode
class msbuild_failure(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def msbuild(platform, configuration, target, file, args, dir):
os.environ['PLATFORM'] = platform
os.environ['CONFIGURATION'] = configuration
os.environ['TARGET'] = target
os.environ['FILE'] = file
os.environ['EXTRA'] = args
bin = os.path.join(os.getcwd(), 'msbuild.bat')
status = shell([bin], dir)
if (status != 0):
raise msbuild_failure(configuration)
def build_sln(name, release, arch, debug):
configuration = get_configuration(release, debug)
if arch == 'x86':
platform = 'Win32'
elif arch == 'x64':
platform = 'x64'
cwd = os.getcwd()
msbuild(platform, configuration, 'Build', name + '.sln', '', 'proj')
def remove_timestamps(path):
try:
os.unlink(path + '.orig')
except OSError:
pass
os.rename(path, path + '.orig')
src = open(path + '.orig', 'r')
dst = open(path, 'w')
for line in src:
if line.find('TimeStamp') == -1:
dst.write(line)
dst.close()
src.close()
def sdv_clean(name):
path = ['proj', name, 'sdv']
print(path)
shutil.rmtree(os.path.join(*path), True)
path = ['proj', name, 'sdv.temp']
print(path)
shutil.rmtree(os.path.join(*path), True)
path = ['proj', name, 'staticdv.job']
print(path)
try:
os.unlink(os.path.join(*path))
except OSError:
pass
path = ['proj', name, 'refine.sdv']
print(path)
try:
os.unlink(os.path.join(*path))
except OSError:
pass
path = ['proj', name, 'sdv-map.h']
print(path)
try:
os.unlink(os.path.join(*path))
except OSError:
pass
def run_sdv(name, dir):
configuration = get_configuration('Windows 8', False)
platform = 'x64'
msbuild(platform, configuration, 'Build', name + '.vcxproj',
'', os.path.join('proj', name))
sdv_clean(name)
msbuild(platform, configuration, 'sdv', name + '.vcxproj',
'/p:Inputs="/scan"', os.path.join('proj', name))
path = ['proj', name, 'sdv-map.h']
file = open(os.path.join(*path), 'r')
for line in file:
print(line)
file.close()
msbuild(platform, configuration, 'sdv', name + '.vcxproj',
'/p:Inputs="/check:default.sdv"', os.path.join('proj', name))
path = ['proj', name, 'sdv', 'SDV.DVL.xml']
remove_timestamps(os.path.join(*path))
msbuild(platform, configuration, 'dvl', name + '.vcxproj',
'', os.path.join('proj', name))
path = ['proj', name, name + '.DVL.XML']
shutil.copy(os.path.join(*path), dir)
path = ['proj', name, 'refine.sdv']
if os.path.isfile(os.path.join(*path)):
msbuild(platform, configuration, 'sdv', name + '.vcxproj',
'/p:Inputs=/refine', os.path.join('proj', name))
def symstore_del(name, age):
symstore_path = [os.environ['KIT'], 'Debuggers']
if os.environ['PROCESSOR_ARCHITECTURE'] == 'x86':
symstore_path.append('x86')
else:
symstore_path.append('x64')
symstore_path.append('symstore.exe')
symstore = os.path.join(*symstore_path)
for id in get_expired_symbols(name, age):
command=['"' + symstore + '"']
command.append('del')
command.append('/i')
command.append(str(id))
command.append('/s')
command.append(os.environ['SYMBOL_SERVER'])
shell(command, None)
def symstore_add(name, release, arch, debug):
target_path = get_target_path(release, arch, debug)
symstore_path = [os.environ['KIT'], 'Debuggers']
if os.environ['PROCESSOR_ARCHITECTURE'] == 'x86':
symstore_path.append('x86')
else:
symstore_path.append('x64')
symstore_path.append('symstore.exe')
symstore = os.path.join(*symstore_path)
version = '.'.join([os.environ['MAJOR_VERSION'],
os.environ['MINOR_VERSION'],
os.environ['MICRO_VERSION'],
os.environ['BUILD_NUMBER']])
command=['"' + symstore + '"']
command.append('add')
command.append('/s')
command.append(os.environ['SYMBOL_SERVER'])
command.append('/r')
command.append('/f')
command.append('*.pdb')
command.append('/t')
command.append(name)
command.append('/v')
command.append(version)
shell(command, target_path)
def manifest():
cmd = ['git', 'ls-tree', '-r', '--name-only', 'HEAD']
sub = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = sub.communicate()[0]
ret = sub.returncode
if ret != 0:
raise(Exception("Error %d in : %s" % (ret, cmd)))
return output.decode('utf-8')
def archive(filename, files, tgz=False):
access='w'
if tgz:
access='w:gz'
tar = tarfile.open(filename, access)
for name in files :
try:
tar.add(name)
except:
pass
tar.close()
if __name__ == '__main__':
debug = { 'checked': True, 'free': False }
sdv = { 'nosdv': False, None: True }
driver = 'xenvif'
os.environ['MAJOR_VERSION'] = '7'
os.environ['MINOR_VERSION'] = '2'
os.environ['MICRO_VERSION'] = '0'
if 'BUILD_NUMBER' not in os.environ.keys():
os.environ['BUILD_NUMBER'] = next_build_number()
print("BUILD_NUMBER=%s" % os.environ['BUILD_NUMBER'])
if 'GIT_REVISION' in os.environ.keys():
revision = open('revision', 'w')
print(os.environ['GIT_REVISION'], file=revision)
revision.close()
make_header()
copy_inf(driver)
symstore_del(driver, 30)
release = 'Windows Vista'
build_sln(driver, release, 'x86', debug[sys.argv[1]])
build_sln(driver, release, 'x64', debug[sys.argv[1]])
symstore_add(driver, release, 'x86', debug[sys.argv[1]])
symstore_add(driver, release, 'x64', debug[sys.argv[1]])
if len(sys.argv) <= 2 or sdv[sys.argv[2]]:
run_sdv('xenvif', driver)
archive(driver + '\\source.tgz', manifest().splitlines(), tgz=True)
archive(driver + '.tar', [driver,'revision'])