Created
June 10, 2016 18:47
-
-
Save robertwb/25ab9838cc2b9b21eed646834cf4a108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fake_cythonize(patterns): | |
import glob, json, os | |
from distutils.extension import Extension | |
def is_package_dir(path): | |
return os.path.exists(os.path.join(path, '__init__.py')) | |
def package(filename): | |
dir = os.path.dirname(str(filename)) | |
if dir != filename and is_package_dir(dir): | |
return package(dir) + (os.path.basename(dir),) | |
else: | |
return () | |
def fully_qualified_name(filename): | |
module = os.path.splitext(os.path.basename(filename))[0] | |
return '.'.join(package(filename) + (module,)) | |
def extract_metadata(lines): | |
for line in lines: | |
if 'BEGIN: Cython Metadata' in line: | |
break | |
else: | |
raise ValueError("No Cython metadata in '%s'" % c_file) | |
for line in lines: | |
if 'END: Cython Metadata' in line: | |
return | |
yield line | |
else: | |
raise ValueError("Malformed Cython metadata in '%s'" % c_file) | |
if isinstance(patterns, str): | |
patterns = [patterns] | |
module_list = [] | |
seen = set() | |
for pattern in patterns: | |
if isinstance(pattern, str): | |
filepattern = pattern | |
name = '*' | |
exn_type = Extension | |
else: | |
for filepattern in pattern.sources: | |
if os.path.splitext(filepattern)[1] in ('.py', '.pyx'): | |
break | |
else: | |
# ignore non-cython modules | |
module_list.append(pattern) | |
continue | |
name = pattern.name | |
exn_type = pattern.__class__ | |
for file in sorted(glob.iglob(filepattern)): | |
file_base, py_ext = os.path.splitext(file) | |
for c_ext in ('.c', '.cpp'): | |
c_file = file_base + c_ext | |
if os.path.exists(c_file): | |
break | |
else: | |
raise ValueError("No compiled version found for '%s'" % file) | |
metadata = json.loads(''.join(extract_metadata(iter(open(c_file))))) | |
module_name = str(metadata.get('module_name', fully_qualified_name(file))) | |
if module_name in seen: | |
continue | |
kwargs = metadata['distutils'] | |
sources = [c_file] + kwargs.pop('sources', []) | |
module_list.append(exn_type( | |
name=module_name, | |
sources=sources, | |
**kwargs)) | |
return module_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment