PyCG 0.0.3__py3-none-any.whl
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.
- micro-benchmark/__init__.py +0 -0
- micro-benchmark/args_test.py +25 -0
- micro-benchmark/assignments_test.py +19 -0
- micro-benchmark/base.py +87 -0
- micro-benchmark/builtins_test.py +16 -0
- micro-benchmark/classes_test.py +73 -0
- micro-benchmark/create_pytests.py +42 -0
- micro-benchmark/decorators_test.py +28 -0
- micro-benchmark/dicts_test.py +43 -0
- micro-benchmark/direct_calls_test.py +19 -0
- micro-benchmark/dynamic_test.py +10 -0
- micro-benchmark/exceptions_test.py +16 -0
- micro-benchmark/external_test.py +25 -0
- micro-benchmark/functions_test.py +19 -0
- micro-benchmark/generators_test.py +25 -0
- micro-benchmark/imports_test.py +49 -0
- micro-benchmark/kwargs_test.py +16 -0
- micro-benchmark/lambdas_test.py +22 -0
- micro-benchmark/lists_test.py +31 -0
- micro-benchmark/mro_test.py +28 -0
- micro-benchmark/returns_test.py +19 -0
- micro-benchmark-key-errs/__init__.py +0 -0
- micro-benchmark-key-errs/assignments_test.py +16 -0
- micro-benchmark-key-errs/base.py +81 -0
- micro-benchmark-key-errs/classes_test.py +25 -0
- micro-benchmark-key-errs/create_pytests.py +42 -0
- micro-benchmark-key-errs/dicts_test.py +34 -0
- micro-benchmark-key-errs/kwargs_test.py +13 -0
- micro-benchmark-key-errs/lists_test.py +13 -0
- micro-benchmark-key-errs/parameters_test.py +16 -0
- pycg/__init__.py +0 -0
- pycg/__main__.py +104 -0
- pycg/formats/__init__.py +23 -0
- pycg/formats/as_graph.py +32 -0
- pycg/formats/base.py +23 -0
- pycg/formats/fasten.py +253 -0
- pycg/formats/simple.py +32 -0
- pycg/machinery/__init__.py +0 -0
- pycg/machinery/callgraph.py +59 -0
- pycg/machinery/classes.py +77 -0
- pycg/machinery/definitions.py +219 -0
- pycg/machinery/imports.py +220 -0
- pycg/machinery/key_err.py +34 -0
- pycg/machinery/modules.py +66 -0
- pycg/machinery/pointers.py +146 -0
- pycg/machinery/scopes.py +143 -0
- pycg/processing/__init__.py +0 -0
- pycg/processing/base.py +522 -0
- pycg/processing/cgprocessor.py +230 -0
- pycg/processing/keyerrprocessor.py +82 -0
- pycg/processing/postprocessor.py +308 -0
- pycg/processing/preprocessor.py +375 -0
- pycg/pycg.py +229 -0
- pycg/utils/__init__.py +22 -0
- pycg/utils/common.py +39 -0
- pycg/utils/constants.py +42 -0
- pycg-0.0.3.dist-info/LICENCE +201 -0
- pycg-0.0.3.dist-info/METADATA +150 -0
- pycg-0.0.3.dist-info/RECORD +62 -0
- pycg-0.0.3.dist-info/WHEEL +5 -0
- pycg-0.0.3.dist-info/entry_points.txt +3 -0
- pycg-0.0.3.dist-info/top_level.txt +3 -0
|
@@ -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,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,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"))
|
pycg/__init__.py
ADDED
|
File without changes
|
pycg/__main__.py
ADDED
|
@@ -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()
|
pycg/formats/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
from .fasten import Fasten
|
|
22
|
+
from .simple import Simple
|
|
23
|
+
from .as_graph import AsGraph
|
pycg/formats/as_graph.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
from .base import BaseFormatter
|
|
22
|
+
|
|
23
|
+
class AsGraph(BaseFormatter):
|
|
24
|
+
def __init__(self, cg_generator):
|
|
25
|
+
self.cg_generator = cg_generator
|
|
26
|
+
|
|
27
|
+
def generate(self):
|
|
28
|
+
graph = self.cg_generator.get_as_graph()
|
|
29
|
+
output = {}
|
|
30
|
+
for key, defi in graph:
|
|
31
|
+
output[key] = list(defi.get_name_pointer().get().copy())
|
|
32
|
+
return output
|
pycg/formats/base.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
class BaseFormatter:
|
|
22
|
+
def generate(self):
|
|
23
|
+
raise NotImplementedError("generate method not implemented in child class")
|
pycg/formats/fasten.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
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
|
+
|
|
23
|
+
from pkg_resources import Requirement
|
|
24
|
+
|
|
25
|
+
from .base import BaseFormatter
|
|
26
|
+
|
|
27
|
+
from pycg import utils
|
|
28
|
+
|
|
29
|
+
class Fasten(BaseFormatter):
|
|
30
|
+
def __init__(self, cg_generator, package, product, forge, version, timestamp):
|
|
31
|
+
self.cg_generator = cg_generator
|
|
32
|
+
self.internal_mods = self.cg_generator.output_internal_mods() or {}
|
|
33
|
+
self.external_mods = self.cg_generator.output_external_mods() or {}
|
|
34
|
+
self.classes = self.cg_generator.output_classes() or {}
|
|
35
|
+
self.edges = self.cg_generator.output_edges() or []
|
|
36
|
+
self.functions = self.cg_generator.output_functions() or []
|
|
37
|
+
self.unique = 0
|
|
38
|
+
self.namespace_map = {}
|
|
39
|
+
self.package = package
|
|
40
|
+
self.product = product
|
|
41
|
+
self.forge = forge
|
|
42
|
+
self.version = version
|
|
43
|
+
self.timestamp = timestamp
|
|
44
|
+
|
|
45
|
+
def get_unique_and_increment(self):
|
|
46
|
+
unique = self.unique
|
|
47
|
+
self.unique += 1
|
|
48
|
+
return unique
|
|
49
|
+
|
|
50
|
+
def to_uri(self, modname, name=""):
|
|
51
|
+
cleared = name
|
|
52
|
+
if name:
|
|
53
|
+
if name == modname:
|
|
54
|
+
cleared = ""
|
|
55
|
+
else:
|
|
56
|
+
if not name.startswith(modname + "."):
|
|
57
|
+
raise Exception("name should start with modname", name, modname)
|
|
58
|
+
|
|
59
|
+
cleared = name[len(modname)+1:]
|
|
60
|
+
|
|
61
|
+
suffix = ""
|
|
62
|
+
if name in self.functions:
|
|
63
|
+
suffix = "()"
|
|
64
|
+
|
|
65
|
+
return "/{}/{}{}".format(modname.replace("-", "_"), cleared, suffix)
|
|
66
|
+
|
|
67
|
+
def to_external_uri(self, modname, name=""):
|
|
68
|
+
if modname == utils.constants.BUILTIN_NAME:
|
|
69
|
+
name = name[len(modname)+1:]
|
|
70
|
+
modname = ".builtin"
|
|
71
|
+
|
|
72
|
+
return "//{}//{}".format(modname.replace("-", "_"), name)
|
|
73
|
+
|
|
74
|
+
def find_dependencies(self, package_path):
|
|
75
|
+
res = []
|
|
76
|
+
if not package_path:
|
|
77
|
+
return res
|
|
78
|
+
requirements_path = os.path.join(package_path, "requirements.txt")
|
|
79
|
+
|
|
80
|
+
if not os.path.exists(requirements_path):
|
|
81
|
+
return res
|
|
82
|
+
|
|
83
|
+
reqs = []
|
|
84
|
+
with open(requirements_path, "r") as f:
|
|
85
|
+
lines = [l.strip() for l in f.readlines()]
|
|
86
|
+
|
|
87
|
+
for line in lines:
|
|
88
|
+
if not line:
|
|
89
|
+
continue
|
|
90
|
+
|
|
91
|
+
req = Requirement.parse(line)
|
|
92
|
+
|
|
93
|
+
product = req.unsafe_name
|
|
94
|
+
specs = req.specs
|
|
95
|
+
|
|
96
|
+
constraints = []
|
|
97
|
+
|
|
98
|
+
def add_range(begin, end):
|
|
99
|
+
if begin and end:
|
|
100
|
+
if begin[1] and end[1]:
|
|
101
|
+
constraints.append("[{}..{}]".format(begin[0], end[0]))
|
|
102
|
+
elif begin[1]:
|
|
103
|
+
constraints.append("[{}..{})".format(begin[0], end[0]))
|
|
104
|
+
elif end[1]:
|
|
105
|
+
constraints.append("({}..{}]".format(begin[0], end[0]))
|
|
106
|
+
else:
|
|
107
|
+
constraints.append("({}..{})".format(begin[0], end[0]))
|
|
108
|
+
elif begin:
|
|
109
|
+
if begin[1]:
|
|
110
|
+
constraints.append("[{}..]".format(begin[0]))
|
|
111
|
+
else:
|
|
112
|
+
constraints.append("({}..]".format(begin[0]))
|
|
113
|
+
elif end:
|
|
114
|
+
if end[1]:
|
|
115
|
+
constraints.append("[..{}]".format(end[0]))
|
|
116
|
+
else:
|
|
117
|
+
constraints.append("[..{})".format(end[0]))
|
|
118
|
+
|
|
119
|
+
begin = None
|
|
120
|
+
end = None
|
|
121
|
+
for key, val in sorted(specs, key=lambda x: x[1]):
|
|
122
|
+
# if begin, then it is already in a range
|
|
123
|
+
if key == "==":
|
|
124
|
+
if begin and end:
|
|
125
|
+
add_range(begin, end)
|
|
126
|
+
begin = None
|
|
127
|
+
end = None
|
|
128
|
+
if not begin:
|
|
129
|
+
constraints.append("[{}]".format(val))
|
|
130
|
+
|
|
131
|
+
if key == ">":
|
|
132
|
+
if end:
|
|
133
|
+
add_range(begin, end)
|
|
134
|
+
end = None
|
|
135
|
+
begin = None
|
|
136
|
+
if not begin:
|
|
137
|
+
begin = (val, False)
|
|
138
|
+
if key == ">=":
|
|
139
|
+
if end:
|
|
140
|
+
add_range(begin, end)
|
|
141
|
+
begin = None
|
|
142
|
+
end = None
|
|
143
|
+
if not begin:
|
|
144
|
+
begin = (val, True)
|
|
145
|
+
|
|
146
|
+
if key == "<":
|
|
147
|
+
end = (val, False)
|
|
148
|
+
if key == "<=":
|
|
149
|
+
end = (val, True)
|
|
150
|
+
add_range(begin, end)
|
|
151
|
+
|
|
152
|
+
res.append({"forge": "PyPI", "product": req.name, "constraints": constraints})
|
|
153
|
+
|
|
154
|
+
return res
|
|
155
|
+
|
|
156
|
+
def get_modules(self):
|
|
157
|
+
mods = {}
|
|
158
|
+
|
|
159
|
+
for modname, module in self.internal_mods.items():
|
|
160
|
+
name = self.to_uri(modname)
|
|
161
|
+
filename = module["filename"]
|
|
162
|
+
namespaces = module["methods"]
|
|
163
|
+
|
|
164
|
+
mods[name] = {
|
|
165
|
+
"sourceFile": filename,
|
|
166
|
+
"namespaces": {}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
for namespace, info in namespaces.items():
|
|
170
|
+
namespace_uri = self.to_uri(modname, info['name'])
|
|
171
|
+
|
|
172
|
+
unique = self.get_unique_and_increment()
|
|
173
|
+
mods[name]["namespaces"][unique] = dict(
|
|
174
|
+
namespace=namespace_uri,
|
|
175
|
+
metadata=dict(first=info['first'], last=info['last']))
|
|
176
|
+
self.namespace_map[namespace_uri] = unique
|
|
177
|
+
|
|
178
|
+
return mods
|
|
179
|
+
|
|
180
|
+
def class_hiearchy(self):
|
|
181
|
+
hierarchy = {}
|
|
182
|
+
for cls_name, cls in self.classes.items():
|
|
183
|
+
cls_uri = self.namespace_map.get(self.to_uri(cls["module"], cls_name))
|
|
184
|
+
hierarchy[cls_uri] = []
|
|
185
|
+
for parent in cls["mro"]:
|
|
186
|
+
if parent == cls_name:
|
|
187
|
+
continue
|
|
188
|
+
|
|
189
|
+
if self.classes.get(parent):
|
|
190
|
+
parent_uri = self.to_uri(self.classes[parent]["module"], parent)
|
|
191
|
+
parent_uri = self.namespace_map.get(parent_uri, parent_uri)
|
|
192
|
+
else:
|
|
193
|
+
parent_mod = parent.split(".")[0]
|
|
194
|
+
parent_uri = self.to_external_uri(parent_mod, parent)
|
|
195
|
+
|
|
196
|
+
hierarchy[cls_uri].append(parent_uri)
|
|
197
|
+
|
|
198
|
+
return hierarchy
|
|
199
|
+
|
|
200
|
+
def create_namespaces_map(self):
|
|
201
|
+
namespaces_maps = [{}, {}]
|
|
202
|
+
for res, hmap in zip(namespaces_maps, [self.internal_mods, self.external_mods]):
|
|
203
|
+
for mod in hmap:
|
|
204
|
+
for namespace in hmap[mod]["methods"]:
|
|
205
|
+
res[namespace] = mod
|
|
206
|
+
|
|
207
|
+
return namespaces_maps
|
|
208
|
+
|
|
209
|
+
def get_graph(self):
|
|
210
|
+
graph = {
|
|
211
|
+
"internalCalls": [],
|
|
212
|
+
"externalCalls": []
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
internal, external = self.create_namespaces_map()
|
|
216
|
+
|
|
217
|
+
for src, dst in self.edges:
|
|
218
|
+
uris = []
|
|
219
|
+
for node in [src, dst]:
|
|
220
|
+
if node in internal:
|
|
221
|
+
mod = internal[node]
|
|
222
|
+
uri = self.to_uri(mod, node)
|
|
223
|
+
uris.append(self.namespace_map.get(uri, uri))
|
|
224
|
+
elif node in external:
|
|
225
|
+
mod = external[node]
|
|
226
|
+
uris.append(self.to_external_uri(mod, node))
|
|
227
|
+
|
|
228
|
+
if len(uris) == 2:
|
|
229
|
+
# internal uris have been converted to ints
|
|
230
|
+
if type(uris[1]) == str and uris[1].startswith("//"):
|
|
231
|
+
graph["externalCalls"].append([
|
|
232
|
+
uris[0],
|
|
233
|
+
uris[1]
|
|
234
|
+
])
|
|
235
|
+
else:
|
|
236
|
+
graph["internalCalls"].append([
|
|
237
|
+
uris[0],
|
|
238
|
+
uris[1]
|
|
239
|
+
])
|
|
240
|
+
return graph
|
|
241
|
+
|
|
242
|
+
def generate(self):
|
|
243
|
+
return {
|
|
244
|
+
"product": self.product,
|
|
245
|
+
"forge": self.forge,
|
|
246
|
+
"generator": "PyCG",
|
|
247
|
+
"depset": self.find_dependencies(self.package),
|
|
248
|
+
"version": self.version,
|
|
249
|
+
"timestamp": self.timestamp,
|
|
250
|
+
"modules": self.get_modules(),
|
|
251
|
+
"cha": self.class_hiearchy(),
|
|
252
|
+
"graph": self.get_graph()
|
|
253
|
+
}
|