OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Automates running BuildPackageLists, BuildSysroot, and | |
7 UploadSysroot for each supported arch of each sysroot creator. | |
8 """ | |
9 | |
10 import hashlib | |
11 import json | |
12 import multiprocessing | |
13 import os | |
14 import re | |
15 import string | |
16 import subprocess | |
17 import sys | |
18 | |
19 def run_script(args): | |
20 fnull = open(os.devnull, 'w') | |
21 subprocess.check_call(args, stdout=fnull, stderr=fnull) | |
22 | |
23 def sha1sumfile(filename): | |
24 sha1 = hashlib.sha1() | |
25 with open(filename, 'rb') as f: | |
26 while True: | |
27 data = f.read(65536) | |
28 if not data: | |
29 break | |
30 sha1.update(data) | |
31 return sha1.hexdigest() | |
32 | |
33 def get_proc_output(args): | |
34 return subprocess.check_output(args).strip() | |
35 | |
36 def build_and_upload(script_path, distro, release, arch, lock): | |
37 # TODO(thomasanderson): Find out which revision 'git-cl upload' uses to | |
38 # calculate the diff against and use that instead of HEAD. | |
39 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
40 revision = get_proc_output(['git', '-C', script_dir, 'rev-parse', 'HEAD']) | |
41 | |
42 run_script([script_path, 'UpdatePackageLists%s' % arch]) | |
43 run_script([script_path, 'BuildSysroot%s' % arch]) | |
44 run_script([script_path, 'UploadSysroot%s' % arch, revision]) | |
45 | |
46 tarball = '%s_%s_%s_sysroot.tgz' % (distro, release, arch.lower()) | |
47 tgz_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
48 "..", "..", "..", "out", "sysroot-build", | |
49 release, tarball) | |
50 sha1sum = sha1sumfile(tgz_path) | |
51 sysroot_dir = '%s_%s_%s-sysroot' % (distro, release, arch.lower()) | |
52 | |
53 sysroot_metadata = { | |
54 'Revision': revision, | |
55 'Tarball': tarball, | |
56 'Sha1Sum': sha1sum, | |
57 'SysrootDir': sysroot_dir | |
58 } | |
59 lock.acquire() | |
60 try: | |
Sam Clegg
2016/12/12 22:11:52
I seems like "with lock:" should work here.
Tom (Use chromium acct)
2016/12/13 00:06:19
Done.
| |
61 with open('sysroots.json', 'rw+') as f: | |
62 sysroots = json.load(f) | |
63 sysroots["%s_%s" % (release, arch.lower())] = sysroot_metadata | |
64 f.seek(0) | |
65 f.truncate() | |
66 f.write(json.dumps(sysroots, sort_keys=True, indent=4)) | |
67 f.write('\n') | |
68 f.close() | |
Sam Clegg
2016/12/12 22:11:52
No need with close here right?
Tom (Use chromium acct)
2016/12/13 00:06:19
Done.
| |
69 finally: | |
70 lock.release() | |
71 | |
72 def build_and_upload_all(): | |
73 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
74 procs = [] | |
75 lock = multiprocessing.Lock() | |
76 for filename in os.listdir(script_dir): | |
77 if (not os.path.isfile(os.path.join(script_dir, filename)) or | |
78 not re.search(r'sysroot-creator-.+\.sh', filename)): | |
79 continue | |
Sam Clegg
2016/12/12 22:11:52
How about just:
for filename in glob.glob(os.path
Tom (Use chromium acct)
2016/12/13 00:06:19
Done
| |
80 script_path = os.path.join(script_dir, filename) | |
81 distro = get_proc_output([script_path, 'PrintDistro']) | |
82 release = get_proc_output([script_path, 'PrintRelease']) | |
83 architectures = get_proc_output([script_path, 'PrintArchitectures']) | |
84 for arch in architectures.split('\n'): | |
85 proc = multiprocessing.Process(target=build_and_upload, | |
86 args=(script_path, distro, release, arch, | |
87 lock)) | |
88 procs.append(("%s %s (%s)" % (distro, release, arch), proc)) | |
89 proc.start() | |
90 for (_, proc) in procs: | |
91 proc.join() | |
92 | |
93 print "SYSROOT CREATION SUMMARY" | |
94 for (name, proc) in procs: | |
Sam Clegg
2016/12/12 22:11:51
nit: no need to braces when unpacking tuples like
Tom (Use chromium acct)
2016/12/13 00:06:19
Done.
| |
95 status = "FAILURE" if proc.exitcode else "SUCCESS" | |
96 print "%s sysroot creation\t%s" % (name, status) | |
97 | |
98 if __name__ == '__main__': | |
99 build_and_upload_all() | |
Sam Clegg
2016/12/12 22:11:52
The pattern I've prefer is "sys.exit(main())" here
Tom (Use chromium acct)
2016/12/13 00:06:19
Done.
| |
OLD | NEW |