phrour-repograph 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- phrour_repograph/__init__.py +3 -0
- phrour_repograph/cli.py +542 -0
- phrour_repograph/graph.py +623 -0
- phrour_repograph/impact.py +206 -0
- phrour_repograph/index.py +265 -0
- phrour_repograph/indexers/__init__.py +22 -0
- phrour_repograph/indexers/docs.py +146 -0
- phrour_repograph/indexers/infra.py +1363 -0
- phrour_repograph/indexers/python_indexer.py +163 -0
- phrour_repograph/indexers/rules.py +344 -0
- phrour_repograph/mcp.py +484 -0
- phrour_repograph/model.py +147 -0
- phrour_repograph/modulemap.py +25 -0
- phrour_repograph/recipes.py +100 -0
- phrour_repograph/report.py +199 -0
- phrour_repograph/select_tests.py +74 -0
- phrour_repograph-0.1.0.dist-info/METADATA +279 -0
- phrour_repograph-0.1.0.dist-info/RECORD +22 -0
- phrour_repograph-0.1.0.dist-info/WHEEL +5 -0
- phrour_repograph-0.1.0.dist-info/entry_points.txt +2 -0
- phrour_repograph-0.1.0.dist-info/licenses/LICENSE +15 -0
- phrour_repograph-0.1.0.dist-info/top_level.txt +1 -0
phrour_repograph/cli.py
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
"""Command line interface for PhrourRepograph.
|
|
2
|
+
|
|
3
|
+
Every command that prints a result supports --json. Exit codes: 0 success, 1 a finding or a
|
|
4
|
+
failure the caller asked for, 2 a usage error.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from . import __version__
|
|
15
|
+
from .graph import Graph
|
|
16
|
+
from .impact import HIGHLIGHT_KINDS, analyze, changed_files_from_git, is_security_highlight
|
|
17
|
+
from .index import index_repository, index_workspace
|
|
18
|
+
from .recipes import detections, tests_for, uncovered
|
|
19
|
+
from .report import build_record, write_record
|
|
20
|
+
from .select_tests import select as select_tests
|
|
21
|
+
|
|
22
|
+
DEFAULT_DB = ".phrour-repograph/graph.db"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _emit(data, as_json: bool) -> None:
|
|
26
|
+
if as_json:
|
|
27
|
+
print(json.dumps(data, indent=2, sort_keys=True, default=str))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _node_line(node: dict) -> str:
|
|
31
|
+
where = node.get("path") or ""
|
|
32
|
+
if node.get("line"):
|
|
33
|
+
where = f"{where}:{node['line']}"
|
|
34
|
+
return f"{node['kind']:<12} {node['name']} {where}".rstrip()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _split_paths(text: str | None) -> list[str]:
|
|
38
|
+
if not text:
|
|
39
|
+
return []
|
|
40
|
+
return [item.strip() for item in text.split(",") if item.strip()]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _confidence(text: str) -> float:
|
|
44
|
+
try:
|
|
45
|
+
value = float(text)
|
|
46
|
+
except ValueError as exc:
|
|
47
|
+
raise argparse.ArgumentTypeError(f"not a number: {text}") from exc
|
|
48
|
+
if not 0.0 <= value <= 1.0:
|
|
49
|
+
raise argparse.ArgumentTypeError("confidence must be between 0 and 1")
|
|
50
|
+
return value
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
54
|
+
parser = argparse.ArgumentParser(
|
|
55
|
+
prog="phrour-repograph",
|
|
56
|
+
description="Persistent cybersecurity code and infrastructure graph.",
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument("--db", default=DEFAULT_DB, help=f"graph database path (default {DEFAULT_DB})")
|
|
59
|
+
parser.add_argument("--version", action="version", version=f"phrour-repograph {__version__}")
|
|
60
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
61
|
+
|
|
62
|
+
p_init = sub.add_parser("init", help="create the graph database")
|
|
63
|
+
p_init.add_argument("--json", action="store_true")
|
|
64
|
+
|
|
65
|
+
p_index = sub.add_parser("index", help="index a repository")
|
|
66
|
+
p_index.add_argument("path", nargs="?", default=".")
|
|
67
|
+
p_index.add_argument("--json", action="store_true")
|
|
68
|
+
p_index.add_argument("--force", action="store_true", help="reindex unchanged files")
|
|
69
|
+
p_index.add_argument("--now", help="timestamp to record, RFC 3339")
|
|
70
|
+
p_index.add_argument(
|
|
71
|
+
"--workspace",
|
|
72
|
+
action="store_true",
|
|
73
|
+
help="index every child git repository under the path as its own repo",
|
|
74
|
+
)
|
|
75
|
+
p_index.add_argument("--verbose", action="store_true", help="list per-file parse errors")
|
|
76
|
+
p_index.add_argument(
|
|
77
|
+
"--repo",
|
|
78
|
+
help="name this checkout with, instead of the directory name (keeps same-named checkouts apart)",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
p_det = sub.add_parser("detections", help="rules that detect one technique")
|
|
82
|
+
p_det.add_argument("technique", help="a MITRE id such as T1059, or a full technique key")
|
|
83
|
+
p_det.add_argument("--json", action="store_true")
|
|
84
|
+
|
|
85
|
+
p_uncovered = sub.add_parser("uncovered", help="modules that no test imports")
|
|
86
|
+
p_uncovered.add_argument("--limit", type=int, default=100, help="maximum modules to list")
|
|
87
|
+
p_uncovered.add_argument("--json", action="store_true")
|
|
88
|
+
|
|
89
|
+
p_tests_for = sub.add_parser("tests-for", help="tests that reach one node")
|
|
90
|
+
p_tests_for.add_argument("key", help="a graph key, for example file:repo:src/auth.py")
|
|
91
|
+
p_tests_for.add_argument("--depth", type=int, default=3, help="walk depth from each test")
|
|
92
|
+
p_tests_for.add_argument("--json", action="store_true")
|
|
93
|
+
|
|
94
|
+
p_stats = sub.add_parser("stats", help="show graph counts")
|
|
95
|
+
p_stats.add_argument("--json", action="store_true")
|
|
96
|
+
|
|
97
|
+
p_query = sub.add_parser("query", help="find nodes")
|
|
98
|
+
p_query.add_argument("--kind")
|
|
99
|
+
p_query.add_argument("--name")
|
|
100
|
+
p_query.add_argument("--path")
|
|
101
|
+
p_query.add_argument("--repo")
|
|
102
|
+
p_query.add_argument("--limit", type=int, default=200)
|
|
103
|
+
p_query.add_argument("--json", action="store_true")
|
|
104
|
+
|
|
105
|
+
p_search = sub.add_parser("search", help="full-text search over node name, path and attrs")
|
|
106
|
+
p_search.add_argument("text")
|
|
107
|
+
p_search.add_argument("--repo")
|
|
108
|
+
p_search.add_argument("--limit", type=int, default=50)
|
|
109
|
+
p_search.add_argument("--json", action="store_true")
|
|
110
|
+
|
|
111
|
+
p_trace = sub.add_parser("trace", help="walk out from one node")
|
|
112
|
+
p_trace.add_argument("key")
|
|
113
|
+
p_trace.add_argument("--depth", type=int, default=2)
|
|
114
|
+
p_trace.add_argument("--min-confidence", type=_confidence, default=0.0, help="skip edges below this confidence")
|
|
115
|
+
p_trace.add_argument("--json", action="store_true")
|
|
116
|
+
|
|
117
|
+
p_paths = sub.add_parser("paths", help="find paths between two nodes")
|
|
118
|
+
p_paths.add_argument("src")
|
|
119
|
+
p_paths.add_argument("dst")
|
|
120
|
+
p_paths.add_argument("--max-depth", type=int, default=4)
|
|
121
|
+
p_paths.add_argument("--limit", type=int, default=20)
|
|
122
|
+
p_paths.add_argument("--min-confidence", type=_confidence, default=0.0, help="skip edges below this confidence")
|
|
123
|
+
p_paths.add_argument("--json", action="store_true")
|
|
124
|
+
|
|
125
|
+
p_impact = sub.add_parser("impact", help="analyse a change set")
|
|
126
|
+
p_impact.add_argument("path", nargs="?", default=".")
|
|
127
|
+
source = p_impact.add_mutually_exclusive_group()
|
|
128
|
+
source.add_argument("--since", help="git ref to diff against, for example HEAD~1")
|
|
129
|
+
source.add_argument("--files", help="comma separated repo-relative paths")
|
|
130
|
+
p_impact.add_argument("--depth", type=int, default=2)
|
|
131
|
+
p_impact.add_argument("--min-confidence", type=_confidence, default=0.0, help="skip edges below this confidence")
|
|
132
|
+
p_impact.add_argument("--json", action="store_true")
|
|
133
|
+
p_impact.add_argument("--record", help="write a SEF record to this path")
|
|
134
|
+
|
|
135
|
+
p_tests = sub.add_parser("select-tests", help="pick tests that reach changed files")
|
|
136
|
+
p_tests.add_argument("path", nargs="?", default=".")
|
|
137
|
+
p_tests.add_argument("--files", help="comma separated repo-relative paths")
|
|
138
|
+
p_tests.add_argument("--changed", action="append", default=[], help="changed node key, repeatable")
|
|
139
|
+
p_tests.add_argument("--depth", type=int, default=3)
|
|
140
|
+
p_tests.add_argument("--json", action="store_true")
|
|
141
|
+
|
|
142
|
+
p_lens = sub.add_parser("lens", help="list security-relevant nodes")
|
|
143
|
+
p_lens.add_argument("--repo")
|
|
144
|
+
p_lens.add_argument("--limit", type=int, default=200)
|
|
145
|
+
p_lens.add_argument("--json", action="store_true")
|
|
146
|
+
|
|
147
|
+
p_export = sub.add_parser("export", help="export the graph as JSON")
|
|
148
|
+
p_export.add_argument("--out")
|
|
149
|
+
p_export.add_argument("--bundle", help="write a portable single-file SQLite copy here")
|
|
150
|
+
p_export.add_argument("--json", action="store_true")
|
|
151
|
+
|
|
152
|
+
p_mcp = sub.add_parser("mcp", help="serve the graph over MCP on stdio")
|
|
153
|
+
p_mcp.add_argument("--json", action="store_true")
|
|
154
|
+
return parser
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _cmd_init(args) -> int:
|
|
158
|
+
graph = Graph(args.db)
|
|
159
|
+
graph.close()
|
|
160
|
+
if args.json:
|
|
161
|
+
_emit({"db": str(Path(args.db)), "created": True}, True)
|
|
162
|
+
else:
|
|
163
|
+
print(f"created {args.db}")
|
|
164
|
+
print(f"next: phrour-repograph --db {args.db} index .")
|
|
165
|
+
return 0
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _cmd_index(args) -> int:
|
|
169
|
+
graph = Graph(args.db)
|
|
170
|
+
try:
|
|
171
|
+
if args.workspace:
|
|
172
|
+
if args.repo:
|
|
173
|
+
print("phrour-repograph: --repo names one checkout; drop --workspace to use it", file=sys.stderr)
|
|
174
|
+
return 2
|
|
175
|
+
summaries = index_workspace(graph, Path(args.path), now=args.now, force=args.force)
|
|
176
|
+
else:
|
|
177
|
+
summaries = [
|
|
178
|
+
index_repository(graph, Path(args.path), now=args.now, force=args.force, repo=args.repo).as_dict()
|
|
179
|
+
]
|
|
180
|
+
except (NotADirectoryError, OSError) as exc:
|
|
181
|
+
print(f"phrour-repograph: {exc}", file=sys.stderr)
|
|
182
|
+
print("next: phrour-repograph index <existing directory>", file=sys.stderr)
|
|
183
|
+
return 2
|
|
184
|
+
finally:
|
|
185
|
+
graph.close()
|
|
186
|
+
if args.json:
|
|
187
|
+
_emit(summaries if args.workspace else summaries[0], True)
|
|
188
|
+
return 0
|
|
189
|
+
if not summaries:
|
|
190
|
+
print(f"no git repositories found under {Path(args.path).resolve()}")
|
|
191
|
+
return 1
|
|
192
|
+
for data in summaries:
|
|
193
|
+
print(
|
|
194
|
+
f"indexed {data['indexed']} of {data['files']} files in {data['repo']} "
|
|
195
|
+
f"({data['unchanged']} unchanged, {data['nodes']} nodes, {data['edges']} edges)"
|
|
196
|
+
)
|
|
197
|
+
if data["failed"]:
|
|
198
|
+
print(
|
|
199
|
+
f"warning: {data['failed']} file(s) failed to parse; add --verbose for the list",
|
|
200
|
+
file=sys.stderr,
|
|
201
|
+
)
|
|
202
|
+
if args.verbose:
|
|
203
|
+
for message in data["errors"]:
|
|
204
|
+
print(f" parse error: {message}", file=sys.stderr)
|
|
205
|
+
print(f"next: phrour-repograph --db {args.db} stats")
|
|
206
|
+
return 0
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _cmd_detections(args) -> int:
|
|
210
|
+
graph = Graph(args.db)
|
|
211
|
+
try:
|
|
212
|
+
data = detections(graph, args.technique)
|
|
213
|
+
finally:
|
|
214
|
+
graph.close()
|
|
215
|
+
if args.json:
|
|
216
|
+
_emit(data, True)
|
|
217
|
+
else:
|
|
218
|
+
if not data["known"]:
|
|
219
|
+
print(f"unknown technique: {data['technique']}")
|
|
220
|
+
print("next: phrour-repograph query --kind technique")
|
|
221
|
+
return 1
|
|
222
|
+
print(f"{len(data['rules'])} rule(s) detect {data['technique']}")
|
|
223
|
+
for rule in data["rules"]:
|
|
224
|
+
print(f" {_node_line(rule)}")
|
|
225
|
+
return 0 if data["known"] else 1
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _cmd_uncovered(args) -> int:
|
|
229
|
+
graph = Graph(args.db)
|
|
230
|
+
try:
|
|
231
|
+
data = uncovered(graph, limit=args.limit)
|
|
232
|
+
finally:
|
|
233
|
+
graph.close()
|
|
234
|
+
if args.json:
|
|
235
|
+
_emit(data, True)
|
|
236
|
+
else:
|
|
237
|
+
print(
|
|
238
|
+
f"{data['uncovered']} of {data['modules']} modules are imported by no test "
|
|
239
|
+
f"(static lower bound)"
|
|
240
|
+
)
|
|
241
|
+
for item in data["items"]:
|
|
242
|
+
print(f" {_node_line(item)} ({item['functions']} definitions)")
|
|
243
|
+
if data["uncovered"] > data["shown"]:
|
|
244
|
+
print(f" ... {data['uncovered'] - data['shown']} more, raise --limit")
|
|
245
|
+
return 0
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _cmd_tests_for(args) -> int:
|
|
249
|
+
graph = Graph(args.db)
|
|
250
|
+
try:
|
|
251
|
+
data = tests_for(graph, args.key, max_depth=args.depth)
|
|
252
|
+
finally:
|
|
253
|
+
graph.close()
|
|
254
|
+
if args.json:
|
|
255
|
+
_emit(data, True)
|
|
256
|
+
else:
|
|
257
|
+
if not data["known"]:
|
|
258
|
+
print(f"unknown key: {data['key']}")
|
|
259
|
+
return 1
|
|
260
|
+
print(f"{len(data['tests'])} test(s) reach {data['key']}")
|
|
261
|
+
for test in data["tests"]:
|
|
262
|
+
print(f" {test['name']} {' -> '.join(test['via'])}")
|
|
263
|
+
if not data["known"]:
|
|
264
|
+
return 1
|
|
265
|
+
return 0 if data["tests"] else 1
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _cmd_stats(args) -> int:
|
|
269
|
+
graph = Graph(args.db)
|
|
270
|
+
try:
|
|
271
|
+
data = graph.stats()
|
|
272
|
+
finally:
|
|
273
|
+
graph.close()
|
|
274
|
+
if args.json:
|
|
275
|
+
_emit(data, True)
|
|
276
|
+
else:
|
|
277
|
+
print(f"files {data['files']} ontology {data['ontology']}")
|
|
278
|
+
for kind, count in data["nodes"].items():
|
|
279
|
+
print(f"node {kind:<14} {count}")
|
|
280
|
+
for kind, count in data["edges"].items():
|
|
281
|
+
print(f"edge {kind:<14} {count}")
|
|
282
|
+
print(f"next: phrour-repograph --db {args.db} query --kind function")
|
|
283
|
+
return 0
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _cmd_query(args) -> int:
|
|
287
|
+
graph = Graph(args.db)
|
|
288
|
+
try:
|
|
289
|
+
nodes = graph.query(
|
|
290
|
+
kind=args.kind,
|
|
291
|
+
name=args.name,
|
|
292
|
+
path=args.path,
|
|
293
|
+
repo=args.repo,
|
|
294
|
+
limit=args.limit,
|
|
295
|
+
)
|
|
296
|
+
finally:
|
|
297
|
+
graph.close()
|
|
298
|
+
if args.json:
|
|
299
|
+
_emit(nodes, True)
|
|
300
|
+
else:
|
|
301
|
+
for node in nodes:
|
|
302
|
+
print(_node_line(node))
|
|
303
|
+
print(f"{len(nodes)} node(s). next: phrour-repograph --db {args.db} trace <key>")
|
|
304
|
+
return 0
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _cmd_search(args) -> int:
|
|
308
|
+
graph = Graph(args.db)
|
|
309
|
+
try:
|
|
310
|
+
try:
|
|
311
|
+
nodes = graph.search_text(args.text, repo=args.repo, limit=args.limit)
|
|
312
|
+
except RuntimeError as exc:
|
|
313
|
+
print(f"phrour-repograph: {exc}", file=sys.stderr)
|
|
314
|
+
return 2
|
|
315
|
+
finally:
|
|
316
|
+
graph.close()
|
|
317
|
+
if args.json:
|
|
318
|
+
_emit(nodes, True)
|
|
319
|
+
else:
|
|
320
|
+
for node in nodes:
|
|
321
|
+
print(_node_line(node))
|
|
322
|
+
print(f"{len(nodes)} node(s), best match first.")
|
|
323
|
+
return 0 if nodes else 1
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _cmd_trace(args) -> int:
|
|
327
|
+
graph = Graph(args.db)
|
|
328
|
+
try:
|
|
329
|
+
if graph.get(args.key) is None:
|
|
330
|
+
print(f"phrour-repograph: unknown node key {args.key}", file=sys.stderr)
|
|
331
|
+
print(f"next: phrour-repograph --db {args.db} query --name {args.key}", file=sys.stderr)
|
|
332
|
+
return 2
|
|
333
|
+
reached = graph.walk(args.key, depth=args.depth, min_confidence=args.min_confidence)
|
|
334
|
+
finally:
|
|
335
|
+
graph.close()
|
|
336
|
+
if args.json:
|
|
337
|
+
_emit(reached, True)
|
|
338
|
+
else:
|
|
339
|
+
for item in reached:
|
|
340
|
+
print(f"{item['depth']} {_node_line(item['node'])} via {item['edge']['kind']}")
|
|
341
|
+
print(f"{len(reached)} node(s) within depth {args.depth}.")
|
|
342
|
+
return 0
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _cmd_paths(args) -> int:
|
|
346
|
+
graph = Graph(args.db)
|
|
347
|
+
try:
|
|
348
|
+
for key in (args.src, args.dst):
|
|
349
|
+
if graph.get(key) is None:
|
|
350
|
+
print(f"phrour-repograph: unknown node key {key}", file=sys.stderr)
|
|
351
|
+
print(f"next: phrour-repograph --db {args.db} query --name {key}", file=sys.stderr)
|
|
352
|
+
return 2
|
|
353
|
+
found = graph.paths(
|
|
354
|
+
args.src,
|
|
355
|
+
args.dst,
|
|
356
|
+
max_depth=args.max_depth,
|
|
357
|
+
limit=args.limit,
|
|
358
|
+
min_confidence=args.min_confidence,
|
|
359
|
+
)
|
|
360
|
+
finally:
|
|
361
|
+
graph.close()
|
|
362
|
+
if args.json:
|
|
363
|
+
_emit(found, True)
|
|
364
|
+
else:
|
|
365
|
+
for trail in found:
|
|
366
|
+
print(" -> ".join(trail))
|
|
367
|
+
print(f"{len(found)} path(s).")
|
|
368
|
+
return 0 if found else 1
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def _load_impact_files(args) -> list[str]:
|
|
372
|
+
if args.files:
|
|
373
|
+
return _split_paths(args.files)
|
|
374
|
+
return changed_files_from_git(Path(args.path), args.since)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def _cmd_impact(args) -> int:
|
|
378
|
+
if not args.since and not args.files:
|
|
379
|
+
print("phrour-repograph: impact needs --since REF or --files a.py,b.py", file=sys.stderr)
|
|
380
|
+
print("next: phrour-repograph impact . --since HEAD~1 --json", file=sys.stderr)
|
|
381
|
+
return 2
|
|
382
|
+
root = Path(args.path).resolve()
|
|
383
|
+
if not root.is_dir():
|
|
384
|
+
print(f"phrour-repograph: not a directory: {root}", file=sys.stderr)
|
|
385
|
+
return 2
|
|
386
|
+
try:
|
|
387
|
+
files = _load_impact_files(args)
|
|
388
|
+
except RuntimeError as exc:
|
|
389
|
+
print(f"phrour-repograph: {exc}", file=sys.stderr)
|
|
390
|
+
return 2
|
|
391
|
+
|
|
392
|
+
graph = Graph(args.db)
|
|
393
|
+
try:
|
|
394
|
+
index_repository(graph, root)
|
|
395
|
+
impact = analyze(graph, root.name, files, depth=args.depth, min_confidence=args.min_confidence)
|
|
396
|
+
record = None
|
|
397
|
+
if args.record:
|
|
398
|
+
command = f"phrour-repograph impact {root} --files {','.join(files)} --json"
|
|
399
|
+
record = build_record(
|
|
400
|
+
graph, root.name, impact, since=args.since, command=command
|
|
401
|
+
)
|
|
402
|
+
write_record(args.record, record)
|
|
403
|
+
finally:
|
|
404
|
+
graph.close()
|
|
405
|
+
|
|
406
|
+
if args.json:
|
|
407
|
+
_emit({"impact": impact.as_dict(), "record": record}, True)
|
|
408
|
+
else:
|
|
409
|
+
print(f"changed files: {len(impact.changed_files)} affected: {len(impact.affected)}")
|
|
410
|
+
for item in impact.affected:
|
|
411
|
+
flag = "SECURITY " if item.get("security") else ""
|
|
412
|
+
print(f"{item['depth']} {flag}{_node_line(item)}")
|
|
413
|
+
for test in impact.tests:
|
|
414
|
+
print(f"test {test['name']} {test.get('path')}")
|
|
415
|
+
for unknown in impact.unknowns:
|
|
416
|
+
print(f"unknown: {unknown}")
|
|
417
|
+
if args.record:
|
|
418
|
+
print(f"record: {args.record}")
|
|
419
|
+
print(f"next: phrour-repograph --db {args.db} select-tests {root} --files <changed>")
|
|
420
|
+
return 1 if impact.highlighted else 0
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def _cmd_select_tests(args) -> int:
|
|
424
|
+
root = Path(args.path).resolve()
|
|
425
|
+
graph = Graph(args.db)
|
|
426
|
+
try:
|
|
427
|
+
keys = list(args.changed)
|
|
428
|
+
for path in _split_paths(args.files):
|
|
429
|
+
keys.extend(node["key"] for node in graph.query(repo=root.name, path=path, limit=10000))
|
|
430
|
+
if not keys:
|
|
431
|
+
print(
|
|
432
|
+
"phrour-repograph: no changed keys; pass --files or --changed",
|
|
433
|
+
file=sys.stderr,
|
|
434
|
+
)
|
|
435
|
+
print(f"next: phrour-repograph --db {args.db} impact {root} --since HEAD~1", file=sys.stderr)
|
|
436
|
+
return 2
|
|
437
|
+
tests = select_tests(graph, keys, max_depth=args.depth)
|
|
438
|
+
finally:
|
|
439
|
+
graph.close()
|
|
440
|
+
if args.json:
|
|
441
|
+
_emit(tests, True)
|
|
442
|
+
else:
|
|
443
|
+
for test in tests:
|
|
444
|
+
print(f"{test['name']} {test.get('path')}")
|
|
445
|
+
print(f" via {' -> '.join(test['chain'])}")
|
|
446
|
+
print(f"{len(tests)} test(s) selected.")
|
|
447
|
+
return 0
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def _cmd_lens(args) -> int:
|
|
451
|
+
graph = Graph(args.db)
|
|
452
|
+
try:
|
|
453
|
+
nodes = []
|
|
454
|
+
for kind in sorted(HIGHLIGHT_KINDS):
|
|
455
|
+
nodes.extend(graph.query(kind=kind, repo=args.repo, limit=args.limit))
|
|
456
|
+
nodes.extend(
|
|
457
|
+
node
|
|
458
|
+
for node in graph.query(kind="function", repo=args.repo, limit=args.limit)
|
|
459
|
+
if (node.get("attrs") or {}).get("entrypoint")
|
|
460
|
+
)
|
|
461
|
+
nodes = [node for node in nodes if is_security_highlight(node)]
|
|
462
|
+
nodes.sort(key=lambda item: (item["kind"], item["name"], item["key"]))
|
|
463
|
+
nodes = nodes[: args.limit]
|
|
464
|
+
finally:
|
|
465
|
+
graph.close()
|
|
466
|
+
if args.json:
|
|
467
|
+
_emit(nodes, True)
|
|
468
|
+
else:
|
|
469
|
+
for node in nodes:
|
|
470
|
+
print(_node_line(node))
|
|
471
|
+
print(f"{len(nodes)} security node(s).")
|
|
472
|
+
return 0 if nodes else 1
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
def _cmd_export(args) -> int:
|
|
476
|
+
graph = Graph(args.db)
|
|
477
|
+
try:
|
|
478
|
+
data = graph.export()
|
|
479
|
+
bundle = None
|
|
480
|
+
if args.bundle:
|
|
481
|
+
try:
|
|
482
|
+
bundle = str(graph.vacuum_into(args.bundle))
|
|
483
|
+
except RuntimeError as exc:
|
|
484
|
+
print(f"phrour-repograph: {exc}", file=sys.stderr)
|
|
485
|
+
return 2
|
|
486
|
+
finally:
|
|
487
|
+
graph.close()
|
|
488
|
+
if bundle:
|
|
489
|
+
print(f"bundle: {bundle}")
|
|
490
|
+
if args.out:
|
|
491
|
+
target = Path(args.out)
|
|
492
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
493
|
+
target.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
494
|
+
if args.json:
|
|
495
|
+
_emit({"out": str(target), "nodes": len(data["nodes"]), "edges": len(data["edges"])}, True)
|
|
496
|
+
else:
|
|
497
|
+
print(f"wrote {len(data['nodes'])} nodes and {len(data['edges'])} edges to {target}")
|
|
498
|
+
elif args.json:
|
|
499
|
+
_emit(data, True)
|
|
500
|
+
else:
|
|
501
|
+
print(json.dumps(data, indent=2, sort_keys=True))
|
|
502
|
+
return 0
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
def _cmd_mcp(args) -> int:
|
|
506
|
+
try:
|
|
507
|
+
from .mcp import serve
|
|
508
|
+
except ImportError:
|
|
509
|
+
print("phrour-repograph: the MCP server is not installed yet", file=sys.stderr)
|
|
510
|
+
print("next: phrour-repograph stats", file=sys.stderr)
|
|
511
|
+
return 2
|
|
512
|
+
serve(db=args.db)
|
|
513
|
+
return 0
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
COMMANDS = {
|
|
517
|
+
"init": _cmd_init,
|
|
518
|
+
"index": _cmd_index,
|
|
519
|
+
"stats": _cmd_stats,
|
|
520
|
+
"query": _cmd_query,
|
|
521
|
+
"search": _cmd_search,
|
|
522
|
+
"trace": _cmd_trace,
|
|
523
|
+
"paths": _cmd_paths,
|
|
524
|
+
"impact": _cmd_impact,
|
|
525
|
+
"select-tests": _cmd_select_tests,
|
|
526
|
+
"detections": _cmd_detections,
|
|
527
|
+
"uncovered": _cmd_uncovered,
|
|
528
|
+
"tests-for": _cmd_tests_for,
|
|
529
|
+
"lens": _cmd_lens,
|
|
530
|
+
"export": _cmd_export,
|
|
531
|
+
"mcp": _cmd_mcp,
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def main(argv: list[str] | None = None) -> int:
|
|
536
|
+
parser = build_parser()
|
|
537
|
+
args = parser.parse_args(argv)
|
|
538
|
+
return COMMANDS[args.command](args)
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
if __name__ == "__main__":
|
|
542
|
+
raise SystemExit(main())
|