Simplify checking for 3to2

Switch to importing the lib3to2 as a check, which is platform agnostic
and doesn't depend on how 3to2 was installed. Also, take advantage of
the fact that the 3to2 converter recurses by default to avoid spawning
a bunch of subprocesses. Finally, don't depend on the location of the
3to2 script and just use the library directly to do the conversion.
This commit is contained in:
Bryan W. Weber
2017-11-13 11:36:09 -05:00
committed by Ray Speth
parent 11943bbc6f
commit 77ee76c5f3
2 changed files with 20 additions and 31 deletions

View File

@@ -180,21 +180,20 @@ if localenv['python2_package'] == 'full':
# Use 3to2 to convert examples from Python 3 syntax
if env['python_convert_examples']:
def convert_example(target, source, env):
shutil.copyfile(source[0].abspath, target[0].abspath)
subprocess.call(env['threetotwo_cmd'] + ['--no-diff', '-n', '-w','-x', 'str',
'-f', 'all', '-f', 'printfunction', '-x', 'print',
'-x', 'open', target[0].abspath])
for subdir in subdirs('cantera/examples'):
dirpath = pjoin('cantera', 'examples', subdir)
for filename in os.listdir(dirpath):
if not filename.endswith('.py'):
continue
targetdir = '../../build/python2/cantera/examples'
a = build(py2env.Command(pjoin(targetdir, subdir, filename),
pjoin(dirpath, filename),
convert_example))
py2env.Depends(mod, a)
a = build(py2env.Command('#build/python2/cantera/examples', 'cantera/examples',
Copy("$TARGET", "$SOURCE")))
from textwrap import dedent
# In these next few things, the double quotes are necessary
# so the command line is processed properly
threetotwo_options = ['--no-diff', '-n', '-w', '-x', 'str', '-x', 'print', '-x', 'open',
'-f', 'all', '-f', 'printfunction']
threetotwo_options = ', '.join(["'{}'"]*len(threetotwo_options)).format(*threetotwo_options)
ex_loc = "'build/python2/cantera/examples'"
convert_script = dedent("""\
$python2_cmd_esc -c "from lib3to2.main import main; main('lib3to2.fixes', [{opts}, {loc}])"
""".format(opts=threetotwo_options, loc=ex_loc))
b = build(py2env.AddPostAction(a, convert_script))
py2env.Depends(mod, b)
add_dependencies(mod, ext)
install_module(py2env['python2_prefix'], py2_version)