forked from wingtk/gvsbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.py
310 lines (283 loc) · 8.8 KB
/
deps.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright (C) 2017 - Daniele Forghieri
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
"""gvsbuild deps print / .gv graph."""
# Verify we can import from the script directory
try:
import gvsbuild.utils.utils
except ImportError:
# We are probably using an embedded installation
print(
"Error importing utility (running the embedded interpreter ?), fixing paths ..."
)
import os
import sys
# Get the script dir
script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
# and add it at the beginning, emulating the standard python startup
sys.path.insert(0, script_dir)
import argparse
import gvsbuild.groups # noqa: F401
import gvsbuild.projects # noqa: F401
import gvsbuild.tools # noqa: F401
from gvsbuild.utils.base_project import (
GVSBUILD_GROUP,
GVSBUILD_PROJECT,
GVSBUILD_TOOL,
Project,
)
from gvsbuild.utils.utils import ordered_set
def print_deps(flatten=False, add_all=False):
done = []
def dump_single_dep(st, name, flatten):
if flatten:
if not st:
done.append(name)
else:
if st:
# dependency
print(
"%s%s"
% (
st,
name,
)
)
else:
print(" > {}".format(name))
st = " "
done.append(name)
rt = False
p = Project._dict[name]
if p.dependencies:
for d in p.dependencies:
add = True
if not add_all:
ty = Project._dict[d].type
if ty != GVSBUILD_PROJECT:
add = False
if add:
rt = True
if d in done:
if not flatten:
print(
"%s %s *"
% (
st,
d,
)
)
else:
done.append(d)
dump_single_dep(st + " ", d, flatten)
return rt
prj = [x.name for x in Project._projects if x.type == GVSBUILD_PROJECT]
print("Projects dependencies:")
for n in prj:
done = []
if flatten:
print("> {}".format(n))
if dump_single_dep("", n, flatten):
if flatten:
done.remove(n)
for t in sorted(done):
print(" {}".format(t))
else:
print("")
def make_graph(
out_file,
put_all=False,
invert_dep=False,
add_tools=False,
add_groups=False,
skip="",
):
gr_colors = [
0x000080,
0x008000,
0x008080,
0x800000,
0x800080,
0x808000,
0x808080,
0x0000F0,
0x00F000,
0x00F0F0,
0xF00000,
0xF000F0,
0xF0F000,
0xF00080,
0xF08000,
0xF08080,
0x80F000,
0x80F080,
0x00F080,
0x0080F0,
0x8000F0,
0x8080F0,
]
gr_index = 0
to_skip = set(skip.split(","))
with open(out_file, "wt") as fo:
print("Writing file {}".format(out_file))
used = set()
fo.write("digraph gtk3dep {\n")
for n in Project._names:
if n not in to_skip:
t = Project._dict[n]
add = True
if t.type == GVSBUILD_TOOL:
add = add_tools
elif t.type == GVSBUILD_GROUP:
add = add_groups
else:
add = True
if add:
if t.dependencies:
gr_index += 1
gr_index %= len(gr_colors)
for d in t.dependencies:
if d in to_skip:
print(
"Skip '%s' for '%s'"
% (
d,
n,
)
)
else:
if invert_dep:
fo.write(
' "%s" -> "%s" [color="#%06x"];\n'
% (
d,
n,
gr_colors[gr_index],
)
)
else:
fo.write(
' "%s" -> "%s" [color="#%06x"];\n'
% (
n,
d,
gr_colors[gr_index],
)
)
used.add(d)
else:
used.add(t.name)
if put_all:
# Puts all projects that are not referenced from others
for n in Project._names:
if n not in used:
fo.write(
' "%s" -> "%s" [color="#c00080"];\n'
% (
"BUILD",
n,
)
)
fo.write("};\n")
def compute_deps(proj):
if hasattr(proj, "all_dependencies"):
return
deps = ordered_set()
for dep in proj.dependencies:
compute_deps(dep)
for p in dep.all_dependencies:
deps.add(p)
deps.add(dep)
proj.all_dependencies = deps
def main():
parser = argparse.ArgumentParser(description="Gvsbuild dependency print / analyze")
group = parser.add_argument_group("Dependencies print (default)")
# Simple dep dump
group.add_argument(
"-f",
"--flatten",
default=False,
action="store_true",
help="Flatten (and sort) the dependencies dump of the single project.",
)
group.add_argument(
"--dep-tools",
default=False,
action="store_true",
help="Add also the tool projects.",
)
# .gv (dot) graph of dependencies
group = parser.add_argument_group(
"Graph file (.gv, dot format) of the dependencies"
)
group.add_argument(
"-g",
"--graph",
default=False,
action="store_true",
help="Create a .gv graph of the dependencies.",
)
group.add_argument(
"-a",
"--all",
default=False,
action="store_true",
help="Graph: add also all unreferenced projects to the graph.",
)
group.add_argument(
"--add-tools",
default=False,
action="store_true",
help="Graph: add also the tool projects.",
)
group.add_argument(
"--add-groups",
default=False,
action="store_true",
help="Graph: add also the group projects.",
)
group.add_argument(
"-o", "--gv-file", default="wingtk.gv", help="Graph: output file name."
)
group.add_argument(
"-i",
"--invert",
default=False,
action="store_true",
help="Graph: invert the dependency track.",
)
group.add_argument(
"--skip", default="", help="A comma separated list of project(s) not to handle."
)
# get the option(s)
opt = parser.parse_args()
# now add the tools/projects/groups
Project.add_all()
# do what's asked
if opt.graph:
# .gv graph
make_graph(
out_file=opt.gv_file,
put_all=opt.all,
invert_dep=opt.invert,
add_tools=opt.add_tools,
add_groups=opt.add_groups,
skip=opt.skip,
)
else:
# simple dep print
print_deps(flatten=opt.flatten, add_all=opt.dep_tools)
if __name__ == "__main__":
main()