Skip to content

Commit

Permalink
[setup] hooks 3to2 into build command
Browse files Browse the repository at this point in the history
  • Loading branch information
renefritze committed Mar 15, 2016
1 parent 5232a88 commit 380d4e2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Expand Up @@ -21,10 +21,6 @@ before_install:
- ${PYTHON_X} -c 'import dolfin' || echo "import dolfin failed"

install:
- |
if [ "${PYMOR_PYTHON_MAJOR}" == "2" ] ; then
make 3to2
fi
- ${PYTHON_X} setup.py build_ext -i

sudo: required
Expand Down
36 changes: 36 additions & 0 deletions fix_pymor_futures.py
@@ -0,0 +1,36 @@
'''
Add 'from __future__ import absolute_import' to any file
that uses imports.
'''
from lib2to3 import fixer_base
from lib2to3.pygram import python_symbols as syms
from lib3to2.fixer_util import future_import

class FixPymorFutures(fixer_base.BaseFix):
order = 'post'
run_order = 10

def __init__(self, options, log):
super(FixPymorFutures, self).__init__(options, log)
self.__pymor_added = None

def start_tree(self, tree, filename):
super(FixPymorFutures, self).start_tree(tree, filename)
self.__pymor_added = False

def match(self, node):
return (node.type in (syms.import_name, syms.import_from)
and not self.__pymor_added)

def transform(self, node, results):
try:
future_import('absolute_import', node)
future_import('division', node)
future_import('print_function', node)
except ValueError:
pass
else:
self.__pymor_added = True

def finish_tree(self, tree, filename):
fixer_base.BaseFix.finish_tree(self, tree, filename)
46 changes: 44 additions & 2 deletions setup.py
Expand Up @@ -9,6 +9,7 @@
from setuptools import find_packages
from setuptools.command.test import test as TestCommand
from distutils.extension import Extension
from distutils.command.build_py import build_py as _build_py
import itertools

import dependencies
Expand Down Expand Up @@ -94,6 +95,44 @@ def write_version():
revstring = '0.0.0-0-0'
return revstring

# When building under python 2.7, run refactorings from lib3to2
class build_py27(_build_py):
def __init__(self, *args, **kwargs):
_build_py.__init__(self, *args, **kwargs)
import logging
from lib2to3 import refactor
import lib3to2.main
import lib3to2.fixes
rt_logger = logging.getLogger("RefactoringTool")
rt_logger.addHandler(logging.StreamHandler())
fixers = refactor.get_fixers_from_package('lib3to2.fixes')
for fix in ('fix_except', 'fix_int', 'fix_print', 'fix_range', 'fix_str', 'fix_throw',
'fix_unittest', 'fix_absimport'):
fixers.remove('lib3to2.fixes.{}'.format(fix))
fixers.append('fix_pymor_futures')
print(fixers)
self.rtool = lib3to2.main.StdoutRefactoringTool(
fixers,
None,
[],
False,
False
)
self.rtool.refactor_dir('src', write=True)
self.rtool.refactor_dir('docs', write=True)

cmdclass = {}
if sys.version_info[0] < 3:
setup_requires.append('3to2')
# cmdclass allows you to override the distutils commands that are
# run through 'python setup.py somecmd'. Under python 2.7 replace
# the 'build_py' with a custom subclass (build_py27) that invokes
# 3to2 refactoring on each python file as its copied to the build
# directory.
cmdclass['build_py'] = build_py27
print(cmdclass)

# (Under python3 no commands are replaced, so the default command classes are used.)

def _setup(**kwargs):

Expand All @@ -117,7 +156,7 @@ def _setup(**kwargs):
# numpy sometimes expects this attribute, sometimes not. all seems ok if it's set to none
if not hasattr(Cython.Distutils.build_ext, 'fcompiler'):
Cython.Distutils.build_ext.fcompiler = None
cmdclass = {'build_ext': Cython.Distutils.build_ext}
cmdclass.update({'build_ext': Cython.Distutils.build_ext})
from numpy import get_include
include_dirs = [get_include()]
ext_modules = [Extension("pymor.tools.relations", ["src/pymor/tools/relations.pyx"], include_dirs=include_dirs),
Expand Down Expand Up @@ -175,12 +214,15 @@ def setup_package():
install_requires=install_requires,
classifiers=['Development Status :: 4 - Beta',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Intended Audience :: Science/Research',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Scientific/Engineering :: Visualization'],
license='LICENSE.txt',
zip_safe=False,
cmdclass=cmdclass,
)

missing = list(_missing(install_suggests.keys()))
Expand Down

0 comments on commit 380d4e2

Please sign in to comment.