MC/website/scripts/publish-jarvis-dev-form.py
2026-11-27 23:43:05 +00:00

49 lines
3.2 KiB
Python

"""Publish only the new Dev request form/policy; never change existing tickets."""
import argparse,hashlib,json,os,pathlib,subprocess,tempfile
def patch_view(text):
marker="<?php elseif($view==='new'):?>"
start=text.find(marker)
if start<0:raise ValueError('New-request view marker changed; inspect native view')
end=text.find('<?php endif;?>\n<footer',start)
if end<0:raise ValueError('Native view boundary changed; refusing replacement')
new=marker+"\n<?php require __DIR__.'/dev-jarvis-form.php'; ?>\n"
text=text[:start]+new+text[end:]
css='<link rel="stylesheet" href="/dev-jarvis-form.css?v=jarvis-request-1">'
js='<script src="/dev-jarvis-form.js?v=jarvis-request-1" defer></script>'
if css not in text:text=text.replace('</head>',css+js+'</head>',1)
return text
def atomic(path,data):
original=path.stat() if path.exists() else path.parent.stat()
fd,name=tempfile.mkstemp(prefix='.'+path.name+'.',dir=path.parent)
try:
with os.fdopen(fd,'wb')as stream:stream.write(data)
os.chown(name,original.st_uid,original.st_gid);os.chmod(name,0o644);os.replace(name,path)
finally:
if os.path.exists(name):os.unlink(name)
def main():
p=argparse.ArgumentParser();p.add_argument('--apply',action='store_true');p.add_argument('--root',default='/home/dunerealms/apps/integrations/osticket');p.add_argument('--node',default='/home/dunerealms/.nvm/versions/node/v22.23.3/bin/node');a=p.parse_args()
website=pathlib.Path(__file__).resolve().parents[1];source=website/'integrations/osticket';root=pathlib.Path(a.root).resolve(strict=True)
native=root/'dev-view.php';current=native.read_text();updated=patch_view(current)
catalogue=subprocess.check_output([a.node,str(website/'scripts/export-dev-request-catalogue.mjs')])
json.loads(catalogue)
files={name:(source/name).read_bytes() for name in ['dev-policy.php','dev-jarvis-policy.php','dev-jarvis-form.php','dev-jarvis-form.js','dev-jarvis-form.css']}
files['dev-jarvis-catalogue.json']=catalogue;files['dev-view.php']=updated.encode()
baseline='b99cbcc013172b1b6262cad7d2c6aa11e3847f1d6d48ff3475befc94ab4dbd5b'
current_policy=(root/'dev-policy.php').read_bytes()
if hashlib.sha256(current_policy).hexdigest() not in {baseline,hashlib.sha256(files['dev-policy.php']).hexdigest()}:raise RuntimeError('Installed Dev policy changed; inspect before replacing it')
# Check staged PHP syntax before replacing any native file.
with tempfile.TemporaryDirectory(prefix='dune-jarvis-form-')as stage:
for name,data in files.items():
path=pathlib.Path(stage)/name;path.write_bytes(data)
if name.endswith('.php'):subprocess.run(['/usr/bin/php','-l',str(path)],check=True,capture_output=True)
if a.apply:
if native.read_text()!=current or (root/'dev-policy.php').read_bytes()!=current_policy:raise RuntimeError('Native view or policy changed during publication')
# Shared policy is switched last; existing ticket data is never migrated.
for name,data in files.items():
if name!='dev-policy.php':atomic(root/name,data)
atomic(root/'dev-policy.php',files['dev-policy.php'])
print(json.dumps({'applied':a.apply,'files':list(files),'catalogue_actions':len(json.loads(catalogue)['actions']),'native_view_sha256':hashlib.sha256(updated.encode()).hexdigest(),'existing_tickets_modified':0}))
if __name__=='__main__':main()