Test of new build mechanism.
authorRobert Bradshaw <robertwb@math.washington.edu>
Sun, 12 Sep 2010 08:37:35 +0000 (01:37 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sun, 12 Sep 2010 08:37:35 +0000 (01:37 -0700)
Cython/Compiler/Dependencies.py
runtests.py
tests/build/basic_cythonize.srctree [new file with mode: 0644]

index 13c8c85d7e1f43cef0c8347f78ffb8063844ae1d..30f627d483b19f93b8fc64d67b0d7ad5e2723c47 100644 (file)
@@ -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
index 0db8fb6ccc137dbb95fbba310b6403083eed19ff..3c2d392bfe28a8b0218c582d9ff44fe68ca58c7e 100644 (file)
@@ -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 (file)
index 0000000..8acf663
--- /dev/null
@@ -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 ########