PyCG 0.0.3__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.3/PKG-INFO +158 -0
- pycg-0.0.3/README.md +145 -0
- pycg-0.0.3/benchmark/__init__.py +0 -0
- pycg-0.0.3/benchmark/args_test.py +25 -0
- pycg-0.0.3/benchmark/assignments_test.py +19 -0
- pycg-0.0.3/benchmark/base.py +86 -0
- pycg-0.0.3/benchmark/builtins_test.py +16 -0
- pycg-0.0.3/benchmark/classes_test.py +73 -0
- pycg-0.0.3/benchmark/create_pytests.py +42 -0
- pycg-0.0.3/benchmark/decorators_test.py +28 -0
- pycg-0.0.3/benchmark/dicts_test.py +43 -0
- pycg-0.0.3/benchmark/direct_calls_test.py +19 -0
- pycg-0.0.3/benchmark/dynamic_test.py +10 -0
- pycg-0.0.3/benchmark/exceptions_test.py +16 -0
- pycg-0.0.3/benchmark/functions_test.py +19 -0
- pycg-0.0.3/benchmark/generators_test.py +25 -0
- pycg-0.0.3/benchmark/imports_test.py +49 -0
- pycg-0.0.3/benchmark/kwargs_test.py +16 -0
- pycg-0.0.3/benchmark/lambdas_test.py +22 -0
- pycg-0.0.3/benchmark/lists_test.py +31 -0
- pycg-0.0.3/benchmark/mro_test.py +28 -0
- pycg-0.0.3/benchmark/returns_test.py +19 -0
- pycg-0.0.3/pycg/__init__.py +0 -0
- pycg-0.0.3/pycg/__main__.py +71 -0
- pycg-0.0.3/pycg/formats/__init__.py +22 -0
- pycg-0.0.3/pycg/formats/base.py +23 -0
- pycg-0.0.3/pycg/formats/fasten.py +253 -0
- pycg-0.0.3/pycg/formats/simple.py +32 -0
- pycg-0.0.3/pycg/machinery/__init__.py +0 -0
- pycg-0.0.3/pycg/machinery/callgraph.py +59 -0
- pycg-0.0.3/pycg/machinery/classes.py +63 -0
- pycg-0.0.3/pycg/machinery/definitions.py +216 -0
- pycg-0.0.3/pycg/machinery/imports.py +215 -0
- pycg-0.0.3/pycg/machinery/modules.py +66 -0
- pycg-0.0.3/pycg/machinery/pointers.py +146 -0
- pycg-0.0.3/pycg/machinery/scopes.py +142 -0
- pycg-0.0.3/pycg/processing/__init__.py +0 -0
- pycg-0.0.3/pycg/processing/base.py +483 -0
- pycg-0.0.3/pycg/processing/cgprocessor.py +232 -0
- pycg-0.0.3/pycg/processing/postprocessor.py +146 -0
- pycg-0.0.3/pycg/processing/preprocessor.py +485 -0
- pycg-0.0.3/pycg/pycg.py +149 -0
- pycg-0.0.3/pycg/utils/__init__.py +22 -0
- pycg-0.0.3/pycg/utils/common.py +39 -0
- pycg-0.0.3/pycg/utils/constants.py +39 -0
- pycg-0.0.3/pycg.egg-info/PKG-INFO +158 -0
- pycg-0.0.3/pycg.egg-info/SOURCES.txt +51 -0
- pycg-0.0.3/pycg.egg-info/dependency_links.txt +1 -0
- pycg-0.0.3/pycg.egg-info/entry_points.txt +3 -0
- pycg-0.0.3/pycg.egg-info/top_level.txt +2 -0
- pycg-0.0.3/setup.cfg +7 -0
- pycg-0.0.3/setup.py +57 -0
pycg-0.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pycg
|
|
3
|
+
Version: 0.0.3
|
|
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 inherritance 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
|
+
Abstract:
|
|
22
|
+
> Call graphs play an important role in different contexts, such as profiling and vulnerability propagation analysis. Generating call graphs in an efficient manner can be a challenging task when it comes to high-level languages that are modular and incorporate dynamic features and higher-order functions. Despite the language's popularity, there have been very few tools aiming to generate call graphs for Python programs. Worse, these tools suffer from several effectiveness issues that limit their practicality in realistic programs. We propose a pragmatic, static approach for call graph generation in Python. We compute all assignment relations between program identifiers of functions, variables, classes, and modules through an inter-procedural analysis. Based on these assignment relations, we produce the resulting call graph by resolving all calls to potentially invoked functions. Notably, the underlying analysis is designed to be efficient and scalable, handling several Python features, such as modules, generators, function closures, and multiple inheritance. We have evaluated our prototype implementation, which we call PyCG, using two benchmarks: a micro-benchmark suite containing small Python programs and a set of macro-benchmarks with several popular real-world Python packages. Our results indicate that PyCG can efficiently handle thousands of lines of code in less than a second (0.34 seconds for 1k LoC on average). Further, it outperforms the state-of-the-art for Python in both precision and recall: PyCG achieves high rates of precision ~99, and adequate recall ~69.3. Finally, we demonstrate how PyCG can aid dependency impact analysis by showcasing a potential enhancement to GitHub's security advisory notification service using a real-world example.
|
|
23
|
+
|
|
24
|
+
# Installation
|
|
25
|
+
|
|
26
|
+
PyCG is implemented in Python3 and has no dependencies. Simply:
|
|
27
|
+
```
|
|
28
|
+
pip install pycg
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
# Usage
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
~ >>> pycg -h
|
|
35
|
+
usage: pycg [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
|
|
36
|
+
[--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
|
|
37
|
+
[-o OUTPUT]
|
|
38
|
+
[entry_point [entry_point ...]]
|
|
39
|
+
|
|
40
|
+
positional arguments:
|
|
41
|
+
entry_point Entry points to be processed
|
|
42
|
+
|
|
43
|
+
optional arguments:
|
|
44
|
+
-h, --help show this help message and exit
|
|
45
|
+
--package PACKAGE Package containing the code to be analyzed
|
|
46
|
+
--fasten Produce call graph using the FASTEN format
|
|
47
|
+
--product PRODUCT Package name
|
|
48
|
+
--forge FORGE Source the product was downloaded from
|
|
49
|
+
--version VERSION Version of the product
|
|
50
|
+
--timestamp TIMESTAMP
|
|
51
|
+
Timestamp of the package's version
|
|
52
|
+
-o OUTPUT, --output OUTPUT
|
|
53
|
+
Output path
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
where the command line arguments are:
|
|
57
|
+
|
|
58
|
+
- `entry_point`: A list of paths to Python modules that PyCG will analyze.
|
|
59
|
+
It is suggested that this list of paths contains only entry points
|
|
60
|
+
since PyCG automatically discovers all other (local) imported modules.
|
|
61
|
+
- `--package`: The unix path to the module's namespace (i.e. the path from
|
|
62
|
+
which the module would be executed). This parameter is really important for
|
|
63
|
+
the correct resolving of imports.
|
|
64
|
+
- `--fasten`: Output the callgraph in FASTEN format.
|
|
65
|
+
- `-output`: The unix path where the output call graph will be stored in JSON
|
|
66
|
+
format.
|
|
67
|
+
|
|
68
|
+
The following command line arguments should used only when `--fasten` is
|
|
69
|
+
provied:
|
|
70
|
+
|
|
71
|
+
- `--product`: The name of the package.
|
|
72
|
+
- `--forge`: Source the package was downloaded from.
|
|
73
|
+
- `--version`: The version of the package.
|
|
74
|
+
- `--timestamp` : The timestamp of the package's version.
|
|
75
|
+
|
|
76
|
+
# Output
|
|
77
|
+
|
|
78
|
+
## Simple JSON format
|
|
79
|
+
|
|
80
|
+
The call edges are in the form of an adjacency list where an edge `(src, dst)`
|
|
81
|
+
is represented as an entry of `dst` in the list assigned to key `src`:
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
{
|
|
85
|
+
"node1": ["node2", "node3"],
|
|
86
|
+
"node2": ["node3"],
|
|
87
|
+
"node3": []
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## FASTEN Format
|
|
92
|
+
|
|
93
|
+
For an up-to-date description of the FASTEN format refer to the
|
|
94
|
+
[FASTEN
|
|
95
|
+
wiki](https://github.com/fasten-project/fasten/wiki/Extended-Revision-Call-Graph-format#python).
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# Examples
|
|
99
|
+
|
|
100
|
+
All the entry points are known and we want the simple JSON format
|
|
101
|
+
```
|
|
102
|
+
~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
All entry points are not known and we want the simple JSON format
|
|
106
|
+
```
|
|
107
|
+
~ >>> pycg --package django $(find django -type f -name "*.py") -o django.json
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
We want the FASTEN format:
|
|
111
|
+
```
|
|
112
|
+
~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
|
|
113
|
+
--version "0.1" --timestamp 42 \
|
|
114
|
+
pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
# Benchmark
|
|
118
|
+
|
|
119
|
+
We provide an benchmark of minimal snippets of Python code
|
|
120
|
+
testing a wide variety of the features that Python provides.
|
|
121
|
+
|
|
122
|
+
We organize this benchmark into the following categories:
|
|
123
|
+
|
|
124
|
+
| Category | No. Tests | Description |
|
|
125
|
+
|----------------|-----------|-----------------------------------------------|
|
|
126
|
+
| parameters | 6 | Positional arguments that are functions |
|
|
127
|
+
| assignments | 5 | Assignment of functions to variables |
|
|
128
|
+
| built-ins | 3 | Calls to built in functions and data types |
|
|
129
|
+
| classes | 22 | Class construction, attributes, methods |
|
|
130
|
+
| comprehensions | 1 | Comprehensions |
|
|
131
|
+
| decorators | 7 | Function decorators |
|
|
132
|
+
| dicts | 5 | Hashmap with values that are functions |
|
|
133
|
+
| direct calls | 4 | Direct call of a returned function (func()()) |
|
|
134
|
+
| exceptions | 3 | Exceptions |
|
|
135
|
+
| eval | 1 | Usage of eval |
|
|
136
|
+
| functions | 4 | Vanilla function calls |
|
|
137
|
+
| generators | 2 | Generators |
|
|
138
|
+
| imports | 13 | Imported modules, functions classes |
|
|
139
|
+
| kwargs | 3 | Keyword arguments that are functions |
|
|
140
|
+
| lambdas | 5 | Lambdas |
|
|
141
|
+
| mro | 7 | Method Resolution Order (mro) |
|
|
142
|
+
| returns | 4 | Returns that are functions |
|
|
143
|
+
|
|
144
|
+
This benchmark is not dependent on PyCG and can be used to test
|
|
145
|
+
other Python call graph generation tools.
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# Running Tests & Benchmarks
|
|
149
|
+
|
|
150
|
+
From the root directory:
|
|
151
|
+
```
|
|
152
|
+
make test
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Platform: UNKNOWN
|
|
156
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
157
|
+
Classifier: Programming Language :: Python :: 3
|
|
158
|
+
Description-Content-Type: text/markdown
|
pycg-0.0.3/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
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 inherritance 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
|
+
Abstract:
|
|
14
|
+
> Call graphs play an important role in different contexts, such as profiling and vulnerability propagation analysis. Generating call graphs in an efficient manner can be a challenging task when it comes to high-level languages that are modular and incorporate dynamic features and higher-order functions. Despite the language's popularity, there have been very few tools aiming to generate call graphs for Python programs. Worse, these tools suffer from several effectiveness issues that limit their practicality in realistic programs. We propose a pragmatic, static approach for call graph generation in Python. We compute all assignment relations between program identifiers of functions, variables, classes, and modules through an inter-procedural analysis. Based on these assignment relations, we produce the resulting call graph by resolving all calls to potentially invoked functions. Notably, the underlying analysis is designed to be efficient and scalable, handling several Python features, such as modules, generators, function closures, and multiple inheritance. We have evaluated our prototype implementation, which we call PyCG, using two benchmarks: a micro-benchmark suite containing small Python programs and a set of macro-benchmarks with several popular real-world Python packages. Our results indicate that PyCG can efficiently handle thousands of lines of code in less than a second (0.34 seconds for 1k LoC on average). Further, it outperforms the state-of-the-art for Python in both precision and recall: PyCG achieves high rates of precision ~99, and adequate recall ~69.3. Finally, we demonstrate how PyCG can aid dependency impact analysis by showcasing a potential enhancement to GitHub's security advisory notification service using a real-world example.
|
|
15
|
+
|
|
16
|
+
# Installation
|
|
17
|
+
|
|
18
|
+
PyCG is implemented in Python3 and has no dependencies. Simply:
|
|
19
|
+
```
|
|
20
|
+
pip install pycg
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
# Usage
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
~ >>> pycg -h
|
|
27
|
+
usage: pycg [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
|
|
28
|
+
[--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
|
|
29
|
+
[-o OUTPUT]
|
|
30
|
+
[entry_point [entry_point ...]]
|
|
31
|
+
|
|
32
|
+
positional arguments:
|
|
33
|
+
entry_point Entry points to be processed
|
|
34
|
+
|
|
35
|
+
optional arguments:
|
|
36
|
+
-h, --help show this help message and exit
|
|
37
|
+
--package PACKAGE Package containing the code to be analyzed
|
|
38
|
+
--fasten Produce call graph using the FASTEN format
|
|
39
|
+
--product PRODUCT Package name
|
|
40
|
+
--forge FORGE Source the product was downloaded from
|
|
41
|
+
--version VERSION Version of the product
|
|
42
|
+
--timestamp TIMESTAMP
|
|
43
|
+
Timestamp of the package's version
|
|
44
|
+
-o OUTPUT, --output OUTPUT
|
|
45
|
+
Output path
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
where the command line arguments are:
|
|
49
|
+
|
|
50
|
+
- `entry_point`: A list of paths to Python modules that PyCG will analyze.
|
|
51
|
+
It is suggested that this list of paths contains only entry points
|
|
52
|
+
since PyCG automatically discovers all other (local) imported modules.
|
|
53
|
+
- `--package`: The unix path to the module's namespace (i.e. the path from
|
|
54
|
+
which the module would be executed). This parameter is really important for
|
|
55
|
+
the correct resolving of imports.
|
|
56
|
+
- `--fasten`: Output the callgraph in FASTEN format.
|
|
57
|
+
- `-output`: The unix path where the output call graph will be stored in JSON
|
|
58
|
+
format.
|
|
59
|
+
|
|
60
|
+
The following command line arguments should used only when `--fasten` is
|
|
61
|
+
provied:
|
|
62
|
+
|
|
63
|
+
- `--product`: The name of the package.
|
|
64
|
+
- `--forge`: Source the package was downloaded from.
|
|
65
|
+
- `--version`: The version of the package.
|
|
66
|
+
- `--timestamp` : The timestamp of the package's version.
|
|
67
|
+
|
|
68
|
+
# Output
|
|
69
|
+
|
|
70
|
+
## Simple JSON format
|
|
71
|
+
|
|
72
|
+
The call edges are in the form of an adjacency list where an edge `(src, dst)`
|
|
73
|
+
is represented as an entry of `dst` in the list assigned to key `src`:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
{
|
|
77
|
+
"node1": ["node2", "node3"],
|
|
78
|
+
"node2": ["node3"],
|
|
79
|
+
"node3": []
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## FASTEN Format
|
|
84
|
+
|
|
85
|
+
For an up-to-date description of the FASTEN format refer to the
|
|
86
|
+
[FASTEN
|
|
87
|
+
wiki](https://github.com/fasten-project/fasten/wiki/Extended-Revision-Call-Graph-format#python).
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# Examples
|
|
91
|
+
|
|
92
|
+
All the entry points are known and we want the simple JSON format
|
|
93
|
+
```
|
|
94
|
+
~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
All entry points are not known and we want the simple JSON format
|
|
98
|
+
```
|
|
99
|
+
~ >>> pycg --package django $(find django -type f -name "*.py") -o django.json
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
We want the FASTEN format:
|
|
103
|
+
```
|
|
104
|
+
~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
|
|
105
|
+
--version "0.1" --timestamp 42 \
|
|
106
|
+
pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.json
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
# Benchmark
|
|
110
|
+
|
|
111
|
+
We provide an benchmark of minimal snippets of Python code
|
|
112
|
+
testing a wide variety of the features that Python provides.
|
|
113
|
+
|
|
114
|
+
We organize this benchmark into the following categories:
|
|
115
|
+
|
|
116
|
+
| Category | No. Tests | Description |
|
|
117
|
+
|----------------|-----------|-----------------------------------------------|
|
|
118
|
+
| parameters | 6 | Positional arguments that are functions |
|
|
119
|
+
| assignments | 5 | Assignment of functions to variables |
|
|
120
|
+
| built-ins | 3 | Calls to built in functions and data types |
|
|
121
|
+
| classes | 22 | Class construction, attributes, methods |
|
|
122
|
+
| comprehensions | 1 | Comprehensions |
|
|
123
|
+
| decorators | 7 | Function decorators |
|
|
124
|
+
| dicts | 5 | Hashmap with values that are functions |
|
|
125
|
+
| direct calls | 4 | Direct call of a returned function (func()()) |
|
|
126
|
+
| exceptions | 3 | Exceptions |
|
|
127
|
+
| eval | 1 | Usage of eval |
|
|
128
|
+
| functions | 4 | Vanilla function calls |
|
|
129
|
+
| generators | 2 | Generators |
|
|
130
|
+
| imports | 13 | Imported modules, functions classes |
|
|
131
|
+
| kwargs | 3 | Keyword arguments that are functions |
|
|
132
|
+
| lambdas | 5 | Lambdas |
|
|
133
|
+
| mro | 7 | Method Resolution Order (mro) |
|
|
134
|
+
| returns | 4 | Returns that are functions |
|
|
135
|
+
|
|
136
|
+
This benchmark is not dependent on PyCG and can be used to test
|
|
137
|
+
other Python call graph generation tools.
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# Running Tests & Benchmarks
|
|
141
|
+
|
|
142
|
+
From the root directory:
|
|
143
|
+
```
|
|
144
|
+
make test
|
|
145
|
+
```
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ArgsTest(TestBase):
|
|
7
|
+
snippet_dir = "args"
|
|
8
|
+
|
|
9
|
+
def test_nested_call(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("nested_call"))
|
|
11
|
+
|
|
12
|
+
def test_param_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("param_call"))
|
|
14
|
+
|
|
15
|
+
def test_assigned_call(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("assigned_call"))
|
|
17
|
+
|
|
18
|
+
def test_imported_call(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("imported_call"))
|
|
20
|
+
|
|
21
|
+
def test_imported_assigned_call(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("imported_assigned_call"))
|
|
23
|
+
|
|
24
|
+
def test_call(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class AssignmentsTest(TestBase):
|
|
7
|
+
snippet_dir = "assignments"
|
|
8
|
+
|
|
9
|
+
def test_tuple(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("tuple"))
|
|
11
|
+
|
|
12
|
+
def test_chained(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("chained"))
|
|
14
|
+
|
|
15
|
+
def test_recursive_tuple(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("recursive_tuple"))
|
|
17
|
+
|
|
18
|
+
def test_starred(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("starred"))
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 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
|
+
|
|
28
|
+
class TestBase(TestCase):
|
|
29
|
+
snippet_dir = ""
|
|
30
|
+
|
|
31
|
+
def setUp(self):
|
|
32
|
+
def error():
|
|
33
|
+
print ("Invalid module %s.%s" % (cg_mod, cg_class))
|
|
34
|
+
print ("Set environment variables `CALL_GRAPH_CLASS` and `CALL_GRAPH_MODULE` properly")
|
|
35
|
+
sys.exit(1)
|
|
36
|
+
|
|
37
|
+
self.snippets_path = os.environ.get("SNIPPETS_PATH")
|
|
38
|
+
cg_class = os.environ.get('CALL_GRAPH_CLASS', None)
|
|
39
|
+
cg_mod = os.environ.get('CALL_GRAPH_MODULE', None)
|
|
40
|
+
if not cg_class or not cg_mod:
|
|
41
|
+
error()
|
|
42
|
+
try:
|
|
43
|
+
self.cg_mod = importlib.import_module(cg_mod)
|
|
44
|
+
except ImportError:
|
|
45
|
+
error()
|
|
46
|
+
|
|
47
|
+
self.cg_class = getattr(self.cg_mod, cg_class)
|
|
48
|
+
if not self.cg_class:
|
|
49
|
+
error()
|
|
50
|
+
|
|
51
|
+
def validate_snippet(self, snippet_path):
|
|
52
|
+
output = self.get_snippet_output_cg(snippet_path)
|
|
53
|
+
expected = self.get_snippet_expected_cg(snippet_path)
|
|
54
|
+
|
|
55
|
+
self.assertEqual(output, expected)
|
|
56
|
+
|
|
57
|
+
def get_snippet_path(self, name):
|
|
58
|
+
return os.path.join(self.snippets_path, self.snippet_dir, name)
|
|
59
|
+
|
|
60
|
+
def get_snippet_output_cg(self, snippet_path):
|
|
61
|
+
main_path = os.path.join(snippet_path, "main.py")
|
|
62
|
+
try:
|
|
63
|
+
cg = self.cg_class([main_path], snippet_path)
|
|
64
|
+
cg.analyze()
|
|
65
|
+
return cg.output()
|
|
66
|
+
except Exception as e:
|
|
67
|
+
cg.tearDown()
|
|
68
|
+
raise e
|
|
69
|
+
|
|
70
|
+
def get_snippet_expected_cg(self, snippet_path):
|
|
71
|
+
cg_path = os.path.join(snippet_path, "callgraph.json")
|
|
72
|
+
with open(cg_path, "r") as f:
|
|
73
|
+
return json.loads(f.read())
|
|
74
|
+
|
|
75
|
+
def assertEqual(self, actual, expected):
|
|
76
|
+
def do_sorted(d):
|
|
77
|
+
s = {}
|
|
78
|
+
for n in d:
|
|
79
|
+
s[n] = sorted(d[n])
|
|
80
|
+
return s
|
|
81
|
+
|
|
82
|
+
super().assertEqual(do_sorted(actual), do_sorted(expected))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
main()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class BuiltinsTest(TestBase):
|
|
7
|
+
snippet_dir = "builtins"
|
|
8
|
+
|
|
9
|
+
def test_types(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("types"))
|
|
11
|
+
|
|
12
|
+
def test_map(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("map"))
|
|
14
|
+
|
|
15
|
+
def test_functions(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("functions"))
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class ClassesTest(TestBase):
|
|
7
|
+
snippet_dir = "classes"
|
|
8
|
+
|
|
9
|
+
def test_imported_call_without_init(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("imported_call_without_init"))
|
|
11
|
+
|
|
12
|
+
def test_direct_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("direct_call"))
|
|
14
|
+
|
|
15
|
+
def test_return_call_direct(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("return_call_direct"))
|
|
17
|
+
|
|
18
|
+
def test_nested_class_calls(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("nested_class_calls"))
|
|
20
|
+
|
|
21
|
+
def test_super_class_return(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("super_class_return"))
|
|
23
|
+
|
|
24
|
+
def test_base_class_calls_child(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("base_class_calls_child"))
|
|
26
|
+
|
|
27
|
+
def test_tuple_assignment(self):
|
|
28
|
+
self.validate_snippet(self.get_snippet_path("tuple_assignment"))
|
|
29
|
+
|
|
30
|
+
def test_return_call(self):
|
|
31
|
+
self.validate_snippet(self.get_snippet_path("return_call"))
|
|
32
|
+
|
|
33
|
+
def test_nested_call(self):
|
|
34
|
+
self.validate_snippet(self.get_snippet_path("nested_call"))
|
|
35
|
+
|
|
36
|
+
def test_imported_attr_access(self):
|
|
37
|
+
self.validate_snippet(self.get_snippet_path("imported_attr_access"))
|
|
38
|
+
|
|
39
|
+
def test_assigned_call(self):
|
|
40
|
+
self.validate_snippet(self.get_snippet_path("assigned_call"))
|
|
41
|
+
|
|
42
|
+
def test_self_assign_func(self):
|
|
43
|
+
self.validate_snippet(self.get_snippet_path("self_assign_func"))
|
|
44
|
+
|
|
45
|
+
def test_imported_call(self):
|
|
46
|
+
self.validate_snippet(self.get_snippet_path("imported_call"))
|
|
47
|
+
|
|
48
|
+
def test_base_class_attr(self):
|
|
49
|
+
self.validate_snippet(self.get_snippet_path("base_class_attr"))
|
|
50
|
+
|
|
51
|
+
def test_static_method_call(self):
|
|
52
|
+
self.validate_snippet(self.get_snippet_path("static_method_call"))
|
|
53
|
+
|
|
54
|
+
def test_call(self):
|
|
55
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
56
|
+
|
|
57
|
+
def test_instance(self):
|
|
58
|
+
self.validate_snippet(self.get_snippet_path("instance"))
|
|
59
|
+
|
|
60
|
+
def test_imported_nested_attr_access(self):
|
|
61
|
+
self.validate_snippet(self.get_snippet_path("imported_nested_attr_access"))
|
|
62
|
+
|
|
63
|
+
def test_parameter_call(self):
|
|
64
|
+
self.validate_snippet(self.get_snippet_path("parameter_call"))
|
|
65
|
+
|
|
66
|
+
def test_self_assignment(self):
|
|
67
|
+
self.validate_snippet(self.get_snippet_path("self_assignment"))
|
|
68
|
+
|
|
69
|
+
def test_self_call(self):
|
|
70
|
+
self.validate_snippet(self.get_snippet_path("self_call"))
|
|
71
|
+
|
|
72
|
+
def test_assigned_self_call(self):
|
|
73
|
+
self.validate_snippet(self.get_snippet_path("assigned_self_call"))
|
|
@@ -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,28 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class DecoratorsTest(TestBase):
|
|
7
|
+
snippet_dir = "decorators"
|
|
8
|
+
|
|
9
|
+
def test_return_different_func(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("return_different_func"))
|
|
11
|
+
|
|
12
|
+
def test_param_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("param_call"))
|
|
14
|
+
|
|
15
|
+
def test_nested_decorators(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("nested_decorators"))
|
|
17
|
+
|
|
18
|
+
def test_assigned(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("assigned"))
|
|
20
|
+
|
|
21
|
+
def test_return(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("return"))
|
|
23
|
+
|
|
24
|
+
def test_call(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
26
|
+
|
|
27
|
+
def test_nested(self):
|
|
28
|
+
self.validate_snippet(self.get_snippet_path("nested"))
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class DictsTest(TestBase):
|
|
7
|
+
snippet_dir = "dicts"
|
|
8
|
+
|
|
9
|
+
def test_type_coercion(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("type_coercion"))
|
|
11
|
+
|
|
12
|
+
def test_update(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("update"))
|
|
14
|
+
|
|
15
|
+
def test_ext_key(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("ext_key"))
|
|
17
|
+
|
|
18
|
+
def test_return(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("return"))
|
|
20
|
+
|
|
21
|
+
def test_param(self):
|
|
22
|
+
self.validate_snippet(self.get_snippet_path("param"))
|
|
23
|
+
|
|
24
|
+
def test_return_assign(self):
|
|
25
|
+
self.validate_snippet(self.get_snippet_path("return_assign"))
|
|
26
|
+
|
|
27
|
+
def test_call(self):
|
|
28
|
+
self.validate_snippet(self.get_snippet_path("call"))
|
|
29
|
+
|
|
30
|
+
def test_assign(self):
|
|
31
|
+
self.validate_snippet(self.get_snippet_path("assign"))
|
|
32
|
+
|
|
33
|
+
def test_param_key(self):
|
|
34
|
+
self.validate_snippet(self.get_snippet_path("param_key"))
|
|
35
|
+
|
|
36
|
+
def test_new_key_param(self):
|
|
37
|
+
self.validate_snippet(self.get_snippet_path("new_key_param"))
|
|
38
|
+
|
|
39
|
+
def test_nested(self):
|
|
40
|
+
self.validate_snippet(self.get_snippet_path("nested"))
|
|
41
|
+
|
|
42
|
+
def test_add_key(self):
|
|
43
|
+
self.validate_snippet(self.get_snippet_path("add_key"))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from base import TestBase
|
|
5
|
+
|
|
6
|
+
class DirectCallsTest(TestBase):
|
|
7
|
+
snippet_dir = "direct_calls"
|
|
8
|
+
|
|
9
|
+
def test_with_parameters(self):
|
|
10
|
+
self.validate_snippet(self.get_snippet_path("with_parameters"))
|
|
11
|
+
|
|
12
|
+
def test_imported_return_call(self):
|
|
13
|
+
self.validate_snippet(self.get_snippet_path("imported_return_call"))
|
|
14
|
+
|
|
15
|
+
def test_return_call(self):
|
|
16
|
+
self.validate_snippet(self.get_snippet_path("return_call"))
|
|
17
|
+
|
|
18
|
+
def test_assigned_call(self):
|
|
19
|
+
self.validate_snippet(self.get_snippet_path("assigned_call"))
|