FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Removed unused code from project_modules · python-security/pyt@e678c0f · GitHub

Repository navigation

Commit e678c0f

Browse files
committed
Removed unused code from project_modules
1 parent 889df99 commit e678c0f

1 file changed

Lines changed: 0 additions & 138 deletions

File tree

‎pyt/project_handler.py‎

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import ast
77
import os
88

9-
CLASS_FUNCTION_SEPERATOR = ':'
10-
119
def is_python_module(path):
1210
if os.path.splitext(path)[1] == '.py':
1311
return True
@@ -51,145 +49,9 @@ def get_project_module_names(path):
5149
project_module_names.append(project_module[0])
5250
return project_module_names
5351

54-
class Import():
55-
def __init__(self, name, module, path):
56-
self.name = name
57-
self.module = module
58-
self.path = path
59-
60-
61-
class Definition():
62-
def __init__(self, name, path):
63-
self.name = name
64-
self.path = path
65-
66-
def __str__(self):
67-
return ', '.join(('Definition: ', self.name, self.path))
68-
69-
class Module():
70-
def __init__(self, path, imports, classes, functions):
71-
self.path = path
72-
self.imports = imports
73-
self.classes = classes
74-
self.functions = functions
75-
76-
def __str__(self):
77-
return '\n'.join(('Module:', self.path, str([str(s) for s in self.imports]), str([str(s) for s in self.classes]), str([str(s) for s in self.functions])))
78-
79-
80-
class DefinitionVisitor(ast.NodeVisitor):
81-
def __init__(self, path):
82-
self.local_module_names = get_project_module_names(path)
83-
self.imports = list()
84-
self.functions = list()
85-
self.classes = list()
86-
self.latest_class = None
87-
88-
def visit_FunctionDef(self, node):
89-
function_name = ''
90-
if self.latest_class:
91-
function_name = self.latest_class
92-
function_name += CLASS_FUNCTION_SEPERATOR
93-
function_name += node.name
94-
self.functions.append(function_name)
95-
96-
def visit_ClassDef(self, node):
97-
self.latest_class = node.name
98-
self.classes.append(node.name)
99-
for stmt in node.body:
100-
self.visit(stmt)
101-
self.latest_class = None
102-
103-
def visit_Import(self, node):
104-
for name in node.names:
105-
if name.name in self.local_module_names:
106-
self.imports.append(node)
107-
108-
def visit_ImportFrom(self, node):
109-
if node.module.split('.')[-1] in self.local_module_names:
110-
self.imports.append(node)
111-
112-
def generate_ast(path):
113-
"""Generate an Abstract Syntax Tree using the ast module."""
114-
with open(path, 'r') as f:
115-
return ast.parse(f.read())
116-
117-
11852
def is_directory(path):
11953
if os.path.isdir(path):
12054
return True
12155
elif is_python_module(path):
12256
return False
12357
raise Exception(path, ' has to be a python module or a directory.')
124-
125-
126-
def module_name_handler(node, name):
127-
if isinstance(node, ast.Import):
128-
return '.'.join(name.split('.')[0:-1])
129-
elif isinstance(node, ast.ImportFrom):
130-
return node.module
131-
else:
132-
raise Exception('Wrong type of node, ', node)
133-
134-
135-
def get_project_definitions_and_imports(path):
136-
project_modules = get_python_modules(path)
137-
modules = list()
138-
for module in project_modules:
139-
modules.append(get_definitions_and_imports(module[1]))
140-
return modules
141-
142-
def get_definitions_and_imports(module_path):
143-
imports = list()
144-
functions = list()
145-
classes = list()
146-
definition_visitor = DefinitionVisitor(module_path)
147-
tree = generate_ast(module_path)
148-
definition_visitor.visit(tree)
149-
150-
for i in definition_visitor.imports:
151-
for alias in i.names:
152-
_import = Import(alias.name, module_name_handler(i, alias.name), module_path)
153-
imports.append(_import)
154-
155-
for function_name in definition_visitor.functions:
156-
functions.append(Definition(function_name, module_path))
157-
158-
for class_name in definition_visitor.classes:
159-
classes.append(Definition(class_name, module_path))
160-
161-
return Module(module_path, imports, classes, functions)
162-
163-
def file_to_cfg(path):
164-
tree = generate_ast(path)
165-
cfg = CFG()
166-
cfg.create(tree)
167-
return cfg
168-
169-
def file_to_cfgs(path, imports):
170-
tree = generate_ast(path)
171-
cfg = CFG(imports)
172-
cfg.create(tree)
173-
return cfg
174-
175-
def get_cfgs(path):
176-
cfg_list = list()
177-
if is_directory(path):
178-
modules = get_project_definitions_and_imports(path)
179-
for module in modules:
180-
print(module)
181-
#cfg_list.append(file_to_cfgs(project_modules[index][1], imports))
182-
else:
183-
cfg_list.append(file_to_cfg(path))
184-
185-
return cfg_list
186-
187-
if __name__ == '__main__':
188-
import argparse
189-
190-
parser = argparse.ArgumentParser()
191-
192-
parser.add_argument('filename', help = 'Filename of the file that should be analysed.', type = str)
193-
194-
args = parser.parse_args()
195-
print(get_cfgs(args.filename))

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL