Commit 0defa4ed authored by Stefan Behnel's avatar Stefan Behnel

update and clean up cystdlib.py script

parent 902d7747
...@@ -5,13 +5,14 @@ Execute the script either in the CPython 'Lib' directory or pass the ...@@ -5,13 +5,14 @@ Execute the script either in the CPython 'Lib' directory or pass the
option '--current-python' to compile the standard library of the running option '--current-python' to compile the standard library of the running
Python interpreter. Python interpreter.
Pass '--parallel' to get a parallel build. Pass '-j N' to get a parallel build with N processes.
Usage example:: Usage example::
$ python cystdlib.py --current-python build_ext -i $ python cystdlib.py --current-python build_ext -i
""" """
import os
import sys import sys
from distutils.core import setup from distutils.core import setup
from Cython.Build import cythonize from Cython.Build import cythonize
...@@ -78,6 +79,7 @@ del special_directives[:] # currently unused ...@@ -78,6 +79,7 @@ del special_directives[:] # currently unused
def build_extensions(includes='**/*.py', def build_extensions(includes='**/*.py',
excludes=excludes+broken, excludes=excludes+broken,
special_directives=special_directives, special_directives=special_directives,
language_level=sys.version_info[0],
parallel=None): parallel=None):
if isinstance(includes, str): if isinstance(includes, str):
includes = [includes] includes = [includes]
...@@ -97,21 +99,25 @@ def build_extensions(includes='**/*.py', ...@@ -97,21 +99,25 @@ def build_extensions(includes='**/*.py',
cythonize(modules, cythonize(modules,
exclude=exclude_now, exclude=exclude_now,
exclude_failures=True, exclude_failures=True,
language_level=pyver, language_level=language_level,
compiler_directives=d, compiler_directives=d,
nthreads=parallel, nthreads=parallel,
)) ))
return extensions return extensions
def build(extensions): def build(extensions):
try: try:
setup(name = 'stuff', ext_modules = extensions) setup(ext_modules=extensions)
return extensions, True result = True
except: except:
import traceback import traceback
print('error building extensions %s' % ([ext.name for ext in extensions],)) print('error building extensions %s' % (
[ext.name for ext in extensions],))
traceback.print_exc() traceback.print_exc()
return extensions, False result = False
return extensions, result
def _build(args): def _build(args):
sys_args, ext = args sys_args, ext = args
...@@ -119,31 +125,47 @@ def _build(args): ...@@ -119,31 +125,47 @@ def _build(args):
return build([ext]) return build([ext])
def parse_args():
from optparse import OptionParser
parser = OptionParser('%prog [options] [LIB_DIR (default: ./Lib)]')
parser.add_option(
'--current-python', dest='current_python', action='store_true',
help='compile the stdlib of the running Python')
parser.add_option(
'-j', '--jobs', dest='parallel_jobs', metavar='N',
type=int, default=1,
help='run builds in N parallel jobs (default: 1)')
options, args = parser.parse_args()
if not args:
args = ['./Lib']
elif len(args) > 1:
parser.error('only one argument expected, got %d' % len(args))
return options, args
if __name__ == '__main__': if __name__ == '__main__':
import sys options, args = parse_args()
pyver = sys.version_info[0] if options.current_python:
try:
sys.argv.remove('--current-python')
except ValueError:
pass
else:
# assume that the stdlib is where the "os" module lives # assume that the stdlib is where the "os" module lives
import os
os.chdir(os.path.dirname(os.__file__)) os.chdir(os.path.dirname(os.__file__))
else:
try: os.chdir(args[0])
sys.argv.remove('--parallel')
import multiprocessing pool = None
parallel_compiles = multiprocessing.cpu_count() * 2 parallel_jobs = options.parallel_jobs
print("Building in %d parallel processes" % parallel_compiles) if options.parallel_jobs:
except (ValueError, ImportError): try:
parallel_compiles = None import multiprocessing
pool = multiprocessing.Pool(parallel_jobs)
extensions = build_extensions(parallel=parallel_compiles) print("Building in %d parallel processes" % parallel_jobs)
if parallel_compiles: except (ImportError, OSError):
pool = multiprocessing.Pool(parallel_compiles) print("Not building in parallel")
sys_args = sys.argv[1:] parallel_jobs = 0
results = pool.map(_build, [ (sys_args, ext) for ext in extensions ])
extensions = build_extensions(parallel=parallel_jobs)
if pool is not None:
sys_args = ['-i']
results = pool.map(_build, [(sys_args, ext) for ext in extensions])
pool.close() pool.close()
pool.join() pool.join()
for ext, result in results: for ext, result in results:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment