From a8404cbd23bf45f232aa35497cf33559992a8999 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Sun, 12 Sep 2010 01:37:35 -0700 Subject: [PATCH] Test of new build mechanism. --- Cython/Compiler/Dependencies.py | 35 +++++++++++++++-------------- runtests.py | 13 ++++++----- tests/build/basic_cythonize.srctree | 16 +++++++++++++ 3 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 tests/build/basic_cythonize.srctree diff --git a/Cython/Compiler/Dependencies.py b/Cython/Compiler/Dependencies.py index 13c8c85d..30f627d4 100644 --- a/Cython/Compiler/Dependencies.py +++ b/Cython/Compiler/Dependencies.py @@ -1,5 +1,5 @@ from glob import glob -import re +import re, os, sys from distutils.extension import Extension @@ -220,60 +220,61 @@ class DependencyTree(object): del stack[node] _dep_tree = None -def create_dependency_tree(ctx): +def create_dependency_tree(ctx=None): global _dep_tree if _dep_tree is None: + if ctx is None: + from Cython.Compiler.Main import Context, CompilationOptions + ctx = Context(["."], CompilationOptions()) _dep_tree = DependencyTree(ctx) return _dep_tree def create_extension_list(filepatterns, ctx=None): - if ctx is None: - from Cython.Compiler.Main import Context - ctx = Context(["."]) deps = create_dependency_tree(ctx) if isinstance(filepatterns, str): filepatterns = [filepatterns] + module_list = [] for pattern in filepatterns: for file in glob(pattern): pkg = deps.package(file) - name = fully_qualifeid_name(file) - return Extension(name=name, sources=[file]) + name = deps.fully_qualifeid_name(file) + module_list.append(Extension(name=name, sources=[file])) + return module_list -def cythonize(module_list): +def cythonize(module_list, ctx=None): deps = create_dependency_tree(ctx) to_compile = [] for m in module_list: new_sources = [] for source in m.sources: base, ext = os.path.splitext(source) - if ext in ('pyx', 'py'): + if ext in ('.pyx', '.py'): if m.language == 'c++': c_file = base + '.cpp' else: c_file = base + '.c' if os.path.exists(c_file): - c_timestamp = os.path.getmtime(outfile) + c_timestamp = os.path.getmtime(c_file) else: c_timestamp = -1 if c_timestamp < deps.timestamp(source): - dep, dep_timestamp = deps.timestamp(source), source + dep_timestamp, dep = deps.timestamp(source), source priority = 0 else: - dep, dep_timestamp = deps.newest_dependency(source) + dep_timestamp, dep = deps.newest_dependency(source) priority = 2 - (dep in deps.immediate_dependencies(source)) if c_timestamp < dep_timestamp: - print ("Compiling", source, "because it depends on ", dep) - if dep == source: + print "Compiling", source, "because it depends on", dep to_compile.append((priority, source, c_file)) - new_sources.append(outfile) + new_sources.append(c_file) else: new_sources.append(source) m.sources = new_sources to_compile.sort() # TODO: invoke directly - cython_py = os.path.join(os.path.dirname(__FILE__), '../../cython.py') + cython_py = os.path.join(os.path.dirname(__file__), '../../cython.py') for priority, pyx_file, c_file in to_compile: - cmd = "python %s %s -o %s" % (python_py, pyx_file, c_file) + cmd = "%s %s %s -o %s" % (sys.executable, cython_py, pyx_file, c_file) print cmd os.system(cmd) return module_list diff --git a/runtests.py b/runtests.py index 0db8fb6c..3c2d392b 100644 --- a/runtests.py +++ b/runtests.py @@ -163,6 +163,8 @@ class TestBuilder(object): filenames.sort() for filename in filenames: if context == "build" and filename.endswith(".srctree"): + if not [ 1 for match in self.selectors if match(filename) ]: + continue suite.addTest(EndToEndTest(filename, workdir, self.cleanup_workdir)) continue if not (filename.endswith(".pyx") or filename.endswith(".py")): @@ -645,25 +647,24 @@ class EndToEndTest(unittest.TestCase): from Cython.TestUtils import unpack_source_tree _, self.commands = unpack_source_tree(os.path.join('tests', 'build', self.treefile), self.workdir) self.old_dir = os.getcwd() - if not os.path.exists(self.workdir): - os.makedirs(self.workdir) os.chdir(self.workdir) if self.workdir not in sys.path: sys.path.insert(0, self.workdir) def tearDown(self): - try: - sys.path.remove(self.workdir) - except ValueError: - pass if self.cleanup_workdir: shutil.rmtree(self.workdir) os.chdir(self.old_dir) def runTest(self): + # Assumes old_dir is root of the cython directory... commands = (self.commands .replace("CYTHON", "PYTHON %s" % os.path.join(self.old_dir, 'cython.py')) .replace("PYTHON", sys.executable)) + commands = """ + PYTHONPATH="%s%s$PYTHONPATH" + %s + """ % (self.old_dir, os.pathsep, commands) self.assertEqual(0, os.system(commands)) diff --git a/tests/build/basic_cythonize.srctree b/tests/build/basic_cythonize.srctree new file mode 100644 index 00000000..8acf6633 --- /dev/null +++ b/tests/build/basic_cythonize.srctree @@ -0,0 +1,16 @@ +PYTHON setup.py build_ext --inplace +PYTHON -c "import a" + +######## setup.py ######## + + +# TODO: Better interface... +from Cython.Compiler.Dependencies import create_extension_list, cythonize + +from distutils.core import setup + +setup( + ext_modules = cythonize(create_extension_list("*.pyx")), +) + +######## a.pyx ######## -- 2.26.2