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

Data Flow Fix #422

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Make sure you have tree-sitter installed, C complier is needed, more [info](http
```
pip install tree-sitter
```
Note that if the ".so" file is not working properly, it is recommended that run the following commeds to generate a so file for your OS:
```
git clone https://github.com/tree-sitter/tree-sitter-python

python inspect4py/build.py
```

Make sure you have graphviz installed:

Expand Down
11 changes: 11 additions & 0 deletions inspect4py/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from tree_sitter import Language

Language.build_library(
# Store the library in the `build` directory
'my-languages.so',

# Include one or more languages
[
'tree-sitter-python'
]
)
26 changes: 22 additions & 4 deletions inspect4py/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ def _f_definitions(self, functions_definitions):
if self.source_code:
funcs_info[f.name]["source_code"] = ast_to_source_code(f)
if self.data_flow:
code_tokens, dfg = extract_dataflow(funcs_info[f.name]["source_code"], self.parser, "python")
temp_source_code = ast_to_source_code(f)
code_tokens, dfg = extract_dataflow(temp_source_code, self.parser, "python")
funcs_info[f.name]["data_flow"] = dfg
funcs_info[f.name]["code_tokens"] = code_tokens
return funcs_info
Expand Down Expand Up @@ -1263,6 +1264,8 @@ def main(input_path, output_dir, ignore_dir_pattern, ignore_file_pattern, requir
path_to_languages = str(Path(__file__).parent / "resources")
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
language = Language(path_to_languages + os.path.sep + "python_win.so", "python")
elif sys.platform.startswith("darwin"):
language = Language(path_to_languages + os.path.sep + "python_mac.so", "python")
else:
language = Language(path_to_languages + os.path.sep + "python_unix.so", "python")
else:
Expand Down Expand Up @@ -1311,22 +1314,37 @@ def main(input_path, output_dir, ignore_dir_pattern, ignore_file_pattern, requir
except:
print("Readme not found at root level")
for subdir, dirs, files in os.walk(input_path):

# print(subdir, dirs, files)
for ignore_d in ignore_dir_pattern:
dirs[:] = [d for d in dirs if not d.startswith(ignore_d)]
for ignore_f in ignore_file_pattern:
files[:] = [f for f in files if not f.startswith(ignore_f)]
for f in files:
if ".py" in f and not f.endswith(".pyc"):
# path = os.path.join(subdir, f)
# # print(path)
# relative_path = Path(subdir).relative_to(Path(input_path).parent)
# out_dir = str(Path(output_dir) / relative_path)
# cf_dir, json_dir = create_output_dirs(out_dir, control_flow)
# code_info = CodeInspection(path, cf_dir, json_dir, control_flow, abstract_syntax_tree, source_code,
# data_flow, parser)
#
# if code_info.fileJson:
# print(code_info.fileJson[0])
# if out_dir not in dir_info:
# dir_info[out_dir] = [code_info.fileJson[0]]
# else:
# dir_info[out_dir].append(code_info.fileJson[0])
try:

path = os.path.join(subdir, f)
# print(path)
relative_path = Path(subdir).relative_to(Path(input_path).parent)
out_dir = str(Path(output_dir) / relative_path)
cf_dir, json_dir = create_output_dirs(out_dir, control_flow)
code_info = CodeInspection(path, cf_dir, json_dir, control_flow, abstract_syntax_tree, source_code, data_flow, parser)
# print(parsers)

if code_info.fileJson:
# print(code_info.fileJson[0])
if out_dir not in dir_info:
dir_info[out_dir] = [code_info.fileJson[0]]
else:
Expand Down
Binary file added inspect4py/resources/python_mac.so
Binary file not shown.
2 changes: 1 addition & 1 deletion test/test_inspect4py.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def test_data_flow(self):
output_dir = test_out_path + os.path.sep + "output_dir"
control_flow = False
abstract_syntax_tree = False
source_code = True
source_code = False
data_flow = True
path_to_languages = str(Path(__file__).parent.parent / "inspect4py" / "resources")
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
Expand Down