source-survey
v0.1.0
Published
Discover Python import cycles, check boundaries, and query source and Git evidence in SQLite.
Downloads
350
Readme
Source Survey
Source Survey analyzes committed Python packages on one workstation and stores source and Git evidence in SQLite. Use sourcesurvey scan for a boundary you already know, or sourcesurvey cycles to discover dependencies worth investigating. A successful analysis exits 0 even when it finds candidates.
Install
Source Survey requires Git and Python 3.11 or newer. The distribution name is source-survey, the executable is sourcesurvey, and the Python module is source_survey.
For a Python installation, run pipx install source-survey or python -m pip install source-survey in a virtual environment. Start with sourcesurvey --help.
For npm, run npx source-survey --help. The launcher uses uv to run the Python implementation bundled in the npm package. Install uv on your PATH first.
To work from a source checkout, activate a virtual environment and install the project in editable mode.
python3 -m venv .venv
. .venv/bin/activate
python -m pip install -e .
sourcesurvey --helpScan a boundary
This example checks whether pytest's marker definitions import its exception-matching module. Run it with a fresh output catalog. The source root is the directory containing the package at the pinned revision. Omitting --rev uses HEAD.
git clone https://github.com/pytest-dev/pytest.git ../pytest
sourcesurvey scan --repo ../pytest \
--rev 6665a79fb9a6553ce4e1be5574c21a9e3b769eec --source-root src \
--package _pytest --source _pytest.mark.structures --forbidden _pytest.raises \
--db analysis.sqlite3Each finding records the repository-relative path and first source line at the scanned commit. Run this query against analysis.sqlite3.
SELECT s.revision, f.path, f.line, f.statement, s.rule_ref
FROM findings AS f JOIN scans AS s USING (scan_id)
WHERE s.scan_id = 1
ORDER BY f.path, f.line, f.imported;All three commands use source-survey.sqlite3 by default. If that file is absent and the working directory contains a legacy foreman.sqlite3, the commands reuse the existing catalog. Supply --db to select a file explicitly.
The scans table also records completed scans with zero findings. Repeating a scan with the same checkout, commit, boundary, rule reference and tool versions reuses its scan ID. A new commit or rule gets a separate record.
Add Git change history
Use sourcesurvey history on an existing scan. It resolves both modules to their exact paths at the scanned commit. The database retains one measurement per distinct importing/imported module pair, however many import statements produced the finding.
sourcesurvey history --db analysis.sqlite3 --scan-id 1 --days 90 --max-files 30SELECT h.days, p.importer, p.imported, p.status, p.both_commits, p.either_commits,
CASE WHEN p.status = 'measured'
THEN round(100.0 * p.both_commits / nullif(p.either_commits, 0), 1)
END AS percent_together
FROM history_runs AS h JOIN module_coupling AS p USING (history_id)
WHERE h.scan_id = 1
ORDER BY h.days, p.importer, p.imported;
SELECT oid, subject FROM joint_commits
WHERE history_id = 1 AND importer = '_pytest.mark.structures' AND imported = '_pytest.raises'
ORDER BY timestamp DESC, oid;- The denominator counts eligible commits that edited either file. The numerator counts those that edited both. The window uses committer time and ends at the scanned commit. Root commits and merges are excluded. A commit touching more than the specified number of paths anywhere in the repository is excluded. Only modifications to existing regular-file contents count.
- The subject filter recognizes conventional style, formatting and lint prefixes. It can miss mechanical edits, so inspect commit diffs before proposing a change. There is no automatic refactoring score.
path_changedflags observed additions, deletions or type changes to either path in the window. Exact-path counts are retained for inspection and continuity is uncertain.external_targetandsame_filehave no two-file measurement.no_editshas no denominator. A scan with zero findings produces zero pairs.- Mining requires complete Git history. Supply
--repo /path/to/checkoutif the catalog came from another workstation. A completed window replays from SQLite even when the checkout is offline. The first history write extends an existing version-1 catalog transactionally and preserves earlier scans.
Discover import cycles
sourcesurvey cycles takes a package and a full Git checkout. This command reproduces the pytest experiment against its pinned revision. Set --days 365 for the longer window. Source Survey prints the five leading cycles by history and any component-splitting edges whose local uses appear confined to a type-checking block.
sourcesurvey cycles --repo ../pytest \
--rev 6665a79fb9a6553ce4e1be5574c21a9e3b769eec --source-root src --package _pytest \
--days 90 --max-files 30 --db analysis.sqlite3-- History-ranked examples of circular paths through the import graph.
SELECT rank, witness, min_joint, round(min_jaccard, 3) AS min_jaccard, status
FROM cycle_candidates WHERE run_id = 1 ORDER BY rank LIMIT 5;
-- Find small changes for review without specifying an importer or target.
SELECT e.source_path, s.line, s.statement, e.guarded_use_lines,
e.released_modules, e.both_commits, e.either_commits
FROM cycle_edges e JOIN cycle_sites s USING (run_id, importer, imported)
WHERE e.run_id = 1 AND e.released_modules > 0 AND e.guarded_uses_only = 1
ORDER BY e.released_modules DESC, e.source_path, s.line;- The graph contains explicit, non-self imports between committed modules in the selected package. Function-local imports count. Grimp excludes imports under recognized
TYPE_CHECKINGguards. Dynamic imports, implicit package initialization and paths through other packages are outside this graph. - Within each strongly connected component, every cyclic edge gets a shortest directed return path, with lexical tie-breaking. Source Survey deduplicates rotations. This covers every cyclic edge without enumerating every possible cycle. Several witnesses can describe the same underlying dependency.
- One Git history pass covers the participating paths. A witness ranks by its lowest edge-level joint edit count, then lowest edge-level fraction of edits in common, then shortest length and lexical module order. Minima can come from different edges. An absent denominator ranks as zero. Inspect the status, raw counts and commit subjects in
cycle_edgesandcycle_joint_commitsbefore interpreting the score. - Source Survey simulates removing individual edges and records how many modules leave all cycles and the largest component left within the original one. It checks only edges in a forward or reverse spanning tree of the component. Every edge capable of splitting it lies in that union.
- The guarded-use hint looks for a simple eager
fromimport whose bare-name reads all occur in a positive module-levelif TYPE_CHECKINGbody. The hint is deliberately narrow. Runtime reflection and access by downstream consumers require review. Usecycle_stepsto connect an edge to its ranked witnesses andcycle_sitesto inspect every source location.
Scan coverage
- Source Survey reads regular committed
.pyblobs under one top-level package with an__init__.py. It stages them in a private temporary directory and analyzes them without importing the target. Uncommitted edits, tests outside the package, and stubs are outside the scan. - The source and internal forbidden name each select a module and its descendants. A forbidden top-level standard-library name also works. External submodules are grouped under that top-level name.
- Grimp supplies static direct import sites, including imports under
TYPE_CHECKING. The boundary allows indirect imports through a public facade. Runtime loading and third-party dependency resolution are outside this MVP. - Missing internal scopes, incomplete module coverage, unsupported symlinks/submodules, or Git paths the host cannot represent stop publication. The optional rule reference records the policy rationale supplied by the caller.
The source checkout includes the experiment reports, trial patches, and original SQLite catalogs under pilot/. Start with pilot/pytest-cycles.md for the origin of the guarded-use check and pilot/django-cycles.md for its application to 909 modules. To run the integration suite from a source checkout, use python -m unittest discover -s tests -v in the activated virtual environment.
