Skip to content

Commit

Permalink
Added pc_sdist. Version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Dec 5, 2014
1 parent 4bd7d75 commit 615a953
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 48 deletions.
2 changes: 1 addition & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: pycompilation
version: "0.3.7"
version: "0.4.0"

source:
url: https://github.com/bjodah/pycompilation/archive/master.zip
Expand Down
2 changes: 1 addition & 1 deletion pycompilation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.4.0.dev'
__version__ = '0.4.0'

from .compilation import (
compile_sources, link_py_so, src2obj,
Expand Down
2 changes: 1 addition & 1 deletion pycompilation/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def simple_cythonize(src, destdir=None, cwd=None, logger=None,
cwd = cwd or '.'
destdir = destdir or '.'

ext = '.cpp' if cy_kwargs['cplus'] else '.c'
ext = '.cpp' if cy_kwargs.get('cplus', False) else '.c'
c_name = os.path.splitext(os.path.basename(src))[0] + ext

dstfile = os.path.join(destdir, c_name)
Expand Down
122 changes: 79 additions & 43 deletions pycompilation/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import os
import re

from distutils.command import build_ext
from distutils.command import build_ext, sdist
from distutils.extension import Extension

from .compilation import (
extension_mapping, FortranCompilerRunner, CppCompilerRunner,
compile_sources, link_py_so, any_fort,
any_cplus
any_cplus, simple_cythonize
)
from .util import (
copy, get_abspath, import_module_from_file,
MetaReaderWriter, FileNotFoundError
MetaReaderWriter, FileNotFoundError, pyx_is_cplus, make_dirs
)


Expand Down Expand Up @@ -70,15 +70,50 @@ def PCExtension(*args, **kwargs):
return instance


class pc_build_ext(build_ext.build_ext):
def _copy_or_render_source(ext, f, output_dir, render_callback,
skip_copy=False):
"""
build_ext class for PCExtension
Support for template_regexps
Tries to do regex match for each (pattern, target, subsd) tuple
in ext.template_regexps for file f.
"""
# Either render a template or copy the source
dirname = os.path.dirname(f)
filename = os.path.basename(f)
for pattern, target, subsd in ext.template_regexps:
if re.match(pattern, filename):
tgt = os.path.join(dirname, re.sub(
pattern, target, filename))
rw = MetaReaderWriter('.metadata_subsd')
try:
prev_subsd = rw.get_from_metadata_file(output_dir, f)
except (FileNotFoundError, KeyError):
prev_subsd = None

render_callback(
get_abspath(f),
os.path.join(output_dir, tgt),
subsd,
only_update=ext.only_update,
prev_subsd=prev_subsd,
create_dest_dirs=True,
logger=ext.logger)
rw.save_to_metadata_file(output_dir, f, subsd)
return tgt
else:
if not skip_copy:
copy(f,
os.path.join(output_dir,
os.path.dirname(f)),
only_update=ext.only_update,
dest_is_dir=True,
create_dest_dirs=True,
logger=ext.logger)
return f


def render_template_to(self, src, dest, subsd, only_update=False,
prev_subsd=None, create_dest_dirs=True,
logger=None):
def render_python_template_to(src, dest, subsd, only_update=False,
prev_subsd=None, create_dest_dirs=True,
logger=None):
"""
Overload this function if you want to use a template engine such as
e.g. mako.
Expand All @@ -103,39 +138,14 @@ def render_template_to(self, src, dest, subsd, only_update=False,
with open(dest, 'wt') as ofh:
ofh.write(data % subsd)

def _copy_or_render_source(self, ext, f):
# Either render a template or copy the source
dirname = os.path.dirname(f)
filename = os.path.basename(f)
for pattern, target, subsd in ext.template_regexps:
if re.match(pattern, filename):
tgt = os.path.join(dirname, re.sub(
pattern, target, filename))
rw = MetaReaderWriter('.metadata_subsd')
try:
prev_subsd = rw.get_from_metadata_file(self.build_temp, f)
except (FileNotFoundError, KeyError):
prev_subsd = None

self.render_template_to(
get_abspath(f),
os.path.join(self.build_temp, tgt),
subsd,
only_update=ext.only_update,
prev_subsd=prev_subsd,
create_dest_dirs=True,
logger=ext.logger)
rw.save_to_metadata_file(self.build_temp, f, subsd)
return tgt
else:
copy(f,
os.path.join(self.build_temp,
os.path.dirname(f)),
only_update=ext.only_update,
dest_is_dir=True,
create_dest_dirs=True,
logger=ext.logger)
return f

class pc_build_ext(build_ext.build_ext):
"""
build_ext class for PCExtension
Support for template_regexps
"""

render_callback = staticmethod(render_python_template_to)

def run(self):
if self.dry_run:
Expand All @@ -145,7 +155,8 @@ def run(self):
if ext.logger:
ext.logger.info("Copying/rendering sources...")
for f in ext.sources:
sources.append(self._copy_or_render_source(ext, f))
sources.append(_copy_or_render_source(
ext, f, self.build_temp, self.render_callback))

if ext.logger:
ext.logger.info("Copying build_files...")
Expand Down Expand Up @@ -222,3 +233,28 @@ def run(self):
only_update=ext.only_update,
create_dest_dirs=True, logger=ext.logger
)


class pc_sdist(sdist.sdist):

render_callback = staticmethod(render_python_template_to)

def run(self):
for ext in self.distribution.ext_modules:
_sources = []
for src in ext.sources:
if src.endswith('.pyx'):
cy_kwargs = {
'cplus': pyx_is_cplus(src),
'include_path': ext.include_dirs
}
_sources.append(simple_cythonize(
src, os.path.dirname(src), **cy_kwargs))
else:
# Copy or render
_sources.append(_copy_or_render_source(
ext, src, '.',
self.render_callback, skip_copy=True))
ext.sources = _sources
sdist.sdist.run(self)
#super(pc_sdist, self).run()
2 changes: 1 addition & 1 deletion pycompilation/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def cmd(self):
counted.append(envvar)
msg = "Environment variable '{}' undefined.".format(
envvar)
raise CompilationError(msg)
self.logger.error(msg)
raise CompilationError(msg)
return cmd

def run(self):
Expand Down
22 changes: 22 additions & 0 deletions scripts/build_conda_pkgs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -x

# Extract absolute path of script, from:
# http://unix.stackexchange.com/a/9546
# Note: we are assuming this script is inside a subdirectory of the repo root
absolute_repo_path_x="$(readlink -fn -- "$(dirname $0)/.."; echo x)"
absolute_repo_path="${absolute_repo_path_x%x}"

if [[ ! -e build ]]; then
mkdir build
else
if [ "$(ls -A build/)" ]; then
rm -r build/*
fi
fi
cd build/
for CONDA_PY in {27,34}; do
echo $CONDA_PY
echo ========================
CONDA_PY=$CONDA_PY conda build ${absolute_repo_path}/conda-recipe
conda convert -p all $(CONDA_PY=$CONDA_PY conda build --output ${absolute_repo_path}/conda-recipe)
done
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from distutils.core import setup

name_ = 'pycompilation'
version_ = '0.4.0.dev'
version_ = '0.4.0'

classifiers = [
"Development Status :: 3 - Alpha",
Expand Down

0 comments on commit 615a953

Please sign in to comment.