forked from hainm/ambertools-binary-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_all.py
441 lines (381 loc) · 15.1 KB
/
build_all.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
# WARNING: Your current working dir must be outside amber tree.
# See usage below
# Known working version
# conda-build: 2.1.17
''' Building AmberTools binary for conda and non-conda install
for python 2.7, 3.4, 3.5, 3.6 for Linux and MacOS
Require: MacOS + docker + Python stuff + two cups of coffee.
How to run?
mkdir $HOME/TMP # or any name/dir you want
cd $HOME/TMP
python /path/to/AMBERHOME/AmberTools/src/conda_tools/build_all.py
# use -h or --help for further usage
Expectation
This script will create a folder tree (below) in current folder:
amber-conda-bld:
linux-64
osx-64
'''
import os
import sys
import subprocess
import argparse
from glob import glob
CONTAINER_FOLDER = 'amber-conda-bld'
AMBER_BINARY_BUILD_DIR = os.path.abspath(os.path.dirname(__file__))
CONDA_TOOLS_DIR = os.path.join(AMBER_BINARY_BUILD_DIR, 'conda_tools')
DOCKER_BUILD_SCRIPT = os.path.join(
AMBER_BINARY_BUILD_DIR,
'conda-recipe/scripts/run_docker_build.sh'
)
BZ2_FILES = []
def write_meta_file(amber_ver):
# assume amber_ver is this format 17.4
version, bugfix = amber_ver.split('.')
recipe_dir = os.path.join(os.path.dirname(__file__),
'conda-ambertools-all-python')
with open(os.path.join(recipe_dir, 'meta.template')) as fh, \
open(os.path.join(recipe_dir, 'meta.yaml'), 'w') as fh_new:
content = fh.read()
content = content.replace('{% set version = "0" %}',
'{{% set version = "{}" %}}'.format(version))
content = content.replace('{% set bugfix_version = "0" %}',
'{{% set bugfix_version = "{}" %}}'.format(bugfix))
fh_new.write(content)
def assert_amber_src_exists(amberhome):
if not os.path.exists(amberhome+ '/AmberTools'):
print("AmberTools does not exist in {}".format(amberhome))
sys.exit(1)
def built_tarfile_dir(build_commands):
built_file = subprocess.check_output(build_commands + ['--output'
]).decode().strip()
return built_file
def sh(command):
subprocess.check_call(command, shell=True)
def add_path(my_path='/usr/local/gfortran/bin'):
os.environ['PATH'] = my_path + ':' + os.environ.get('PATH', '')
def copy_tarfile_to_build_folder(build_commands,
container_folder,
dry_run=False):
built_file = built_tarfile_dir(build_commands)
copy_comands = ['cp', built_file, container_folder]
if dry_run:
print(copy_comands)
else:
subprocess.check_call(copy_comands)
BZ2_FILES.append(
os.path.join(container_folder, os.path.basename(built_file)))
def build_all_python_verions_in_one_package(container_folder, opt,
extend_versionss=('3.4', '3.5', '3.6')):
# build full AmberTools for python 2.7 first
print("Start with python 2.7. Additional versions", extend_versionss)
os.environ['AMBER_BUILD_TASK'] = 'ambertools'
recipe_dir = os.path.abspath(os.path.join(AMBER_BINARY_BUILD_DIR, 'conda-recipe'))
tmp_recipe_dir = os.path.abspath(
os.path.join(AMBER_BINARY_BUILD_DIR, 'conda-multi-python'))
py2_build_command = ['conda', 'build', recipe_dir, '--py', '2.7']
if opt.skip_test:
py2_build_command.append('--no-test')
if opt.dry_run:
print(py2_build_command)
else:
subprocess.check_call(py2_build_command)
# build only python packages in AmberTools
for pyver in extend_versionss:
print('pyver', pyver)
build_command = ['conda', 'build', tmp_recipe_dir, '--py', pyver]
if opt.skip_test:
build_command.append("--no-test")
if opt.dry_run:
print(build_command)
else:
subprocess.check_call(build_command)
tarfile = built_tarfile_dir(build_command)
os.environ['AT_TEMP_FILE_FOLDER'] = os.path.dirname(tarfile)
# build all
# copy all python packages for different python versions to
# original full AmberTools build (with python 2.7)
recipe_dir = tmp_recipe_dir + '/../conda-ambertools-all-python'
combine_command = ['conda', 'build', recipe_dir]
if opt.skip_test:
combine_command.append("--no-test")
if opt.dry_run:
print(combine_command)
else:
subprocess.check_call(combine_command)
# sh('cp {} .'.format(built_tarfile_dir(combine_command)))
copy_tarfile_to_build_folder(
combine_command, container_folder, dry_run=opt.dry_run)
def perform_build_with_docker(opt, container_folder, py_versions=[
'2.7',
]):
build_task = opt.build_task
if opt.build_task == 'ambertools_pack_all_pythons':
final_python_versions = [
'2.7'
]
print("Ignoring {}".format(str(py_versions)))
else:
final_python_versions = py_versions
all_tarfiles = []
for ver in final_python_versions:
# find AT file path in docker run
print('docker, ver', ver)
docker_command_build = [
'bash',
DOCKER_BUILD_SCRIPT,
build_task,
ver,
opt.amberhome,
AMBER_BINARY_BUILD_DIR,
opt.ambertools_version,
]
print('docker_command_build')
print(" ".join(docker_command_build))
# dry run to immediately getting output.
output = subprocess.check_output(docker_command_build + [
'True',
]).decode()
# find package name from output generated from docker build.
line_having_AT = [
line for line in output.split('\n')
if "['cp', '/root/miniconda3/conda-bld/linux-64/" in line
][0]
tarfile = os.path.basename(
line_having_AT.split(',')[1].replace("'", ''))
all_tarfiles.append(tarfile)
if not opt.dry_run:
subprocess.check_call(docker_command_build + [
'False',
])
else:
print(docker_command_build + ['True'])
print(output)
for tarfile in all_tarfiles:
abspath_tarfile = os.path.join(opt.amberhome, 'linux-64', tarfile)
move_command = ['mv', abspath_tarfile, container_folder]
if opt.sudo:
move_command.insert(0, 'sudo')
BZ2_FILES.append(os.path.join(container_folder, tarfile))
if opt.dry_run:
print(move_command)
else:
subprocess.check_call(move_command)
def perform_build_without_docker(opt,
recipe_dir,
container_folder,
py_versions=[
'2.7',
]):
if opt.build_task != 'ambermini':
if opt.build_task == 'ambertools_pack_all_pythons':
print('py_versions', py_versions)
print('Build a single AmberTools with different Python versions')
final_version = py_versions[:]
print('final_version', final_version)
try:
# we will build py2.7 seperately
final_version.remove('2.7')
except ValueError:
pass
print('final_version', final_version)
build_all_python_verions_in_one_package(
container_folder=container_folder, opt=opt,
extend_versionss=tuple(final_version))
else:
for ver in py_versions:
build_commands = ['conda', 'build', recipe_dir, '--py', ver]
if opt.skip_test:
build_commands.append("--no-test")
if opt.dry_run:
print(build_commands)
else:
subprocess.call(build_commands)
copy_tarfile_to_build_folder(
build_commands, container_folder, dry_run=opt.dry_run)
else:
# build ambermini, don't require python
print("Building ambermini, ignore {}".format(py_versions))
amber_mini_recipe_dir = recipe_dir + '/../conda-ambermini-recipe'
build_commands = ['conda', 'build', amber_mini_recipe_dir]
if opt.skip_test:
build_commands.append("--no-test")
if opt.dry_run:
print(build_commands)
else:
subprocess.call(build_commands)
copy_tarfile_to_build_folder(
build_commands, container_folder, dry_run=opt.dry_run)
def main(args=None):
global CONTAINER_FOLDER, DOCKER_BUILD_SCRIPT, BZ2_FILES
parser = argparse.ArgumentParser()
parser.add_argument(
"amberhome", help="Path to amber source code")
parser.add_argument(
"--exclude-linux",
action='store_true',
dest="exclude_linux",
help="no Linux build")
parser.add_argument(
"--exclude-osx",
action='store_true',
dest="exclude_osx",
help="No OSX build")
parser.add_argument(
"--exclude-non-conda-user",
action='store_true',
dest="exclude_non_conda_user",
help="Exclude build for non-conda-user")
parser.add_argument(
"--post-processing-all",
action='store_true',
dest="post_processing_all",
help="Post processing all found packages for non-conda install")
parser.add_argument(
"-t",
"--task",
type=str,
default='ambertools',
dest="build_task",
help="which target you want to build? (default ambertools?)")
parser.add_argument(
'-d', "--dry-run", action='store_true', dest="dry_run", help="dry run")
parser.add_argument(
"--skip-test", action='store_true', dest="skip_test", help="do not run tests")
parser.add_argument(
'--py',
'--py-version',
default=None,
help=
'Python version. Default: build all versions (2.7, 3.4, 3.5, 3.6)')
parser.add_argument(
'-v',
'--ambertools-version',
help=
'AmberTools version, must be something like 17.4',
required=True)
parser.add_argument(
'--no-docker',
action='store_true',
dest="no_docker",
help=
'Not using docker for building Linux target. This is only for testing, otherwise you must use centos5'
)
parser.add_argument(
'--date',
action='store_true',
help=
'If given, add date to non-conda package (mostly for phenix intergration)'
)
parser.add_argument(
'--sudo',
action='store_true',
help='use sudo to move files. Note: for circleci')
opt = parser.parse_args(args)
opt.amberhome = os.path.abspath(opt.amberhome)
if opt.build_task in ['ambertools', 'ambertools_pack_all_pythons']:
py_versions = ([str(opt.py),] if opt.py not in [None, 'None']
else ['2.7', '3.4', '3.5', '3.6'])
opt.build_task = 'ambertools_pack_all_pythons'
else:
py_versions = [
str(opt.py),
]
if sys.platform.startswith('darwin'):
# force macos build to use gfortran/gcc/g++
add_path(my_path='/usr/local/gfortran/bin')
write_meta_file(opt.ambertools_version)
opt.amberhome = opt.amberhome or os.path.abspath(AMBER_BINARY_BUILD_DIR + '/../../../')
assert_amber_src_exists(opt.amberhome)
ORIGINAL_FOLDER = os.getcwd()
print("Current directory = {}".format(ORIGINAL_FOLDER))
# store built files
if not os.path.exists(CONTAINER_FOLDER):
os.mkdir(CONTAINER_FOLDER)
CONTAINER_FOLDER = os.path.abspath(CONTAINER_FOLDER)
container_folder_osx = os.path.join(CONTAINER_FOLDER, 'osx-64')
if not os.path.exists(container_folder_osx):
os.mkdir(container_folder_osx)
container_folder_osx = os.path.abspath(container_folder_osx)
container_folder_linux = os.path.join(CONTAINER_FOLDER, 'linux-64')
if not os.path.exists(container_folder_linux):
os.mkdir(container_folder_linux)
container_folder_linux = os.path.abspath(container_folder_linux)
recipe_dir = os.path.join(AMBER_BINARY_BUILD_DIR, 'conda-recipe')
ambertools_src = os.path.abspath(os.path.join(opt.amberhome, 'AmberTools', 'src'))
pack_non_conda_package_script = os.path.join(
CONDA_TOOLS_DIR, 'pack_non_conda.py')
assert os.path.exists(recipe_dir)
assert os.path.exists(ambertools_src)
assert os.path.exists(DOCKER_BUILD_SCRIPT)
assert os.path.exists(pack_non_conda_package_script)
build_task = opt.build_task
print('AMBER_BUILD_TASK = {}'.format(build_task))
print('AMBERHOME = {}'.format(opt.amberhome))
print('recipe dir = {}'.format(recipe_dir))
# go to AmberTools/src
# We do not got here since conda-build try to remove "dat" folder. ack.
os.chdir(ambertools_src)
print('Current working dir = {}'.format(os.getcwd()))
os.environ['AMBER_BUILD_TASK'] = build_task
os.environ['AMBER_SRC'] = opt.amberhome
# OSX build using your MacOS computer
if not opt.exclude_osx:
print("Start MacOS build")
perform_build_without_docker(
opt,
recipe_dir=recipe_dir,
container_folder=container_folder_osx,
py_versions=py_versions)
if not opt.exclude_linux:
if opt.no_docker:
perform_build_without_docker(
opt,
recipe_dir=recipe_dir,
container_folder=container_folder_linux,
py_versions=py_versions)
else:
print("Linux build via docker container")
os.chdir(opt.amberhome)
print('Current working dir = {}'.format(os.getcwd()))
# ambermini do not require python but still need to pass here
# since run_docker_build.sh require 2nd argument
# TODO: remove that
final_verions = [
'2.7',
] if build_task == 'ambermini' else py_versions
perform_build_with_docker(
opt=opt,
container_folder=container_folder_linux,
py_versions=final_verions)
# Post-process conda-built packages for non-conda users
if not opt.exclude_non_conda_user:
print("Post processing conda packages for non-conda user")
os.chdir(CONTAINER_FOLDER)
bz2_files = []
if opt.post_processing_all:
bz2_files = glob('linux-64/*bz2') + glob('osx-64/*bz2')
else:
bz2_files = BZ2_FILES
print('BZ2_FILES', bz2_files)
final_files = bz2_files[:]
for fn in bz2_files:
command_pack = ['python', pack_non_conda_package_script, fn]
if opt.date:
command_pack.append('--date')
command_pack_dry_run = command_pack + ['-d']
out_fn = [
line.split()[-1]
for line in (subprocess.check_output(command_pack_dry_run)
.decode().split('\n'))
if 'ABSOLUTE FILENAME DIR' in line
].pop()
final_files.append(out_fn)
print(command_pack)
if not opt.dry_run:
subprocess.check_call(command_pack)
print("FINAL")
for fn in final_files:
print(fn)
if __name__ == '__main__':
main()