barcode-claim-audit 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.
- barcode_claim_audit-0.1.0/LICENSE +21 -0
- barcode_claim_audit-0.1.0/MANIFEST.in +2 -0
- barcode_claim_audit-0.1.0/PKG-INFO +92 -0
- barcode_claim_audit-0.1.0/README.md +82 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit/__init__.py +2 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit/__main__.py +38 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit/audit.py +108 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit/phrases.py +3 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit.egg-info/PKG-INFO +92 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit.egg-info/SOURCES.txt +103 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit.egg-info/dependency_links.txt +1 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit.egg-info/entry_points.txt +2 -0
- barcode_claim_audit-0.1.0/barcode_claim_audit.egg-info/top_level.txt +1 -0
- barcode_claim_audit-0.1.0/pyproject.toml +18 -0
- barcode_claim_audit-0.1.0/setup.cfg +4 -0
- barcode_claim_audit-0.1.0/tests/__init__.py +0 -0
- barcode_claim_audit-0.1.0/tests/cases.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/.env +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_cv2/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_cv2/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_dmtx/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_dmtx/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_pyzbar/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_pyzbar/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_zxing/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/alias_zxing/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bad/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bad/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/barcode_detector/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/barcode_detector/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bare-word/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bare-word/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bare_import/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bare_import/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bound/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bound/app.py +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bytes_decode/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/bytes_decode/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/camera/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/camera/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/chained/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/chained/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/cv2/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/cv2/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/decorator/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/decorator/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/dmtx/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/dmtx/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/docstring/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/docstring/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/fake_path/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/fake_path/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/fenced-code/README.md +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/fenced-code/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/generation/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/generation/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/generation-claim/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/generation-claim/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/good/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/good/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/imread/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/imread/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/inline-code/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/inline-code/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/local_decode/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/local_decode/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/local_detector/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/local_detector/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/negation/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/negation/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-barcode/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-barcode/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-claim/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-claim/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-source-negative/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/no-source-positive/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/pyzbar_attribute/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/pyzbar_attribute/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/qr_constructor/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/qr_constructor/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/self_bound/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/self_bound/app.py +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/shadow_decode/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/shadow_decode/app.py +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/shadow_detector/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/shadow_detector/app.py +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/string/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/string/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/subprocess/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/subprocess/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/testonly/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/testonly/tests/test_reader.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/unbound/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/unbound/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/user-instruction/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/user-instruction/app.py +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/with_bound/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/with_bound/app.py +3 -0
- barcode_claim_audit-0.1.0/tests/fixtures/writer/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/writer/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/zxing/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/zxing/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/fixtures/zxing_plural/README.md +1 -0
- barcode_claim_audit-0.1.0/tests/fixtures/zxing_plural/app.py +2 -0
- barcode_claim_audit-0.1.0/tests/test_audit.py +161 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 barcode-claim-audit contributors
|
|
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,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: barcode-claim-audit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Offline audit of documented code-reading capabilities
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# barcode-claim-audit 0.1.0
|
|
12
|
+
|
|
13
|
+
An offline static audit of documented code-reading claims against Python source.
|
|
14
|
+
Requires Python >=3.9, with no runtime dependencies. Source is parsed with AST
|
|
15
|
+
and never executed. Environment files are never opened.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python3 -m pip install .
|
|
21
|
+
barcode-claim-audit /path/to/project
|
|
22
|
+
python3 -m barcode_claim_audit /path/to/project --json
|
|
23
|
+
python3 -m barcode_claim_audit /path/to/project --verbose --ignore 'vendor/*'
|
|
24
|
+
python3 -m barcode_claim_audit /path/to/project --fail-on MEDIUM
|
|
25
|
+
python3 -m barcode_claim_audit --version
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
JSON stdout contains verdict, findings (rule, severity, message), evidence flags,
|
|
29
|
+
and inspected file paths. Human output shows the verdict and all findings.
|
|
30
|
+
Repeat --ignore to exclude relative paths using glob patterns; matching directories
|
|
31
|
+
are pruned. --verbose adds a file count in human output. Default threshold is HIGH;
|
|
32
|
+
MEDIUM reports remain visible but exit successfully. Exit codes: 0 for PASS,
|
|
33
|
+
2 for findings meeting the threshold, 1 for usage or filesystem errors.
|
|
34
|
+
|
|
35
|
+
## Matching and rules
|
|
36
|
+
|
|
37
|
+
Only root README.md and Markdown files with a docs path component contribute
|
|
38
|
+
claims. Fenced code is excluded; inline code counts. Matching lowercases text,
|
|
39
|
+
expands n't to not, replaces hyphens with spaces and joins whitespace within
|
|
40
|
+
blank-line-separated paragraphs. Exact templates use word boundaries, longest
|
|
41
|
+
first. Negative spans are removed before positive matching. The full template
|
|
42
|
+
sets are in barcode_claim_audit/phrases.py. Examples:
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
scans qr codes
|
|
46
|
+
supports barcode scanning
|
|
47
|
+
does not scan qr codes
|
|
48
|
+
no barcode support
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
One finding per family, at most two: documented-barcode-claim-false is HIGH
|
|
52
|
+
when a positive template has no production evidence, or MEDIUM when evidence
|
|
53
|
+
exists only in tests. documented-no-barcode-claim-false is HIGH when a negative
|
|
54
|
+
template conflicts with production evidence. Without claims the result is PASS.
|
|
55
|
+
With no source, a positive template yields HIGH and a negative one yields PASS.
|
|
56
|
+
|
|
57
|
+
Production evidence excludes tests/, test_*.py, *_test.py, fixtures/, docs/,
|
|
58
|
+
examples/, and symlinks. Test evidence is limited to tests/ and test-named files
|
|
59
|
+
outside the other excluded directories. Imports are resolved per module, including
|
|
60
|
+
aliases; local functions and classes shadow imported names.
|
|
61
|
+
|
|
62
|
+
Supported calls: pyzbar.pyzbar.decode, pylibdmtx.pylibdmtx.decode,
|
|
63
|
+
zxingcpp.read_barcode/read_barcodes, cv2.QRCodeDetector and
|
|
64
|
+
cv2.barcode_BarcodeDetector. Construction alone counts for cv2. Bound detector
|
|
65
|
+
methods detect, detectAndDecode and detectAndDecodeMulti (including self
|
|
66
|
+
attributes, with targets and chained construction) consequently qualify too.
|
|
67
|
+
|
|
68
|
+
## Limitations
|
|
69
|
+
|
|
70
|
+
Only the exact templates count. A bare QR or barcode word is insufficient.
|
|
71
|
+
Generation, camera capture, image opening, file export and setup instructions
|
|
72
|
+
about a user holding a code up to a camera are outside the claim vocabulary.
|
|
73
|
+
Comments, docstrings and strings alone are never evidence. Imports without calls,
|
|
74
|
+
image creation, camera access and unbound lookalike methods do not qualify.
|
|
75
|
+
System code reader helpers invoked through subprocess are not counted; command
|
|
76
|
+
names inside strings are never evidence. Other scan stacks, including phone-camera
|
|
77
|
+
SDKs and hardware scanner wedges that type keystrokes, are out of scope; only
|
|
78
|
+
the Python decode routes listed above are counted.
|
|
79
|
+
|
|
80
|
+
This checks syntax rather than runtime success, reachability, dependency availability,
|
|
81
|
+
image quality or actual hardware. Dynamic imports and reassignment are not modeled.
|
|
82
|
+
Local definitions conservatively shadow imports throughout a module. Syntax-invalid
|
|
83
|
+
source contributes no evidence. Filesystem errors produce a usage error.
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
python3 -m unittest discover
|
|
89
|
+
python3 -m build
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The sdist includes the test suite and fixtures. License: MIT.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# barcode-claim-audit 0.1.0
|
|
2
|
+
|
|
3
|
+
An offline static audit of documented code-reading claims against Python source.
|
|
4
|
+
Requires Python >=3.9, with no runtime dependencies. Source is parsed with AST
|
|
5
|
+
and never executed. Environment files are never opened.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python3 -m pip install .
|
|
11
|
+
barcode-claim-audit /path/to/project
|
|
12
|
+
python3 -m barcode_claim_audit /path/to/project --json
|
|
13
|
+
python3 -m barcode_claim_audit /path/to/project --verbose --ignore 'vendor/*'
|
|
14
|
+
python3 -m barcode_claim_audit /path/to/project --fail-on MEDIUM
|
|
15
|
+
python3 -m barcode_claim_audit --version
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
JSON stdout contains verdict, findings (rule, severity, message), evidence flags,
|
|
19
|
+
and inspected file paths. Human output shows the verdict and all findings.
|
|
20
|
+
Repeat --ignore to exclude relative paths using glob patterns; matching directories
|
|
21
|
+
are pruned. --verbose adds a file count in human output. Default threshold is HIGH;
|
|
22
|
+
MEDIUM reports remain visible but exit successfully. Exit codes: 0 for PASS,
|
|
23
|
+
2 for findings meeting the threshold, 1 for usage or filesystem errors.
|
|
24
|
+
|
|
25
|
+
## Matching and rules
|
|
26
|
+
|
|
27
|
+
Only root README.md and Markdown files with a docs path component contribute
|
|
28
|
+
claims. Fenced code is excluded; inline code counts. Matching lowercases text,
|
|
29
|
+
expands n't to not, replaces hyphens with spaces and joins whitespace within
|
|
30
|
+
blank-line-separated paragraphs. Exact templates use word boundaries, longest
|
|
31
|
+
first. Negative spans are removed before positive matching. The full template
|
|
32
|
+
sets are in barcode_claim_audit/phrases.py. Examples:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
scans qr codes
|
|
36
|
+
supports barcode scanning
|
|
37
|
+
does not scan qr codes
|
|
38
|
+
no barcode support
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
One finding per family, at most two: documented-barcode-claim-false is HIGH
|
|
42
|
+
when a positive template has no production evidence, or MEDIUM when evidence
|
|
43
|
+
exists only in tests. documented-no-barcode-claim-false is HIGH when a negative
|
|
44
|
+
template conflicts with production evidence. Without claims the result is PASS.
|
|
45
|
+
With no source, a positive template yields HIGH and a negative one yields PASS.
|
|
46
|
+
|
|
47
|
+
Production evidence excludes tests/, test_*.py, *_test.py, fixtures/, docs/,
|
|
48
|
+
examples/, and symlinks. Test evidence is limited to tests/ and test-named files
|
|
49
|
+
outside the other excluded directories. Imports are resolved per module, including
|
|
50
|
+
aliases; local functions and classes shadow imported names.
|
|
51
|
+
|
|
52
|
+
Supported calls: pyzbar.pyzbar.decode, pylibdmtx.pylibdmtx.decode,
|
|
53
|
+
zxingcpp.read_barcode/read_barcodes, cv2.QRCodeDetector and
|
|
54
|
+
cv2.barcode_BarcodeDetector. Construction alone counts for cv2. Bound detector
|
|
55
|
+
methods detect, detectAndDecode and detectAndDecodeMulti (including self
|
|
56
|
+
attributes, with targets and chained construction) consequently qualify too.
|
|
57
|
+
|
|
58
|
+
## Limitations
|
|
59
|
+
|
|
60
|
+
Only the exact templates count. A bare QR or barcode word is insufficient.
|
|
61
|
+
Generation, camera capture, image opening, file export and setup instructions
|
|
62
|
+
about a user holding a code up to a camera are outside the claim vocabulary.
|
|
63
|
+
Comments, docstrings and strings alone are never evidence. Imports without calls,
|
|
64
|
+
image creation, camera access and unbound lookalike methods do not qualify.
|
|
65
|
+
System code reader helpers invoked through subprocess are not counted; command
|
|
66
|
+
names inside strings are never evidence. Other scan stacks, including phone-camera
|
|
67
|
+
SDKs and hardware scanner wedges that type keystrokes, are out of scope; only
|
|
68
|
+
the Python decode routes listed above are counted.
|
|
69
|
+
|
|
70
|
+
This checks syntax rather than runtime success, reachability, dependency availability,
|
|
71
|
+
image quality or actual hardware. Dynamic imports and reassignment are not modeled.
|
|
72
|
+
Local definitions conservatively shadow imports throughout a module. Syntax-invalid
|
|
73
|
+
source contributes no evidence. Filesystem errors produce a usage error.
|
|
74
|
+
|
|
75
|
+
## Development
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
python3 -m unittest discover
|
|
79
|
+
python3 -m build
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The sdist includes the test suite and fixtures. License: MIT.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Command line interface."""
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
from . import __version__
|
|
6
|
+
from .audit import audit
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main(argv=None):
|
|
10
|
+
parser = argparse.ArgumentParser(description='Compare documented capabilities with Python AST evidence.')
|
|
11
|
+
parser.add_argument('root')
|
|
12
|
+
parser.add_argument('--json', action='store_true')
|
|
13
|
+
parser.add_argument('--verbose', action='store_true')
|
|
14
|
+
parser.add_argument('--ignore', action='append', default=[])
|
|
15
|
+
parser.add_argument('--fail-on', choices=['HIGH', 'MEDIUM'], default='HIGH')
|
|
16
|
+
parser.add_argument('--version', action='version', version=__version__)
|
|
17
|
+
try:
|
|
18
|
+
args = parser.parse_args(argv)
|
|
19
|
+
except SystemExit as exc:
|
|
20
|
+
return 0 if exc.code == 0 else 1
|
|
21
|
+
try:
|
|
22
|
+
result = audit(args.root, args.ignore, args.fail_on)
|
|
23
|
+
except (OSError, ValueError) as exc:
|
|
24
|
+
print(str(exc), file=sys.stderr)
|
|
25
|
+
return 1
|
|
26
|
+
if args.json:
|
|
27
|
+
print(json.dumps(result))
|
|
28
|
+
else:
|
|
29
|
+
print(result['verdict'])
|
|
30
|
+
for finding in result['findings']:
|
|
31
|
+
print('{severity} {rule}: {message}'.format(**finding))
|
|
32
|
+
if args.verbose:
|
|
33
|
+
print('Inspected {} files'.format(len(result['files'])))
|
|
34
|
+
return 2 if result['verdict'] == 'FAIL' else 0
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
if __name__ == '__main__':
|
|
38
|
+
sys.exit(main())
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Documentation matching and provider-resolved AST inspection."""
|
|
2
|
+
import ast
|
|
3
|
+
import fnmatch
|
|
4
|
+
import re
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from .phrases import BARCODE, NOBARCODE
|
|
7
|
+
|
|
8
|
+
DIRECT = {'pyzbar.pyzbar.decode', 'pylibdmtx.pylibdmtx.decode', 'zxingcpp.read_barcode', 'zxingcpp.read_barcodes'}
|
|
9
|
+
CONSTRUCTORS = {'cv2.QRCodeDetector', 'cv2.barcode_BarcodeDetector'}
|
|
10
|
+
MODULES = {'pyzbar.pyzbar', 'pylibdmtx.pylibdmtx', 'zxingcpp', 'cv2'}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def claims(text):
|
|
14
|
+
"""Match exact templates outside fenced code blocks."""
|
|
15
|
+
lines = []
|
|
16
|
+
fence = None
|
|
17
|
+
for line in text.splitlines():
|
|
18
|
+
match = re.match(r'^\s{0,3}(`{3,}|~{3,})(.*)$', line)
|
|
19
|
+
if fence:
|
|
20
|
+
if match and match[1][0] == fence[0] and len(match[1]) >= len(fence) and not match[2].strip():
|
|
21
|
+
fence = None
|
|
22
|
+
lines.append('')
|
|
23
|
+
elif match:
|
|
24
|
+
fence = match[1]
|
|
25
|
+
lines.append('')
|
|
26
|
+
else:
|
|
27
|
+
lines.append(line)
|
|
28
|
+
positive = negative = False
|
|
29
|
+
def pattern(items):
|
|
30
|
+
return r'\b(?:' + '|'.join(re.escape(p) for p in sorted(items, key=len, reverse=True)) + r')\b'
|
|
31
|
+
for paragraph in re.split(r'\n\s*\n', '\n'.join(lines)):
|
|
32
|
+
normalized = re.sub(r'\s+', ' ', paragraph.lower().replace("n't", ' not').replace('-', ' '))
|
|
33
|
+
negative |= bool(re.search(pattern(NOBARCODE), normalized))
|
|
34
|
+
normalized = re.sub(pattern(NOBARCODE), ' ', normalized)
|
|
35
|
+
positive |= bool(re.search(pattern(BARCODE), normalized))
|
|
36
|
+
return positive, negative
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def source_evidence(text):
|
|
40
|
+
"""Resolve imports; local function and class definitions shadow them."""
|
|
41
|
+
try:
|
|
42
|
+
tree = ast.parse(text)
|
|
43
|
+
except SyntaxError:
|
|
44
|
+
return False
|
|
45
|
+
aliases = {}
|
|
46
|
+
shadows = set()
|
|
47
|
+
for node in ast.walk(tree):
|
|
48
|
+
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
|
49
|
+
shadows.add(node.name)
|
|
50
|
+
elif isinstance(node, ast.Import):
|
|
51
|
+
for alias in node.names:
|
|
52
|
+
if alias.name in MODULES:
|
|
53
|
+
aliases[alias.asname or alias.name.split('.')[0]] = alias.name if alias.asname else alias.name.split('.')[0]
|
|
54
|
+
elif isinstance(node, ast.ImportFrom) and node.module in MODULES:
|
|
55
|
+
for alias in node.names:
|
|
56
|
+
aliases[alias.asname or alias.name] = node.module + '.' + alias.name
|
|
57
|
+
def resolve(node):
|
|
58
|
+
if isinstance(node, ast.Name):
|
|
59
|
+
return '' if node.id in shadows else aliases.get(node.id, '')
|
|
60
|
+
if isinstance(node, ast.Attribute):
|
|
61
|
+
base = resolve(node.value)
|
|
62
|
+
return base + '.' + node.attr if base else ''
|
|
63
|
+
return ''
|
|
64
|
+
# Every bound/chained detector route contains a constructor call,
|
|
65
|
+
# which is independently sufficient evidence under this audit's rules.
|
|
66
|
+
return any(isinstance(n, ast.Call) and resolve(n.func) in DIRECT | CONSTRUCTORS for n in ast.walk(tree))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def audit(root, ignore=(), fail_on='HIGH'):
|
|
70
|
+
"""Inspect docs and source without executing code or opening environment files."""
|
|
71
|
+
root = Path(root)
|
|
72
|
+
if not root.is_dir():
|
|
73
|
+
raise ValueError('root must be a readable directory')
|
|
74
|
+
positive = negative = prod = test = False
|
|
75
|
+
scanned = []
|
|
76
|
+
excluded = {'fixtures', 'docs', 'examples'}
|
|
77
|
+
def walk(directory):
|
|
78
|
+
for path in sorted(directory.iterdir()):
|
|
79
|
+
rel = path.relative_to(root).as_posix()
|
|
80
|
+
if path.is_symlink() or any(fnmatch.fnmatchcase(rel, g) for g in ignore):
|
|
81
|
+
continue
|
|
82
|
+
if path.is_dir():
|
|
83
|
+
yield from walk(path)
|
|
84
|
+
else:
|
|
85
|
+
yield path
|
|
86
|
+
for path in walk(root):
|
|
87
|
+
rel = path.relative_to(root)
|
|
88
|
+
parts = rel.parts
|
|
89
|
+
if (rel.as_posix() == 'README.md' or 'docs' in parts) and path.suffix.lower() == '.md':
|
|
90
|
+
p, n = claims(path.read_text(encoding='utf-8', errors='replace'))
|
|
91
|
+
positive |= p
|
|
92
|
+
negative |= n
|
|
93
|
+
scanned.append(rel.as_posix())
|
|
94
|
+
elif path.suffix == '.py' and not excluded.intersection(parts[:-1]):
|
|
95
|
+
is_test = 'tests' in parts[:-1] or path.name.startswith('test_') or path.name.endswith('_test.py')
|
|
96
|
+
evidence = source_evidence(path.read_text(encoding='utf-8', errors='replace'))
|
|
97
|
+
if is_test:
|
|
98
|
+
test |= evidence
|
|
99
|
+
else:
|
|
100
|
+
prod |= evidence
|
|
101
|
+
scanned.append(rel.as_posix())
|
|
102
|
+
findings = []
|
|
103
|
+
if positive and not prod:
|
|
104
|
+
findings.append({'rule': 'documented-barcode-claim-false', 'severity': 'MEDIUM' if test else 'HIGH', 'message': 'Documented capability has only test evidence.' if test else 'Documented capability lacks production evidence.'})
|
|
105
|
+
if negative and prod:
|
|
106
|
+
findings.append({'rule': 'documented-no-barcode-claim-false', 'severity': 'HIGH', 'message': 'Documented absence conflicts with production evidence.'})
|
|
107
|
+
failed = any(f['severity'] == 'HIGH' or fail_on == 'MEDIUM' for f in findings)
|
|
108
|
+
return {'verdict': 'FAIL' if failed else 'PASS', 'findings': findings, 'evidence': {'production': prod, 'tests': test}, 'files': scanned}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"""Exact claim templates."""
|
|
2
|
+
BARCODE = ('extracts data from qr codes', 'extract data from qr codes', 'extracts data from barcodes', 'extract data from barcodes', 'supports qr code scanning', 'support qr code scanning', 'supports barcode scanning', 'support barcode scanning', 'reads qr code data', 'read qr code data', 'reads barcode data', 'read barcode data', 'detects qr codes', 'detect qr codes', 'detects barcodes', 'detect barcodes', 'scans qr codes', 'scan qr codes', 'scans barcodes', 'scan barcodes', 'reads qr codes', 'read qr codes', 'reads barcodes', 'read barcodes', 'decodes qr codes', 'decode qr codes', 'decodes barcodes', 'decode barcodes', 'scans qr code', 'scan qr code', 'scans barcode', 'scan barcode', 'reads qr code', 'read qr code', 'reads barcode', 'read barcode', 'qr code scanning', 'barcode scanning', 'qr code support', 'barcode support', 'supports qr codes', 'support qr codes', 'supports barcodes', 'support barcodes', 'uses qr codes', 'use qr codes', 'uses barcodes', 'use barcodes')
|
|
3
|
+
NOBARCODE = ('does not scan qr codes', 'does not scan barcodes', 'does not read qr codes', 'does not read barcodes', 'does not decode qr codes', 'does not decode barcodes', 'does not detect qr codes', 'does not detect barcodes', 'does not support qr codes', 'does not support barcodes', 'does not support qr code scanning', 'does not support barcode scanning', 'qr codes not supported', 'barcodes not supported', 'qr code not supported', 'barcode not supported', 'no qr code support', 'without qr code support', 'no barcode support', 'without barcode support', 'no qr code scanning', 'without qr code scanning', 'no barcode scanning', 'without barcode scanning', 'no qr codes', 'without qr codes', 'no barcodes', 'without barcodes')
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: barcode-claim-audit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Offline audit of documented code-reading capabilities
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# barcode-claim-audit 0.1.0
|
|
12
|
+
|
|
13
|
+
An offline static audit of documented code-reading claims against Python source.
|
|
14
|
+
Requires Python >=3.9, with no runtime dependencies. Source is parsed with AST
|
|
15
|
+
and never executed. Environment files are never opened.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python3 -m pip install .
|
|
21
|
+
barcode-claim-audit /path/to/project
|
|
22
|
+
python3 -m barcode_claim_audit /path/to/project --json
|
|
23
|
+
python3 -m barcode_claim_audit /path/to/project --verbose --ignore 'vendor/*'
|
|
24
|
+
python3 -m barcode_claim_audit /path/to/project --fail-on MEDIUM
|
|
25
|
+
python3 -m barcode_claim_audit --version
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
JSON stdout contains verdict, findings (rule, severity, message), evidence flags,
|
|
29
|
+
and inspected file paths. Human output shows the verdict and all findings.
|
|
30
|
+
Repeat --ignore to exclude relative paths using glob patterns; matching directories
|
|
31
|
+
are pruned. --verbose adds a file count in human output. Default threshold is HIGH;
|
|
32
|
+
MEDIUM reports remain visible but exit successfully. Exit codes: 0 for PASS,
|
|
33
|
+
2 for findings meeting the threshold, 1 for usage or filesystem errors.
|
|
34
|
+
|
|
35
|
+
## Matching and rules
|
|
36
|
+
|
|
37
|
+
Only root README.md and Markdown files with a docs path component contribute
|
|
38
|
+
claims. Fenced code is excluded; inline code counts. Matching lowercases text,
|
|
39
|
+
expands n't to not, replaces hyphens with spaces and joins whitespace within
|
|
40
|
+
blank-line-separated paragraphs. Exact templates use word boundaries, longest
|
|
41
|
+
first. Negative spans are removed before positive matching. The full template
|
|
42
|
+
sets are in barcode_claim_audit/phrases.py. Examples:
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
scans qr codes
|
|
46
|
+
supports barcode scanning
|
|
47
|
+
does not scan qr codes
|
|
48
|
+
no barcode support
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
One finding per family, at most two: documented-barcode-claim-false is HIGH
|
|
52
|
+
when a positive template has no production evidence, or MEDIUM when evidence
|
|
53
|
+
exists only in tests. documented-no-barcode-claim-false is HIGH when a negative
|
|
54
|
+
template conflicts with production evidence. Without claims the result is PASS.
|
|
55
|
+
With no source, a positive template yields HIGH and a negative one yields PASS.
|
|
56
|
+
|
|
57
|
+
Production evidence excludes tests/, test_*.py, *_test.py, fixtures/, docs/,
|
|
58
|
+
examples/, and symlinks. Test evidence is limited to tests/ and test-named files
|
|
59
|
+
outside the other excluded directories. Imports are resolved per module, including
|
|
60
|
+
aliases; local functions and classes shadow imported names.
|
|
61
|
+
|
|
62
|
+
Supported calls: pyzbar.pyzbar.decode, pylibdmtx.pylibdmtx.decode,
|
|
63
|
+
zxingcpp.read_barcode/read_barcodes, cv2.QRCodeDetector and
|
|
64
|
+
cv2.barcode_BarcodeDetector. Construction alone counts for cv2. Bound detector
|
|
65
|
+
methods detect, detectAndDecode and detectAndDecodeMulti (including self
|
|
66
|
+
attributes, with targets and chained construction) consequently qualify too.
|
|
67
|
+
|
|
68
|
+
## Limitations
|
|
69
|
+
|
|
70
|
+
Only the exact templates count. A bare QR or barcode word is insufficient.
|
|
71
|
+
Generation, camera capture, image opening, file export and setup instructions
|
|
72
|
+
about a user holding a code up to a camera are outside the claim vocabulary.
|
|
73
|
+
Comments, docstrings and strings alone are never evidence. Imports without calls,
|
|
74
|
+
image creation, camera access and unbound lookalike methods do not qualify.
|
|
75
|
+
System code reader helpers invoked through subprocess are not counted; command
|
|
76
|
+
names inside strings are never evidence. Other scan stacks, including phone-camera
|
|
77
|
+
SDKs and hardware scanner wedges that type keystrokes, are out of scope; only
|
|
78
|
+
the Python decode routes listed above are counted.
|
|
79
|
+
|
|
80
|
+
This checks syntax rather than runtime success, reachability, dependency availability,
|
|
81
|
+
image quality or actual hardware. Dynamic imports and reassignment are not modeled.
|
|
82
|
+
Local definitions conservatively shadow imports throughout a module. Syntax-invalid
|
|
83
|
+
source contributes no evidence. Filesystem errors produce a usage error.
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
python3 -m unittest discover
|
|
89
|
+
python3 -m build
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The sdist includes the test suite and fixtures. License: MIT.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
barcode_claim_audit/__init__.py
|
|
6
|
+
barcode_claim_audit/__main__.py
|
|
7
|
+
barcode_claim_audit/audit.py
|
|
8
|
+
barcode_claim_audit/phrases.py
|
|
9
|
+
barcode_claim_audit.egg-info/PKG-INFO
|
|
10
|
+
barcode_claim_audit.egg-info/SOURCES.txt
|
|
11
|
+
barcode_claim_audit.egg-info/dependency_links.txt
|
|
12
|
+
barcode_claim_audit.egg-info/entry_points.txt
|
|
13
|
+
barcode_claim_audit.egg-info/top_level.txt
|
|
14
|
+
tests/__init__.py
|
|
15
|
+
tests/cases.py
|
|
16
|
+
tests/test_audit.py
|
|
17
|
+
tests/fixtures/.env
|
|
18
|
+
tests/fixtures/alias_cv2/README.md
|
|
19
|
+
tests/fixtures/alias_cv2/app.py
|
|
20
|
+
tests/fixtures/alias_dmtx/README.md
|
|
21
|
+
tests/fixtures/alias_dmtx/app.py
|
|
22
|
+
tests/fixtures/alias_pyzbar/README.md
|
|
23
|
+
tests/fixtures/alias_pyzbar/app.py
|
|
24
|
+
tests/fixtures/alias_zxing/README.md
|
|
25
|
+
tests/fixtures/alias_zxing/app.py
|
|
26
|
+
tests/fixtures/bad/README.md
|
|
27
|
+
tests/fixtures/bad/app.py
|
|
28
|
+
tests/fixtures/barcode_detector/README.md
|
|
29
|
+
tests/fixtures/barcode_detector/app.py
|
|
30
|
+
tests/fixtures/bare-word/README.md
|
|
31
|
+
tests/fixtures/bare-word/app.py
|
|
32
|
+
tests/fixtures/bare_import/README.md
|
|
33
|
+
tests/fixtures/bare_import/app.py
|
|
34
|
+
tests/fixtures/bound/README.md
|
|
35
|
+
tests/fixtures/bound/app.py
|
|
36
|
+
tests/fixtures/bytes_decode/README.md
|
|
37
|
+
tests/fixtures/bytes_decode/app.py
|
|
38
|
+
tests/fixtures/camera/README.md
|
|
39
|
+
tests/fixtures/camera/app.py
|
|
40
|
+
tests/fixtures/chained/README.md
|
|
41
|
+
tests/fixtures/chained/app.py
|
|
42
|
+
tests/fixtures/cv2/README.md
|
|
43
|
+
tests/fixtures/cv2/app.py
|
|
44
|
+
tests/fixtures/decorator/README.md
|
|
45
|
+
tests/fixtures/decorator/app.py
|
|
46
|
+
tests/fixtures/dmtx/README.md
|
|
47
|
+
tests/fixtures/dmtx/app.py
|
|
48
|
+
tests/fixtures/docstring/README.md
|
|
49
|
+
tests/fixtures/docstring/app.py
|
|
50
|
+
tests/fixtures/fake_path/README.md
|
|
51
|
+
tests/fixtures/fake_path/app.py
|
|
52
|
+
tests/fixtures/fenced-code/README.md
|
|
53
|
+
tests/fixtures/fenced-code/app.py
|
|
54
|
+
tests/fixtures/generation/README.md
|
|
55
|
+
tests/fixtures/generation/app.py
|
|
56
|
+
tests/fixtures/generation-claim/README.md
|
|
57
|
+
tests/fixtures/generation-claim/app.py
|
|
58
|
+
tests/fixtures/good/README.md
|
|
59
|
+
tests/fixtures/good/app.py
|
|
60
|
+
tests/fixtures/imread/README.md
|
|
61
|
+
tests/fixtures/imread/app.py
|
|
62
|
+
tests/fixtures/inline-code/README.md
|
|
63
|
+
tests/fixtures/inline-code/app.py
|
|
64
|
+
tests/fixtures/local_decode/README.md
|
|
65
|
+
tests/fixtures/local_decode/app.py
|
|
66
|
+
tests/fixtures/local_detector/README.md
|
|
67
|
+
tests/fixtures/local_detector/app.py
|
|
68
|
+
tests/fixtures/negation/README.md
|
|
69
|
+
tests/fixtures/negation/app.py
|
|
70
|
+
tests/fixtures/no-barcode/README.md
|
|
71
|
+
tests/fixtures/no-barcode/app.py
|
|
72
|
+
tests/fixtures/no-claim/README.md
|
|
73
|
+
tests/fixtures/no-claim/app.py
|
|
74
|
+
tests/fixtures/no-source-negative/README.md
|
|
75
|
+
tests/fixtures/no-source-positive/README.md
|
|
76
|
+
tests/fixtures/pyzbar_attribute/README.md
|
|
77
|
+
tests/fixtures/pyzbar_attribute/app.py
|
|
78
|
+
tests/fixtures/qr_constructor/README.md
|
|
79
|
+
tests/fixtures/qr_constructor/app.py
|
|
80
|
+
tests/fixtures/self_bound/README.md
|
|
81
|
+
tests/fixtures/self_bound/app.py
|
|
82
|
+
tests/fixtures/shadow_decode/README.md
|
|
83
|
+
tests/fixtures/shadow_decode/app.py
|
|
84
|
+
tests/fixtures/shadow_detector/README.md
|
|
85
|
+
tests/fixtures/shadow_detector/app.py
|
|
86
|
+
tests/fixtures/string/README.md
|
|
87
|
+
tests/fixtures/string/app.py
|
|
88
|
+
tests/fixtures/subprocess/README.md
|
|
89
|
+
tests/fixtures/subprocess/app.py
|
|
90
|
+
tests/fixtures/testonly/README.md
|
|
91
|
+
tests/fixtures/testonly/tests/test_reader.py
|
|
92
|
+
tests/fixtures/unbound/README.md
|
|
93
|
+
tests/fixtures/unbound/app.py
|
|
94
|
+
tests/fixtures/user-instruction/README.md
|
|
95
|
+
tests/fixtures/user-instruction/app.py
|
|
96
|
+
tests/fixtures/with_bound/README.md
|
|
97
|
+
tests/fixtures/with_bound/app.py
|
|
98
|
+
tests/fixtures/writer/README.md
|
|
99
|
+
tests/fixtures/writer/app.py
|
|
100
|
+
tests/fixtures/zxing/README.md
|
|
101
|
+
tests/fixtures/zxing/app.py
|
|
102
|
+
tests/fixtures/zxing_plural/README.md
|
|
103
|
+
tests/fixtures/zxing_plural/app.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
barcode_claim_audit
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "barcode-claim-audit"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Offline audit of documented code-reading capabilities"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
dependencies = []
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
barcode-claim-audit = "barcode_claim_audit.__main__:main"
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
include = ["barcode_claim_audit*"]
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"""Source cases: never executed by the audit."""
|
|
2
|
+
CASES = {'good': ('from pyzbar.pyzbar import decode\ndecode(img)', True), 'pyzbar_attribute': ('import pyzbar.pyzbar\npyzbar.pyzbar.decode(img)', True), 'zxing': ('from zxingcpp import read_barcode\nread_barcode(img)', True), 'zxing_plural': ('import zxingcpp\nzxingcpp.read_barcodes(img)', True), 'dmtx': ('from pylibdmtx.pylibdmtx import decode\ndecode(img)', True), 'cv2': ('import cv2\ncv2.QRCodeDetector()', True), 'bound': ('import cv2\nd = cv2.QRCodeDetector()\nd.detectAndDecode(img)', True), 'chained': ('import cv2\ncv2.QRCodeDetector().detectAndDecode(img)', True), 'barcode_detector': ('import cv2\ncv2.barcode_BarcodeDetector()', True), 'bad': ('value = 1', False), 'bare_import': ('import pyzbar.pyzbar', False), 'imread': ('import cv2\ncv2.imread("image")', False), 'camera': ('import cv2\ncv2.VideoCapture(0)', False), 'generation': ('import qrcode\nqrcode.make("value")', False), 'unbound': ('detector.detectAndDecode(img)', False), 'string': ('value = "123456"', False), 'subprocess': ('import subprocess\nsubprocess.run(["zbarimg", "image"])', False), 'local_decode': ('def decode(img): pass\ndecode(img)', False), 'local_detector': ('class QRCodeDetector: pass\nQRCodeDetector()', False), 'decorator': ('@app.get("/")\ndef handler(): pass', False), 'alias_pyzbar': ('import pyzbar.pyzbar as pz\npz.decode(img)', True), 'alias_dmtx': ('from pylibdmtx.pylibdmtx import decode as dec\ndec(img)', True), 'alias_zxing': ('import zxingcpp as zx\nzx.read_barcodes(img)', True), 'alias_cv2': ('from cv2 import QRCodeDetector as D\nD()', True), 'self_bound': ('import cv2\nself.d = cv2.QRCodeDetector()\nself.d.detect(img)', True), 'with_bound': ('import cv2\nwith cv2.QRCodeDetector() as d:\n d.detectAndDecodeMulti(img)', True), 'shadow_decode': ('from pyzbar.pyzbar import decode\ndef decode(img): pass\ndecode(img)', False), 'shadow_detector': ('from cv2 import QRCodeDetector\nclass QRCodeDetector: pass\nQRCodeDetector()', False), 'fake_path': ('import unrelated as cv2\ncv2.QRCodeDetector()', False), 'bytes_decode': ('b"x".decode()', False), 'writer': ('import barcode\nbarcode.writer.ImageWriter()', False), 'qr_constructor': ('import qrcode\nqrcode.QRCode()', False), 'docstring': ('"""from pyzbar.pyzbar import decode; decode(img)"""', False)}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# MARKER ONLY: CONTENT MUST NEVER BE READ
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
barcode
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import pyzbar.pyzbar
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
b"x".decode()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""from pyzbar.pyzbar import decode; decode(img)"""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
generates qr codes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
`scans qr codes`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
does not scan qr codes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool does not scan qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
A neutral tool.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
does not scan qr codes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
scans qr codes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = "123456"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
detector.detectAndDecode(img)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hold the code up to the camera first
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
value = 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This tool scans qr codes.
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""Behavior, CLI, scope and packaging contracts."""
|
|
2
|
+
import builtins
|
|
3
|
+
import io
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
import tempfile
|
|
9
|
+
import unittest
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from unittest.mock import patch
|
|
12
|
+
from barcode_claim_audit import __version__
|
|
13
|
+
from barcode_claim_audit.audit import audit, claims, source_evidence
|
|
14
|
+
from barcode_claim_audit.phrases import BARCODE, NOBARCODE
|
|
15
|
+
from tests.cases import CASES
|
|
16
|
+
|
|
17
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
18
|
+
FIX = ROOT / 'tests/fixtures'
|
|
19
|
+
POS = 'documented-barcode-claim-false'
|
|
20
|
+
NEG = 'documented-no-barcode-claim-false'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class AuditTests(unittest.TestCase):
|
|
24
|
+
def tree(self, text, source=None, location='app.py'):
|
|
25
|
+
temp = tempfile.TemporaryDirectory()
|
|
26
|
+
self.addCleanup(temp.cleanup)
|
|
27
|
+
root = Path(temp.name)
|
|
28
|
+
(root / 'README.md').write_text(text)
|
|
29
|
+
if source is not None:
|
|
30
|
+
path = root / location
|
|
31
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
path.write_text(source)
|
|
33
|
+
return root
|
|
34
|
+
|
|
35
|
+
def cli(self, *args):
|
|
36
|
+
return subprocess.run([sys.executable, '-m', 'barcode_claim_audit', *map(str, args)], text=True, capture_output=True)
|
|
37
|
+
|
|
38
|
+
def test_testonly(self):
|
|
39
|
+
for threshold, verdict in [('HIGH', 'PASS'), ('MEDIUM', 'FAIL')]:
|
|
40
|
+
result = audit(FIX / 'testonly', fail_on=threshold)
|
|
41
|
+
self.assertEqual(result['verdict'], verdict)
|
|
42
|
+
self.assertEqual([(f['rule'], f['severity']) for f in result['findings']], [(POS, 'MEDIUM')])
|
|
43
|
+
|
|
44
|
+
def test_negative(self):
|
|
45
|
+
result = audit(FIX / 'no-barcode')
|
|
46
|
+
self.assertEqual(result['verdict'], 'FAIL')
|
|
47
|
+
self.assertEqual([(f['rule'], f['severity']) for f in result['findings']], [(NEG, 'HIGH')])
|
|
48
|
+
|
|
49
|
+
def test_self_audit(self):
|
|
50
|
+
self.assertEqual(claims((ROOT / 'README.md').read_text()), (False, False))
|
|
51
|
+
self.assertEqual(audit(ROOT)['findings'], [])
|
|
52
|
+
self.assertEqual(audit(ROOT)['verdict'], 'PASS')
|
|
53
|
+
|
|
54
|
+
def test_normalization(self):
|
|
55
|
+
for text, expected in [("DOESN'T scan\nQR-codes", (False, True)), ('scans\nQR-codes', (True, False)), ('scans\n\nqr codes', (False, False)), ('prescans barcodesuffix', (False, False)), ('`scans qr codes`', (True, False)), ('```text\nscans qr codes\n```', (False, False)), ('~~~~\nscans qr codes\n~~~~', (False, False))]:
|
|
56
|
+
self.assertEqual(claims(text), expected)
|
|
57
|
+
|
|
58
|
+
def test_docs_ignore(self):
|
|
59
|
+
root = self.tree('Neutral')
|
|
60
|
+
path = root / 'nested/docs/guide.md'
|
|
61
|
+
path.parent.mkdir(parents=True)
|
|
62
|
+
path.write_text('scans qr codes')
|
|
63
|
+
self.assertEqual(audit(root)['verdict'], 'FAIL')
|
|
64
|
+
self.assertEqual(audit(root, ['nested/docs/*'])['verdict'], 'PASS')
|
|
65
|
+
self.assertEqual(self.cli(root, '--ignore', 'nested/docs/*', '--json').returncode, 0)
|
|
66
|
+
(root / 'OTHER.md').write_text('scans qr codes')
|
|
67
|
+
self.assertEqual(audit(root, ['nested'])['findings'], [])
|
|
68
|
+
|
|
69
|
+
def test_scope(self):
|
|
70
|
+
for location, severity in [('tests/reader.py', 'MEDIUM'), ('test_reader.py', 'MEDIUM'), ('reader_test.py', 'MEDIUM'), ('fixtures/reader.py', 'HIGH'), ('docs/reader.py', 'HIGH'), ('examples/reader.py', 'HIGH')]:
|
|
71
|
+
with self.subTest(location=location):
|
|
72
|
+
self.assertEqual(audit(self.tree('scans qr codes', CASES['good'][0], location))['findings'][0]['severity'], severity)
|
|
73
|
+
|
|
74
|
+
def test_symlinks(self):
|
|
75
|
+
root = self.tree('scans qr codes')
|
|
76
|
+
outside = self.tree('Neutral', CASES['good'][0])
|
|
77
|
+
(root / 'link.py').symlink_to(outside / 'app.py')
|
|
78
|
+
(root / 'linked').symlink_to(outside, target_is_directory=True)
|
|
79
|
+
self.assertEqual(audit(root)['findings'][0]['severity'], 'HIGH')
|
|
80
|
+
|
|
81
|
+
def test_env_never_opened(self):
|
|
82
|
+
def guard(delegate):
|
|
83
|
+
def checked(file, *args, **kwargs):
|
|
84
|
+
if not isinstance(file, int) and Path(file).name == '.env':
|
|
85
|
+
raise AssertionError('environment file opened')
|
|
86
|
+
return delegate(file, *args, **kwargs)
|
|
87
|
+
return checked
|
|
88
|
+
with patch('builtins.open', guard(builtins.open)), patch('io.open', guard(io.open)):
|
|
89
|
+
audit(ROOT)
|
|
90
|
+
audit(FIX)
|
|
91
|
+
|
|
92
|
+
def test_cli(self):
|
|
93
|
+
self.assertEqual(self.cli('--version').stdout.strip(), '0.1.0')
|
|
94
|
+
self.assertEqual(self.cli('/nonexistent-barcode-root').returncode, 1)
|
|
95
|
+
self.assertEqual(self.cli('--unknown').returncode, 1)
|
|
96
|
+
self.assertEqual(self.cli(FIX / 'good', '--verbose').returncode, 0)
|
|
97
|
+
for name, code, findings in [('good', 0, []), ('bad', 2, [(POS, 'HIGH')]), ('testonly', 0, [(POS, 'MEDIUM')]), ('no-barcode', 2, [(NEG, 'HIGH')])]:
|
|
98
|
+
result = self.cli(FIX / name, '--json')
|
|
99
|
+
self.assertEqual(result.returncode, code)
|
|
100
|
+
data = json.loads(result.stdout)
|
|
101
|
+
self.assertEqual([(f['rule'], f['severity']) for f in data['findings']], findings)
|
|
102
|
+
self.assertEqual(self.cli(FIX / 'testonly', '--fail-on', 'MEDIUM').returncode, 2)
|
|
103
|
+
|
|
104
|
+
def test_packaging(self):
|
|
105
|
+
metadata = (ROOT / 'pyproject.toml').read_text()
|
|
106
|
+
self.assertIn('barcode_claim_audit.__main__:main', metadata)
|
|
107
|
+
self.assertIn('requires-python = ">=3.9"', metadata)
|
|
108
|
+
self.assertIn('version = "0.1.0"', metadata)
|
|
109
|
+
self.assertEqual(__version__, '0.1.0')
|
|
110
|
+
self.assertIn('MIT License', (ROOT / 'LICENSE').read_text())
|
|
111
|
+
self.assertIn('recursive-include tests *.py *.md .env', (ROOT / 'MANIFEST.in').read_text())
|
|
112
|
+
self.assertTrue((FIX / '.env').is_file())
|
|
113
|
+
paths = list((ROOT / 'barcode_claim_audit').rglob('*.py')) + list((ROOT / 'tests').rglob('*.py')) + [ROOT / 'README.md']
|
|
114
|
+
for path in paths:
|
|
115
|
+
text = path.read_text()
|
|
116
|
+
self.assertIsNone(re.search(r'\bpython\s+(?:-m|-c|\w+\.py)', text), str(path))
|
|
117
|
+
self.assertIsNone(re.search(r'(?:AKIA[0-9A-Z]{16}|ghp_[A-Za-z0-9]{36}|' + '-----BEGIN ' + 'PRIVATE KEY-----)', text), str(path))
|
|
118
|
+
|
|
119
|
+
def test_invalid_syntax(self):
|
|
120
|
+
self.assertFalse(source_evidence('def broken('))
|
|
121
|
+
|
|
122
|
+
def test_duplicate_findings(self):
|
|
123
|
+
result = audit(self.tree('scans qr codes and scans barcodes\n\ndoes not scan qr codes'))
|
|
124
|
+
self.assertEqual(len(result['findings']), 1)
|
|
125
|
+
self.assertEqual(result['findings'][0]['rule'], POS)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def add_cases():
|
|
129
|
+
for name, (source, good) in CASES.items():
|
|
130
|
+
def check(self, name=name, good=good):
|
|
131
|
+
result = audit(FIX / name)
|
|
132
|
+
self.assertEqual(result['verdict'], 'PASS' if good else 'FAIL')
|
|
133
|
+
self.assertEqual([(f['rule'], f['severity']) for f in result['findings']], [] if good else [(POS, 'HIGH')])
|
|
134
|
+
setattr(AuditTests, 'test_source_' + name, check)
|
|
135
|
+
for family, phrases in [('positive', BARCODE), ('negative', NOBARCODE)]:
|
|
136
|
+
for index, phrase in enumerate(phrases):
|
|
137
|
+
def check(self, phrase=phrase, family=family):
|
|
138
|
+
self.assertEqual(claims(phrase), (family == 'positive', family == 'negative'))
|
|
139
|
+
result = audit(self.tree(phrase))
|
|
140
|
+
self.assertEqual([(f['rule'], f['severity']) for f in result['findings']], [] if family == 'negative' else [(POS, 'HIGH')])
|
|
141
|
+
setattr(AuditTests, 'test_phrase_{}_{}'.format(family, index), check)
|
|
142
|
+
for index, text in enumerate(['Neutral tool.', 'generates qr codes', 'creates qr codes', 'renders a barcode', 'hold the code up to the camera first', 'barcode', 'camera capture and image opening', 'file export']):
|
|
143
|
+
def check(self, text=text):
|
|
144
|
+
self.assertEqual(audit(self.tree(text))['findings'], [])
|
|
145
|
+
setattr(AuditTests, 'test_nonclaim_{}'.format(index), check)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
add_cases()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def add_document_cases():
|
|
152
|
+
for name in ['no-claim', 'generation-claim', 'user-instruction', 'bare-word', 'fenced-code', 'inline-code', 'negation', 'no-source-positive', 'no-source-negative']:
|
|
153
|
+
def check(self, name=name):
|
|
154
|
+
result = audit(FIX / name)
|
|
155
|
+
failed = name in {'inline-code', 'no-source-positive'}
|
|
156
|
+
self.assertEqual(result['verdict'], 'FAIL' if failed else 'PASS')
|
|
157
|
+
self.assertEqual([(f['rule'], f['severity']) for f in result['findings']], [(POS, 'HIGH')] if failed else [])
|
|
158
|
+
setattr(AuditTests, 'test_doc_fixture_' + name.replace('-', '_'), check)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
add_document_cases()
|