File size: 3,503 Bytes
75e20e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""Check source completeness, signatures and optional host dependencies."""
import argparse
import hashlib
import importlib
import json
from pathlib import Path
import re
import sys

sys.dont_write_bytecode = True

ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / 'src'
sys.path.insert(0, str(SOURCE))


def check(dependencies=False):
    failures = []
    manifest = json.loads((ROOT / 'source-manifest.json').read_text())
    for row in manifest['files']:
        path = ROOT / row['path']
        if not path.is_file() or hashlib.sha256(path.read_bytes()).hexdigest() != row['sha256']:
            failures.append('Missing or changed source: ' + row['path'])
    evidence = json.loads((ROOT / 'docs/source-completeness.json').read_text())
    for relative in evidence['source_modules'] + evidence['required_small_archives']:
        if not (SOURCE / relative).is_file():
            failures.append('Required source or fixture missing: ' + relative)
    modules = list(SOURCE.rglob('*.py'))
    for path in modules:
        try:
            compile(path.read_bytes(), str(path.relative_to(ROOT)), 'exec')
        except (SyntaxError, UnicodeError) as exc:
            failures.append(str(exc))
    for path in [ROOT / 'README.md', *sorted((ROOT / 'docs').glob('*.md'))]:
        text = path.read_text()
        if re.search(r'/(?:mnt|data|home|Users)/', text):
            failures.append('Machine path in public documentation: ' + str(path.relative_to(ROOT)))
        for link in re.findall(r'\]\(([^)]+)\)', text):
            target = link.split('#', 1)[0]
            if target and '://' not in target and not (path.parent / target).exists():
                failures.append('Broken documentation link: ' + link)
    from generation_dependencies import validate_generation_dependencies
    try:
        package = validate_generation_dependencies(SOURCE / 'engine')
    except (ValueError, FileNotFoundError) as exc:
        failures.append(str(exc))
        package = {}
    sys.path.insert(0, str(SOURCE / 'tools/release'))
    from check_source_integrity import check as check_engine
    engine = check_engine(SOURCE)
    failures.extend('Engine signature mismatch: ' + row.get('path', row.get('reason', 'unknown'))
                    for row in engine['mismatches'])
    missing_packages = []
    if dependencies:
        for name in ('numpy', 'scipy', 'shapely', 'manifold3d', 'PIL', 'trimesh', 'rtree', 'yaml'):
            try:
                importlib.import_module(name)
            except ImportError:
                missing_packages.append(name)
        if missing_packages:
            failures.append('Install requirements.txt; missing packages: ' + ', '.join(missing_packages))
    return {'passed': not failures, 'manifest_files': len(manifest['files']),
            'python_modules': len(modules),
            'upstream_modules_present': len(evidence['source_modules']),
            'dynamic_dependencies': package.get('checked_count', 0),
            'engine_inventory': engine['inventory_files'],
            'engine_implementations': engine['revision_files'],
            'failures': failures}


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--dependencies', action='store_true', help='also import host dependencies')
    args = parser.parse_args()
    result = check(args.dependencies)
    print(json.dumps(result, indent=2))
    raise SystemExit(0 if result['passed'] else 1)