Skip to content

Commit f3b1311

Browse files
authored
Merge pull request #423 from SoftwareUnderstanding/dev
Dev
2 parents 21550f8 + 33054f5 commit f3b1311

6 files changed

Lines changed: 57 additions & 18 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Make sure you have tree-sitter installed, C complier is needed, more [info](http
7070
```
7171
pip install tree-sitter
7272
```
73+
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:
74+
```
75+
git clone https://github.com/tree-sitter/tree-sitter-python
76+
77+
python inspect4py/build.py
78+
```
7379

7480
Make sure you have graphviz installed:
7581

inspect4py/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.7'
1+
__version__ = '0.0.8'

inspect4py/build.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from tree_sitter import Language
2+
3+
Language.build_library(
4+
# Store the library in the `build` directory
5+
'my-languages.so',
6+
7+
# Include one or more languages
8+
[
9+
'tree-sitter-python'
10+
]
11+
)

inspect4py/cli.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ def _f_definitions(self, functions_definitions):
595595
if self.source_code:
596596
funcs_info[f.name]["source_code"] = ast_to_source_code(f)
597597
if self.data_flow:
598-
code_tokens, dfg = extract_dataflow(funcs_info[f.name]["source_code"], self.parser, "python")
598+
temp_source_code = ast_to_source_code(f)
599+
code_tokens, dfg = extract_dataflow(temp_source_code, self.parser, "python")
599600
funcs_info[f.name]["data_flow"] = dfg
600601
funcs_info[f.name]["code_tokens"] = code_tokens
601602
return funcs_info
@@ -1254,22 +1255,26 @@ def create_output_dirs(output_dir, control_flow):
12541255
help="extract metadata of the target repository using Github API. (requires repository to have the .git folder)")
12551256
@click.option('-df', '--data_flow', type=bool, is_flag=True,
12561257
help="extract data flow graph of every function in the target repository")
1257-
12581258
def main(input_path, output_dir, ignore_dir_pattern, ignore_file_pattern, requirements, html_output, call_list,
12591259
control_flow, directory_tree, software_invocation, abstract_syntax_tree, source_code, license_detection, readme,
12601260
metadata, data_flow, symbol_table):
12611261
if data_flow:
1262-
if symbol_table == "my_language.so": # default option
1263-
path_to_languages = str(Path(__file__).parent / "resources")
1264-
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
1265-
language = Language(path_to_languages + os.path.sep + "python_win.so", "python")
1262+
try:
1263+
if symbol_table == "my_language.so": # default option
1264+
path_to_languages = str(Path(__file__).parent / "resources")
1265+
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
1266+
language = Language(path_to_languages + os.path.sep + "python_win.so", "python")
1267+
elif sys.platform.startswith("darwin"):
1268+
language = Language(path_to_languages + os.path.sep + "python_mac.so", "python")
1269+
else:
1270+
language = Language(path_to_languages + os.path.sep + "python_unix.so", "python")
12661271
else:
1267-
language = Language(path_to_languages + os.path.sep + "python_unix.so", "python")
1268-
else:
1269-
language = Language(symbol_table, "python")
1270-
parser = Parser()
1271-
parser.set_language(language)
1272-
parser = [parser, DFG_python]
1272+
language = Language(symbol_table, "python")
1273+
parser = Parser()
1274+
parser.set_language(language)
1275+
parser = [parser, DFG_python]
1276+
except Exception as e:
1277+
print("Problem loading language file " + str(e))
12731278
else:
12741279
parser = []
12751280

@@ -1311,22 +1316,37 @@ def main(input_path, output_dir, ignore_dir_pattern, ignore_file_pattern, requir
13111316
except:
13121317
print("Readme not found at root level")
13131318
for subdir, dirs, files in os.walk(input_path):
1314-
1319+
# print(subdir, dirs, files)
13151320
for ignore_d in ignore_dir_pattern:
13161321
dirs[:] = [d for d in dirs if not d.startswith(ignore_d)]
13171322
for ignore_f in ignore_file_pattern:
13181323
files[:] = [f for f in files if not f.startswith(ignore_f)]
13191324
for f in files:
13201325
if ".py" in f and not f.endswith(".pyc"):
1326+
# path = os.path.join(subdir, f)
1327+
# # print(path)
1328+
# relative_path = Path(subdir).relative_to(Path(input_path).parent)
1329+
# out_dir = str(Path(output_dir) / relative_path)
1330+
# cf_dir, json_dir = create_output_dirs(out_dir, control_flow)
1331+
# code_info = CodeInspection(path, cf_dir, json_dir, control_flow, abstract_syntax_tree, source_code,
1332+
# data_flow, parser)
1333+
#
1334+
# if code_info.fileJson:
1335+
# print(code_info.fileJson[0])
1336+
# if out_dir not in dir_info:
1337+
# dir_info[out_dir] = [code_info.fileJson[0]]
1338+
# else:
1339+
# dir_info[out_dir].append(code_info.fileJson[0])
13211340
try:
1322-
13231341
path = os.path.join(subdir, f)
1342+
# print(path)
13241343
relative_path = Path(subdir).relative_to(Path(input_path).parent)
13251344
out_dir = str(Path(output_dir) / relative_path)
13261345
cf_dir, json_dir = create_output_dirs(out_dir, control_flow)
13271346
code_info = CodeInspection(path, cf_dir, json_dir, control_flow, abstract_syntax_tree, source_code, data_flow, parser)
1328-
# print(parsers)
1347+
13291348
if code_info.fileJson:
1349+
# print(code_info.fileJson[0])
13301350
if out_dir not in dir_info:
13311351
dir_info[out_dir] = [code_info.fileJson[0]]
13321352
else:

inspect4py/resources/python_mac.so

536 KB
Binary file not shown.

test/test_inspect4py.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,14 @@ def test_data_flow(self):
481481
output_dir = test_out_path + os.path.sep + "output_dir"
482482
control_flow = False
483483
abstract_syntax_tree = False
484-
source_code = True
484+
source_code = False
485485
data_flow = True
486486
path_to_languages = str(Path(__file__).parent.parent / "inspect4py" / "resources")
487487
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
488488
language = Language(path_to_languages + os.path.sep + "python_win.so", "python")
489-
else: # mac and unix should be compatible
489+
elif sys.platform.startswith("darwin"):
490+
language = Language(path_to_languages + os.path.sep + "python_mac.so", "python")
491+
else: # unix should be compatible
490492
language = Language(path_to_languages + os.path.sep + "python_unix.so", "python")
491493
parser = Parser()
492494
parser.set_language(language)

0 commit comments

Comments
 (0)