Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusively build for pwdata and filter out modules #3

Merged
merged 38 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fcefe35
Add `exclusives` and `ignored_mods` to filter out irrelevant modules
jacksongoode Apr 22, 2024
bc8ec6c
Linting
jacksongoode Apr 23, 2024
4484b96
Return even earlier
jacksongoode Apr 24, 2024
332ed7e
Hm
jacksongoode Apr 24, 2024
9145079
Hmm
jacksongoode Apr 24, 2024
3fbb617
Fix
jacksongoode Apr 24, 2024
09ace52
Hm
jacksongoode Apr 24, 2024
8f2107b
Remove print
jacksongoode Apr 24, 2024
227325d
Log
jacksongoode Apr 24, 2024
4668151
Hm
jacksongoode Apr 24, 2024
0c7f1ec
Fix
jacksongoode Apr 24, 2024
165a26d
Fix
jacksongoode Apr 24, 2024
9d70e92
Fix
jacksongoode Apr 24, 2024
0f498e2
Test
jacksongoode Apr 24, 2024
d150cb5
Remove
jacksongoode Apr 24, 2024
712ebb1
Add default
jacksongoode Apr 30, 2024
0f25a10
Print
jacksongoode Apr 30, 2024
cdf8c30
Edit
jacksongoode Apr 30, 2024
a478da3
Fix
jacksongoode Apr 30, 2024
7f944eb
Edit
jacksongoode Apr 30, 2024
7270fe7
Edit
jacksongoode Apr 30, 2024
9482506
Edit
jacksongoode Apr 30, 2024
3c36d6f
Edit
jacksongoode Apr 30, 2024
0036baf
Fix
jacksongoode Apr 30, 2024
9a5ce92
Fix
jacksongoode Apr 30, 2024
c482243
Edit
jacksongoode May 1, 2024
5713719
Fix
jacksongoode May 1, 2024
5d950da
Fix
jacksongoode May 1, 2024
a159b35
Fast re search
jacksongoode May 1, 2024
dc182c7
Edit
jacksongoode May 1, 2024
c5319c5
Opps
jacksongoode May 1, 2024
48f5731
Move imported_name down
jacksongoode May 1, 2024
b57b4c3
Fix
jacksongoode May 1, 2024
8946577
Tet
jacksongoode May 1, 2024
5cb4728
Skip
jacksongoode May 1, 2024
7daf94e
Remove prints
jacksongoode May 1, 2024
697c4ad
Cleanup
jacksongoode May 1, 2024
b2a5d15
Fix
jacksongoode May 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pycg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def main():
parser.add_argument(
"--package", help="Package containing the code to be analyzed", default=None
)
parser.add_argument(
"--exclusives", help="Exclusive package to analyze", default=[])
parser.add_argument(
"--skip-classes", help="Classes to skip during traversal", default=[])
parser.add_argument(
"--fasten",
help="Produce call graph using the FASTEN format",
Expand Down Expand Up @@ -57,7 +61,9 @@ def main():
args = parser.parse_args()

cg = CallGraphGenerator(
args.entry_point, args.package, args.max_iter, args.operation
args.entry_point, args.package,
args.exclusives, args.skip_classes,
args.max_iter, args.operation
)
cg.analyze()

Expand Down
10 changes: 10 additions & 0 deletions pycg/processing/postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __init__(
self,
input_file,
modname,
exclusives,
skip_classes,
import_manager,
scope_manager,
def_manager,
Expand All @@ -38,6 +40,8 @@ def __init__(
modules_analyzed=None,
):
super().__init__(input_file, modname, modules_analyzed)
self.exclusives = exclusives
self.skip_classes = skip_classes
self.import_manager = import_manager
self.scope_manager = scope_manager
self.def_manager = def_manager
Expand Down Expand Up @@ -174,6 +178,10 @@ def visit_FunctionDef(self, node):
super().visit_FunctionDef(node)

def visit_ClassDef(self, node):
# Return if visiting class should be skipped
if node.name in self.skip_classes:
return

# create a definition for the class (node.name)
cls_def = self.def_manager.handle_class_def(self.current_ns, node.name)

Expand Down Expand Up @@ -329,6 +337,8 @@ def update_parent_classes(self, defi):
def analyze_submodules(self):
super().analyze_submodules(
PostProcessor,
self.exclusives,
self.skip_classes,
self.import_manager,
self.scope_manager,
self.def_manager,
Expand Down
29 changes: 29 additions & 0 deletions pycg/processing/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# under the License.
#
import ast
import re

from pycg import utils
from pycg.machinery.definitions import Definition
Expand All @@ -30,6 +31,8 @@
self,
filename,
modname,
exclusives,
skip_classes,
import_manager,
scope_manager,
def_manager,
Expand All @@ -40,6 +43,8 @@
super().__init__(filename, modname, modules_analyzed)

self.modname = modname
self.exclusives = exclusives
self.skip_classes = skip_classes
self.mod_dir = "/".join(self.filename.split("/")[:-1])

self.import_manager = import_manager
Expand All @@ -48,6 +53,8 @@
self.class_manager = class_manager
self.module_manager = module_manager

self.skip_classes_pattern = re.compile('|'.join(map(re.escape, self.skip_classes)))

Check failure on line 56 in pycg/processing/preprocessor.py

View workflow job for this annotation

GitHub Actions / flake8

line too long (91 > 89 characters)

def _get_fun_defaults(self, node):
defaults = {}
start = len(node.args.args) - len(node.args.defaults)
Expand All @@ -71,6 +78,8 @@
super().analyze_submodule(
PreProcessor,
modname,
self.exclusives,
self.skip_classes,
self.import_manager,
self.scope_manager,
self.def_manager,
Expand All @@ -81,6 +90,11 @@

def visit_Module(self, node):
def iterate_mod_items(items, const):
items = [
item for item in items

Check warning on line 94 in pycg/processing/preprocessor.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace
if not self.skip_classes_pattern.search(item)
]

Check warning on line 97 in pycg/processing/preprocessor.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
for item in items:
defi = self.def_manager.get(item)
if not defi:
Expand Down Expand Up @@ -203,6 +217,15 @@
for import_item in node.names:
src_name = handle_src_name(import_item.name)
tgt_name = import_item.asname if import_item.asname else import_item.name

Check warning on line 220 in pycg/processing/preprocessor.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
# Limit to exclusive module if exclusives exist
if self.exclusives and src_name.split(".")[0] not in self.exclusives:
continue

# Skip specified classes
if tgt_name in self.skip_classes:
continue

imported_name = self.import_manager.handle_import(src_name, level)

if not imported_name:
Expand All @@ -223,6 +246,9 @@

# handle all modules that were not analyzed
for modname in self.import_manager.get_imports(self.modname):
if self.exclusives and modname.split(".")[0] not in self.exclusives:
continue

fname = self.import_manager.get_filepath(modname)

if not fname:
Expand Down Expand Up @@ -402,6 +428,9 @@

def visit_ClassDef(self, node):
# create a definition for the class (node.name)
if node.name in self.skip_classes:
return

cls_def = self.def_manager.handle_class_def(self.current_ns, node.name)

mod = self.module_manager.get(self.modname)
Expand Down
10 changes: 9 additions & 1 deletion pycg/pycg.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@


class CallGraphGenerator(object):
def __init__(self, entry_points, package, max_iter, operation):
def __init__(
self, entry_points, package, exclusives, skip_classes, max_iter, operation
):
self.entry_points = entry_points
self.package = package
self.exclusives = exclusives
self.skip_classes = skip_classes
self.state = None
self.max_iter = max_iter
self.operation = operation
Expand Down Expand Up @@ -162,6 +166,8 @@ def analyze(self):
self.do_pass(
PreProcessor,
True,
self.exclusives,
self.skip_classes,
self.import_manager,
self.scope_manager,
self.def_manager,
Expand All @@ -179,6 +185,8 @@ def analyze(self):
self.do_pass(
PostProcessor,
False,
self.exclusives,
self.skip_classes,
self.import_manager,
self.scope_manager,
self.def_manager,
Expand Down
Loading