Skip to content

Commit 1d088fc

Browse files
author
Ross Bayer
committed
[Build System: update-checkout] Move the run_parallel and check_parallel_results functions from swift_build_support into update_checkout which is the only caller.
1 parent 525708e commit 1d088fc

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

utils/swift_build_support/swift_build_support/shell.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import subprocess
2323
import sys
2424
from contextlib import contextmanager
25-
from multiprocessing import Lock, Pool, cpu_count
2625

2726
from . import diagnostics
2827

@@ -230,37 +229,3 @@ def run(*args, **kwargs):
230229
eout.stderr = stderr
231230
raise eout
232231
return (stdout, 0, args)
233-
234-
235-
def init(l):
236-
global lock
237-
lock = l
238-
239-
240-
def run_parallel(fn, pool_args, n_processes=0):
241-
if n_processes == 0:
242-
n_processes = cpu_count() * 2
243-
244-
lk = Lock()
245-
print("Running ``%s`` with up to %d processes." %
246-
(fn.__name__, n_processes))
247-
pool = Pool(processes=n_processes, initializer=init, initargs=(lk,))
248-
results = pool.map_async(func=fn, iterable=pool_args).get(999999)
249-
pool.close()
250-
pool.join()
251-
return results
252-
253-
254-
def check_parallel_results(results, op):
255-
fail_count = 0
256-
if results is None:
257-
return 0
258-
for r in results:
259-
if r is not None:
260-
if fail_count == 0:
261-
print("======%s FAILURES======" % op)
262-
print("%s failed (ret=%d): %s" % (r.repo_path, r.ret, r))
263-
fail_count += 1
264-
if r.stderr:
265-
print(r.stderr)
266-
return fail_count

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import sys
1919
import traceback
2020
from functools import reduce
21-
from multiprocessing import freeze_support
21+
from multiprocessing import Lock, Pool, cpu_count, freeze_support
2222

2323
from swift_build_support.swift_build_support import shell
2424
from swift_build_support.swift_build_support.SwiftBuildSupport import \
@@ -29,6 +29,53 @@
2929
SCRIPT_DIR = os.path.dirname(SCRIPT_FILE)
3030

3131

32+
def run_parallel(fn, pool_args, n_processes=0):
33+
"""Function used to run a given closure in parallel.
34+
35+
NOTE: This function was originally located in the shell module of
36+
swift_build_support and should eventually be replaced with a better
37+
parallel implementation.
38+
"""
39+
40+
def init(l):
41+
global lock
42+
lock = l
43+
44+
if n_processes == 0:
45+
n_processes = cpu_count() * 2
46+
47+
lk = Lock()
48+
print("Running ``%s`` with up to %d processes." %
49+
(fn.__name__, n_processes))
50+
pool = Pool(processes=n_processes, initializer=init, initargs=(lk,))
51+
results = pool.map_async(func=fn, iterable=pool_args).get(999999)
52+
pool.close()
53+
pool.join()
54+
return results
55+
56+
57+
def check_parallel_results(results, op):
58+
"""Function used to check the results of run_parallel.
59+
60+
NOTE: This function was originally located in the shell module of
61+
swift_build_support and should eventually be replaced with a better
62+
parallel implementation.
63+
"""
64+
65+
fail_count = 0
66+
if results is None:
67+
return 0
68+
for r in results:
69+
if r is not None:
70+
if fail_count == 0:
71+
print("======%s FAILURES======" % op)
72+
print("%s failed (ret=%d): %s" % (r.repo_path, r.ret, r))
73+
fail_count += 1
74+
if r.stderr:
75+
print(r.stderr)
76+
return fail_count
77+
78+
3279
def confirm_tag_in_repo(tag, repo_name):
3380
tag_exists = shell.capture(['git', 'ls-remote', '--tags',
3481
'origin', tag], echo=False)
@@ -200,8 +247,7 @@ def update_all_repositories(args, config, scheme_name, cross_repos_pr):
200247
cross_repos_pr]
201248
pool_args.append(my_args)
202249

203-
return shell.run_parallel(update_single_repository, pool_args,
204-
args.n_processes)
250+
return run_parallel(update_single_repository, pool_args, args.n_processes)
205251

206252

207253
def obtain_additional_swift_sources(pool_args):
@@ -295,8 +341,8 @@ def obtain_all_additional_swift_sources(args, config, with_ssh, scheme_name,
295341
print("Not cloning any repositories.")
296342
return
297343

298-
return shell.run_parallel(obtain_additional_swift_sources, pool_args,
299-
args.n_processes)
344+
return run_parallel(
345+
obtain_additional_swift_sources, pool_args, args.n_processes)
300346

301347

302348
def dump_repo_hashes(args, config, branch_scheme_name='repro'):
@@ -546,8 +592,8 @@ def main():
546592
update_results = update_all_repositories(args, config, scheme,
547593
cross_repos_pr)
548594
fail_count = 0
549-
fail_count += shell.check_parallel_results(clone_results, "CLONE")
550-
fail_count += shell.check_parallel_results(update_results, "UPDATE")
595+
fail_count += check_parallel_results(clone_results, "CLONE")
596+
fail_count += check_parallel_results(update_results, "UPDATE")
551597
if fail_count > 0:
552598
print("update-checkout failed, fix errors and try again")
553599
else:

0 commit comments

Comments
 (0)