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

@ -1294,23 +1294,13 @@ for py_ver in [2, 3]:
# Check for 3to2. See http://pypi.python.org/pypi/3to2
# Only needed for Python 2 package
if env['python2_package'] == 'full':
from textwrap import dedent
script = dedent("""\
from lib3to2.main import main
print(main('lib3to2.fixes', ['-l']))
""")
try:
python_dir = os.path.dirname(which(env['python2_cmd']))
if env['OS'] == 'Windows':
threetotwo_cmd = pjoin(python_dir, 'Scripts', '3to2')
# Conda installs 3to2 as an EXE file that can be executed directly
# but pip installs only a script. Try executing the EXE file first,
# and if it fails because the file doesn't exist, try the script
try:
ret = getCommandOutput(threetotwo_cmd, '-l')
env['threetotwo_cmd'] = [threetotwo_cmd]
except WindowsError:
ret = getCommandOutput(env['python2_cmd'], threetotwo_cmd, '-l')
env['threetotwo_cmd'] = [env['python2_cmd'], threetotwo_cmd]
else:
threetotwo_cmd = pjoin(python_dir, '3to2')
ret = getCommandOutput(threetotwo_cmd, '-l')
env['threetotwo_cmd'] = [threetotwo_cmd]
ret = getCommandOutput(env['python2_cmd'], '-c', script)
except (OSError, subprocess.CalledProcessError) as err:
if env['VERBOSE']:
print('Error checking for 3to2:')

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)