forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_topological_sort_of_all_services.py
152 lines (128 loc) · 5.78 KB
/
create_topological_sort_of_all_services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# coding: utf-8
#
# Copyright 2019 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script generates topological sort of all the services based on how
services are dependent on each other.
"""
from __future__ import absolute_import # pylint: disable=import-only-modules
from __future__ import unicode_literals # pylint: disable=import-only-modules
import collections
import os
import sys
import python_utils
_PARENT_DIR = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
_PATHS_TO_INSERT = [
os.path.join(_PARENT_DIR, 'oppia_tools', 'esprima-4.0.1'),
]
for path in _PATHS_TO_INSERT:
sys.path.insert(0, path)
# pylint: disable=wrong-import-position
import esprima # isort:skip
# pylint: enable=wrong-import-position
DIRECTORY_NAMES = ['core/templates/dev/head', 'extensions']
SERVICE_FILES_SUFFICES = ('.service.ts', 'Service.ts', 'Factory.ts')
def dfs(node, topo_sort_stack, adj_list, visit_stack):
"""Depth First Search starting with node.
Args:
node: str. The service name from which dfs will begin.
topo_sort_stack: list(str). Stores topological sort of services
in reveresed way.
adj_list: dict. Adjacency list of the graph formed with services
as nodes and dependencies as edges.
visit_stack: list(str). Keeps track of visited and unvisited nodes.
"""
visit_stack.append(node)
for pt in adj_list[node]:
if pt not in visit_stack:
dfs(pt, topo_sort_stack, adj_list, visit_stack)
topo_sort_stack.append(node)
def make_graph():
"""Creates an adjaceny list considering services as node and dependencies
as edges.
Returns:
tuple(dict, set(str)). Adjancency list of the graph formed with
services as nodes and dependencies as edges, set of all the services.
"""
adj_list = collections.defaultdict(list)
nodes_set = set()
for dirname in DIRECTORY_NAMES:
for root, _, filenames in os.walk(dirname):
for filename in filenames:
if filename.endswith(SERVICE_FILES_SUFFICES):
nodes_set.add(filename)
filepath = os.path.join(root, filename)
with python_utils.open_file(filepath, 'r') as f:
file_lines = f.readlines()
dep_lines = ''
index = 0
while index < len(file_lines):
line = file_lines[index]
if line.startswith('require'):
while not line.endswith(';\n'):
dep_lines = dep_lines + line
index += 1
line = file_lines[index]
dep_lines = dep_lines + line
index += 1
elif line.startswith('import'):
while not line.endswith(';\n'):
index += 1
line = file_lines[index]
if '\'' in line:
break
dep_lines = dep_lines + (
'require (' + line[
line.find('\''):line.rfind('\'') + 1
] + ');\n')
index += 1
else:
index += 1
parsed_script = esprima.parseScript(dep_lines, comment=True)
parsed_nodes = parsed_script.body
for parsed_node in parsed_nodes:
# For require statements.
if parsed_node.type == 'ExpressionStatement' and (
parsed_node.expression.callee.name == (
'require')):
arguments = parsed_node.expression.arguments
for argument in arguments:
dep_path = argument.value
if argument.operator == '+':
dep_path = (
argument.left.value +
argument.right.value)
if not dep_path.endswith('.ts'):
dep_path = dep_path + '.ts'
if dep_path.endswith(SERVICE_FILES_SUFFICES):
dep_name = os.path.basename(dep_path)
adj_list[dep_name].append(filename)
return (adj_list, nodes_set)
def main():
"""Prints the topological order of the services based on the
dependencies.
"""
adj_list, nodes_set = make_graph()
visit_stack = []
topo_sort_stack = []
for unchecked_node in nodes_set:
if unchecked_node not in visit_stack:
dfs(unchecked_node, topo_sort_stack, adj_list, visit_stack)
topo_sort_stack.reverse()
for service in topo_sort_stack:
python_utils.PRINT(service)
# The 'no coverage' pragma is used as this line is un-testable. This is because
# it will only be called when build.py is used as a script.
if __name__ == '__main__': # pragma: no cover
main()