This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Code for doing symbol extraction #65
Open
CJ-Wright
wants to merge
5
commits into
regro:master
Choose a base branch
from
CJ-Wright:symbols
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0bc250a
WIP on adding symbols inspection
CJ-Wright a442166
add test for symbol extraction code
CJ-Wright 760ac9c
use reap system
CJ-Wright 312cb35
WIP use dask for multiprocessing
CJ-Wright 15a4bea
use dask so that memory leaks are handled
CJ-Wright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import jedi | ||
|
||
import os | ||
|
||
from tqdm import tqdm | ||
|
||
|
||
def file_path_to_import(file_path: str): | ||
return file_path.replace("/__init__.py", "").replace(".py", "").replace("/", ".") | ||
|
||
|
||
def get_all_symbol_names(top_dir): | ||
# Note Jedi seems to pick up things that are protected by a | ||
# __name__ == '__main__' if statement | ||
# this could cause some over-reporting of viable imports this | ||
# shouldn't cause issues with an audit since we don't expect 3rd parties | ||
# to depend on those | ||
symbols_dict = {} | ||
module_import = top_dir.split("/")[-1] | ||
# walk all the files looking for python files | ||
for root, dirs, files in tqdm(os.walk(top_dir)): | ||
_files = [f for f in files if f.endswith(".py")] | ||
for file in _files: | ||
file_name = os.path.join(root, file) | ||
import_name = file_path_to_import( | ||
"".join(file_name.rpartition(module_import)[1:]) | ||
) | ||
data = jedi.Script(path=file_name).complete() | ||
symbols_from_script = { | ||
k.full_name: k.type | ||
for k in data | ||
if k.full_name and module_import + "." in k.full_name | ||
} | ||
|
||
# cull statements within functions and classes, which are not importable | ||
classes_and_functions = { | ||
k for k, v in symbols_from_script.items() if v in ["class", "function"] | ||
} | ||
for k in list(symbols_from_script): | ||
for cf in classes_and_functions: | ||
if k != cf and k.startswith(cf) and k in symbols_from_script: | ||
symbols_from_script.pop(k) | ||
|
||
symbols_dict[import_name] = set(symbols_from_script) | ||
|
||
symbols = set() | ||
# handle star imports, which don't usually get added but are valid symbols | ||
for k, v in symbols_dict.items(): | ||
symbols.update(v) | ||
symbols.update({f"{k}.{vv.rsplit('.', 1)[-1]}" for vv in v}) | ||
return symbols |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pathlib import Path | ||
|
||
from libcflib.symbol_inspection import get_all_symbol_names | ||
|
||
|
||
def test_get_all_symbol_names(): | ||
top_dir = Path(__file__).parent / Path("..") / Path("libcflib") | ||
assert "libcflib.symbol_inspection.get_all_symbol_names" in get_all_symbol_names( | ||
str(top_dir) | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might only want to walk _files if there is a
__init__.py
(unless its one of those non directory packages)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, those non-directory pkgs has burned me in the past.