Commit a8404cbd authored by Robert Bradshaw's avatar Robert Bradshaw

Test of new build mechanism.

parent 9452c45f
from glob import glob from glob import glob
import re import re, os, sys
from distutils.extension import Extension from distutils.extension import Extension
...@@ -220,60 +220,61 @@ class DependencyTree(object): ...@@ -220,60 +220,61 @@ class DependencyTree(object):
del stack[node] del stack[node]
_dep_tree = None _dep_tree = None
def create_dependency_tree(ctx): def create_dependency_tree(ctx=None):
global _dep_tree global _dep_tree
if _dep_tree is None: if _dep_tree is None:
if ctx is None:
from Cython.Compiler.Main import Context, CompilationOptions
ctx = Context(["."], CompilationOptions())
_dep_tree = DependencyTree(ctx) _dep_tree = DependencyTree(ctx)
return _dep_tree return _dep_tree
def create_extension_list(filepatterns, ctx=None): def create_extension_list(filepatterns, ctx=None):
if ctx is None:
from Cython.Compiler.Main import Context
ctx = Context(["."])
deps = create_dependency_tree(ctx) deps = create_dependency_tree(ctx)
if isinstance(filepatterns, str): if isinstance(filepatterns, str):
filepatterns = [filepatterns] filepatterns = [filepatterns]
module_list = []
for pattern in filepatterns: for pattern in filepatterns:
for file in glob(pattern): for file in glob(pattern):
pkg = deps.package(file) pkg = deps.package(file)
name = fully_qualifeid_name(file) name = deps.fully_qualifeid_name(file)
return Extension(name=name, sources=[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) deps = create_dependency_tree(ctx)
to_compile = [] to_compile = []
for m in module_list: for m in module_list:
new_sources = [] new_sources = []
for source in m.sources: for source in m.sources:
base, ext = os.path.splitext(source) base, ext = os.path.splitext(source)
if ext in ('pyx', 'py'): if ext in ('.pyx', '.py'):
if m.language == 'c++': if m.language == 'c++':
c_file = base + '.cpp' c_file = base + '.cpp'
else: else:
c_file = base + '.c' c_file = base + '.c'
if os.path.exists(c_file): if os.path.exists(c_file):
c_timestamp = os.path.getmtime(outfile) c_timestamp = os.path.getmtime(c_file)
else: else:
c_timestamp = -1 c_timestamp = -1
if c_timestamp < deps.timestamp(source): if c_timestamp < deps.timestamp(source):
dep, dep_timestamp = deps.timestamp(source), source dep_timestamp, dep = deps.timestamp(source), source
priority = 0 priority = 0
else: else:
dep, dep_timestamp = deps.newest_dependency(source) dep_timestamp, dep = deps.newest_dependency(source)
priority = 2 - (dep in deps.immediate_dependencies(source)) priority = 2 - (dep in deps.immediate_dependencies(source))
if c_timestamp < dep_timestamp: if c_timestamp < dep_timestamp:
print ("Compiling", source, "because it depends on ", dep) print "Compiling", source, "because it depends on", dep
if dep == source:
to_compile.append((priority, source, c_file)) to_compile.append((priority, source, c_file))
new_sources.append(outfile) new_sources.append(c_file)
else: else:
new_sources.append(source) new_sources.append(source)
m.sources = new_sources m.sources = new_sources
to_compile.sort() to_compile.sort()
# TODO: invoke directly # 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: 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 print cmd
os.system(cmd) os.system(cmd)
return module_list return module_list
...@@ -163,6 +163,8 @@ class TestBuilder(object): ...@@ -163,6 +163,8 @@ class TestBuilder(object):
filenames.sort() filenames.sort()
for filename in filenames: for filename in filenames:
if context == "build" and filename.endswith(".srctree"): 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)) suite.addTest(EndToEndTest(filename, workdir, self.cleanup_workdir))
continue continue
if not (filename.endswith(".pyx") or filename.endswith(".py")): if not (filename.endswith(".pyx") or filename.endswith(".py")):
...@@ -645,25 +647,24 @@ class EndToEndTest(unittest.TestCase): ...@@ -645,25 +647,24 @@ class EndToEndTest(unittest.TestCase):
from Cython.TestUtils import unpack_source_tree from Cython.TestUtils import unpack_source_tree
_, self.commands = unpack_source_tree(os.path.join('tests', 'build', self.treefile), self.workdir) _, self.commands = unpack_source_tree(os.path.join('tests', 'build', self.treefile), self.workdir)
self.old_dir = os.getcwd() self.old_dir = os.getcwd()
if not os.path.exists(self.workdir):
os.makedirs(self.workdir)
os.chdir(self.workdir) os.chdir(self.workdir)
if self.workdir not in sys.path: if self.workdir not in sys.path:
sys.path.insert(0, self.workdir) sys.path.insert(0, self.workdir)
def tearDown(self): def tearDown(self):
try:
sys.path.remove(self.workdir)
except ValueError:
pass
if self.cleanup_workdir: if self.cleanup_workdir:
shutil.rmtree(self.workdir) shutil.rmtree(self.workdir)
os.chdir(self.old_dir) os.chdir(self.old_dir)
def runTest(self): def runTest(self):
# Assumes old_dir is root of the cython directory...
commands = (self.commands commands = (self.commands
.replace("CYTHON", "PYTHON %s" % os.path.join(self.old_dir, 'cython.py')) .replace("CYTHON", "PYTHON %s" % os.path.join(self.old_dir, 'cython.py'))
.replace("PYTHON", sys.executable)) .replace("PYTHON", sys.executable))
commands = """
PYTHONPATH="%s%s$PYTHONPATH"
%s
""" % (self.old_dir, os.pathsep, commands)
self.assertEqual(0, os.system(commands)) self.assertEqual(0, os.system(commands))
......
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 ########
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