diff --git a/bin/importmagic b/bin/importmagic new file mode 100644 index 0000000..f7772a8 --- /dev/null +++ b/bin/importmagic @@ -0,0 +1,43 @@ +#!/usr/bin/env python +""" +Update python imports using importmagic. +""" + +import os +import argparse +import sys + +import importmagic + + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('file_name') +parser.add_argument( + '--refresh', + action='store_true', + help='If set, forces a refresh of the importmagic index.' +) +parser.add_argument( + '--exclude-current-path', + action='store_true', + help='If set, will not automatically add the current directory' + ' to the import path when building the index.' +) + +args = parser.parse_args() + +path = sys.path if args.exclude_current_path else sys.path + [os.getcwd()] + +index = importmagic.SymbolIndex() +index.get_or_create_index(paths=path, refresh=args.refresh) + +with open(args.file_name) as f: + python_source = f.read() + +scope = importmagic.Scope.from_source(python_source) + +unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols() +python_source = importmagic.update_imports(python_source, index, unresolved, unreferenced) + +with open(args.file_name, 'w') as f: + f.write(python_source) diff --git a/setup.py b/setup.py index 0528a39..d1dd33f 100644 --- a/setup.py +++ b/setup.py @@ -40,4 +40,5 @@ def run(self): 'setuptools >= 0.6b1', ], cmdclass={'test': PyTest}, + scripts=['bin/importmagic'], )