Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 123 lines (106 sloc) 3.8 KB
#!/usr/bin/env python3.2
"""Wrapper to run 2to3 automatically at import time.
auto2to3 -m mypackage.main_module
auto2to3 mypackage/
By default, all modules imported from a subdirectory of the current
directory will be run through `2to3`. To change this behavior, use the
`--package` or `--dir` flags to `auto2to3` to specify which packages or
directories contain Python 2 code that should be converted.
2to3 output is cached on disk between runs for speed.
Based on by Georg Brandl:
import argparse
import os
import sys
import imp
import runpy
from io import StringIO
from pkgutil import ImpImporter, ImpLoader
import runpy
import sys
import tempfile
import lib2to3
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
fixes = get_fixers_from_package('lib2to3.fixes')
rt = RefactoringTool(fixes)
DIRS = []
def maybe_2to3(filename, modname=None):
"""Returns a python3 version of filename."""
need_2to3 = False
filename = os.path.abspath(filename)
if any(filename.startswith(d) for d in DIRS):
need_2to3 = True
elif modname is not None and any(modname.startswith(p) for p in PACKAGES):
need_2to3 = True
if not need_2to3:
return filename
outfilename = '/_auto2to3_'.join(os.path.split(filename))
if (not os.path.exists(outfilename) or
os.stat(filename).st_mtime > os.stat(outfilename).st_mtime):
with open(filename) as file:
contents =
contents = rt.refactor_docstring(contents, filename)
tree = rt.refactor_string(contents, filename)
except Exception as err:
raise ImportError("2to3 couldn't convert %r" % filename)
outfile = open(outfilename, 'wb')
return outfilename
class ToThreeImporter(ImpImporter):
def find_module(self, fullname, path=None):
# this duplicates most of ImpImporter.find_module
subname = fullname.split(".")[-1]
if subname != fullname and self.path is None:
return None
if self.path is None:
path = None
path = [os.path.realpath(self.path)]
file, filename, etc = imp.find_module(subname, path)
except ImportError:
return None
if file and etc[2] == imp.PY_SOURCE:
outfilename = maybe_2to3(filename, modname=fullname)
if outfilename != filename:
filename = outfilename
file = open(filename, 'rb')
return ImpLoader(fullname, file, filename, etc)
# setup the hook
for key in sys.path_importer_cache:
if sys.path_importer_cache[key] is None:
sys.path_importer_cache[key] = ToThreeImporter(key)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--package', action='append')
parser.add_argument('--dir', action='append')
parser.add_argument('-m', action='store', metavar='MODULE')
args, rest = parser.parse_known_args()
if args.package:
if args.dir:
DIRS.extend(os.path.abspath(d) for d in args.dir)
if not PACKAGES and not DIRS:
if args.m:
sys.argv[1:] = rest
runpy.run_module(args.m, run_name='__main__', alter_sys=True)
elif rest:
sys.argv = rest
converted = maybe_2to3(rest[0])
with open(converted) as f:
new_globals = dict(__name__='__main__',
exec(, new_globals)
import code
if __name__ == '__main__':