def test_importing_found_exportables(): """ This test gets all of the exportable objects then tries to parse and compile each import statement. """ analyzer = CodeAnalyzer(top_dir) exportables = analyzer.exportables() for exportable in exportables: try: tree = ast.parse(exportable.import_str, "eval") compile(tree, "<string>", "exec") info(exportable.import_str) assert True, exportable.import_str except Exception as ex: assert False, exportable.import_str + " : " + str(ex)
def execute(self, settings): """ Execute the tasks specified in the settings object. :param settings: the application settings :type settings: argparse.Namespace :return: None :raises: ArgumentError """ Logger.set_verbosity(settings.verbosity) if settings.logfile is not None and settings.logfile: Logger.add_logger(FileLogger(settings.logfile)) with GracefulInterruptHandler() as handler: analyzer = CodeAnalyzer(settings.top_dir) if settings.trace: print("Trace Imports") print("top_dir: {dir}".format(dir=settings.top_dir)) for module in analyzer.find_modules( settings.top_dir, start_dir=os.path.join(settings.top_dir, "refactor_imports") ): print("-" * 60) print("{name}:".format(name=module.module_spec)) tracer = ImportTracer(module_spec=module.module_spec, file_spec=module.file_spec) patch = tracer.execute() print(patch) else: if settings.dump: for module in analyzer.find_modules(settings.top_dir): print("{name}:".format(name=module.file_spec)) print(module.dump_tree()) print("\n") exportables = analyzer.exportables() if settings.all: for exportable in exportables: print(exportable.import_str) if settings.usages: calls = analyzer.calls() for module_name in sorted(calls.keys()): print("{module}:".format(module=module_name)) for call in calls[module_name]: print(" " + call)