-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscovery.py
More file actions
639 lines (528 loc) · 21 KB
/
discovery.py
File metadata and controls
639 lines (528 loc) · 21 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"""Symbolic mapping and context discovery utilities for METAINFORMANT.
Provides functions for discovering functions, configs, output patterns,
call graphs, and cross-module relationships across the repository.
"""
from __future__ import annotations
import ast
import re
import threading
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@dataclass
class FunctionInfo:
"""Information about a discovered function."""
name: str
signature: str
file_path: Path
line_number: int
docstring: str | None = None
decorators: list[str] = field(default_factory=list)
parameters: list[str] = field(default_factory=list)
return_type: str | None = None
@dataclass
class ConfigInfo:
"""Information about a discovered config file."""
path: Path
domain: str | None = None
format: str = "yaml" # yaml, toml, json
size: int = 0
modified_time: float = 0.0
@dataclass
class OutputPattern:
"""Information about output directory patterns."""
module: str
base_pattern: str
subdirs: list[str] = field(default_factory=list)
examples: list[str] = field(default_factory=list)
@dataclass
class SymbolUsage:
"""Information about symbol usage."""
file: Path
line: int
column: int
context: str | None = None
@dataclass
class ModuleDependency:
"""Information about module dependencies."""
module: str
imports: list[str] = field(default_factory=list)
from_imports: dict[str, list[str]] = field(default_factory=dict)
class _DiscoveryCache:
"""Thread-safe mtime-based cache for parsed AST results.
Stores parsed AST trees keyed by file path. On cache hit, compares the
file's current mtime to the cached mtime; if the file has been modified
the entry is evicted and the caller re-parses.
"""
def __init__(self) -> None:
self._lock = threading.Lock()
# path_str -> (mtime, ast.Module)
self._entries: dict[str, tuple[float, ast.Module]] = {}
def get(self, path: Path) -> ast.Module | None:
"""Return cached AST if the file has not been modified since caching."""
key = str(path)
with self._lock:
entry = self._entries.get(key)
if entry is None:
return None
cached_mtime, tree = entry
try:
current_mtime = path.stat().st_mtime
except OSError:
# File disappeared; evict.
self._entries.pop(key, None)
return None
if current_mtime > cached_mtime:
# Stale; evict and signal re-parse.
self._entries.pop(key, None)
return None
return tree
def put(self, path: Path, tree: ast.Module) -> None:
"""Store a parsed AST with the file's current mtime."""
key = str(path)
try:
mtime = path.stat().st_mtime
except OSError:
return # Cannot stat; do not cache.
with self._lock:
self._entries[key] = (mtime, tree)
def invalidate(self, path: Path) -> None:
"""Remove a single path from the cache."""
with self._lock:
self._entries.pop(str(path), None)
def clear(self) -> None:
"""Remove all entries from the cache."""
with self._lock:
self._entries.clear()
# Module-level singleton used by discover_functions / find_symbol_usage.
_discovery_cache = _DiscoveryCache()
def invalidate_discovery_cache(path: Path | str | None = None) -> None:
"""Public helper to invalidate the discovery AST cache.
Args:
path: If given, only that file is invalidated. If ``None`` the
entire cache is cleared.
"""
if path is None:
_discovery_cache.clear()
else:
_discovery_cache.invalidate(Path(path))
def _cached_parse(path: Path) -> ast.Module:
"""Parse a Python file, using the mtime-based cache when possible.
Raises:
SyntaxError: If the file cannot be parsed.
OSError: If the file cannot be read.
"""
tree = _discovery_cache.get(path)
if tree is not None:
return tree
with open(path, "rt", encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=str(path))
_discovery_cache.put(path, tree)
return tree
def _get_cache_dir(repo_root: Path) -> Path:
"""Get cache directory for discovery results."""
cache_dir = Path(repo_root) / "output" / ".discovery_cache"
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir
def _parse_function_signature(node: ast.FunctionDef) -> str:
"""Parse function signature from AST node."""
args = []
defaults_start = len(node.args.args) - len(node.args.defaults)
for i, arg in enumerate(node.args.args):
arg_str = arg.arg
if arg.annotation:
try:
arg_str += f": {ast.unparse(arg.annotation)}"
except Exception:
# Fallback for complex annotations
if isinstance(arg.annotation, ast.Name):
arg_str += f": {arg.annotation.id}"
else:
arg_str += ": ..."
# Add default value if present
if i >= defaults_start:
default_idx = i - defaults_start
try:
default_str = ast.unparse(node.args.defaults[default_idx])
arg_str += f" = {default_str}"
except Exception:
arg_str += " = ..."
args.append(arg_str)
# Handle *args and **kwargs
if node.args.vararg:
vararg_name = node.args.vararg.arg
if node.args.vararg.annotation:
try:
vararg_name += f": {ast.unparse(node.args.vararg.annotation)}"
except Exception:
pass
args.append(f"*{vararg_name}")
if node.args.kwarg:
kwarg_name = node.args.kwarg.arg
if node.args.kwarg.annotation:
try:
kwarg_name += f": {ast.unparse(node.args.kwarg.annotation)}"
except Exception:
pass
args.append(f"**{kwarg_name}")
sig = f"({', '.join(args)})"
if node.returns:
try:
sig += f" -> {ast.unparse(node.returns)}"
except Exception:
# Fallback for complex return types
if isinstance(node.returns, ast.Name):
sig += f" -> {node.returns.id}"
else:
sig += " -> ..."
return sig
def discover_functions(module_path: str | Path, pattern: str | None = None) -> list[FunctionInfo]:
"""Discover all functions in a module with their signatures.
Args:
module_path: Path to Python module file
pattern: Optional regex pattern to filter function names
Returns:
List of FunctionInfo objects for each discovered function
Raises:
FileNotFoundError: If module_path does not exist
SyntaxError: If module cannot be parsed
"""
module_path = Path(module_path)
if not module_path.exists():
raise FileNotFoundError(f"Module not found: {module_path}")
try:
tree = _cached_parse(module_path)
except SyntaxError as e:
raise SyntaxError(f"Failed to parse {module_path}: {e}") from e
functions: list[FunctionInfo] = []
pattern_re = re.compile(pattern) if pattern else None
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
if pattern_re and not pattern_re.search(node.name):
continue
# Extract decorators
decorators = []
for decorator in node.decorator_list:
try:
if isinstance(decorator, ast.Name):
decorators.append(decorator.id)
elif isinstance(decorator, ast.Attribute):
decorators.append(ast.unparse(decorator))
else:
# Try to unparse complex decorators
try:
decorators.append(ast.unparse(decorator))
except Exception:
decorators.append("<complex>")
except Exception:
decorators.append("<unknown>")
# Extract parameters
parameters = [arg.arg for arg in node.args.args]
# Extract return type
return_type = None
if node.returns:
try:
return_type = ast.unparse(node.returns)
except Exception:
# Fallback for complex return types
if isinstance(node.returns, ast.Name):
return_type = node.returns.id
else:
return_type = "..."
# Extract docstring
docstring = ast.get_docstring(node)
# Get signature
try:
signature = _parse_function_signature(node)
except Exception:
# Fallback to simple signature
signature = f"({', '.join(parameters)})"
func_info = FunctionInfo(
name=node.name,
signature=signature,
file_path=module_path,
line_number=node.lineno,
docstring=docstring,
decorators=decorators,
parameters=parameters,
return_type=return_type,
)
functions.append(func_info)
return functions
def discover_configs(repo_root: str | Path, domain: str | None = None) -> list[ConfigInfo]:
"""Discover all config files in the repository.
Args:
repo_root: Root directory of the repository
domain: Optional domain filter (e.g., 'rna', 'gwas')
Returns:
List of ConfigInfo objects for each discovered config file
"""
repo_root = Path(repo_root)
config_dir = repo_root / "config"
if not config_dir.exists():
return []
configs: list[ConfigInfo] = []
# Search in config directory
for config_file in config_dir.rglob("*"):
if not config_file.is_file():
continue
suffix = config_file.suffix.lower()
if suffix not in {".yaml", ".yml", ".toml", ".json"}:
continue
# Extract domain from path
rel_path = config_file.relative_to(config_dir)
path_parts = rel_path.parts
file_domain = path_parts[0] if len(path_parts) > 1 else None
# Filter by domain if specified
if domain and file_domain != domain:
continue
try:
stat = config_file.stat()
config_info = ConfigInfo(
path=config_file,
domain=file_domain,
format=suffix.lstrip("."),
size=stat.st_size,
modified_time=stat.st_mtime,
)
configs.append(config_info)
except OSError:
# Skip files that can't be accessed
continue
return sorted(configs, key=lambda x: x.path)
def discover_output_patterns(module_name: str) -> OutputPattern:
"""Discover output directory patterns for a module.
Args:
module_name: Name of the module (e.g., 'rna', 'gwas', 'dna')
Returns:
OutputPattern with base pattern and subdirectories
"""
# Map from .cursorrules patterns
patterns_map = {
"rna": OutputPattern(
module="rna",
base_pattern="output/amalgkit/<species>/<step>/",
subdirs=["quant/", "work/", "logs/"],
examples=["output/amalgkit/Apis_mellifera/quant/", "output/amalgkit/Apis_mellifera/work/"],
),
"gwas": OutputPattern(
module="gwas",
base_pattern="output/gwas/<analysis_type>/",
subdirs=["association/", "plots/", "qc/"],
examples=["output/gwas/association/", "output/gwas/plots/"],
),
"life_events": OutputPattern(
module="life_events",
base_pattern="output/life_events/<workflow>/",
subdirs=["embeddings/", "models/", "plots/"],
examples=["output/life_events/embeddings/", "output/life_events/models/"],
),
"multiomics": OutputPattern(
module="multiomics",
base_pattern="output/multiomics/<integration>/",
subdirs=["integrated/", "plots/"],
examples=["output/multiomics/integrated/", "output/multiomics/plots/"],
),
"singlecell": OutputPattern(
module="singlecell",
base_pattern="output/singlecell/<analysis>/",
subdirs=["preprocessing/", "clustering/"],
examples=["output/singlecell/preprocessing/", "output/singlecell/clustering/"],
),
"networks": OutputPattern(
module="networks",
base_pattern="output/networks/<network_type>/",
subdirs=["ppi/", "regulatory/", "pathways/"],
examples=["output/networks/ppi/", "output/networks/regulatory/"],
),
"information": OutputPattern(
module="information",
base_pattern="output/information/<analysis_type>/",
subdirs=["entropy/", "complexity/"],
examples=["output/information/entropy/", "output/information/complexity/"],
),
"dna": OutputPattern(
module="dna",
base_pattern="output/dna/<analysis_type>/",
subdirs=["phylogeny/", "population/", "variants/"],
examples=["output/dna/phylogeny/", "output/dna/population/"],
),
"protein": OutputPattern(
module="protein",
base_pattern="output/protein/<analysis_type>/",
subdirs=["structures/", "alignments/"],
examples=["output/protein/structures/", "output/protein/alignments/"],
),
"quality": OutputPattern(
module="quality",
base_pattern="output/quality/<dataset>/",
subdirs=["fastq/", "reports/"],
examples=["output/quality/fastq/", "output/quality/reports/"],
),
"math": OutputPattern(
module="math",
base_pattern="output/math/<type>/",
subdirs=["simulations/", "models/", "plots/"],
examples=["output/math/simulations/", "output/math/models/"],
),
"simulation": OutputPattern(
module="simulation",
base_pattern="output/simulation/<type>/",
subdirs=["sequences/", "ecosystems/"],
examples=["output/simulation/sequences/", "output/simulation/ecosystems/"],
),
}
# Return known pattern or default
if module_name.lower() in patterns_map:
return patterns_map[module_name.lower()]
# Default pattern
return OutputPattern(
module=module_name,
base_pattern=f"output/{module_name}/",
subdirs=[],
examples=[f"output/{module_name}/"],
)
def build_call_graph(entry_point: str | Path, repo_root: str | Path | None = None) -> dict[str, list[str]]:
"""Build function call graph from an entry point.
Args:
entry_point: Path to entry point module or function name
repo_root: Root directory of repository (for resolving imports)
Returns:
Dictionary mapping function names to lists of called functions
"""
# This is a simplified implementation
# Full implementation would require more sophisticated AST traversal
entry_path = Path(entry_point)
if not entry_path.exists():
return {}
try:
tree = _cached_parse(entry_path)
except (SyntaxError, OSError):
return {}
call_graph: dict[str, list[str]] = {}
class CallVisitor(ast.NodeVisitor):
current_function: str | None = None
def visit_FunctionDef(self, node: ast.FunctionDef):
self.current_function = node.name
call_graph[self.current_function] = []
self.generic_visit(node)
self.current_function = None
def visit_Call(self, node: ast.Call):
if self.current_function:
if isinstance(node.func, ast.Name):
call_graph[self.current_function].append(node.func.id)
elif isinstance(node.func, ast.Attribute):
# Handle method calls like obj.method()
call_graph[self.current_function].append(node.func.attr)
self.generic_visit(node)
visitor = CallVisitor()
visitor.visit(tree)
return call_graph
def find_symbol_usage(symbol_name: str, repo_root: str | Path) -> list[SymbolUsage]:
"""Find all usages of a symbol across the repository.
Args:
symbol_name: Name of symbol to find
repo_root: Root directory of repository
Returns:
List of SymbolUsage objects for each occurrence
"""
repo_root = Path(repo_root)
usages: list[SymbolUsage] = []
# Search Python files
for py_file in repo_root.rglob("*.py"):
if "__pycache__" in str(py_file) or ".pyc" in str(py_file):
continue
try:
# Read source lines (needed for context snippets regardless of cache).
with open(py_file, "rt", encoding="utf-8") as f:
content = f.read()
lines = content.splitlines()
try:
tree = _cached_parse(py_file)
except SyntaxError:
# Skip files with syntax errors
continue
class UsageVisitor(ast.NodeVisitor):
def visit_Name(self, node: ast.Name) -> None:
if node.id == symbol_name:
# Get context line
line = lines[node.lineno - 1] if node.lineno <= len(lines) else ""
usage = SymbolUsage(
file=py_file,
line=node.lineno,
column=node.col_offset,
context=line.strip()[:100] if line else None,
)
usages.append(usage)
self.generic_visit(node)
visitor = UsageVisitor()
visitor.visit(tree)
except (OSError, UnicodeDecodeError):
continue
return usages
def get_module_dependencies(module_path: str | Path) -> ModuleDependency:
"""Extract import dependencies from a module.
Args:
module_path: Path to Python module file
Returns:
ModuleDependency with imports and from_imports
"""
module_path = Path(module_path)
if not module_path.exists():
return ModuleDependency(module=str(module_path), imports=[], from_imports={})
try:
tree = _cached_parse(module_path)
except (SyntaxError, OSError):
return ModuleDependency(module=str(module_path), imports=[], from_imports={})
imports: list[str] = []
from_imports: dict[str, list[str]] = {}
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
imports.append(alias.name)
elif isinstance(node, ast.ImportFrom):
module = node.module or ""
names = [alias.name for alias in node.names]
if module not in from_imports:
from_imports[module] = []
from_imports[module].extend(names)
return ModuleDependency(module=str(module_path), imports=imports, from_imports=from_imports)
def discover_workflows(repo_root: str | Path | None = None) -> list[dict[str, Any]]:
"""Discover all workflow entry points and their configs.
Args:
repo_root: Root directory of repository (defaults to current directory)
Returns:
List of dictionaries with workflow information
"""
if repo_root is None:
repo_root = Path.cwd()
else:
repo_root = Path(repo_root)
workflows: list[dict[str, Any]] = []
# Known workflow patterns
workflow_files = [
("rna", "src/metainformant/rna/workflow.py", "execute_workflow", "config/amalgkit/"),
("gwas", "src/metainformant/gwas/workflow.py", "run_gwas_workflow", "config/gwas/"),
("life_events", "src/metainformant/life_events/workflow.py", "analyze_life_course", "config/"),
("simulation", "src/metainformant/simulation/workflow.py", "run_sequence_simulation_workflow", "config/"),
]
for domain, workflow_path, entry_point, config_pattern in workflow_files:
full_path = repo_root / workflow_path
if full_path.exists():
# Find configs for this workflow
config_dir = repo_root / config_pattern
configs = []
if config_dir.exists():
for config_file in config_dir.rglob("*.yaml"):
if config_file.is_file() and "template" not in config_file.name:
configs.append(str(config_file.relative_to(repo_root)))
workflows.append(
{
"domain": domain,
"workflow_file": str(workflow_path),
"entry_point": entry_point,
"configs": configs,
"exists": True,
}
)
return workflows