FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
HydroModPy/evaluation/extract_architecture.py at dev · HydroModPy/HydroModPy · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
HydroModPy
/
HydroModPy
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
11
Code
Issues
18
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
HydroModPy
/
evaluation
/
extract_architecture.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
319 lines (219 loc) · 8.42 KB
Breadcrumbs
HydroModPy
/
evaluation
/
extract_architecture.py
Copy path
File metadata and controls
319 lines (219 loc) · 8.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
__future__
import
annotations
import
argparse
import
ast
import
json
import
sys
from
pathlib
import
Path
from
typing
import
Any
REPO_ROOT
=
Path
(
__file__
).
resolve
().
parents
[
1
]
if
str
(
REPO_ROOT
)
not
in
sys
.
path
:
sys
.
path
.
insert
(
0
,
str
(
REPO_ROOT
))
from
evaluation
.
_utils
import
(
count_lines
,
iter_python_files
,
module_name_from_path
,
parse_ast
,
resolve_repository_root
,
)
try
:
import
networkx
as
nx
except
ImportError
as
exc
:
nx
=
None
NETWORKX_ERROR
=
exc
else
:
NETWORKX_ERROR
=
None
def
get_import_name
(
node
:
ast
.
AST
)
->
str
|
None
:
"""
Extract full module name from import nodes.
"""
if
isinstance
(
node
,
ast
.
Import
):
if
node
.
names
:
return
node
.
names
[
0
].
name
elif
isinstance
(
node
,
ast
.
ImportFrom
):
if
node
.
module
:
return
node
.
module
return
None
def
collect_architecture
(
root
:
Path
)
->
dict
[
str
,
Any
]:
files
=
iter_python_files
(
root
)
module_map
=
{
path
:
module_name_from_path
(
root
,
path
)
for
path
in
files
}
internal_modules
=
set
(
module_map
.
values
())
graph_data
:
dict
[
str
,
Any
]
=
{
"root"
:
str
(
root
),
"modules"
: [],
"classes"
:
0
,
"functions"
:
0
,
"module_count"
:
len
(
files
),
"dependencies"
: [],
}
graph
=
nx
.
DiGraph
()
if
nx
else
None
dependency_edges
:
set
[
tuple
[
str
,
str
]]
=
set
()
for
path
in
files
:
module
=
module_map
[
path
]
tree
=
parse_ast
(
path
)
class_count
=
0
function_count
=
0
internal_dependencies
:
set
[
str
]
=
set
()
external_dependencies
:
set
[
str
]
=
set
()
if
tree
is
not
None
:
for
node
in
ast
.
walk
(
tree
):
if
isinstance
(
node
,
ast
.
ClassDef
):
class_count
+=
1
elif
isinstance
(
node
, (
ast
.
FunctionDef
,
ast
.
AsyncFunctionDef
)):
function_count
+=
1
elif
isinstance
(
node
, (
ast
.
Import
,
ast
.
ImportFrom
)):
imported
=
get_import_name
(
node
)
if
imported
is
None
:
continue
# recherche d'un module interne
matching_internal
=
None
for
internal
in
internal_modules
:
if
imported
==
internal
or
imported
.
startswith
(
internal
+
"."
):
matching_internal
=
internal
break
if
matching_internal
:
internal_dependencies
.
add
(
matching_internal
)
dependency_edges
.
add
((
module
,
matching_internal
))
else
:
external_dependencies
.
add
(
imported
.
split
(
"."
)[
0
])
module_info
=
{
"module"
:
module
,
"path"
:
str
(
path
),
"lines"
:
count_lines
(
path
),
"classes"
:
class_count
,
"functions"
:
function_count
,
"internal_dependencies"
:
sorted
(
internal_dependencies
),
"external_dependencies"
:
sorted
(
external_dependencies
),
}
graph_data
[
"modules"
].
append
(
module_info
)
graph_data
[
"classes"
]
+=
class_count
graph_data
[
"functions"
]
+=
function_count
if
graph
is
not
None
:
graph
.
add_node
(
module
,
classes
=
class_count
,
functions
=
function_count
,
lines
=
count_lines
(
path
)
)
for
dependency
in
internal_dependencies
:
graph
.
add_edge
(
module
,
dependency
)
graph_data
[
"dependencies"
]
=
[
{
"source"
:
source
,
"target"
:
target
}
for
source
,
target
in
sorted
(
dependency_edges
)
]
return
{
"data"
:
graph_data
,
"graph"
:
graph
}
def
_module_to_package
(
module
:
str
)
->
str
:
return
module
.
rsplit
(
"."
,
1
)[
0
]
if
"."
in
module
else
module
def
_truncate_depth
(
package
:
str
,
depth
:
int
|
None
)
->
str
:
if
depth
is
None
:
return
package
truncated
=
"."
.
join
(
package
.
split
(
"."
)[:
depth
])
return
truncated
or
package
def
aggregate_graph_to_packages
(
graph
:
Any
,
depth
:
int
|
None
=
1
,
top_n
:
int
=
30
,
node_is_module
:
bool
=
True
,
)
->
Any
:
"""
Collapse a module-level dependency graph into a package-level one.
Drawing every module (hundreds to thousands of nodes for a real
repository) produces an unreadable image, so image exports go through
this aggregation by default; GraphML/JSON exports keep full module
detail since they're meant for external tools, not direct viewing.
`node_is_module` controls whether nodes are raw module names (so the
immediate containing package is derived first) or already package
names (e.g. a graph built by a caller that already aggregated by
package) — in the latter case only the depth truncation applies.
"""
if
nx
is
None
or
graph
is
None
:
return
graph
def
to_package
(
node
:
str
)
->
str
:
package
=
_module_to_package
(
node
)
if
node_is_module
else
node
return
_truncate_depth
(
package
,
depth
)
node_counts
:
dict
[
str
,
int
]
=
{}
for
node
in
graph
.
nodes
():
package
=
to_package
(
node
)
node_counts
[
package
]
=
node_counts
.
get
(
package
,
0
)
+
1
top_packages
=
set
(
sorted
(
node_counts
,
key
=
lambda
package
:
node_counts
[
package
],
reverse
=
True
)[:
top_n
]
)
package_graph
=
nx
.
DiGraph
()
package_graph
.
add_nodes_from
(
top_packages
)
for
source
,
target
in
graph
.
edges
():
source_package
=
to_package
(
source
)
target_package
=
to_package
(
target
)
if
source_package
==
target_package
:
continue
if
source_package
not
in
top_packages
or
target_package
not
in
top_packages
:
continue
if
package_graph
.
has_edge
(
source_package
,
target_package
):
package_graph
[
source_package
][
target_package
][
"weight"
]
+=
1
else
:
package_graph
.
add_edge
(
source_package
,
target_package
,
weight
=
1
)
return
package_graph
def
save_graph
(
graph
:
Any
,
output_path
:
Path
,
package_depth
:
int
|
None
=
1
,
top_n
:
int
=
30
,
node_is_module
:
bool
=
True
,
)
->
None
:
if
graph
is
None
:
return
output_path
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
if
output_path
.
suffix
.
lower
()
==
".graphml"
:
nx
.
write_graphml
(
graph
,
output_path
)
return
if
output_path
.
suffix
.
lower
()
==
".json"
:
data
=
{
"nodes"
:
list
(
graph
.
nodes
(
data
=
True
)),
"edges"
:
list
(
graph
.
edges
()),
}
output_path
.
write_text
(
json
.
dumps
(
data
,
indent
=
2
),
encoding
=
"utf-8"
)
return
try
:
import
matplotlib
.
pyplot
as
plt
except
ImportError
as
exc
:
raise
RuntimeError
(
"matplotlib is required for image graph output"
)
from
exc
graph
=
aggregate_graph_to_packages
(
graph
,
package_depth
,
top_n
,
node_is_module
)
plt
.
figure
(
figsize
=
(
16
,
10
))
positions
=
nx
.
spring_layout
(
graph
,
seed
=
42
)
nx
.
draw_networkx
(
graph
,
positions
,
node_size
=
500
,
font_size
=
7
,
arrows
=
True
)
plt
.
axis
(
"off"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_path
,
dpi
=
200
)
plt
.
close
()
def
main
()
->
None
:
parser
=
argparse
.
ArgumentParser
(
description
=
"Extract architecture metrics from a Python repository"
)
parser
.
add_argument
(
"--root"
,
type
=
Path
,
default
=
Path
.
cwd
())
parser
.
add_argument
(
"--repo"
,
type
=
str
,
default
=
None
)
parser
.
add_argument
(
"--branch"
,
type
=
str
,
default
=
None
)
parser
.
add_argument
(
"--output"
,
type
=
Path
,
default
=
None
)
parser
.
add_argument
(
"--graph-output"
,
type
=
Path
,
default
=
None
)
parser
.
add_argument
(
"--package-depth"
,
type
=
int
,
default
=
1
,
help
=
"Number of dotted segments used to group packages in image graph output (1 = top-level package)"
,
)
parser
.
add_argument
(
"--top-n-packages"
,
type
=
int
,
default
=
30
,
help
=
"Maximum number of packages shown in image graph output"
,
)
args
=
parser
.
parse_args
()
if
nx
is
None
:
raise
SystemExit
(
f"networkx is required:
{
NETWORKX_ERROR
}
"
)
with
resolve_repository_root
(
args
.
root
,
args
.
repo
,
args
.
branch
)
as
root
:
result
=
collect_architecture
(
root
)
data
=
result
[
"data"
]
graph
=
result
[
"graph"
]
output
=
json
.
dumps
(
data
,
indent
=
2
)
if
args
.
output
is
None
:
print
(
output
)
else
:
args
.
output
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
args
.
output
.
write_text
(
output
,
encoding
=
"utf-8"
)
if
args
.
graph_output
:
save_graph
(
graph
,
args
.
graph_output
,
args
.
package_depth
,
args
.
top_n_packages
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL