-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.py
executable file
·101 lines (76 loc) · 3.56 KB
/
main.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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
#
# SPDX-License-Identifier: Apache-2.0
"""Python script to query and visualize nix package dependencies"""
import argparse
import pathlib
from common.utils import (
check_positive,
exit_unless_nix_artifact,
get_py_pkg_version,
set_log_verbosity,
try_resolve_flakeref,
)
from nixgraph.graph import NixDependencies
###############################################################################
def getargs():
"""Parse command line arguments"""
desc = "Visualize nix artifact dependencies"
epil = "Example: nixgraph /path/to/derivation.drv "
parser = argparse.ArgumentParser(description=desc, epilog=epil)
helps = (
"Target nix store path (e.g. derivation file or nix output path) or flakeref"
)
parser.add_argument("NIXREF", help=helps, type=str)
parser.add_argument("--version", action="version", version=get_py_pkg_version())
helps = "Scan buildtime dependencies instead of runtime dependencies"
parser.add_argument("--buildtime", help=helps, action="store_true")
helps = "Set the graph maxdepth (default: --depth=1)"
parser.add_argument("--depth", help=helps, type=check_positive, default=1)
helps = (
"Draw inverse graph starting from node (path) names that match the "
"specified regular expression"
)
parser.add_argument("--inverse", help=helps)
helps = (
"Set the output file name, default is 'graph.png'. "
"The output filename extension determines the output format. "
"Common supported formats include: png, jpg, pdf, and dot. "
"For a full list of supported output formats, see: "
"https://graphviz.org/doc/info/output.html. In addition to graphviz "
"supported output formats, the tool supports output in csv to "
"allow post-processing the output data. Specify output file with "
".csv extension to output the query result in textual csv format."
)
parser.add_argument("--out", nargs="?", help=helps, default="graph.png")
helps = "Colorize nodes that match the specified regular expression"
parser.add_argument("--colorize", help=helps)
helps = (
"Keep drawing the dependencies until package name matches "
"the specified regular expression. This option works together with "
"--depth so that drawing stops when the first of the two "
"conditions match: when the package name matches the given regex "
"or when the specified graph depth is reached."
)
parser.add_argument("--until", help=helps)
helps = "Show nix store path in node label, together with package name"
parser.add_argument("--pathnames", help=helps, action="store_true")
helps = "Set the debug verbosity level between 0-3 (default: --verbose=1)"
parser.add_argument("--verbose", help=helps, type=int, default=1)
return parser.parse_args()
################################################################################
def main():
"""main entry point"""
args = getargs()
set_log_verbosity(args.verbose)
runtime = args.buildtime is False
target_path = try_resolve_flakeref(args.NIXREF, force_realise=runtime)
if not target_path:
target_path = pathlib.Path(args.NIXREF).resolve().as_posix()
exit_unless_nix_artifact(args.NIXREF, force_realise=runtime)
deps = NixDependencies(target_path, args.buildtime)
deps.graph(args)
if __name__ == "__main__":
main()
################################################################################