forked from alecthomas/importmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,5 @@ def run(self): | |
'setuptools >= 0.6b1', | ||
], | ||
cmdclass={'test': PyTest}, | ||
scripts=['bin/importmagic'], | ||
) |