litmus-cli 0.3.0__tar.gz
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.
- litmus_cli-0.3.0/.gitignore +23 -0
- litmus_cli-0.3.0/LICENSE +21 -0
- litmus_cli-0.3.0/PKG-INFO +108 -0
- litmus_cli-0.3.0/README.md +76 -0
- litmus_cli-0.3.0/SECURITY.md +28 -0
- litmus_cli-0.3.0/pyproject.toml +86 -0
- litmus_cli-0.3.0/src/litmus/__init__.py +27 -0
- litmus_cli-0.3.0/src/litmus/artifact.py +179 -0
- litmus_cli-0.3.0/src/litmus/cli.py +342 -0
- litmus_cli-0.3.0/src/litmus/detectors/__init__.py +3 -0
- litmus_cli-0.3.0/src/litmus/detectors/base.py +56 -0
- litmus_cli-0.3.0/src/litmus/exif.py +158 -0
- litmus_cli-0.3.0/src/litmus/inspectors/__init__.py +32 -0
- litmus_cli-0.3.0/src/litmus/inspectors/base.py +36 -0
- litmus_cli-0.3.0/src/litmus/inspectors/c2pa.py +159 -0
- litmus_cli-0.3.0/src/litmus/inspectors/file_metadata.py +210 -0
- litmus_cli-0.3.0/src/litmus/inspectors/office_metadata.py +54 -0
- litmus_cli-0.3.0/src/litmus/inspectors/text_markers.py +75 -0
- litmus_cli-0.3.0/src/litmus/inspectors/unicode_scan.py +308 -0
- litmus_cli-0.3.0/src/litmus/io.py +54 -0
- litmus_cli-0.3.0/src/litmus/model.py +173 -0
- litmus_cli-0.3.0/src/litmus/office.py +198 -0
- litmus_cli-0.3.0/src/litmus/pipeline.py +464 -0
- litmus_cli-0.3.0/src/litmus/providers/__init__.py +17 -0
- litmus_cli-0.3.0/src/litmus/providers/registry.py +239 -0
- litmus_cli-0.3.0/src/litmus/reporting.py +150 -0
- litmus_cli-0.3.0/src/litmus/server.py +176 -0
- litmus_cli-0.3.0/src/litmus/transform/__init__.py +12 -0
- litmus_cli-0.3.0/src/litmus/transform/base.py +30 -0
- litmus_cli-0.3.0/src/litmus/transform/code_structural.py +119 -0
- litmus_cli-0.3.0/src/litmus/transform/file_strip.py +148 -0
- litmus_cli-0.3.0/src/litmus/transform/text_normalize.py +257 -0
- litmus_cli-0.3.0/src/litmus/validate/__init__.py +3 -0
- litmus_cli-0.3.0/src/litmus/validate/binary.py +75 -0
- litmus_cli-0.3.0/src/litmus/validate/code.py +165 -0
- litmus_cli-0.3.0/src/litmus/validate/office.py +73 -0
- litmus_cli-0.3.0/src/litmus/validate/structural.py +136 -0
- litmus_cli-0.3.0/src/litmus/validate/text.py +158 -0
- litmus_cli-0.3.0/src/litmus/validate/treesitter_code.py +129 -0
- litmus_cli-0.3.0/src/litmus/web_ui.py +728 -0
- litmus_cli-0.3.0/tests/conftest.py +157 -0
- litmus_cli-0.3.0/tests/test_cli.py +158 -0
- litmus_cli-0.3.0/tests/test_code_treesitter.py +87 -0
- litmus_cli-0.3.0/tests/test_exif_location.py +58 -0
- litmus_cli-0.3.0/tests/test_file_inspectors.py +71 -0
- litmus_cli-0.3.0/tests/test_office_metadata.py +118 -0
- litmus_cli-0.3.0/tests/test_registry_and_report.py +93 -0
- litmus_cli-0.3.0/tests/test_removal.py +163 -0
- litmus_cli-0.3.0/tests/test_server.py +130 -0
- litmus_cli-0.3.0/tests/test_structural_imports.py +206 -0
- litmus_cli-0.3.0/tests/test_transform_and_validate.py +162 -0
- litmus_cli-0.3.0/tests/test_unicode_inspector.py +73 -0
- litmus_cli-0.3.0/tests/test_visible_content_invariant.py +73 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
build/
|
|
6
|
+
dist/
|
|
7
|
+
|
|
8
|
+
# Virtual environments
|
|
9
|
+
.venv/
|
|
10
|
+
venv/
|
|
11
|
+
|
|
12
|
+
# Tooling caches
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.pytest_cache/
|
|
15
|
+
.ruff_cache/
|
|
16
|
+
|
|
17
|
+
# OS / editor
|
|
18
|
+
.DS_Store
|
|
19
|
+
.idea/
|
|
20
|
+
.vscode/
|
|
21
|
+
|
|
22
|
+
# Artifacts a run may leave behind
|
|
23
|
+
*.bak
|
litmus_cli-0.3.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Atanu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: litmus-cli
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: AI provenance inspector and quality-preserving transformation system
|
|
5
|
+
Project-URL: Homepage, https://github.com/LastDev-coder/litmus
|
|
6
|
+
Project-URL: Repository, https://github.com/LastDev-coder/litmus
|
|
7
|
+
Project-URL: Issues, https://github.com/LastDev-coder/litmus/issues
|
|
8
|
+
Author: Atanu
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-detection,c2pa,provenance,unicode,watermark
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
16
|
+
Classifier: Topic :: Text Processing
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Requires-Dist: pydantic>=2.7
|
|
19
|
+
Requires-Dist: typer>=0.12
|
|
20
|
+
Provides-Extra: c2pa
|
|
21
|
+
Requires-Dist: c2pa-python>=0.5; extra == 'c2pa'
|
|
22
|
+
Provides-Extra: code
|
|
23
|
+
Requires-Dist: tree-sitter-language-pack>=0.9; extra == 'code'
|
|
24
|
+
Requires-Dist: tree-sitter>=0.23; extra == 'code'
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
29
|
+
Requires-Dist: tree-sitter-language-pack>=0.9; extra == 'dev'
|
|
30
|
+
Requires-Dist: tree-sitter>=0.23; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# litmus
|
|
34
|
+
|
|
35
|
+
**See what's hiding in your files — and remove it, safely.**
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
litmus is a private, fully local tool that reveals invisible characters, hidden
|
|
40
|
+
file information and content credentials in your documents, code and images —
|
|
41
|
+
then strips them out without changing anything you can see. Every clean-up is
|
|
42
|
+
checked before it is saved: if a change cannot be confirmed safe, litmus keeps
|
|
43
|
+
your original untouched.
|
|
44
|
+
|
|
45
|
+
- **Check** — see what's hidden: invisible characters, embedded file info, content credentials
|
|
46
|
+
- **Clean** — remove it, while what you see stays exactly the same
|
|
47
|
+
- **Trust** — nothing is saved unless the result is verified safe
|
|
48
|
+
- **Private** — 100% offline; your files never leave your machine
|
|
49
|
+
|
|
50
|
+
Works with text, source code, PNG, JPEG and SVG files.
|
|
51
|
+
|
|
52
|
+
## Get started in one minute
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
git clone https://github.com/LastDev-coder/litmus
|
|
56
|
+
cd litmus
|
|
57
|
+
./start.sh # macOS / Linux
|
|
58
|
+
./start.ps1 # Windows (PowerShell)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
That's the whole setup — it prepares everything and opens the app in your
|
|
62
|
+
browser. Drop a file in, choose **Check** or **Clean**, and download the clean
|
|
63
|
+
result. Requires Python 3.11+ (or [uv](https://docs.astral.sh/uv/), which
|
|
64
|
+
fetches Python for you).
|
|
65
|
+
|
|
66
|
+
## Using the app
|
|
67
|
+
|
|
68
|
+
1. **Drop a file** onto the page.
|
|
69
|
+
2. **Check** shows what's hiding inside, explained in plain language.
|
|
70
|
+
3. **Clean** removes it and hands you the cleaned file. Your original is never
|
|
71
|
+
modified.
|
|
72
|
+
|
|
73
|
+
Pick a cleaning level to control how much is done:
|
|
74
|
+
|
|
75
|
+
| Level | What it does |
|
|
76
|
+
|---|---|
|
|
77
|
+
| Minimal | Remove invisible characters only |
|
|
78
|
+
| Standard *(default)* | Also normalize unusual spaces, line endings and letterforms |
|
|
79
|
+
| Tidy | Also neaten extra blank lines |
|
|
80
|
+
| Code-aware | Also remove unused Python imports |
|
|
81
|
+
|
|
82
|
+
## Command line
|
|
83
|
+
|
|
84
|
+
Prefer a terminal? After setup, the same features are available as commands:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
source .venv/bin/activate
|
|
88
|
+
|
|
89
|
+
litmus inspect myfile.txt # what's hidden? (read-only)
|
|
90
|
+
litmus transform myfile.txt # print the cleaned version
|
|
91
|
+
litmus transform myfile.txt --in-place --backup # save it, keep a .bak
|
|
92
|
+
litmus serve # open the web app
|
|
93
|
+
litmus --help # everything else
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
For source-code files, litmus is deliberately strict: it saves a cleaned code
|
|
97
|
+
file only when it can verify the program still behaves the same. If it can't
|
|
98
|
+
verify, it refuses and tells you why.
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
## Privacy
|
|
102
|
+
|
|
103
|
+
Everything runs on your machine. No accounts, no uploads, no telemetry, no
|
|
104
|
+
network access. The web app binds to `127.0.0.1` and analyses files in memory.
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# litmus
|
|
2
|
+
|
|
3
|
+
**See what's hiding in your files — and remove it, safely.**
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
litmus is a private, fully local tool that reveals invisible characters, hidden
|
|
8
|
+
file information and content credentials in your documents, code and images —
|
|
9
|
+
then strips them out without changing anything you can see. Every clean-up is
|
|
10
|
+
checked before it is saved: if a change cannot be confirmed safe, litmus keeps
|
|
11
|
+
your original untouched.
|
|
12
|
+
|
|
13
|
+
- **Check** — see what's hidden: invisible characters, embedded file info, content credentials
|
|
14
|
+
- **Clean** — remove it, while what you see stays exactly the same
|
|
15
|
+
- **Trust** — nothing is saved unless the result is verified safe
|
|
16
|
+
- **Private** — 100% offline; your files never leave your machine
|
|
17
|
+
|
|
18
|
+
Works with text, source code, PNG, JPEG and SVG files.
|
|
19
|
+
|
|
20
|
+
## Get started in one minute
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
git clone https://github.com/LastDev-coder/litmus
|
|
24
|
+
cd litmus
|
|
25
|
+
./start.sh # macOS / Linux
|
|
26
|
+
./start.ps1 # Windows (PowerShell)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
That's the whole setup — it prepares everything and opens the app in your
|
|
30
|
+
browser. Drop a file in, choose **Check** or **Clean**, and download the clean
|
|
31
|
+
result. Requires Python 3.11+ (or [uv](https://docs.astral.sh/uv/), which
|
|
32
|
+
fetches Python for you).
|
|
33
|
+
|
|
34
|
+
## Using the app
|
|
35
|
+
|
|
36
|
+
1. **Drop a file** onto the page.
|
|
37
|
+
2. **Check** shows what's hiding inside, explained in plain language.
|
|
38
|
+
3. **Clean** removes it and hands you the cleaned file. Your original is never
|
|
39
|
+
modified.
|
|
40
|
+
|
|
41
|
+
Pick a cleaning level to control how much is done:
|
|
42
|
+
|
|
43
|
+
| Level | What it does |
|
|
44
|
+
|---|---|
|
|
45
|
+
| Minimal | Remove invisible characters only |
|
|
46
|
+
| Standard *(default)* | Also normalize unusual spaces, line endings and letterforms |
|
|
47
|
+
| Tidy | Also neaten extra blank lines |
|
|
48
|
+
| Code-aware | Also remove unused Python imports |
|
|
49
|
+
|
|
50
|
+
## Command line
|
|
51
|
+
|
|
52
|
+
Prefer a terminal? After setup, the same features are available as commands:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
source .venv/bin/activate
|
|
56
|
+
|
|
57
|
+
litmus inspect myfile.txt # what's hidden? (read-only)
|
|
58
|
+
litmus transform myfile.txt # print the cleaned version
|
|
59
|
+
litmus transform myfile.txt --in-place --backup # save it, keep a .bak
|
|
60
|
+
litmus serve # open the web app
|
|
61
|
+
litmus --help # everything else
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For source-code files, litmus is deliberately strict: it saves a cleaned code
|
|
65
|
+
file only when it can verify the program still behaves the same. If it can't
|
|
66
|
+
verify, it refuses and tells you why.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## Privacy
|
|
70
|
+
|
|
71
|
+
Everything runs on your machine. No accounts, no uploads, no telemetry, no
|
|
72
|
+
network access. The web app binds to `127.0.0.1` and analyses files in memory.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Security
|
|
2
|
+
|
|
3
|
+
## Reporting a vulnerability
|
|
4
|
+
|
|
5
|
+
Please report suspected vulnerabilities privately via GitHub's "Report a
|
|
6
|
+
vulnerability" (Security → Advisories) rather than a public issue. We aim to
|
|
7
|
+
acknowledge within a few days.
|
|
8
|
+
|
|
9
|
+
## Security posture
|
|
10
|
+
|
|
11
|
+
This tool is designed to be safe to run on private, proprietary source code:
|
|
12
|
+
|
|
13
|
+
- **Local by default.** The base install has no network-capable dependency.
|
|
14
|
+
Nothing is uploaded, and no telemetry is collected.
|
|
15
|
+
- **No silent overwrites.** Writes are atomic, refuse to follow a symlink at
|
|
16
|
+
the destination, and never persist an unvalidated transformation. Use
|
|
17
|
+
`--backup` to keep a `.bak`.
|
|
18
|
+
- **Malformed input is data, not a crash.** Parsers degrade to an explicit
|
|
19
|
+
finding rather than raising.
|
|
20
|
+
- **Untrusted input classes.** The tool parses attacker-influenceable files
|
|
21
|
+
(images, source, text). Parsing is stdlib-based and bounded by a size cap;
|
|
22
|
+
report any input that causes a crash, hang, or write outside the target path.
|
|
23
|
+
|
|
24
|
+
## Optional dependencies and network
|
|
25
|
+
|
|
26
|
+
- The `code` extra (tree-sitter) and `c2pa` extra run locally.
|
|
27
|
+
- Any future network-dependent detector must declare `requires_network` and is
|
|
28
|
+
opt-in; it will never transmit an artifact without explicit consent.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "litmus-cli"
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "AI provenance inspector and quality-preserving transformation system"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = { text = "MIT" }
|
|
8
|
+
authors = [{ name = "Atanu" }]
|
|
9
|
+
keywords = ["provenance", "watermark", "c2pa", "unicode", "ai-detection"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"License :: OSI Approved :: MIT License",
|
|
12
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
13
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
14
|
+
"Topic :: Text Processing",
|
|
15
|
+
"Environment :: Console",
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"pydantic>=2.7",
|
|
19
|
+
"typer>=0.12",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
# Optional. Absent by default so the base install has no network-capable
|
|
24
|
+
# dependency and proprietary source stays local.
|
|
25
|
+
c2pa = ["c2pa-python>=0.5"]
|
|
26
|
+
# Multi-language structural code validation (JS/TS/Java/Kotlin/Swift). Python
|
|
27
|
+
# is validated with the stdlib and needs nothing here.
|
|
28
|
+
code = ["tree-sitter>=0.23", "tree-sitter-language-pack>=0.9"]
|
|
29
|
+
dev = [
|
|
30
|
+
"pytest>=8.0",
|
|
31
|
+
"mypy>=1.10",
|
|
32
|
+
"ruff>=0.5",
|
|
33
|
+
"tree-sitter>=0.23",
|
|
34
|
+
"tree-sitter-language-pack>=0.9",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/LastDev-coder/litmus"
|
|
39
|
+
Repository = "https://github.com/LastDev-coder/litmus"
|
|
40
|
+
Issues = "https://github.com/LastDev-coder/litmus/issues"
|
|
41
|
+
|
|
42
|
+
[project.scripts]
|
|
43
|
+
litmus = "litmus.cli:main"
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["hatchling"]
|
|
47
|
+
build-backend = "hatchling.build"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src/litmus"]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.sdist]
|
|
53
|
+
# Explicit allowlist: local working files (CLAUDE.md, docs/, CHANGELOG.md, ...)
|
|
54
|
+
# exist in this tree but are not part of the published project. Hatchling does
|
|
55
|
+
# not honor .git/info/exclude, so only these paths ship in the sdist.
|
|
56
|
+
only-include = ["src/litmus", "tests", "README.md", "LICENSE", "SECURITY.md"]
|
|
57
|
+
|
|
58
|
+
[tool.pytest.ini_options]
|
|
59
|
+
testpaths = ["tests"]
|
|
60
|
+
pythonpath = ["src"]
|
|
61
|
+
|
|
62
|
+
[tool.mypy]
|
|
63
|
+
python_version = "3.11"
|
|
64
|
+
strict = true
|
|
65
|
+
mypy_path = "src"
|
|
66
|
+
packages = ["litmus"]
|
|
67
|
+
|
|
68
|
+
[[tool.mypy.overrides]]
|
|
69
|
+
# Optional dependencies with no type stubs. Listed here so the type check is
|
|
70
|
+
# clean whether or not the extra happens to be installed.
|
|
71
|
+
module = ["tree_sitter_language_pack.*", "tree_sitter.*", "c2pa.*"]
|
|
72
|
+
ignore_missing_imports = true
|
|
73
|
+
|
|
74
|
+
[tool.ruff]
|
|
75
|
+
line-length = 100
|
|
76
|
+
src = ["src", "tests"]
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint]
|
|
79
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
80
|
+
|
|
81
|
+
[tool.ruff.lint.per-file-ignores]
|
|
82
|
+
# typer's declarative API requires calls in argument defaults.
|
|
83
|
+
"src/litmus/cli.py" = ["B008"]
|
|
84
|
+
# This module embeds the web UI's HTML/CSS/JS template as a string literal;
|
|
85
|
+
# wrapping those lines to 100 columns would harm the markup's readability.
|
|
86
|
+
"src/litmus/web_ui.py" = ["E501"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""AI provenance inspector and quality-preserving transformation system.
|
|
2
|
+
|
|
3
|
+
Inspects files for locally detectable provenance signals (hidden characters,
|
|
4
|
+
embedded metadata, content credentials) and strips them behind proofs that the
|
|
5
|
+
visible content is unchanged. It never claims to detect statistical text
|
|
6
|
+
watermarks; no public detector exists for them.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from .artifact import Artifact, load_bytes, load_path
|
|
12
|
+
from .model import TOOL_VERSION, Report
|
|
13
|
+
from .pipeline import TransformOptions, analyze, inspect, transform
|
|
14
|
+
|
|
15
|
+
__version__ = TOOL_VERSION
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"Artifact",
|
|
19
|
+
"Report",
|
|
20
|
+
"TransformOptions",
|
|
21
|
+
"__version__",
|
|
22
|
+
"analyze",
|
|
23
|
+
"inspect",
|
|
24
|
+
"load_bytes",
|
|
25
|
+
"load_path",
|
|
26
|
+
"transform",
|
|
27
|
+
]
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Artifact loading and classification.
|
|
2
|
+
|
|
3
|
+
Loading is deliberately conservative: files are read as bytes, decoding is
|
|
4
|
+
attempted rather than assumed, and a failure to decode downgrades the artifact
|
|
5
|
+
to ``BINARY`` instead of raising or silently mangling content.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import codecs
|
|
11
|
+
import hashlib
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from .model import ArtifactKind, ArtifactRef
|
|
16
|
+
|
|
17
|
+
MAX_DEFAULT_BYTES = 32 * 1024 * 1024
|
|
18
|
+
|
|
19
|
+
# Extension -> language name. Only languages the project targets are named;
|
|
20
|
+
# anything else stays ``None`` and is treated as generic text.
|
|
21
|
+
_LANGUAGE_BY_SUFFIX: dict[str, str] = {
|
|
22
|
+
".kt": "kotlin",
|
|
23
|
+
".kts": "kotlin",
|
|
24
|
+
".java": "java",
|
|
25
|
+
".swift": "swift",
|
|
26
|
+
".ts": "typescript",
|
|
27
|
+
".tsx": "typescript",
|
|
28
|
+
".js": "javascript",
|
|
29
|
+
".jsx": "javascript",
|
|
30
|
+
".mjs": "javascript",
|
|
31
|
+
".cjs": "javascript",
|
|
32
|
+
".py": "python",
|
|
33
|
+
".pyi": "python",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_MEDIA_TYPE_BY_SUFFIX: dict[str, str] = {
|
|
37
|
+
".png": "image/png",
|
|
38
|
+
".jpg": "image/jpeg",
|
|
39
|
+
".jpeg": "image/jpeg",
|
|
40
|
+
".svg": "image/svg+xml",
|
|
41
|
+
".webp": "image/webp",
|
|
42
|
+
".gif": "image/gif",
|
|
43
|
+
".pdf": "application/pdf",
|
|
44
|
+
".md": "text/markdown",
|
|
45
|
+
".txt": "text/plain",
|
|
46
|
+
".json": "application/json",
|
|
47
|
+
".html": "text/html",
|
|
48
|
+
".xml": "text/xml",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
# Directories that are never worth walking.
|
|
52
|
+
SKIP_DIRS = frozenset(
|
|
53
|
+
{
|
|
54
|
+
".git",
|
|
55
|
+
".hg",
|
|
56
|
+
".svn",
|
|
57
|
+
"node_modules",
|
|
58
|
+
".gradle",
|
|
59
|
+
"build",
|
|
60
|
+
"out",
|
|
61
|
+
"dist",
|
|
62
|
+
".venv",
|
|
63
|
+
"venv",
|
|
64
|
+
"__pycache__",
|
|
65
|
+
".mypy_cache",
|
|
66
|
+
".pytest_cache",
|
|
67
|
+
".idea",
|
|
68
|
+
"DerivedData",
|
|
69
|
+
".build",
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class ArtifactError(Exception):
|
|
75
|
+
"""Raised when an artifact cannot be loaded at all."""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True)
|
|
79
|
+
class Artifact:
|
|
80
|
+
"""An artifact plus its decoded text, when decoding succeeded."""
|
|
81
|
+
|
|
82
|
+
ref: ArtifactRef
|
|
83
|
+
data: bytes
|
|
84
|
+
text: str | None
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def is_textual(self) -> bool:
|
|
88
|
+
return self.text is not None
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def sha256_hex(data: bytes) -> str:
|
|
92
|
+
return hashlib.sha256(data).hexdigest()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _decode(data: bytes) -> tuple[str | None, str | None]:
|
|
96
|
+
"""Return (text, encoding). UTF-8 only; anything else is treated as binary.
|
|
97
|
+
|
|
98
|
+
Guessing legacy encodings would make offsets and normalization unreliable,
|
|
99
|
+
which is worse than declining to decode.
|
|
100
|
+
|
|
101
|
+
A byte order mark is detected explicitly and **kept** in the decoded text as
|
|
102
|
+
U+FEFF. Decoding with ``utf-8-sig`` would silently swallow it, which both
|
|
103
|
+
hides a real finding and shifts every character offset by one relative to
|
|
104
|
+
the byte stream.
|
|
105
|
+
"""
|
|
106
|
+
if b"\x00" in data[:8192]:
|
|
107
|
+
return None, None
|
|
108
|
+
try:
|
|
109
|
+
text = data.decode("utf-8")
|
|
110
|
+
except UnicodeDecodeError:
|
|
111
|
+
return None, None
|
|
112
|
+
return text, "utf-8-sig" if data.startswith(codecs.BOM_UTF8) else "utf-8"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def classify(path: Path | None, data: bytes, text: str | None) -> tuple[ArtifactKind, str | None]:
|
|
116
|
+
language = _LANGUAGE_BY_SUFFIX.get(path.suffix.lower()) if path else None
|
|
117
|
+
if text is None:
|
|
118
|
+
return ArtifactKind.BINARY, None
|
|
119
|
+
if language is not None:
|
|
120
|
+
return ArtifactKind.SOURCE_CODE, language
|
|
121
|
+
return ArtifactKind.TEXT, None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def load_bytes(data: bytes, *, path: Path | None = None) -> Artifact:
|
|
125
|
+
text, encoding = _decode(data)
|
|
126
|
+
kind, language = classify(path, data, text)
|
|
127
|
+
media_type = _MEDIA_TYPE_BY_SUFFIX.get(path.suffix.lower()) if path else None
|
|
128
|
+
ref = ArtifactRef(
|
|
129
|
+
path=str(path) if path else None,
|
|
130
|
+
kind=kind,
|
|
131
|
+
language=language,
|
|
132
|
+
media_type=media_type,
|
|
133
|
+
sha256=sha256_hex(data),
|
|
134
|
+
size_bytes=len(data),
|
|
135
|
+
encoding=encoding,
|
|
136
|
+
decoded=text is not None,
|
|
137
|
+
)
|
|
138
|
+
return Artifact(ref=ref, data=data, text=text)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def load_path(path: Path, *, max_bytes: int = MAX_DEFAULT_BYTES) -> Artifact:
|
|
142
|
+
try:
|
|
143
|
+
size = path.stat().st_size
|
|
144
|
+
except OSError as exc:
|
|
145
|
+
raise ArtifactError(f"cannot stat {path}: {exc}") from exc
|
|
146
|
+
if size > max_bytes:
|
|
147
|
+
raise ArtifactError(f"{path} is {size} bytes, above the {max_bytes} byte limit")
|
|
148
|
+
try:
|
|
149
|
+
data = path.read_bytes()
|
|
150
|
+
except OSError as exc:
|
|
151
|
+
raise ArtifactError(f"cannot read {path}: {exc}") from exc
|
|
152
|
+
return load_bytes(data, path=path)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def walk(root: Path, *, follow_symlinks: bool = False) -> list[Path]:
|
|
156
|
+
"""Collect regular files under ``root``, skipping build and VCS directories.
|
|
157
|
+
|
|
158
|
+
Symlinks are not followed by default: a tree scan should not be steerable
|
|
159
|
+
out of the tree it was pointed at.
|
|
160
|
+
"""
|
|
161
|
+
if root.is_file():
|
|
162
|
+
return [root]
|
|
163
|
+
found: list[Path] = []
|
|
164
|
+
stack = [root]
|
|
165
|
+
while stack:
|
|
166
|
+
current = stack.pop()
|
|
167
|
+
try:
|
|
168
|
+
entries = sorted(current.iterdir())
|
|
169
|
+
except OSError:
|
|
170
|
+
continue
|
|
171
|
+
for entry in entries:
|
|
172
|
+
if entry.is_symlink() and not follow_symlinks:
|
|
173
|
+
continue
|
|
174
|
+
if entry.is_dir():
|
|
175
|
+
if entry.name not in SKIP_DIRS:
|
|
176
|
+
stack.append(entry)
|
|
177
|
+
elif entry.is_file():
|
|
178
|
+
found.append(entry)
|
|
179
|
+
return sorted(found)
|