domdrift 0.1.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.
- domdrift-0.1.0/.github/workflows/ci.yml +36 -0
- domdrift-0.1.0/.github/workflows/release.yml +41 -0
- domdrift-0.1.0/.gitignore +18 -0
- domdrift-0.1.0/LICENSE +21 -0
- domdrift-0.1.0/PKG-INFO +119 -0
- domdrift-0.1.0/README.md +92 -0
- domdrift-0.1.0/pyproject.toml +69 -0
- domdrift-0.1.0/src/domdrift/__init__.py +17 -0
- domdrift-0.1.0/src/domdrift/cli.py +107 -0
- domdrift-0.1.0/src/domdrift/contract.py +359 -0
- domdrift-0.1.0/src/domdrift/differ.py +74 -0
- domdrift-0.1.0/src/domdrift/dom.py +339 -0
- domdrift-0.1.0/src/domdrift/jsonpaths.py +179 -0
- domdrift-0.1.0/src/domdrift/mcp_server.py +160 -0
- domdrift-0.1.0/src/domdrift/py.typed +0 -0
- domdrift-0.1.0/src/domdrift/redact.py +50 -0
- domdrift-0.1.0/src/domdrift/report.py +102 -0
- domdrift-0.1.0/src/domdrift/softblock.py +109 -0
- domdrift-0.1.0/src/domdrift/source.py +101 -0
- domdrift-0.1.0/tests/test_contract_and_bundle.py +184 -0
- domdrift-0.1.0/tests/test_diff_cli_mcp.py +226 -0
- domdrift-0.1.0/tests/test_dom_selectors.py +61 -0
- domdrift-0.1.0/tests/test_redact.py +67 -0
- domdrift-0.1.0/tests/test_softblock.py +70 -0
- domdrift-0.1.0/uv.lock +108 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
strategy:
|
|
15
|
+
fail-fast: false
|
|
16
|
+
matrix:
|
|
17
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
20
|
+
with:
|
|
21
|
+
persist-credentials: false
|
|
22
|
+
- uses: astral-sh/setup-uv@f94ec6bedd8674c4426838e6b50417d36b6ab231 # v5.3.1
|
|
23
|
+
with:
|
|
24
|
+
enable-cache: true
|
|
25
|
+
- name: Install Python and dependencies
|
|
26
|
+
env:
|
|
27
|
+
MATRIX_PYTHON: ${{ matrix.python-version }}
|
|
28
|
+
run: |
|
|
29
|
+
uv python install "$MATRIX_PYTHON"
|
|
30
|
+
uv sync --locked --dev
|
|
31
|
+
- name: Lint with Ruff
|
|
32
|
+
run: uv run ruff check .
|
|
33
|
+
- name: Run test suite
|
|
34
|
+
run: uv run pytest -v
|
|
35
|
+
- name: Build sdist and wheel
|
|
36
|
+
run: uv build
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
16
|
+
with:
|
|
17
|
+
persist-credentials: false
|
|
18
|
+
- uses: astral-sh/setup-uv@f94ec6bedd8674c4426838e6b50417d36b6ab231 # v5.3.1
|
|
19
|
+
- name: Build distributions
|
|
20
|
+
run: uv build
|
|
21
|
+
- uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
|
|
22
|
+
with:
|
|
23
|
+
name: dist
|
|
24
|
+
path: dist/
|
|
25
|
+
|
|
26
|
+
publish:
|
|
27
|
+
needs: build
|
|
28
|
+
if: github.event_name == 'workflow_dispatch' || vars.PYPI_OIDC_CONFIGURED == 'true'
|
|
29
|
+
runs-on: ubuntu-latest
|
|
30
|
+
environment:
|
|
31
|
+
name: pypi
|
|
32
|
+
url: https://pypi.org/p/domdrift
|
|
33
|
+
permissions:
|
|
34
|
+
id-token: write
|
|
35
|
+
contents: read
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4.1.9
|
|
38
|
+
with:
|
|
39
|
+
name: dist
|
|
40
|
+
path: dist/
|
|
41
|
+
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1
|
domdrift-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kite (@ByKite)
|
|
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.
|
domdrift-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: domdrift
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fail-closed HTML and JSON structural contract guard with soft-block detection and <2KB redacted drift snapshots for CLI and MCP agents.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ByKite/domdrift
|
|
6
|
+
Project-URL: Repository, https://github.com/ByKite/domdrift
|
|
7
|
+
Project-URL: Issues, https://github.com/ByKite/domdrift/issues
|
|
8
|
+
Author-email: Kite <192934406+ByKite@users.noreply.github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-agents,cli,css-selectors,dom-diff,json-schema,mcp,model-context-protocol,web-scraping
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
22
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
23
|
+
Classifier: Topic :: Software Development :: Testing
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# domdrift
|
|
29
|
+
|
|
30
|
+
domdrift checks saved HTML and JSON responses against a structural contract and tells a scraper when it got a challenge page instead.
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
uvx domdrift diff yesterday.html today.html
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Save a contract from a known-good response. Successful checks print nothing:
|
|
37
|
+
|
|
38
|
+
```console
|
|
39
|
+
$ printf '<main><span class="price">$12</span></main>' > good.html
|
|
40
|
+
$ uvx domdrift init good.html --select 'price=main > .price' -o contract.json
|
|
41
|
+
$ uvx domdrift check -c contract.json good.html; echo $?
|
|
42
|
+
0
|
|
43
|
+
$ printf '<main><span class="sale-price">$10</span></main>' | uvx domdrift check -c contract.json --max-bytes 512 > repair.txt
|
|
44
|
+
$ echo $?
|
|
45
|
+
2
|
|
46
|
+
$ wc -c < repair.txt
|
|
47
|
+
256
|
|
48
|
+
$ cat repair.txt
|
|
49
|
+
DRIFT_DETECTED
|
|
50
|
+
violations: rule=price selector=main > .price reason=matched 0, need >= 1
|
|
51
|
+
survivors: selector=main count=1 broken=.price
|
|
52
|
+
candidates: selector=main > .sale-price count=1
|
|
53
|
+
candidates: selector=main > span count=1
|
|
54
|
+
<main>
|
|
55
|
+
<span.sale-price>
|
|
56
|
+
$ printf '<title>Just a moment...</title>' | uvx domdrift check -c contract.json
|
|
57
|
+
SOFT_BLOCK_DETECTED
|
|
58
|
+
Cloudflare: challenge page title. Keep existing selectors.
|
|
59
|
+
$ echo $?
|
|
60
|
+
3
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
For an MCP client that launches local stdio servers:
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"mcpServers": {
|
|
68
|
+
"domdrift": { "command": "uvx", "args": ["domdrift", "mcp"] }
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`domdrift_init` takes baseline content or a public URL. Pass its contract to `domdrift_check` with the next response. `domdrift_diff` compares two content strings directly. Check and diff return `status`, `exit_code` and `report`.
|
|
74
|
+
|
|
75
|
+
## Installation
|
|
76
|
+
|
|
77
|
+
Run without installing via [`uv`](https://docs.astral.sh/uv/):
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
uvx domdrift --version
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Or install into a persistent environment:
|
|
84
|
+
|
|
85
|
+
```sh
|
|
86
|
+
uv tool install domdrift
|
|
87
|
+
# or
|
|
88
|
+
pip install domdrift
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Requires Python 3.11+ and uses only the Python standard library (`dependencies = []`).
|
|
92
|
+
|
|
93
|
+
## Contracts
|
|
94
|
+
|
|
95
|
+
`init` accepts a file path, an HTTP(S) URL or stdin. Without `--select`, it looks for landmarks, repeating HTML items and their fields, or infers JSON paths and types. Use `--auto-landmarks` to add automatic HTML rules to explicit selections.
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
uvx domdrift init catalog.json --select 'price=$.data.items[*].unit_price' -o catalog.contract
|
|
99
|
+
uvx domdrift check -c catalog.contract next.json --format json --max-bytes 1024
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
HTML rules support `min_count`, `max_count`, `min_non_empty_ratio`, `required_attr` and a Python regex `pattern` on the attribute or visible text. CSS supports groups, descendant, child and sibling combinators, attribute operators, `:not`, `:has`, `:contains`, and first/last/nth-child. The parser works on supplied markup; it does not execute JavaScript.
|
|
103
|
+
|
|
104
|
+
JSON paths start at `$` and use `.key` and `[*]`. Inferred rules record types, nullability, field presence, non-empty ratios and minimum array sizes. A check permits value changes that satisfy those rules. A snapshot diff also reports inventory additions, removals, count changes, moves between HTML containers and type changes in optional JSON fields.
|
|
105
|
+
|
|
106
|
+
## Exit codes and limits
|
|
107
|
+
|
|
108
|
+
| Code | Meaning |
|
|
109
|
+
| ---: | --- |
|
|
110
|
+
| 0 | Contract holds, or no structural change in a diff. |
|
|
111
|
+
| 1 | Invalid arguments, contract or input, or a read error. |
|
|
112
|
+
| 2 | Structural drift. Inspect the surviving prefix, candidate and skeleton. |
|
|
113
|
+
| 3 | Likely challenge, access gate, rate limit or empty hydration shell. Keep the current selectors. |
|
|
114
|
+
|
|
115
|
+
Reports default to 2048 UTF-8 bytes. `--max-bytes` accepts budgets of 256 bytes or more. JSON output stays valid when details are trimmed. Evidence uses structural labels and JSON types rather than body text or JSON scalar values. Keep contracts from trusted sources: selectors and regexes are executable checks on incoming documents.
|
|
116
|
+
|
|
117
|
+
Input reads are capped at 10 MiB. URL connections use a 15-second socket timeout and check public addresses on each redirect and connection. `--allow-private-net` permits local HTTP fixtures on the CLI. MCP URL inputs always require public HTTP(S) addresses.
|
|
118
|
+
|
|
119
|
+
From a source checkout, run `uv run domdrift --help`. Python 3.11 or newer is required; the installed package uses only the standard library.
|
domdrift-0.1.0/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# domdrift
|
|
2
|
+
|
|
3
|
+
domdrift checks saved HTML and JSON responses against a structural contract and tells a scraper when it got a challenge page instead.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
uvx domdrift diff yesterday.html today.html
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Save a contract from a known-good response. Successful checks print nothing:
|
|
10
|
+
|
|
11
|
+
```console
|
|
12
|
+
$ printf '<main><span class="price">$12</span></main>' > good.html
|
|
13
|
+
$ uvx domdrift init good.html --select 'price=main > .price' -o contract.json
|
|
14
|
+
$ uvx domdrift check -c contract.json good.html; echo $?
|
|
15
|
+
0
|
|
16
|
+
$ printf '<main><span class="sale-price">$10</span></main>' | uvx domdrift check -c contract.json --max-bytes 512 > repair.txt
|
|
17
|
+
$ echo $?
|
|
18
|
+
2
|
|
19
|
+
$ wc -c < repair.txt
|
|
20
|
+
256
|
|
21
|
+
$ cat repair.txt
|
|
22
|
+
DRIFT_DETECTED
|
|
23
|
+
violations: rule=price selector=main > .price reason=matched 0, need >= 1
|
|
24
|
+
survivors: selector=main count=1 broken=.price
|
|
25
|
+
candidates: selector=main > .sale-price count=1
|
|
26
|
+
candidates: selector=main > span count=1
|
|
27
|
+
<main>
|
|
28
|
+
<span.sale-price>
|
|
29
|
+
$ printf '<title>Just a moment...</title>' | uvx domdrift check -c contract.json
|
|
30
|
+
SOFT_BLOCK_DETECTED
|
|
31
|
+
Cloudflare: challenge page title. Keep existing selectors.
|
|
32
|
+
$ echo $?
|
|
33
|
+
3
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
For an MCP client that launches local stdio servers:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"mcpServers": {
|
|
41
|
+
"domdrift": { "command": "uvx", "args": ["domdrift", "mcp"] }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`domdrift_init` takes baseline content or a public URL. Pass its contract to `domdrift_check` with the next response. `domdrift_diff` compares two content strings directly. Check and diff return `status`, `exit_code` and `report`.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
Run without installing via [`uv`](https://docs.astral.sh/uv/):
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
uvx domdrift --version
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or install into a persistent environment:
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
uv tool install domdrift
|
|
60
|
+
# or
|
|
61
|
+
pip install domdrift
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Requires Python 3.11+ and uses only the Python standard library (`dependencies = []`).
|
|
65
|
+
|
|
66
|
+
## Contracts
|
|
67
|
+
|
|
68
|
+
`init` accepts a file path, an HTTP(S) URL or stdin. Without `--select`, it looks for landmarks, repeating HTML items and their fields, or infers JSON paths and types. Use `--auto-landmarks` to add automatic HTML rules to explicit selections.
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
uvx domdrift init catalog.json --select 'price=$.data.items[*].unit_price' -o catalog.contract
|
|
72
|
+
uvx domdrift check -c catalog.contract next.json --format json --max-bytes 1024
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
HTML rules support `min_count`, `max_count`, `min_non_empty_ratio`, `required_attr` and a Python regex `pattern` on the attribute or visible text. CSS supports groups, descendant, child and sibling combinators, attribute operators, `:not`, `:has`, `:contains`, and first/last/nth-child. The parser works on supplied markup; it does not execute JavaScript.
|
|
76
|
+
|
|
77
|
+
JSON paths start at `$` and use `.key` and `[*]`. Inferred rules record types, nullability, field presence, non-empty ratios and minimum array sizes. A check permits value changes that satisfy those rules. A snapshot diff also reports inventory additions, removals, count changes, moves between HTML containers and type changes in optional JSON fields.
|
|
78
|
+
|
|
79
|
+
## Exit codes and limits
|
|
80
|
+
|
|
81
|
+
| Code | Meaning |
|
|
82
|
+
| ---: | --- |
|
|
83
|
+
| 0 | Contract holds, or no structural change in a diff. |
|
|
84
|
+
| 1 | Invalid arguments, contract or input, or a read error. |
|
|
85
|
+
| 2 | Structural drift. Inspect the surviving prefix, candidate and skeleton. |
|
|
86
|
+
| 3 | Likely challenge, access gate, rate limit or empty hydration shell. Keep the current selectors. |
|
|
87
|
+
|
|
88
|
+
Reports default to 2048 UTF-8 bytes. `--max-bytes` accepts budgets of 256 bytes or more. JSON output stays valid when details are trimmed. Evidence uses structural labels and JSON types rather than body text or JSON scalar values. Keep contracts from trusted sources: selectors and regexes are executable checks on incoming documents.
|
|
89
|
+
|
|
90
|
+
Input reads are capped at 10 MiB. URL connections use a 15-second socket timeout and check public addresses on each redirect and connection. `--allow-private-net` permits local HTTP fixtures on the CLI. MCP URL inputs always require public HTTP(S) addresses.
|
|
91
|
+
|
|
92
|
+
From a source checkout, run `uv run domdrift --help`. Python 3.11 or newer is required; the installed package uses only the standard library.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "domdrift"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Fail-closed HTML and JSON structural contract guard with soft-block detection and <2KB redacted drift snapshots for CLI and MCP agents."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Kite", email = "192934406+ByKite@users.noreply.github.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"ai-agents",
|
|
17
|
+
"cli",
|
|
18
|
+
"css-selectors",
|
|
19
|
+
"dom-diff",
|
|
20
|
+
"json-schema",
|
|
21
|
+
"mcp",
|
|
22
|
+
"model-context-protocol",
|
|
23
|
+
"web-scraping",
|
|
24
|
+
]
|
|
25
|
+
classifiers = [
|
|
26
|
+
"Development Status :: 4 - Beta",
|
|
27
|
+
"Environment :: Console",
|
|
28
|
+
"Intended Audience :: Developers",
|
|
29
|
+
"License :: OSI Approved :: MIT License",
|
|
30
|
+
"Operating System :: OS Independent",
|
|
31
|
+
"Programming Language :: Python :: 3",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Topic :: Internet :: WWW/HTTP :: Indexing/Search",
|
|
36
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
37
|
+
"Topic :: Software Development :: Testing",
|
|
38
|
+
"Typing :: Typed",
|
|
39
|
+
]
|
|
40
|
+
dependencies = []
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = "https://github.com/ByKite/domdrift"
|
|
44
|
+
Repository = "https://github.com/ByKite/domdrift"
|
|
45
|
+
Issues = "https://github.com/ByKite/domdrift/issues"
|
|
46
|
+
|
|
47
|
+
[project.scripts]
|
|
48
|
+
domdrift = "domdrift.cli:main"
|
|
49
|
+
|
|
50
|
+
[dependency-groups]
|
|
51
|
+
dev = [
|
|
52
|
+
"pytest>=8.3.0",
|
|
53
|
+
"ruff>=0.9.0",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[tool.hatch.build.targets.wheel]
|
|
57
|
+
packages = ["src/domdrift"]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
testpaths = ["tests"]
|
|
61
|
+
addopts = "-q"
|
|
62
|
+
|
|
63
|
+
[tool.ruff]
|
|
64
|
+
line-length = 100
|
|
65
|
+
target-version = "py311"
|
|
66
|
+
|
|
67
|
+
[tool.ruff.lint]
|
|
68
|
+
select = ["E", "F", "I", "B", "UP", "SIM"]
|
|
69
|
+
ignore = ["E501"]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Public snapshot inference, checking and comparison API."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
|
|
5
|
+
from .contract import check_contract, infer_contract, infer_html_contract, infer_json_contract
|
|
6
|
+
from .differ import diff_snapshots
|
|
7
|
+
from .dom import Element, SelectorSyntaxError, parse_html, query_selector_all
|
|
8
|
+
from .redact import redact_attrs, redact_text
|
|
9
|
+
from .report import CheckResult, format_repair_bundle
|
|
10
|
+
from .softblock import SoftBlockReport, detect_soft_block
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"CheckResult", "Element", "SelectorSyntaxError", "SoftBlockReport", "__version__",
|
|
14
|
+
"check_contract", "detect_soft_block", "diff_snapshots", "format_repair_bundle",
|
|
15
|
+
"infer_contract", "infer_html_contract", "infer_json_contract", "parse_html",
|
|
16
|
+
"query_selector_all", "redact_attrs", "redact_text",
|
|
17
|
+
]
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""Unix command-line entry point."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from tempfile import NamedTemporaryFile
|
|
9
|
+
|
|
10
|
+
from . import __version__, check_contract, diff_snapshots, format_repair_bundle, infer_contract
|
|
11
|
+
from .jsonpaths import parse_json
|
|
12
|
+
from .redact import redact_text
|
|
13
|
+
from .report import validate_budget
|
|
14
|
+
from .source import load_source
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Parser(argparse.ArgumentParser):
|
|
18
|
+
def error(self, message):
|
|
19
|
+
self.exit(1, f"{self.prog}: {redact_text(message)}\n")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _parser():
|
|
23
|
+
parser = Parser(prog="domdrift", description="Check HTML and JSON snapshot contracts.")
|
|
24
|
+
parser.add_argument("--version", action="version", version=f"domdrift {__version__}")
|
|
25
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
26
|
+
init = sub.add_parser("init", help="Infer a contract from a known-good response.")
|
|
27
|
+
check = sub.add_parser("check", help="Check a response against a saved contract.")
|
|
28
|
+
diff = sub.add_parser("diff", help="Compare two snapshots without saving a contract.")
|
|
29
|
+
sub.add_parser("mcp", help="Serve MCP tools on stdin/stdout.")
|
|
30
|
+
for cmd in (init, check, diff):
|
|
31
|
+
cmd.add_argument("--allow-private-net", action="store_true",
|
|
32
|
+
help="Allow HTTP connections to non-public addresses.")
|
|
33
|
+
for cmd in (init, check):
|
|
34
|
+
cmd.add_argument("source", nargs="?", default="-", help="File, HTTP(S) URL or - for stdin.")
|
|
35
|
+
init.add_argument("-o", "--output", metavar="FILE")
|
|
36
|
+
init.add_argument("--select", action="append", default=[], metavar="NAME=EXPR")
|
|
37
|
+
init.add_argument("--auto-landmarks", action="store_true",
|
|
38
|
+
help="Include inferred HTML landmarks alongside explicit rules.")
|
|
39
|
+
init.add_argument("--format", choices=("auto", "html", "json"), default="auto")
|
|
40
|
+
check.add_argument("-c", "--contract", required=True, metavar="FILE")
|
|
41
|
+
check.add_argument("-q", "--quiet", action="store_true")
|
|
42
|
+
check.add_argument("--no-soft-block", action="store_true")
|
|
43
|
+
diff.add_argument("old_source")
|
|
44
|
+
diff.add_argument("new_source")
|
|
45
|
+
diff.add_argument("--doc-format", choices=("auto", "html", "json"), default="auto")
|
|
46
|
+
for cmd in (check, diff):
|
|
47
|
+
cmd.add_argument("--max-bytes", type=int, default=2048)
|
|
48
|
+
cmd.add_argument("--format", choices=("json", "text"), default="text")
|
|
49
|
+
return parser
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _write_contract(text, path):
|
|
53
|
+
if not path or path == "-":
|
|
54
|
+
sys.stdout.write(text)
|
|
55
|
+
return
|
|
56
|
+
with NamedTemporaryFile(dir=Path(path).parent, delete=False) as tmp:
|
|
57
|
+
try:
|
|
58
|
+
tmp.write(text.encode())
|
|
59
|
+
tmp.close()
|
|
60
|
+
os.replace(tmp.name, path)
|
|
61
|
+
finally:
|
|
62
|
+
Path(tmp.name).unlink(missing_ok=True)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def main(argv=None) -> int:
|
|
66
|
+
args = _parser().parse_args(argv)
|
|
67
|
+
try:
|
|
68
|
+
if args.command == "mcp":
|
|
69
|
+
from .mcp_server import serve
|
|
70
|
+
return serve()
|
|
71
|
+
if args.command == "init":
|
|
72
|
+
selects = {}
|
|
73
|
+
for pair in args.select:
|
|
74
|
+
name, sep, expr = pair.partition("=")
|
|
75
|
+
if not sep or not name.strip() or not expr.strip() or name in selects:
|
|
76
|
+
raise ValueError("Use --select NAME=EXPR with a unique name")
|
|
77
|
+
selects[name] = expr
|
|
78
|
+
contract = infer_contract(load_source(args.source, args.allow_private_net),
|
|
79
|
+
args.format, selects, auto_landmarks=args.auto_landmarks)
|
|
80
|
+
_write_contract(json.dumps(contract, indent=2) + "\n", args.output)
|
|
81
|
+
return 0
|
|
82
|
+
validate_budget(args.max_bytes)
|
|
83
|
+
if args.command == "check":
|
|
84
|
+
contract = parse_json(Path(args.contract).read_text(encoding="utf-8-sig"))
|
|
85
|
+
result = check_contract(contract, load_source(args.source, args.allow_private_net),
|
|
86
|
+
no_soft_block=args.no_soft_block)
|
|
87
|
+
if args.quiet or not result.exit_code:
|
|
88
|
+
return result.exit_code
|
|
89
|
+
report = format_repair_bundle(result, args.max_bytes, args.format)
|
|
90
|
+
else:
|
|
91
|
+
if args.old_source == args.new_source == "-":
|
|
92
|
+
raise ValueError("Only one snapshot may read stdin")
|
|
93
|
+
result = diff_snapshots(load_source(args.old_source, args.allow_private_net),
|
|
94
|
+
load_source(args.new_source, args.allow_private_net),
|
|
95
|
+
fmt=args.doc_format, max_bytes=args.max_bytes, output_format=args.format)
|
|
96
|
+
report = result.report
|
|
97
|
+
if report:
|
|
98
|
+
data = report.encode()
|
|
99
|
+
sys.stdout.buffer.write(data + (b"\n" if len(data) < args.max_bytes else b""))
|
|
100
|
+
return result.exit_code
|
|
101
|
+
except (ValueError, OSError, RecursionError, OverflowError) as exc:
|
|
102
|
+
print(f"domdrift: {redact_text(str(exc))}", file=sys.stderr)
|
|
103
|
+
return 1
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
sys.exit(main())
|