PyCG 0.0.3__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycg-0.0.4/PKG-INFO +148 -0
- pycg-0.0.4/README.md +135 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/base.py +2 -1
- pycg-0.0.4/micro-benchmark/create_pytests.py +42 -0
- pycg-0.0.4/micro-benchmark/external_test.py +25 -0
- pycg-0.0.4/micro-benchmark-key-errs/assignments_test.py +16 -0
- pycg-0.0.4/micro-benchmark-key-errs/base.py +81 -0
- pycg-0.0.4/micro-benchmark-key-errs/classes_test.py +25 -0
- pycg-0.0.4/micro-benchmark-key-errs/dicts_test.py +34 -0
- pycg-0.0.4/micro-benchmark-key-errs/kwargs_test.py +13 -0
- pycg-0.0.4/micro-benchmark-key-errs/lists_test.py +13 -0
- pycg-0.0.4/micro-benchmark-key-errs/parameters_test.py +16 -0
- pycg-0.0.4/pycg/__main__.py +104 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/formats/__init__.py +1 -0
- pycg-0.0.4/pycg/formats/as_graph.py +32 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/formats/fasten.py +2 -2
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/classes.py +18 -4
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/definitions.py +4 -1
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/imports.py +16 -11
- pycg-0.0.4/pycg/machinery/key_err.py +34 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/scopes.py +4 -3
- pycg-0.0.4/pycg/processing/__init__.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/processing/base.py +58 -19
- {pycg-0.0.3 → pycg-0.0.4}/pycg/processing/cgprocessor.py +8 -10
- pycg-0.0.4/pycg/processing/keyerrprocessor.py +82 -0
- pycg-0.0.4/pycg/processing/postprocessor.py +308 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/processing/preprocessor.py +21 -131
- {pycg-0.0.3 → pycg-0.0.4}/pycg/pycg.py +91 -11
- {pycg-0.0.3 → pycg-0.0.4}/pycg/utils/constants.py +3 -0
- pycg-0.0.4/pycg.egg-info/PKG-INFO +148 -0
- pycg-0.0.4/pycg.egg-info/SOURCES.txt +64 -0
- pycg-0.0.4/pycg.egg-info/top_level.txt +3 -0
- {pycg-0.0.3 → pycg-0.0.4}/setup.py +2 -2
- pycg-0.0.3/PKG-INFO +0 -158
- pycg-0.0.3/README.md +0 -145
- pycg-0.0.3/pycg/__main__.py +0 -71
- pycg-0.0.3/pycg/processing/postprocessor.py +0 -146
- pycg-0.0.3/pycg.egg-info/PKG-INFO +0 -158
- pycg-0.0.3/pycg.egg-info/SOURCES.txt +0 -51
- pycg-0.0.3/pycg.egg-info/top_level.txt +0 -2
- {pycg-0.0.3/pycg → pycg-0.0.4/micro-benchmark}/__init__.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/args_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/assignments_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/builtins_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/classes_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/decorators_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/dicts_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/direct_calls_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/dynamic_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/exceptions_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/functions_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/generators_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/imports_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/kwargs_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/lambdas_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/lists_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/mro_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark}/returns_test.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark-key-errs}/__init__.py +0 -0
- {pycg-0.0.3/benchmark → pycg-0.0.4/micro-benchmark-key-errs}/create_pytests.py +0 -0
- {pycg-0.0.3/pycg/machinery → pycg-0.0.4/pycg}/__init__.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/formats/base.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/formats/simple.py +0 -0
- {pycg-0.0.3/pycg/processing → pycg-0.0.4/pycg/machinery}/__init__.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/callgraph.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/modules.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/machinery/pointers.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/utils/__init__.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg/utils/common.py +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg.egg-info/dependency_links.txt +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/pycg.egg-info/entry_points.txt +0 -0
- {pycg-0.0.3 → pycg-0.0.4}/setup.cfg +0 -0
pycg-0.0.4/PKG-INFO
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pycg
|
|
3
|
+
Version: 0.0.4
|
|
4
|
+
Summary: Practical Python Call Graphs
|
|
5
|
+
Home-page: https://github.com/vitsalis/pycg
|
|
6
|
+
Author: Vitalis Salis
|
|
7
|
+
Author-email: vitsalis@gmail.com
|
|
8
|
+
License: Apache Software License
|
|
9
|
+
Description: # PyCG - Practical Python Call Graphs
|
|
10
|
+
|
|
11
|
+
PyCG generates call graphs for Python code using static analysis.
|
|
12
|
+
It efficiently supports
|
|
13
|
+
* Higher order functions
|
|
14
|
+
* Twisted class inheritance schemes
|
|
15
|
+
* Automatic discovery of imported modules for further analysis
|
|
16
|
+
* Nested definitions
|
|
17
|
+
|
|
18
|
+
You can read the full methodology as well as a complete evaluation on the
|
|
19
|
+
[ICSE 2021 paper](https://vitsalis.com/papers/pycg.pdf).
|
|
20
|
+
|
|
21
|
+
You can cite PyCG as follows.
|
|
22
|
+
Vitalis Salis, Thodoris Sotiropoulos, Panos Louridas, Diomidis Spinellis and Dimitris Mitropoulos.
|
|
23
|
+
PyCG: Practical Call Graph Generation in Python.
|
|
24
|
+
In _43rd International Conference on Software Engineering, ICSE '21_,
|
|
25
|
+
25–28 May 2021.
|
|
26
|
+
|
|
27
|
+
# Installation
|
|
28
|
+
|
|
29
|
+
PyCG is implemented in Python3 and has no dependencies. Simply:
|
|
30
|
+
```
|
|
31
|
+
pip install pycg
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
# Usage
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
~ >>> pycg -h
|
|
38
|
+
usage: __main__.py [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
|
|
39
|
+
[--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
|
|
40
|
+
[--max-iter MAX_ITER] [--operation {call-graph,key-error}]
|
|
41
|
+
[--as-graph-output AS_GRAPH_OUTPUT] [-o OUTPUT]
|
|
42
|
+
[entry_point ...]
|
|
43
|
+
|
|
44
|
+
positional arguments:
|
|
45
|
+
entry_point Entry points to be processed
|
|
46
|
+
|
|
47
|
+
optional arguments:
|
|
48
|
+
-h, --help show this help message and exit
|
|
49
|
+
--package PACKAGE Package containing the code to be analyzed
|
|
50
|
+
--fasten Produce call graph using the FASTEN format
|
|
51
|
+
--product PRODUCT Package name
|
|
52
|
+
--forge FORGE Source the product was downloaded from
|
|
53
|
+
--version VERSION Version of the product
|
|
54
|
+
--timestamp TIMESTAMP
|
|
55
|
+
Timestamp of the package's version
|
|
56
|
+
--max-iter MAX_ITER Maximum number of iterations through source code. If not specified a fix-point iteration will be performed.
|
|
57
|
+
--operation {call-graph,key-error}
|
|
58
|
+
Operation to perform. Choose call-graph for call graph generation (default) or key-error for key error detection on dictionaries.
|
|
59
|
+
--as-graph-output AS_GRAPH_OUTPUT
|
|
60
|
+
Output for the assignment graph
|
|
61
|
+
-o OUTPUT, --output OUTPUT
|
|
62
|
+
Output path
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The following command line arguments should used only when `--fasten` is
|
|
66
|
+
provied:
|
|
67
|
+
|
|
68
|
+
- `--product`: The name of the package.
|
|
69
|
+
- `--forge`: Source the package was downloaded from.
|
|
70
|
+
- `--version`: The version of the package.
|
|
71
|
+
- `--timestamp` : The timestamp of the package's version.
|
|
72
|
+
|
|
73
|
+
# Call Graph Output
|
|
74
|
+
|
|
75
|
+
## Simple JSON format
|
|
76
|
+
|
|
77
|
+
The call edges are in the form of an adjacency list where an edge `(src, dst)`
|
|
78
|
+
is represented as an entry of `dst` in the list assigned to key `src`:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
{
|
|
82
|
+
"node1": ["node2", "node3"],
|
|
83
|
+
"node2": ["node3"],
|
|
84
|
+
"node3": []
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## FASTEN Format
|
|
89
|
+
|
|
90
|
+
For an up-to-date description of the FASTEN format refer to the
|
|
91
|
+
[FASTEN
|
|
92
|
+
wiki](https://github.com/fasten-project/fasten/wiki/Extended-Revision-Call-Graph-format#python).
|
|
93
|
+
|
|
94
|
+
# Key Errors Output
|
|
95
|
+
|
|
96
|
+
We are currently experimenting on identifying potential invalid dictionary
|
|
97
|
+
accesses on Python dictionaries (key errors).
|
|
98
|
+
The output format for key errors is a list of dictionaries containing:
|
|
99
|
+
- The file name in which the key error was identified
|
|
100
|
+
- The line number inside the file
|
|
101
|
+
- The namespace of the accessed dictionary
|
|
102
|
+
- The key used to access the dictionary
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
[{
|
|
106
|
+
"filename": "mod.py",
|
|
107
|
+
"lineno": 2,
|
|
108
|
+
"namespace": "mod.<dict1>",
|
|
109
|
+
"key": "key2"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"filename": "mod.py",
|
|
113
|
+
"lineno": 8,
|
|
114
|
+
"namespace": "mod.<dict1>",
|
|
115
|
+
"key": "nokey"
|
|
116
|
+
}]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
# Examples
|
|
120
|
+
|
|
121
|
+
All the entry points are known and we want the simple JSON format
|
|
122
|
+
```
|
|
123
|
+
~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
All entry points are not known and we want the simple JSON format
|
|
127
|
+
```
|
|
128
|
+
~ >>> pycg --package django $(find django -type f -name "*.py") -o django.json
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
We want the FASTEN format:
|
|
132
|
+
```
|
|
133
|
+
~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
|
|
134
|
+
--version "0.1" --timestamp 42 \
|
|
135
|
+
pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
# Running Tests
|
|
139
|
+
|
|
140
|
+
From the root directory:
|
|
141
|
+
```
|
|
142
|
+
make test
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Platform: UNKNOWN
|
|
146
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
147
|
+
Classifier: Programming Language :: Python :: 3
|
|
148
|
+
Description-Content-Type: text/markdown
|
pycg-0.0.4/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# PyCG - Practical Python Call Graphs
|
|
2
|
+
|
|
3
|
+
PyCG generates call graphs for Python code using static analysis.
|
|
4
|
+
It efficiently supports
|
|
5
|
+
* Higher order functions
|
|
6
|
+
* Twisted class inheritance schemes
|
|
7
|
+
* Automatic discovery of imported modules for further analysis
|
|
8
|
+
* Nested definitions
|
|
9
|
+
|
|
10
|
+
You can read the full methodology as well as a complete evaluation on the
|
|
11
|
+
[ICSE 2021 paper](https://vitsalis.com/papers/pycg.pdf).
|
|
12
|
+
|
|
13
|
+
You can cite PyCG as follows.
|
|
14
|
+
Vitalis Salis, Thodoris Sotiropoulos, Panos Louridas, Diomidis Spinellis and Dimitris Mitropoulos.
|
|
15
|
+
PyCG: Practical Call Graph Generation in Python.
|
|
16
|
+
In _43rd International Conference on Software Engineering, ICSE '21_,
|
|
17
|
+
25–28 May 2021.
|
|
18
|
+
|
|
19
|
+
# Installation
|
|
20
|
+
|
|
21
|
+
PyCG is implemented in Python3 and has no dependencies. Simply:
|
|
22
|
+
```
|
|
23
|
+
pip install pycg
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
# Usage
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
~ >>> pycg -h
|
|
30
|
+
usage: __main__.py [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
|
|
31
|
+
[--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
|
|
32
|
+
[--max-iter MAX_ITER] [--operation {call-graph,key-error}]
|
|
33
|
+
[--as-graph-output AS_GRAPH_OUTPUT] [-o OUTPUT]
|
|
34
|
+
[entry_point ...]
|
|
35
|
+
|
|
36
|
+
positional arguments:
|
|
37
|
+
entry_point Entry points to be processed
|
|
38
|
+
|
|
39
|
+
optional arguments:
|
|
40
|
+
-h, --help show this help message and exit
|
|
41
|
+
--package PACKAGE Package containing the code to be analyzed
|
|
42
|
+
--fasten Produce call graph using the FASTEN format
|
|
43
|
+
--product PRODUCT Package name
|
|
44
|
+
--forge FORGE Source the product was downloaded from
|
|
45
|
+
--version VERSION Version of the product
|
|
46
|
+
--timestamp TIMESTAMP
|
|
47
|
+
Timestamp of the package's version
|
|
48
|
+
--max-iter MAX_ITER Maximum number of iterations through source code. If not specified a fix-point iteration will be performed.
|
|
49
|
+
--operation {call-graph,key-error}
|
|
50
|
+
Operation to perform. Choose call-graph for call graph generation (default) or key-error for key error detection on dictionaries.
|
|
51
|
+
--as-graph-output AS_GRAPH_OUTPUT
|
|
52
|
+
Output for the assignment graph
|
|
53
|
+
-o OUTPUT, --output OUTPUT
|
|
54
|
+
Output path
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The following command line arguments should used only when `--fasten` is
|
|
58
|
+
provied:
|
|
59
|
+
|
|
60
|
+
- `--product`: The name of the package.
|
|
61
|
+
- `--forge`: Source the package was downloaded from.
|
|
62
|
+
- `--version`: The version of the package.
|
|
63
|
+
- `--timestamp` : The timestamp of the package's version.
|
|
64
|
+
|
|
65
|
+
# Call Graph Output
|
|
66
|
+
|
|
67
|
+
## Simple JSON format
|
|
68
|
+
|
|
69
|
+
The call edges are in the form of an adjacency list where an edge `(src, dst)`
|
|
70
|
+
is represented as an entry of `dst` in the list assigned to key `src`:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
{
|
|
74
|
+
"node1": ["node2", "node3"],
|
|
75
|
+
"node2": ["node3"],
|
|
76
|
+
"node3": []
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## FASTEN Format
|
|
81
|
+
|
|
82
|
+
For an up-to-date description of the FASTEN format refer to the
|
|
83
|
+
[FASTEN
|
|
84
|
+
wiki](https://github.com/fasten-project/fasten/wiki/Extended-Revision-Call-Graph-format#python).
|
|
85
|
+
|
|
86
|
+
# Key Errors Output
|
|
87
|
+
|
|
88
|
+
We are currently experimenting on identifying potential invalid dictionary
|
|
89
|
+
accesses on Python dictionaries (key errors).
|
|
90
|
+
The output format for key errors is a list of dictionaries containing:
|
|
91
|
+
- The file name in which the key error was identified
|
|
92
|
+
- The line number inside the file
|
|
93
|
+
- The namespace of the accessed dictionary
|
|
94
|
+
- The key used to access the dictionary
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
[{
|
|
98
|
+
"filename": "mod.py",
|
|
99
|
+
"lineno": 2,
|
|
100
|
+
"namespace": "mod.<dict1>",
|
|
101
|
+
"key": "key2"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"filename": "mod.py",
|
|
105
|
+
"lineno": 8,
|
|
106
|
+
"namespace": "mod.<dict1>",
|
|
107
|
+
"key": "nokey"
|
|
108
|
+
}]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
# Examples
|
|
112
|
+
|
|
113
|
+
All the entry points are known and we want the simple JSON format
|
|
114
|
+
```
|
|
115
|
+
~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
All entry points are not known and we want the simple JSON format
|
|
119
|
+
```
|
|
120
|
+
~ >>> pycg --package django $(find django -type f -name "*.py") -o django.json
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
We want the FASTEN format:
|
|
124
|
+
```
|
|
125
|
+
~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
|
|
126
|
+
--version "0.1" --timestamp 42 \
|
|
127
|
+
pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
# Running Tests
|
|
131
|
+
|
|
132
|
+
From the root directory:
|
|
133
|
+
```
|
|
134
|
+
make test
|
|
135
|
+
```
|
|
@@ -24,6 +24,7 @@ import importlib
|
|
|
24
24
|
import json
|
|
25
25
|
|
|
26
26
|
from unittest import TestCase, main
|
|
27
|
+
from pycg import utils
|
|
27
28
|
|
|
28
29
|
class TestBase(TestCase):
|
|
29
30
|
snippet_dir = ""
|
|
@@ -60,7 +61,7 @@ class TestBase(TestCase):
|
|
|
60
61
|
def get_snippet_output_cg(self, snippet_path):
|
|
61
62
|
main_path = os.path.join(snippet_path, "main.py")
|
|
62
63
|
try:
|
|
63
|
-
cg = self.cg_class([main_path], snippet_path)
|
|
64
|
+
cg = self.cg_class([main_path], snippet_path, -1, utils.constants.CALL_GRAPH_OP)
|
|
64
65
|
cg.analyze()
|
|
65
66
|
return cg.output()
|
|
66
67
|
except Exception as e:
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
FILE_DIR = os.path.dirname(os.path.relpath(__file__))
|
|
6
|
+
SNIPPETS_DIR = "snippets"
|
|
7
|
+
|
|
8
|
+
base_template = """
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from base import TestBase
|
|
12
|
+
|
|
13
|
+
class {cls}Test(TestBase):
|
|
14
|
+
snippet_dir = "{dir}"
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
test_template = """
|
|
18
|
+
def test_{name}(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("{name}"))
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def create_test_case(name):
|
|
23
|
+
test_name = name + "_test.py"
|
|
24
|
+
# TODO: capitalize
|
|
25
|
+
capitalized = "".join([x.title() for x in name.split("_")])
|
|
26
|
+
template = base_template.format(
|
|
27
|
+
cls=capitalized,
|
|
28
|
+
dir=name
|
|
29
|
+
)
|
|
30
|
+
for name in os.listdir(os.path.join(FILE_DIR, SNIPPETS_DIR, name)):
|
|
31
|
+
if name == "." or name == "..":
|
|
32
|
+
continue
|
|
33
|
+
template += test_template.format(name=name)
|
|
34
|
+
|
|
35
|
+
with open(os.path.join(FILE_DIR, test_name), "w+") as f:
|
|
36
|
+
f.write(template)
|
|
37
|
+
|
|
38
|
+
for name in os.listdir(os.path.join(FILE_DIR, SNIPPETS_DIR)):
|
|
39
|
+
if name == "." or name == "..":
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
create_test_case(name)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ExternalTest(TestBase):
|
|
7
|
+
snippet_dir = "external"
|
|
8
|
+
|
|
9
|
+
def test_attribute(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("attribute"))
|
|
11
|
+
|
|
12
|
+
def test_function_assigned(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("function_assigned"))
|
|
14
|
+
|
|
15
|
+
def test_attribute_assigned(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("attribute_assigned"))
|
|
17
|
+
|
|
18
|
+
def test_function_asname(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("function_asname"))
|
|
20
|
+
|
|
21
|
+
def test_function(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("function"))
|
|
23
|
+
|
|
24
|
+
def test_cls_parent(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("cls_parent"))
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class AssignmentsTest(TestBase):
|
|
7
|
+
snippet_dir = "assignments"
|
|
8
|
+
|
|
9
|
+
def test_chained(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("chained"))
|
|
11
|
+
|
|
12
|
+
def test_recursive_tuple(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("recursive_tuple"))
|
|
14
|
+
|
|
15
|
+
def test_starred(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("starred"))
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
import os
|
|
22
|
+
import sys
|
|
23
|
+
import importlib
|
|
24
|
+
import json
|
|
25
|
+
|
|
26
|
+
from unittest import TestCase, main
|
|
27
|
+
from pycg import utils
|
|
28
|
+
|
|
29
|
+
class TestBase(TestCase):
|
|
30
|
+
snippet_dir = ""
|
|
31
|
+
|
|
32
|
+
def setUp(self):
|
|
33
|
+
def error():
|
|
34
|
+
print ("Invalid module %s.%s" % (cg_mod, cg_class))
|
|
35
|
+
print ("Set environment variables `CALL_GRAPH_CLASS` and `CALL_GRAPH_MODULE` properly")
|
|
36
|
+
sys.exit(1)
|
|
37
|
+
|
|
38
|
+
self.snippets_path = os.environ.get("SNIPPETS_PATH")
|
|
39
|
+
cg_class = os.environ.get('CALL_GRAPH_CLASS', None)
|
|
40
|
+
cg_mod = os.environ.get('CALL_GRAPH_MODULE', None)
|
|
41
|
+
if not cg_class or not cg_mod:
|
|
42
|
+
error()
|
|
43
|
+
try:
|
|
44
|
+
self.cg_mod = importlib.import_module(cg_mod)
|
|
45
|
+
except ImportError:
|
|
46
|
+
error()
|
|
47
|
+
|
|
48
|
+
self.cg_class = getattr(self.cg_mod, cg_class)
|
|
49
|
+
if not self.cg_class:
|
|
50
|
+
error()
|
|
51
|
+
|
|
52
|
+
def validate_snippet(self, snippet_path):
|
|
53
|
+
output = self.get_snippet_output_cg(snippet_path)
|
|
54
|
+
expected = self.get_snippet_expected_cg(snippet_path)
|
|
55
|
+
|
|
56
|
+
self.assertEqual(output, expected)
|
|
57
|
+
|
|
58
|
+
def get_snippet_path(self, name):
|
|
59
|
+
return os.path.join(self.snippets_path, self.snippet_dir, name)
|
|
60
|
+
|
|
61
|
+
def get_snippet_output_cg(self, snippet_path):
|
|
62
|
+
main_path = os.path.join(snippet_path, "main.py")
|
|
63
|
+
try:
|
|
64
|
+
cg = self.cg_class([main_path], snippet_path, -1, utils.constants.KEY_ERR_OP)
|
|
65
|
+
cg.analyze()
|
|
66
|
+
return cg.output_key_errs()
|
|
67
|
+
except Exception as e:
|
|
68
|
+
cg.tearDown()
|
|
69
|
+
raise e
|
|
70
|
+
|
|
71
|
+
def get_snippet_expected_cg(self, snippet_path):
|
|
72
|
+
cg_path = os.path.join(snippet_path, "key_errs.json")
|
|
73
|
+
with open(cg_path, "r") as f:
|
|
74
|
+
return json.loads(f.read())
|
|
75
|
+
|
|
76
|
+
def assertEqual(self, actual, expected):
|
|
77
|
+
super().assertEqual(actual, expected)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
main()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ClassesTest(TestBase):
|
|
7
|
+
snippet_dir = "classes"
|
|
8
|
+
|
|
9
|
+
def test_base_class_const(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("base_class_const"))
|
|
11
|
+
|
|
12
|
+
def test_return_str(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("return_str"))
|
|
14
|
+
|
|
15
|
+
def test_assigned_str(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("assigned_str"))
|
|
17
|
+
|
|
18
|
+
def test_self_const(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("self_const"))
|
|
20
|
+
|
|
21
|
+
def test_self_dict(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("self_dict"))
|
|
23
|
+
|
|
24
|
+
def test_self_assignment(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("self_assignment"))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class DictsTest(TestBase):
|
|
7
|
+
snippet_dir = "dicts"
|
|
8
|
+
|
|
9
|
+
def test_return_direct(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("return_direct"))
|
|
11
|
+
|
|
12
|
+
def test_type_coercion(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("type_coercion"))
|
|
14
|
+
|
|
15
|
+
def test_update(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("update"))
|
|
17
|
+
|
|
18
|
+
def test_ext_key(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("ext_key"))
|
|
20
|
+
|
|
21
|
+
def test_mutable_param(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("mutable_param"))
|
|
23
|
+
|
|
24
|
+
def test_return(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("return"))
|
|
26
|
+
|
|
27
|
+
def test_simple(self):
|
|
28
|
+
self.validate_snippet(self.get_snippet_path("simple"))
|
|
29
|
+
|
|
30
|
+
def test_nested(self):
|
|
31
|
+
self.validate_snippet(self.get_snippet_path("nested"))
|
|
32
|
+
|
|
33
|
+
def test_add_key(self):
|
|
34
|
+
self.validate_snippet(self.get_snippet_path("add_key"))
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class KwargsTest(TestBase):
|
|
7
|
+
snippet_dir = "kwargs"
|
|
8
|
+
|
|
9
|
+
def test_assigned_call(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("assigned_call"))
|
|
11
|
+
|
|
12
|
+
def test_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ListsTest(TestBase):
|
|
7
|
+
snippet_dir = "lists"
|
|
8
|
+
|
|
9
|
+
def test_comprehension(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("comprehension"))
|
|
11
|
+
|
|
12
|
+
def test_list_str(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("list_str"))
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ParametersTest(TestBase):
|
|
7
|
+
snippet_dir = "parameters"
|
|
8
|
+
|
|
9
|
+
def test_assigned_call(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("assigned_call"))
|
|
11
|
+
|
|
12
|
+
def test_imported_assigned_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("imported_assigned_call"))
|
|
14
|
+
|
|
15
|
+
def test_call(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import json
|
|
4
|
+
import argparse
|
|
5
|
+
|
|
6
|
+
from pycg.pycg import CallGraphGenerator
|
|
7
|
+
from pycg import formats
|
|
8
|
+
from pycg.utils.constants import CALL_GRAPH_OP, KEY_ERR_OP
|
|
9
|
+
|
|
10
|
+
def main():
|
|
11
|
+
parser = argparse.ArgumentParser()
|
|
12
|
+
parser.add_argument("entry_point",
|
|
13
|
+
nargs="*",
|
|
14
|
+
help="Entry points to be processed")
|
|
15
|
+
parser.add_argument(
|
|
16
|
+
"--package",
|
|
17
|
+
help="Package containing the code to be analyzed",
|
|
18
|
+
default=None
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--fasten",
|
|
22
|
+
help="Produce call graph using the FASTEN format",
|
|
23
|
+
action="store_true",
|
|
24
|
+
default=False
|
|
25
|
+
)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--product",
|
|
28
|
+
help="Package name",
|
|
29
|
+
default=""
|
|
30
|
+
)
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"--forge",
|
|
33
|
+
help="Source the product was downloaded from",
|
|
34
|
+
default=""
|
|
35
|
+
)
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
"--version",
|
|
38
|
+
help="Version of the product",
|
|
39
|
+
default=""
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--timestamp",
|
|
43
|
+
help="Timestamp of the package's version",
|
|
44
|
+
default=0
|
|
45
|
+
)
|
|
46
|
+
parser.add_argument(
|
|
47
|
+
"--max-iter",
|
|
48
|
+
type=int,
|
|
49
|
+
help=("Maximum number of iterations through source code. " +
|
|
50
|
+
"If not specified a fix-point iteration will be performed."),
|
|
51
|
+
default=-1
|
|
52
|
+
)
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
'--operation',
|
|
55
|
+
type=str,
|
|
56
|
+
choices=[CALL_GRAPH_OP, KEY_ERR_OP],
|
|
57
|
+
help=("Operation to perform. " +
|
|
58
|
+
"Choose " + CALL_GRAPH_OP + " for call graph generation (default)" +
|
|
59
|
+
" or " + KEY_ERR_OP + " for key error detection on dictionaries."),
|
|
60
|
+
default=CALL_GRAPH_OP
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"--as-graph-output",
|
|
65
|
+
help="Output for the assignment graph",
|
|
66
|
+
default=None
|
|
67
|
+
)
|
|
68
|
+
parser.add_argument(
|
|
69
|
+
"-o",
|
|
70
|
+
"--output",
|
|
71
|
+
help="Output path",
|
|
72
|
+
default=None
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
args = parser.parse_args()
|
|
76
|
+
|
|
77
|
+
cg = CallGraphGenerator(args.entry_point, args.package,
|
|
78
|
+
args.max_iter, args.operation)
|
|
79
|
+
cg.analyze()
|
|
80
|
+
|
|
81
|
+
if args.operation == CALL_GRAPH_OP:
|
|
82
|
+
if args.fasten:
|
|
83
|
+
formatter = formats.Fasten(cg, args.package,
|
|
84
|
+
args.product, args.forge, args.version, args.timestamp)
|
|
85
|
+
else:
|
|
86
|
+
formatter = formats.Simple(cg)
|
|
87
|
+
output = formatter.generate()
|
|
88
|
+
else:
|
|
89
|
+
output = cg.output_key_errs()
|
|
90
|
+
|
|
91
|
+
as_formatter = formats.AsGraph(cg)
|
|
92
|
+
|
|
93
|
+
if args.output:
|
|
94
|
+
with open(args.output, "w+") as f:
|
|
95
|
+
f.write(json.dumps(output))
|
|
96
|
+
else:
|
|
97
|
+
print (json.dumps(output))
|
|
98
|
+
|
|
99
|
+
if args.as_graph_output:
|
|
100
|
+
with open(args.as_graph_output, "w+") as f:
|
|
101
|
+
f.write(json.dumps(as_formatter.generate()))
|
|
102
|
+
|
|
103
|
+
if __name__ == "__main__":
|
|
104
|
+
main()
|