File size: 3,178 Bytes
870b85a
 
 
 
 
 
 
 
3f15e9f
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Apply the reviewed TV-bay coarse-box correction to this v26 copy only."""
from pathlib import Path
import argparse
import ast
import difflib
import hashlib
import json

ROOT = Path(__file__).resolve().parent
BASE_SHA = '5c40e2a692b13d2b5c2fd98f8cbc246389a4354d334d3fd3663834138f345d79'
RELATIVE = 'scripts/internal/scene_generation/real_assets/joint_layout.py'


def digest(data):
    return hashlib.sha256(data).hexdigest()


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--apply', action='store_true')
    args = parser.parse_args()
    base = ROOT / 'native_patches/base/joint_layout.py'
    original = base.read_bytes()
    if digest(original) != BASE_SHA:
        raise ValueError('Original source differs from the frozen v25 module')
    helper = (ROOT / 'qualified_tv_bay.py').read_text()
    function = next(n for n in ast.parse(helper).body
                    if isinstance(n, ast.FunctionDef) and n.name == 'qualified_tv_bay_bbox_overlap')
    function_text = '\n'.join(helper.splitlines()[function.lineno-1:function.end_lineno])
    source = original.decode()
    marker = 'def measured_surface_bbox_overlap(parent, child):'
    guard = '                and not measured_surface_bbox_overlap(pose,other)):'
    if source.count(marker) != 1 or source.count(guard) != 1:
        raise ValueError('Expected native patch sites are not unique')
    patched = source.replace(marker, function_text + '\n\n\n' + marker)
    patched = patched.replace(guard,
        '                and not measured_surface_bbox_overlap(pose,other)\n'
        '                and not qualified_tv_bay_bbox_overlap(pose,other)\n'
        '                and not qualified_tv_bay_bbox_overlap(other,pose)):')
    ast.parse(patched)
    raw = patched.encode()
    target = ROOT / 'engine/source' / RELATIVE
    if args.apply:
        if target.is_symlink() or ROOT not in target.resolve().parents:
            raise ValueError('Patch target must stay inside this v26 workspace')
        current = target.read_bytes()
        if digest(current) not in (BASE_SHA, digest(raw)):
            raise ValueError('Refusing to overwrite an unknown native revision')
        if current != raw:
            temporary = target.with_suffix('.py.v26.tmp')
            temporary.write_bytes(raw)
            temporary.replace(target)
    (ROOT / 'native_patches/joint_layout.py').write_bytes(raw)
    (ROOT / 'native_patches/qualified-tv-bay.diff').write_text(''.join(
        difflib.unified_diff(source.splitlines(True), patched.splitlines(True),
            fromfile='v25/' + RELATIVE, tofile='v26/' + RELATIVE)))
    receipt = dict(schema='rhg.native-patch.v1', path=RELATIVE,
                   baseline_sha256=BASE_SHA, patched_sha256=digest(raw),
                   helper_sha256=digest(helper.encode()), applied=args.apply,
                   scope='coarse_enclosing_box_deferral_only',
                   saved_mesh_checks_required=True, physical_qualification=False)
    (ROOT / 'native_patches/patch_receipt.json').write_text(
        json.dumps(receipt, indent=2) + '\n')
    print(json.dumps(receipt))


if __name__ == '__main__':
    main()