-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild_test_manual.py
executable file
·546 lines (454 loc) · 19.1 KB
/
build_test_manual.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env python
""" a script to automatically try and verify the different
build options with the on the current system available
fortran compilers.
"""
# Copyright J. de Kloe
# This software is licensed under the terms of the LGPLv3 Licence
# which can be obtained from https://www.gnu.org/licenses/lgpl.html
# #[ plan:
# search for available fortran (and c?) compilers
#
# for each compiler
# -clone the repository
# -edit the setup.cfg file to choose the compiler
# -build the software
# -run the manual build step
# -run the unit tests
# -convert source code to python3
# -do the build test again
#
# #]
# #[ imported modules
from __future__ import (absolute_import, division,
print_function) # , unicode_literals)
import os, sys, glob
import subprocess # support running additional executables
# #]
# #[ settings
REPODIR = '../pybufr-ecmwf'
TESTDIR = '../pybufr_ecmwf_test_builds'
DO_MANUAL_TESTS = True
DO_SETUP_BUILD_TESTS = True
DO_SETUP_SDIST_TESTS = True
DO_PIP_INSTALL_TESTS = True
DO_MANUAL_PY3_TESTS = False # True
# #]
# #[ helper functions
def run_shell_command(cmd_to_run, sp_env=None):
# #[
""" a wrapper routine around subprocess.Popen intended
to make it a bit easier to call this functionality.
"""
#print("Executing command: ", cmd_to_run)
this_env = sp_env
if sp_env is None:
this_env = os.environ
subpr = subprocess.Popen(cmd_to_run, shell=True, env=this_env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
ls_stdout = subpr.stdout.readlines()
ls_stderr = subpr.stderr.readlines()
return (ls_stdout, ls_stderr)
# #]
# #]
# #[ init
if not os.path.exists(REPODIR):
print('ERROR: could not find REPODIR: '+REPODIR)
sys.exit(1)
if not os.path.exists(TESTDIR):
os.mkdir(TESTDIR)
# search for available fortran (and c?) compilers
POSSIBLE_FORTRAN_COMPILERS = ['g95', 'gfortran', 'ifort', 'pgf90', 'f90']
AVAILABLE_POSSIBLE_COMPILERS = []
for fc in POSSIBLE_FORTRAN_COMPILERS:
cmd = 'which '+fc
# print('running: ',cmd)
(lines_stdout, lines_stderr) = run_shell_command(cmd)
# print('(lines_stdout, lines_stderr) = ',(lines_stdout, lines_stderr))
if len(lines_stderr) == 0:
AVAILABLE_POSSIBLE_COMPILERS.append(fc)
print('available fortran compilers: ',
', '.join(fc for fc in AVAILABLE_POSSIBLE_COMPILERS))
# #]
TESTRESULTS = []
for fc in AVAILABLE_POSSIBLE_COMPILERS:
if DO_MANUAL_TESTS:
# #[ build using the manual build script and check the result
# -create a temporary working dir for the build
build_dir_name = 'manual_build_'+fc
temp_build_dir = os.path.join(TESTDIR, build_dir_name)
if os.path.exists(temp_build_dir):
print('dir: ', temp_build_dir, ' exists; removing it first')
cmd = '\\rm -rf '+temp_build_dir
os.system(cmd)
# not needed, the clone command creates the dir
# print('creating dir: ', temp_build_dir)
# os.mkdir(temp_build_dir)
# -clone the repository
cmd = 'cd '+TESTDIR+'; hg clone '+REPODIR+' '+build_dir_name
print("Executing command: ", cmd)
os.system(cmd)
# step into the test dir
saved_cwd = os.getcwd()
os.chdir(temp_build_dir)
print('saved_cwd = ', saved_cwd)
print('os.getcwd() = ', os.getcwd())
# -build the software
sys.path.append(os.getcwd())
# print('sys.path = ', sys.path)
from build_interface import InstallBUFRInterfaceECMWF
from pybufr_ecmwf.custom_exceptions import BuildException
BI = InstallBUFRInterfaceECMWF(verbose=True,
preferred_fortran_compiler=fc,
download_library_sources=False)
# make sure we are in the right directory
BUILD_DIR = 'pybufr_ecmwf'
os.chdir(BUILD_DIR)
this_result = []
build_succesfull = True
try:
BI.build()
except BuildException:
this_result.append('manual build failed for compiler: '+fc)
build_succesfull = False
del BI
# restore the original directory
os.chdir(saved_cwd)
# -verify the presence of the generated ecmwfbufr.so file
so_files = glob.glob(os.path.join(temp_build_dir, BUILD_DIR,
'ecmwfbufr.so'))
this_result.append('test results for manual build for: '+fc)
if len(so_files) > 0:
this_result.append('so file found: '+so_files[0])
else:
this_result.append('ERROR: so file NOT found!')
if build_succesfull:
# -run the unit tests
cmd = 'cd '+temp_build_dir+';'+\
'python ./unittests.py'
# os.system(cmd)
(lines_stdout, lines_stderr) = run_shell_command(cmd)
for l in lines_stdout:
this_result.append('STDOUT: '+l.replace('\n', ''))
for l in lines_stderr:
this_result.append('STDERR: '+l.replace('\n', ''))
else:
this_result.append('unittests skipped')
TESTRESULTS.append(this_result)
# #]
if DO_SETUP_BUILD_TESTS:
# #[ build using the setup tool and check the result
# -create a temporary working dir for the build
build_dir_name = 'build_'+fc
temp_build_dir = os.path.join(TESTDIR, build_dir_name)
if os.path.exists(temp_build_dir):
print('dir: ', temp_build_dir, ' exists; removing it first')
cmd = '\\rm -rf '+temp_build_dir
os.system(cmd)
# -clone the repository
cmd = 'cd '+TESTDIR+'; hg clone '+REPODIR+' '+build_dir_name
print("Executing command: ", cmd)
os.system(cmd)
# -edit the setup.cfg file to choose the compiler
# and to prevent the library download
cfg_file = os.path.join(temp_build_dir, 'setup.cfg')
cfg_lines = open(cfg_file).readlines()
# save a backup copy by moving the original
# os.rename(cfg_file, cfg_file+'.orig')
fd = open(cfg_file, 'w')
for l in cfg_lines:
if 'preferred_fortran_compiler' in l:
fd.write('preferred_fortran_compiler='+fc+'\n')
elif 'download_library_sources' in l:
fd.write('download_library_sources = False\n')
else:
fd.write(l)
fd.close()
# commit the modified code, to track what happended incase
# I would pull from this modified repository by mistake.
cmd = 'cd '+temp_build_dir+';'+\
'hg commit -m "automatic commit by the build_test tool"'
(lines_stdout, lines_stderr) = run_shell_command(cmd)
# -build the software
cmd = 'cd '+temp_build_dir+';'+\
'python ./setup.py build'
os.system(cmd)
# -verify the presence of the generated ecmwfbufr.so file
so_files = glob.glob(os.path.join(temp_build_dir,
'build/lib*/pybufr_ecmwf',
'ecmwfbufr.so'))
this_result = []
this_result.append('test results for build for: '+fc)
if len(so_files) > 0:
this_result.append('so file found: '+so_files[0])
else:
this_result.append('ERROR: so file NOT found!')
# -run the unit tests
try:
env = os.environ
env['PYTHONPATH'] = os.path.split(so_files[0])[0]
cmd = 'cd '+temp_build_dir+';'+\
'python ./unittests.py'
# os.system(cmd)
(lines_stdout, lines_stderr) = run_shell_command(cmd, sp_env=env)
for l in lines_stdout:
this_result.append('STDOUT: '+l.replace('\n', ''))
for l in lines_stderr:
this_result.append('STDERR: '+l.replace('\n', ''))
except IndexError:
this_result.append('skipping unittests')
TESTRESULTS.append(this_result)
# #]
if DO_SETUP_SDIST_TESTS:
# #[ build a tarfile using setup sdist, unpack, build and check
# -create a temporary working dir for the build
build_dir_name = 'build_sdist_'+fc
temp_build_dir = os.path.join(TESTDIR, build_dir_name)
if os.path.exists(temp_build_dir):
print('dir: ', temp_build_dir, ' exists; removing it first')
cmd = '\\rm -rf '+temp_build_dir
os.system(cmd)
# -clone the repository
cmd = 'cd '+TESTDIR+'; hg clone '+REPODIR+' '+build_dir_name
print("Executing command: ", cmd)
os.system(cmd)
# -edit the setup.cfg file to choose the compiler
# and to prevent the library download
cfg_file = os.path.join(temp_build_dir, 'setup.cfg')
cfg_lines = open(cfg_file).readlines()
# save a backup copy by moving the original
# os.rename(cfg_file, cfg_file+'.orig')
fd = open(cfg_file, 'w')
for l in cfg_lines:
if 'preferred_fortran_compiler' in l:
fd.write('preferred_fortran_compiler='+fc+'\n')
elif 'download_library_sources' in l:
fd.write('download_library_sources = False\n')
else:
fd.write(l)
fd.close()
# commit the modified code, to track what happended incase
# I would pull from this modified repository by mistake.
cmd = 'cd '+temp_build_dir+';'+\
'hg commit -m "automatic commit by the build_test tool"'
(lines_stdout, lines_stderr) = run_shell_command(cmd)
# -build the software
cmd = 'cd '+temp_build_dir+';'+\
'python ./setup.py sdist'
os.system(cmd)
# -verify the presence of the generated tar file
pattern = os.path.join(temp_build_dir, 'dist/pybufr-ecmwf*gz')
tar_files = glob.glob(pattern)
this_result = []
this_result.append('test results for setup sdist test for: '+fc)
if len(tar_files) > 0:
this_result.append('tar file found: '+tar_files[0])
else:
this_result.append('ERROR: tar file NOT found!')
# create a test directory
test_dir = os.path.join(temp_build_dir, 'temp_test')
os.makedirs(test_dir)
# split path and filename
(tar_path, tar_file) = os.path.split(tar_files[0])
# unpack the created ter file
cmd = 'cd '+test_dir+'; tar zxvf ../dist/'+tar_file
os.system(cmd)
pattern = os.path.join(test_dir, 'pybufr-ecmwf-*')
unpacked_sdist_path = glob.glob(pattern)[0]
# print('TESTJOS: unpacked_sdist_path = ', unpacked_sdist_path)
# -edit the setup.cfg file to choose the compiler
# and to prevent the library download
cfg_file = os.path.join(unpacked_sdist_path, 'setup.cfg')
cfg_lines = open(cfg_file).readlines()
# save a backup copy by moving the original
#os.rename(cfg_file, cfg_file+'.orig')
# this mod is not needed since the sdist takes the
# modification made before running the setup sdist
# command with it.
fd = open(cfg_file, 'w')
for l in cfg_lines:
if 'preferred_fortran_compiler' in l:
fd.write('preferred_fortran_compiler='+fc+'\n')
elif 'download_library_sources' in l:
fd.write('download_library_sources = False\n')
else:
fd.write(l)
fd.close()
# -build the software
cmd = 'cd '+unpacked_sdist_path+';'+\
'python ./setup.py build'
os.system(cmd)
# -verify the presence of the generated ecmwfbufr.so file
so_files = glob.glob(os.path.join(unpacked_sdist_path,
'build/lib*/pybufr_ecmwf',
'ecmwfbufr.so'))
this_result.append('test results for sdist build for: '+fc)
if len(so_files) > 0:
this_result.append('so file found: '+so_files[0])
else:
this_result.append('ERROR: so file NOT found!')
# -run the unit tests
cmd = 'cd '+unpacked_sdist_path+';'+\
'python ./unittests.py'
# os.system(cmd)
(lines_stdout, lines_stderr) = run_shell_command(cmd)
for l in lines_stdout:
this_result.append('STDOUT: '+l.replace('\n', ''))
for l in lines_stderr:
this_result.append('STDERR: '+l.replace('\n', ''))
TESTRESULTS.append(this_result)
# #]
if DO_PIP_INSTALL_TESTS:
# #[ build a tarfile using setup sdist, unpack, build and check
# -create a temporary working dir for the build
build_dir_name = 'build_pip_'+fc
temp_build_dir = os.path.join(TESTDIR, build_dir_name)
if os.path.exists(temp_build_dir):
print('dir: ', temp_build_dir, ' exists; removing it first')
cmd = '\\rm -rf '+temp_build_dir
os.system(cmd)
# -clone the repository
cmd = 'cd '+TESTDIR+'; hg clone '+REPODIR+' '+build_dir_name
print("Executing command: ", cmd)
os.system(cmd)
# -edit the setup.cfg file to choose the compiler
# and to prevent the library download
cfg_file = os.path.join(temp_build_dir, 'setup.cfg')
cfg_lines = open(cfg_file).readlines()
# save a backup copy by moving the original
# os.rename(cfg_file, cfg_file+'.orig')
fd = open(cfg_file, 'w')
for l in cfg_lines:
if 'preferred_fortran_compiler' in l:
fd.write('preferred_fortran_compiler='+fc+'\n')
elif 'download_library_sources' in l:
fd.write('download_library_sources = False\n')
else:
fd.write(l)
fd.close()
# commit the modified code, to track what happended incase
# I would pull from this modified repository by mistake.
cmd = 'cd '+temp_build_dir+';'+\
'hg commit -m "automatic commit by the build_test tool"'
(lines_stdout, lines_stderr) = run_shell_command(cmd)
# -build the software
cmd = 'cd '+temp_build_dir+';'+\
'python ./setup.py sdist'
os.system(cmd)
# -verify the presence of the generated tar file
pattern = os.path.join(temp_build_dir, 'dist/pybufr-ecmwf*gz')
tar_files = glob.glob(pattern)
this_result = []
this_result.append('test results for pip test for: '+fc)
if len(tar_files) > 0:
this_result.append('tar file found: '+tar_files[0])
else:
this_result.append('ERROR: tar file NOT found!')
# create a test directory
test_dir = os.path.join(temp_build_dir, 'temp_test')
os.makedirs(test_dir)
# split path and filename
(tar_path, tar_file) = os.path.split(tar_files[0])
# setup a virtualenv and try to install the package in it
cmds = ['cd '+test_dir,
'virtualenv myenv --system-site-packages',
'source myenv/bin/activate',
'pip install pybufr-ecmwf --no-index '+\
'--find-links '+\
os.path.abspath(os.path.join(temp_build_dir, 'dist')),
'python -c "import pybufr_ecmwf;'+\
'print \'pybufr_ecmwf.__file__ = \', pybufr_ecmwf.__file__"',
'deactivate',
'rm -rf myenv/']
cmd = ';'.join(c for c in cmds)
print('executing cmd: [{}]'.format(cmd))
(lines_stdout, lines_stderr) = run_shell_command(cmd)
# -verify the output
module_properly_loaded = False
for l in lines_stdout:
if ('pybufr_ecmwf.__file__' in l) and ('__init__.py' in l):
module_properly_loaded = True
if module_properly_loaded:
this_result.append('module_properly_loaded.')
else:
this_result.append('ERROR: something seems wrong: '+\
'module not properlyloaded!')
TESTRESULTS.append(this_result)
# #]
if DO_MANUAL_PY3_TESTS:
# #[ convert to python3, and test the manual build
# -create a temporary working dir for the build
build_dir_name = 'py3_manual_build_'+fc
temp_build_dir = os.path.join(TESTDIR, build_dir_name)
if os.path.exists(temp_build_dir):
print('dir: ', temp_build_dir, ' exists; removing it first')
cmd = '\\rm -rf '+temp_build_dir
os.system(cmd)
# not needed, the clone command creates the dir
# print('creating dir: ', temp_build_dir)
# os.mkdir(temp_build_dir)
# -clone the repository
cmd = 'cd '+TESTDIR+'; hg clone '+REPODIR+' '+build_dir_name
print("Executing command: ", cmd)
os.system(cmd)
# step into the test dir
saved_cwd = os.getcwd()
os.chdir(temp_build_dir)
print('temp_build_dir = ', temp_build_dir)
print('saved_cwd = ', saved_cwd)
print('os.getcwd() = ', os.getcwd())
# convert the source code to python3
cmd = './port_2to3.py'
print("Executing command: ", cmd)
os.system(cmd)
temp_py3_build_dir = 'tmp_2to3_converted_sources'
os.chdir(temp_py3_build_dir)
print('temp_py3_build_dir = ', temp_py3_build_dir)
print('os.getcwd() = ', os.getcwd())
# -build the software
sys.path.append(os.getcwd())
# print('sys.path = ', sys.path)
# note: this re-import is intentional, since this should be a
# different copy, then the one used by DO_MANUAL_TESTS,
# so temporarily disable the re-import pylint warning
#pylint: disable=W0404
from build_interface import InstallBUFRInterfaceECMWF
#pylint: enable=W0404
BI = InstallBUFRInterfaceECMWF(verbose=True,
preferred_fortran_compiler=fc,
download_library_sources=False)
# make sure we are in the right directory
BUILD_DIR = 'pybufr_ecmwf'
os.chdir(BUILD_DIR)
BI.build()
del BI
# restore the original directory
os.chdir(saved_cwd)
# -verify the presence of the generated ecmwfbufr.so file
so_files = glob.glob(os.path.join(temp_build_dir, BUILD_DIR,
'ecmwfbufr.*.so'))
this_result = []
this_result.append('test results for manual build for: '+fc)
if len(so_files) > 0:
this_result.append('so file found: '+so_files[0])
else:
this_result.append('ERROR: so file NOT found!')
# -run the unit tests
cmd = 'cd '+temp_build_dir+';'+\
'python3 ./unittests.py'
# os.system(cmd)
(lines_stdout, lines_stderr) = run_shell_command(cmd)
for l in lines_stdout:
this_result.append('STDOUT: '+l.replace('\n', ''))
for l in lines_stderr:
this_result.append('STDERR: '+l.replace('\n', ''))
TESTRESULTS.append(this_result)
# #]
print(50*'=')
for result in TESTRESULTS:
# #[ display the results
for l in result:
print(l)
print(50*'=')
# #]