def cython_compile(path_pattern, options): pool = None paths = map(os.path.abspath, extended_iglob(path_pattern)) try: for path in paths: if options.build_inplace: base_dir = path while not os.path.isdir(base_dir) or is_package_dir(base_dir): base_dir = os.path.dirname(base_dir) else: base_dir = None if os.path.isdir(path): # recursively compiling a package paths = [ os.path.join(path, '**', '*.%s' % ext) for ext in ('py', 'pyx') ] else: # assume it's a file(-like thing) paths = [path] cwd = os.getcwd() try: if base_dir: os.chdir(base_dir) ext_modules = cythonize(paths, nthreads=options.parallel, exclude_failures=options.keep_going, exclude=options.excludes, compiler_directives=options.directives, force=options.force, quiet=options.quiet, **options.options) finally: if base_dir: os.chdir(cwd) if ext_modules and options.build: if len(ext_modules) > 1 and options.parallel > 1: if pool is None: try: pool = multiprocessing.Pool(options.parallel) except OSError: pool = _FakePool() pool.map_async(run_distutils, [(base_dir, [ext]) for ext in ext_modules]) else: run_distutils((base_dir, ext_modules)) except: if pool is not None: pool.terminate() raise else: if pool is not None: pool.close() pool.join()
def cython_compile(path_pattern, options): pool = None paths = map(os.path.abspath, extended_iglob(path_pattern)) try: for path in paths: if options.build_inplace: base_dir = path while not os.path.isdir(base_dir) or is_package_dir(base_dir): base_dir = os.path.dirname(base_dir) else: base_dir = None if os.path.isdir(path): # recursively compiling a package paths = [os.path.join(path, '**', '*.%s' % ext) for ext in ('py', 'pyx')] else: # assume it's a file(-like thing) paths = [path] cwd = os.getcwd() try: if base_dir: os.chdir(base_dir) ext_modules = cythonize( paths, nthreads=options.parallel, exclude_failures=options.keep_going, exclude=options.excludes, compiler_directives=options.directives, force=options.force, quiet=options.quiet, **options.options) finally: if base_dir: os.chdir(cwd) if ext_modules and options.build: if len(ext_modules) > 1 and options.parallel > 1: if pool is None: try: pool = multiprocessing.Pool(options.parallel) except OSError: pool = _FakePool() pool.map_async(run_distutils, [ (base_dir, [ext]) for ext in ext_modules]) else: run_distutils((base_dir, ext_modules)) except: if pool is not None: pool.terminate() raise else: if pool is not None: pool.close() pool.join()
def find_extra_files(src_dir, modules, cythonized_dir, special_filenames=[]): """ Find all extra files which should be installed. These are (for each ``module`` in ``modules``): 1. From ``src_dir/module``: all .pyx, .pxd and .pxi files and files listed in ``special_filenames``. 2. From ``cythonized_dir/module``: all .h files (both the .h files from the sources, as well as all Cython-generated .h files). The directories are searched recursively, but only package directories (containing ``__init__.py`` or a Cython equivalent) are considered. INPUT: - ``src_dir`` -- root directory for the sources - ``modules`` -- sequence of strings: the top-level directories in ``src_dir`` to be considered - ``cythonized_dir`` -- the directory where the Cython-generated files are - ``special_filenames`` -- a list of filenames to be installed from ``src_dir`` OUTPUT: dict with items ``{dir: files}`` where ``dir`` is a directory relative to ``src_dir`` and ``files`` is a list of filenames inside that directory. EXAMPLES:: sage: from sage_setup.find import find_extra_files sage: from sage.env import SAGE_SRC sage: cythonized_dir = os.path.join(SAGE_SRC, "build", "cythonized") sage: extras = find_extra_files(SAGE_SRC, ["sage"], cythonized_dir) sage: extras["sage/libs/mpfr"] [...sage/libs/mpfr/types.pxd...] sage: extras["sage/ext/interpreters"] ['.../src/sage/ext/interpreters/wrapper_cdf.pxd', ...wrapper_cdf.h...] """ from Cython.Utils import is_package_dir data_files = {} cy_exts = ('.pxd', '.pxi', '.pyx') cwd = os.getcwd() try: os.chdir(src_dir) for module in modules: for dir, dirnames, filenames in os.walk(module): if not is_package_dir(dir): continue sdir = os.path.join(src_dir, dir) cydir = os.path.join(cythonized_dir, dir) files = [ os.path.join(sdir, f) for f in filenames if f.endswith(cy_exts) or f in special_filenames ] if os.path.isdir( cydir): # Not every directory contains Cython files files += [ os.path.join(cydir, f) for f in os.listdir(cydir) if f.endswith(".h") ] if files: data_files[dir] = files finally: os.chdir(cwd) return data_files