Created
December 21, 2016 04:02
-
-
Save mandarjog/d7c58d799b68726db69f730744c1f0ca to your computer and use it in GitHub Desktop.
ast example
This file contains hidden or 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
| def find_imports(file_list, verbose=False, init_file=None): | |
| not_found = defaultdict(list) | |
| modules = set() | |
| pw_packages = defaultdict(list) | |
| pw_modules = defaultdict(list) | |
| init_modules = defaultdict(dict) | |
| used_names = defaultdict(list) | |
| for _fl in file_list: | |
| fl = os.path.abspath(_fl) | |
| pw_packages[os.path.dirname(fl)].append(os.path.basename(fl)) | |
| pw_modules[os.path.basename(fl)].append(os.path.dirname(fl)) | |
| for _fl in file_list: | |
| init_module = os.path.basename(_fl).startswith('__init__.py') | |
| fl = os.path.abspath(_fl) | |
| if 'site-packages' in fl or 'deprecated' in fl : continue | |
| if not os.path.exists(fl): continue | |
| src=open(fl).read() | |
| try: | |
| tree=ast.parse(src, fl) | |
| for stmt in ast.walk(tree): | |
| stmttype= type(stmt) | |
| if stmttype == ast.Import: | |
| for name in stmt.names: | |
| if verbose: print fl, "ast.Import", stmt.lineno, name.name | |
| found, mdl = check_import(fl, name.name, pw_packages, pw_modules, verbo> | |
| print mdl | |
| if found is True: | |
| modules.add(name.name) | |
| else: | |
| not_found[name.name].append ( "%s:%s" %(fl, | |
| stmt.lineno)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment