-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grapher.pl
407 lines (344 loc) · 15 KB
/
grapher.pl
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Author: Pieter Van den Abeele
E-mail: pvdabeel@mac.com
Copyright (c) 2005-2024, Pieter Van den Abeele
Distributed under the terms of the LICENSE file in the root directory of this
project.
*/
/** <module> GRAPHER
This file contains predicates that convert:
- ebuilds into DOT language directed graphs showing ebuild dependencies in detail
- ebuilds into DOT language directed graphs showing full dependency tree
We have can output two type of diagrams: High level tree diagrams or detailled
diagrams showing, for a given ebuild, all dependencies and the ebuilds that
could satisfy the dependency.
*/
:- module(grapher, []).
% ********************
% GRAPHER declarations
% ********************
%! grapher:graph(+Type,+Id)
%
% For a given ebuild, identified by an Id, create a Graphviz dot file
grapher:graph(detail,Repository://Id) :-
writeln('digraph prolog {'),
nl,
writeln('# *************'),
writeln('# Graph options'),
writeln('# *************'),
nl,
writeln('newrank=true;'),
writeln('concentrate=true;'),
writeln('compound=true;'),
writeln('graph [rankdir=LR,fontname=Helvetica,fontsize=10,ranksep=1.5];#, ranksep=2.5, nodesep=0.2];'),
writeln('edge [arrowhead=vee];'),
writeln('node [fontname=Helvetica,fontsize=10];'),
nl,
writeln('# **********'),
writeln('# The ebuild'),
writeln('# **********'),
nl,
write('subgraph cluster_leftcol {'),nl,
write('color=gray;'),nl,
%write('rank=same;'),nl,
write('label=<<i>ebuild</i>>;'),nl,
write('id [label=\"'),write(Repository://Id),write('\", color=red, width=4, href=\"../'),write(Id),write('.svg\"];'),nl,
write('}'),nl,
nl,
writeln('# ****************'),
writeln('# The dependencies'),
writeln('# ****************'),
nl,
write('subgraph cluster_midcol {'),nl,
write('color=gray;'),nl,
write('label=<<i>dependencies</i>>;'),nl,
query:search(all(depend(C)),Repository://Id),
query:search(all(rdepend(R)),Repository://Id),
list_to_ord_set(C,OC),
list_to_ord_set(R,OR),
ord_intersection(OC,OR,OCR,OPR),
ord_intersection(OR,OC,OCR,OPC),
writeln('subgraph cluster_compile {'),
writeln('fillcolor="#eeeeee";'),
writeln('style=filled;'),
writeln('label=<<i>compile</i>>;'),
findall(Ch,(member(equal(D),OPC),grapher:handle(detail,solid,vee,id,D,Ch)),AllChoices1),
writeln('}'),
writeln('subgraph cluster_compileandrun {'),
writeln('fillcolor="#eeeeee";'),
writeln('style=filled;'),
writeln('label=<<i>compile and run</i>>;'),
findall(Ch,(member(equal(D),OCR),grapher:handle(detail,solid,odotvee,id,D,Ch)),AllChoices2),
writeln('}'),
writeln('subgraph cluster_run {'),
writeln('fillcolor="#eeeeee";'),
writeln('style=filled;'),
writeln('label=<<i>run</i>>;'),
findall(Ch,(member(equal(D),OPR),grapher:handle(detail,solid,odot,id,D,Ch)),AllChoices3),
writeln('}'),
union(AllChoices1,AllChoices2,AllChoices12),
union(AllChoices12,AllChoices3,AllChoices),
write('}'),nl,
nl,
writeln('# **************'),
writeln('# The candidates'),
writeln('# **************'),
nl,
writeln('subgraph cluster_choices {'),
writeln('rank=same;'),
writeln('color=gray;'),
write('label=<<i>candidates</i>>;'),nl,
nl,
grapher:choices(detail,AllChoices),
writeln('}'),
nl,
writeln('}').
%! grapher:graph(+Type,+Id)
%
% For a given ebuild, identified by an Id, create a full dependency diagram.
grapher:graph(Type,Repository://Id) :-
member(Type,[depend,rdepend]),!,
writeln('digraph prolog {'),
nl,
writeln('newrank=true;'),
writeln('concentrate=true;'),
writeln('compound=true;'),
writeln('graph [rankdir=TD];#, ranksep=2.5, nodesep=0.2];'),
writeln('edge [arrowhead=vee];'), % arrowsize=0.6 constraint= true
writeln('node [fontname=Helvetica,fontsize=10];'),
nl,
retractall(graph_visited(_)),
grapher:write_tree(Repository://Id,Type),
writeln('}'),
retractall(graph_visited(_)).
%! grapher:write_tree(+Id)
%
% For a given ebuild, identified by an Id, create a tree diagram.
grapher:write_tree(Repository://Id, Type) :-
not(graph_visited(Repository://Id)),!,
write('\"'),write(Repository://Id),write('\" [color=red, href=\"../'),write(Id),write('-'),write(Type),write('.svg\"];'),nl,
Statement =.. [Type,equal(DS)],
query:search(Statement,Repository://Id),
findall(Ch,(member(D,DS),grapher:handle(Type,solid,vee,Repository://Id,D,Ch)),AllChoices),
assert(graph_visited(Repository://Id)),
grapher:choices(Type,AllChoices),
forall(member(arrow(_,[Chs]),AllChoices),
grapher:write_tree(Chs,Type)).
grapher:write_tree(Repository://Id,_Type) :-
graph_visited(Repository://Id),!.
%! grapher:enconvert(+Id,-Code)
%
% Create a unique name for a given ebuild.
grapher:enconvert(_Repository://Id,Code) :-
!,
atom_codes(Id,List),
atomic_list_concat([choice|List],Code).
grapher:enconvert(Id,Code) :-
!,
atom_codes(Id,List),
atomic_list_concat([choice|List],Code).
%! grapher:choices(+Type,+List)
%
% Given a graph type (detail or full), outputs a list of ebuilds satisfying
% a dependency
grapher:choices(_,[]) :-
!,true.
grapher:choices(detail,[arrow(D,Choices)|Rest]) :-
!,
gensym(choice,C),
write('subgraph '),write(C),write(' {'),nl,
write('color=black;'),nl,
write('nodesep=1;'),nl,
forall(member(Repository://Ch,Choices),(
grapher:enconvert(Repository://Ch,Code),
write(Code),
write(' [label=\"'),write(Repository://Ch),write('\", color=red, width=4,href=\"../'),write(Ch),write('.svg\"];'),nl)),
forall(member(Repository://Ch,Choices),(
write(D),
write(':e -> '),
grapher:enconvert(Repository://Ch,Code),
write(Code),write(':w [style=dotted,weight=\"100\"];'),nl)),
writeln('}'),
grapher:choices(detail,Rest).
grapher:choices(depend,[arrow(D,[Choice])|Rest]) :-
!,
write('\"'),write(D),write('\"'),
write(' -> '),
write('\"'),write(Choice),write('\"'),nl,
grapher:choices(depend,Rest).
grapher:choices(cdepend,[arrow(D,[Choice])|Rest]) :-
!,
write('\"'),write(D),write('\"'),
write(' -> '),
write('\"'),write(Choice),write('\"'),nl,
grapher:choices(cdepend,Rest).
grapher:choices(rdepend,[arrow(D,[Choice])|Rest]) :-
!,
write('\"'),write(D),write('\"'),
write(' -> '),
write('\"'),write(Choice),write('\"'),nl,
grapher:choices(rdepend,Rest).
grapher:choices(Kind,[L|Rest]) :-
!,
grapher:choices(Kind,L),
grapher:choices(Kind,Rest).
%! grapher:handle(+Type,+Style,+ArrowStyle,+Master,+Dependency,-Output)
%
% For a given graph style, create a meta reprensentation of a dependency
grapher:handle(depend,_Style,_Arrow,Mastercontext://Master,package_dependency(_,_,_Type,Cat,Name,_Comp,_Ver,_,_),arrow(Mastercontext://Master,[Choicecontext://Choice])) :-
query:search([name(equal(Name)),category(equal(Cat))],Choicecontext://Choice),
!, true.
grapher:handle(depend,_Style,_Arrow,_Master,use_conditional_group(_,_Type,_Use,_Deps),[]) :- !.
grapher:handle(depend,_Style,_Arrow,_Master,any_of_group(_),[]) :- !.
grapher:handle(depend,_Style,_Arrow,_Master,all_of_group(_),[]) :- !.
grapher:handle(depend,_Style,_Arrow,_Master,exactly_one_of_group(_),[]) :- !.
grapher:handle(depend,_Style,_Arrow,_Master,at_most_one_of_group(_),[]) :- !.
grapher:handle(depend,_Style,_Arrow,_Master,_,[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,Mastercontext://Master,package_dependency(_,_,_Type,Cat,Name,_Comp,_Ver,_,_),arrow(Mastercontext://Master,[Choicecontext://Choice])) :-
query:search([name(equal(Name)),category(equal(Cat))],Choicecontext://Choice),
!, true.
grapher:handle(rdepend,_Style,_Arrow,_Master,use_conditional_group(_,_Type,_Use,_Deps),[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,_Master,any_of_group(_),[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,_Master,all_of_group(_),[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,_Master,exactly_one_of_group(_),[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,_Master,at_most_one_of_group(_),[]) :- !.
grapher:handle(rdepend,_Style,_Arrow,_Master,_,[]) :- !.
grapher:handle(detail,Style,Arrow,Master,package_dependency(_,Type,_,Cat,Name,Comp,Ver,_,_),arrow(D,Choices)) :-
!,
gensym(pack,P),
write('subgraph '),write(P),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\" WIDTH=\"220\"><TR><TD ROWSPAN=\"6\" CELLPADDING=\"30\">pack_dep</TD></TR><TR><TD WIDTH=\"110\">'),
write(Type),write('</TD></TR><TR><TD>'),write(Cat),write('</TD></TR><TR><TD>'),write(Name),write('</TD></TR><TR><TD>'),
write(Comp),write('</TD></TR><TR><TD>'),write(Ver),write('</TD></TR></TABLE>>, shape=none, color=blue];'),nl,
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl,
findall(R,query:search([name(equal(Name)),category(equal(Cat))],R),Choices),
!, true.
grapher:handle(detail,Style,Arrow,Master,use_conditional_group(Type,Use,_,Deps),Choices) :-
!,
gensym(cond,C),
write('subgraph '),write(C),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR><TD ROWSPAN=\"3\" CELLPADDING=\"10\">use_conditional</TD></TR><TR><TD>'),write(Type),write('</TD></TR><TR><TD>'),write(Use),
write('</TD></TR></TABLE>>, shape=none, color=red];'),nl,
findall(Ch,(member(Dep,Deps),grapher:handle(detail,dashed,vee,D,Dep,Ch)),Choices),
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl.
grapher:handle(detail,Style,Arrow,Master,any_of_group(Deps),Choices) :-
!,
gensym(any,A),
write('subgraph '),write(A),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR><TD CELLPADDING=\"10\">any_of_group</TD></TR></TABLE>>, shape=none, color=red];'),% nl, % nl
findall(Ch,(member(Dep,Deps),grapher:handle(detail,dotted,oinv,D,Dep,Ch)),Choices),
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl.
grapher:handle(detail,Style,Arrow,Master,all_of_group(Deps),Choices) :-
!,
gensym(all,A),
write('subgraph '),write(A),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR><TD CELLPADDING=\"10\">all_of_group</TD></TR></TABLE>>, shape=none, color=red];'),% nl, % nl
findall(Ch,(member(Dep,Deps),grapher:handle(detail,solid,inv,D,Dep,Ch)),Choices),
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl.
grapher:handle(detail,Style,Arrow,Master,exactly_one_of_group(Deps),Choices) :-
!,
gensym(exactlyone,A),
write('subgraph '),write(A),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR><TD CELLPADDING=\"10\">exactly_one_of_group</TD></TR></TABLE>>, shape=none, color=red];'), % nl, % nl,
findall(Ch,(member(Dep,Deps),grapher:handle(detail,dotted,tee,D,Dep,Ch)),Choices),
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl.
grapher:handle(detail,Style,Arrow,Master,at_most_one_of_group(Deps),Choices) :-
!,
gensym(atmostone,A),
write('subgraph '),write(A),write(' {'),nl,
gensym(dependency,D),
write(D),write(' [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"><TR><TD CELLPADDING=\"10\">at_most_one_of_group</TD></TR></TABLE>>, shape=none, color=red];'), %nl, % nl,
findall(Ch,(member(Dep,Deps),grapher:handle(detail,dotted,onormal,D,Dep,Ch)),Choices),
write('}'),nl,
write(Master),write(':e -> '),write(D),write(':w [weight=20,style="'),write(Style),write('",arrowhead="'),write(Arrow),write('"];'),nl.
grapher:handle(detail,_Style,_Arrow,Master,S,[]) :-
!,
writeln('# *** BEGIN UNKNOWN DEPENDENCY TYPE (TODO) ***'),
write('# '),write(Master), write(' -> '), write(S),nl,
writeln('# *** END UNKNOWN DEPENDENCY TYPE (TODO) ***'),
nl.
%! grapher:test(+Repository)
%
% For a given 'eapi' repository, create Graphviz dot files in the graph
% directory for all ebuilds in the repository. Triggers a script to convert
% the dot files into interactive scalable vector graphics (SVG).
%
% When an SVG graph is opened in a modern browser, it will show for a given
% ebuild what dependencies that ebuild has. The SVG diagram will have links
% to ebuilds that can satisfy the dependency. Each ebuild linked to can be
% opened by clicking on it in the diagram, enabling manual dependency graph
% traversal to debug issues with ebuild dependencies.
grapher:prepare_directory(D,Repository) :-
config:hostname(H),
config:graph_directory(H,D),
system:exists_directory(D),!,
message:scroll_notice(['Directory already exists! Updating...']),
pkg:create_repository_dirs(Repository,D).
grapher:prepare_directory(D,Repository) :-
config:hostname(H),
config:graph_directory(H,D),
not(system:exists_directory(D)),!,
pkg:make_repository_dirs(Repository,D).
grapher:write_dot_files(D,Repository://Id) :-
with_mutex(mutex,message:scroll_notice(['Graphing - Ebuild: ',Id])),
atomic_list_concat([D,'/',Id,'.dot'],Fdetail),
tell(Fdetail),
grapher:graph(detail,Repository://Id),
told.
% atomic_list_concat([D,'/',Id,'-depend.dot'],Fdepend),
% tell(Fdepend),
% grapher:graph(depend,Repository://Id),
% told,
% atomic_list_concat([D,'/',Id,'-rdepend.dot'],Frdepend),
% tell(Frdepend),
% grapher:graph(rdepend,Repository://Id),
% told.
%! grapher:test(Repository)
%
% Create or update a graph for a given repository
grapher:test(Repository) :-
config:graph_modified_only(true),!,
config:number_of_cpus(Cpus),
grapher:prepare_directory(D,Repository),
message:title(['Graphing (',Cpus,' threads) - Changed ebuilds only']),
flush_output,
findall((grapher:write_dot_files(D,Repository://E)),
(Repository:entry(E,Time),
Repository:get_ebuild_file(E,Ebuild),
system:exists_file(Ebuild),
system:time_file(Ebuild,Modified),
Modified > Time),
Calls),
concurrent(Cpus,Calls,[]),
length(Calls,L),
message:title_reset,
message:scroll_notice(['Graphed ',L,' ebuilds (',Cpus,' threads). Now running Graphviz dot.']),
script:exec(graph,['dot',D]),
message:clean,
message:scroll_notice(['Graphed ',L,' ebuilds (',Cpus,' threads). Done running Graphviz dot.']).
grapher:test(Repository) :-
not(config:graph_modified_only(true)),!,
config:number_of_cpus(Cpus),
grapher:prepare_directory(D,Repository),
message:title(['Graphing (',Cpus,' threads) - All ebuilds']),
flush_output,
findall((grapher:write_dot_files(D,Repository://E)),
Repository:entry(E),
Calls),
concurrent(Cpus,Calls,[]),
length(Calls,L),
message:title_reset,
message:scroll_notice(['Graphed ',L,' ebuilds (',Cpus,' threads). Now running Graphviz dot.']),
script:exec(graph,['dot',D]),
message:clean,
message:scroll_notice(['Graphed ',L,' ebuilds (',Cpus,' threads). Done running Graphviz dot.']).