Skip to content
Snippets Groups Projects
Commit 6b98bfa7 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Fix bug in import handling

parent 272326b3
Branches
No related tags found
No related merge requests found
...@@ -111,25 +111,13 @@ class ImportVisitor(ast.NodeVisitor): ...@@ -111,25 +111,13 @@ class ImportVisitor(ast.NodeVisitor):
def visit_Import(self, node): def visit_Import(self, node):
for name in [ n.name for n in node.names ]: for name in [ n.name for n in node.names ]:
self.add_import(name, '.'.join(name.split('.')[0:-1])) self.add_import(name, '.'.join(name.split('.')[0:-1]))
continue
print("X",name)
sub_path = name.replace('.', '/')
full_path = os.path.join(self.root_dir, sub_path)
if os.path.isdir(full_path):
# We need to look for __init__.py for module
self.add_file(os.path.join(full_path, '__init__.py'),
module=name)
pass
else:
self.add_file(full_path + '.py', module=self.module)
pass
pass pass
ast.NodeVisitor.generic_visit(self, node) ast.NodeVisitor.generic_visit(self, node)
pass pass
def visit_ImportFrom(self, node): def visit_ImportFrom(self, node):
for name in [ n.name for n in node.names ]: for name in [ n.name for n in node.names ]:
module = node.module if node.module else self.module module = '.'.join([ s for s in [self.module, node.module] if s])
module = module.split('.') if module else [] module = module.split('.') if module else []
if node.level > 1: if node.level > 1:
module = module[0:-node.level+1] module = module[0:-node.level+1]
...@@ -144,7 +132,7 @@ class ImportVisitor(ast.NodeVisitor): ...@@ -144,7 +132,7 @@ class ImportVisitor(ast.NodeVisitor):
if __name__ == '__main__': if __name__ == '__main__':
usage = "%prog [options] <main module> <additional modules>*" usage = "%prog [options] MAIN <additional modules>*"
optParser = optparse.OptionParser(usage=usage) optParser = optparse.OptionParser(usage=usage)
optParser.add_option('-d','--documentation', optParser.add_option('-d','--documentation',
action='store_true', action='store_true',
...@@ -162,7 +150,7 @@ if __name__ == '__main__': ...@@ -162,7 +150,7 @@ if __name__ == '__main__':
help='Strip this part from filenames') help='Strip this part from filenames')
optParser.add_option('--filter-imports', optParser.add_option('--filter-imports',
action='store_true', action='store_true',
help='Filter out fies that are not imported') help='Filter out files that are not imported')
(options, args) = optParser.parse_args(sys.argv[1:]) (options, args) = optParser.parse_args(sys.argv[1:])
if options.documentation: if options.documentation:
...@@ -170,6 +158,10 @@ if __name__ == '__main__': ...@@ -170,6 +158,10 @@ if __name__ == '__main__':
sys.exit(0) sys.exit(0)
pass pass
if args == []:
raise Exception('No MAIN module specified')
sys.exit(1)
if options.filter_imports: if options.filter_imports:
# Only add imported modules to archive # Only add imported modules to archive
visitor = ImportVisitor(args[0]) visitor = ImportVisitor(args[0])
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment