文件
dayu-color/tools/deliver.py
T

211 行
14 KiB
Python

#!/usr/bin/env python3
"""Single-install Release delivery; keeps a product lock through host acceptance."""
from pathlib import Path
import subprocess,os,json,shutil,plistlib,uuid,sys,datetime,hashlib,time,argparse,tempfile,re
from template_payload import template_files
ROOT=Path(__file__).resolve().parent.parent
CACHE=Path('/Users/mac/Library/Caches/CodexBuilds/com.dayu.color')
LOCK=Path('/Users/mac/.codex/app-delivery-locks/com.dayu.color.lock')
DEST=Path('/Library/Plug-Ins/FxPlug/大鱼调色.app')
APP=CACHE/'DerivedData/Build/Products/Release/大鱼调色.app'
LOOSE=CACHE/'DerivedData/Build/Products/Release/大鱼调色 XPC Service.pluginkit'
PLUGIN='Contents/PlugIns/大鱼调色 XPC Service.pluginkit'
TEMPLATE_ROOT=Path.home()/'Movies/Motion Templates.localized/Effects.localized/大鱼调色'
TEMPLATE=TEMPLATE_ROOT/'大鱼调色/大鱼调色.moef'
BUNDLED_TEMPLATES=Path('Contents/Resources/FCP Templates')
IDENTITY='Developer ID Application: zhong luo (B3W25FX5H5)'
PROFILE='codex-dayu-notary-B3W25FX5H5'
KEYCHAIN='/Users/mac/Library/Keychains/login.keychain-db'
def run(*args,log=None):
if log:
with Path(log).open('w') as f:subprocess.run(args,cwd=ROOT,stdout=f,stderr=subprocess.STDOUT,check=True)
return ''
return subprocess.check_output(args,cwd=ROOT,text=True,stderr=subprocess.STDOUT).strip()
def hosts_closed(wait=False):
announced=False
while True:
lines=run('ps','-axo','pid=,comm=').splitlines()
live=[l for l in lines if l.endswith(('/Contents/MacOS/Final Cut Pro','/Contents/MacOS/Motion'))]
if not live:return
if not wait:raise RuntimeError('请先正常退出宿主,再继续交付:'+str(live))
if not announced:print('Notarized APP retained; waiting for the host to save and quit normally before replacement.',flush=True);announced=True
time.sleep(1)
def identity(path,expected):
p=plistlib.loads((path/'Contents/Info.plist').read_bytes())
if p['CFBundleIdentifier']!=expected:raise RuntimeError('产品身份不匹配: '+str(path))
def unregister(path):
reg=run('pluginkit','-m','-A','-D','-v','-i','com.dayu.color.XPC')
if any(l.rstrip().endswith('\t'+str(path)) for l in reg.splitlines()):run('pluginkit','-r',str(path))
def self_test(executable,report):
result=subprocess.run([str(executable),'--self-test'],cwd=ROOT,text=True,capture_output=True,env=dict(os.environ,DY_DIAGNOSTIC_FACE_FIXTURE=str(ROOT/'tools/fixtures/astronaut.png')))
report.write_text(result.stdout)
report.with_suffix('.stderr').write_text(result.stderr)
try:parsed=json.loads(result.stdout)
except json.JSONDecodeError:raise RuntimeError('原生自检未返回有效结果,诊断已保留:'+str(report))
if result.returncode or not parsed.get('pass'):
failed=[x.get('name') for x in parsed.get('results',[]) if not x.get('pass',True)]
raise RuntimeError('原生自检未通过,正式旧版未替换。诊断:'+str(report)+';失败项:'+str(failed))
return parsed
def install_templates():
# Keep the previous Motion Templates files available until the new set is
# verified. Stage each replacement on the same volume as the destination.
changed=[]
hashes={}
try:
for _,bundled,installed in template_files():
source=DEST/BUNDLED_TEMPLATES/bundled
target=TEMPLATE_ROOT/installed
target.parent.mkdir(parents=True,exist_ok=True)
previous=(target.read_bytes(),target.stat().st_mode & 0o777,target.stat().st_flags) if target.exists() else None
data=source.read_bytes()
changed.append((target,previous))
write_template_file(target,data,0o644,2 if target.suffix=='.png' else 0)
assert target.read_bytes()==data
hashes[str(installed)]=hashlib.sha256(data).hexdigest()
except Exception:
for target,previous in reversed(changed):
if previous is not None:
write_template_file(target,*previous)
elif target.exists():
os.chflags(target,0)
target.unlink()
raise
return hashes
def write_template_file(target,data,mode,flags):
temp=target.with_name('.dayu-color-stage-'+uuid.uuid4().hex)
try:
temp.write_bytes(data)
temp.chmod(mode)
if target.exists() and target.stat().st_flags:os.chflags(target,0)
os.replace(temp,target)
if flags:os.chflags(target,flags)
finally:
temp.unlink(missing_ok=True)
def resume_notary(submission,archive,expected_hash,source):
if not re.fullmatch(r'[0-9a-f]{40}',source or ''):raise RuntimeError('恢复须提供原始完整编译提交')
if not re.fullmatch(r'[0-9a-f]{64}',expected_hash or '') or hashlib.sha256(archive.read_bytes()).hexdigest()!=expected_hash:raise RuntimeError('公证提交 ZIP 摘要不匹配,停止恢复')
run('git','cat-file','-e',source+'^{commit}');identity(APP,'com.dayu.color');run('codesign','--verify','--deep','--strict',str(APP))
# Validate the exact submitted bundle, including symlinks; never rebuild or re-sign.
with tempfile.TemporaryDirectory(prefix='notary-resume-',dir=CACHE) as scratch:
run('ditto','-x','-k',str(archive),scratch)
submitted=Path(scratch)/APP.name;identity(submitted,'com.dayu.color')
live={str(p.relative_to(APP)) for p in APP.rglob('*')};saved={str(p.relative_to(submitted)) for p in submitted.rglob('*')}
if live!=saved:raise RuntimeError('待安装 APP 与已提交 ZIP 内容清单不同')
for relative in live:
a=APP/relative;b=submitted/relative
if a.is_symlink() or b.is_symlink():same=a.is_symlink() and b.is_symlink() and os.readlink(a)==os.readlink(b)
elif a.is_dir() or b.is_dir():same=a.is_dir() and b.is_dir()
else:same=a.read_bytes()==b.read_bytes()
if not same:raise RuntimeError('待安装 APP 与已提交 ZIP 不一致:'+relative)
unregister(submitted/PLUGIN)
preflight=json.loads((CACHE/'release-preflight.json').read_text())
if not preflight.get('pass'):raise RuntimeError('缺少通过的原始发布前检查')
print('Exact submitted APP verified; querying original Apple submission '+submission,flush=True)
failures=0
while True:
try:
result=json.loads(run('xcrun','notarytool','info',submission,'--keychain-profile',PROFILE,'--keychain',KEYCHAIN,'--output-format','json'));failures=0
except subprocess.CalledProcessError:
failures+=1
if failures>=3:raise RuntimeError('连续三次无法读取 Apple 公证状态;产物保留,可继续原提交恢复')
time.sleep(15);continue
if result.get('id')!=submission:raise RuntimeError('公证编号不匹配')
if result.get('status')!='In Progress':return result
time.sleep(15)
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--resume-notary');parser.add_argument('--submitted-zip-sha256');parser.add_argument('--compiled-source')
args=parser.parse_args()
if not args.resume_notary and (args.submitted_zip_sha256 or args.compiled_source):parser.error('摘要与编译提交仅用于恢复原公证提交')
hosts_closed();CACHE.mkdir(parents=True,exist_ok=True);LOCK.parent.mkdir(parents=True,exist_ok=True);LOCK.mkdir()
(LOCK/'owner.json').write_text(json.dumps(dict(PID=os.getpid(),cwd=str(ROOT),product='大鱼调色',started=datetime.datetime.now(datetime.timezone.utc).isoformat())))
old=None;stage=None
try:
ids=run('security','find-identity','-v','-p','codesigning')
if IDENTITY not in ids:raise RuntimeError('有效发布证书或私钥不可用')
if any(l.endswith('/Contents/MacOS/大鱼调色') for l in run('ps','-axo','comm=').splitlines()):raise RuntimeError('请先正常退出当前大鱼调色窗口')
archive=CACHE/'notarization-submission.zip'
if args.resume_notary:
result=resume_notary(args.resume_notary,archive,args.submitted_zip_sha256,args.compiled_source)
(CACHE/'notary-result.json').write_text(json.dumps(result,indent=2))
else:
print('Building final Release',flush=True)
run('xcodebuild','-project','大鱼调色.xcodeproj','-scheme','Wrapper Application','-configuration','Release','-enableAddressSanitizer','NO','-derivedDataPath',str(CACHE/'DerivedData'),'clean','build',log=CACHE/'release-build.log')
for source,bundled,_ in template_files():
target=APP/BUNDLED_TEMPLATES/bundled
target.parent.mkdir(parents=True,exist_ok=True)
shutil.copyfile(ROOT/'template'/source,target)
target.chmod(0o644)
# Sources on exFAT can be exposed as owner-only. Installer changes ownership to root.
(APP/'Contents/Resources/Basis_balanceWheel.png').chmod(0o644)
identity(APP,'com.dayu.color');plugin=APP/PLUGIN;identity(plugin,'com.dayu.color.XPC')
# Xcode registers its build product with Launch Services. Keep the host from
# discovering this generated copy while signing/notarization are pending.
unregister(plugin);unregister(LOOSE)
for name in ['FxPlug','PluginManager']:run('codesign','--force','--timestamp','--options','runtime','--sign',IDENTITY,str(plugin/'Contents/Frameworks'/(name+'.framework')))
run('codesign','--force','--timestamp','--options','runtime','--sign',IDENTITY,str(plugin))
run('codesign','--force','--timestamp','--options','runtime','--entitlements',str(ROOT/'大鱼调色/Wrapper Application/SandboxEntitlements.entitlements'),'--sign',IDENTITY,str(APP))
run('codesign','--verify','--deep','--strict',str(APP))
details=run('codesign','-dvvv',str(APP))
if 'TeamIdentifier=B3W25FX5H5' not in details or 'Timestamp=' not in details or 'runtime' not in details:raise RuntimeError('签名身份/时间戳/runtime未通过')
preflight=self_test(plugin/'Contents/MacOS/大鱼调色 XPC Service',CACHE/'release-preflight.json')
(CACHE/'release-preflight.json').write_text(json.dumps(preflight,ensure_ascii=False,indent=2)+'\n')
if not preflight.get('pass'):raise RuntimeError('Release集成自检失败,正式旧版未替换')
archive=CACHE/'notarization-submission.zip'
if archive.exists():archive.unlink()
run('ditto','-c','-k','--keepParent',str(APP),str(archive));print('Submitting signed Release to Apple',flush=True)
result=json.loads(run('xcrun','notarytool','submit',str(archive),'--keychain-profile',PROFILE,'--keychain',KEYCHAIN,'--wait','--output-format','json'));(CACHE/'notary-result.json').write_text(json.dumps(result,indent=2))
if result.get('status')!='Accepted':
if result.get('id'):run('xcrun','notarytool','log',result['id'],'--keychain-profile',PROFILE,'--keychain',KEYCHAIN,str(CACHE/'notary-rejection.json'))
raise RuntimeError('Apple未Accepted,正式旧版未替换')
run('xcrun','stapler','staple',str(APP));run('xcrun','stapler','validate',str(APP));hosts_closed(wait=True)
stage=DEST.parent/('.dayu-color-staging-'+uuid.uuid4().hex+'.app');run('ditto',str(APP),str(stage))
if DEST.exists():
identity(DEST,'com.dayu.color');old=DEST.parent/('.dayu-color-previous-'+uuid.uuid4().hex+'.app');os.replace(DEST,old)
os.replace(stage,DEST);stage=None
try:
identity(DEST,'com.dayu.color');run('codesign','--verify','--deep','--strict',str(DEST));run('xcrun','stapler','validate',str(DEST));gate=run('spctl','--assess','--type','execute','--verbose=2',str(DEST))
installedTest=self_test(DEST/PLUGIN/'Contents/MacOS/大鱼调色 XPC Service',CACHE/'installed-self-test.json')
if not installedTest.get('pass'):raise RuntimeError('正式安装版原生集成自检失败')
(ROOT/'verification/InstalledSelfTest.json').write_text(json.dumps(installedTest,ensure_ascii=False,indent=2)+'\n')
run('open',str(DEST))
for attempt in range(20):
if str(DEST/'Contents/MacOS/大鱼调色') in run('ps','-axo','comm=').splitlines():break
time.sleep(.25)
else:raise RuntimeError('未从正式安装路径启动')
template_hashes=install_templates()
except BaseException:
failed=DEST.parent/('.dayu-color-failed-'+uuid.uuid4().hex+'.app');os.replace(DEST,failed)
if old:os.replace(old,DEST);old=None
raise
# All generated copies are removed before final registry/host verification.
for copy,expected in [(APP,'com.dayu.color'),(LOOSE,'com.dayu.color.XPC')]:
if copy.exists():identity(copy,expected);unregister(copy/PLUGIN if copy.suffix=='.app' else copy);shutil.rmtree(copy)
if old:unregister(old/PLUGIN)
archive.unlink()
# Unregistering an old copy can race the host's launch registration. Re-add
# only the verified installed path; never repeat signing/notarization/install.
for attempt in range(3):
run('pluginkit','-a',str(DEST/PLUGIN));registry=run('pluginkit','-m','-A','-D','-v','-i','com.dayu.color.XPC')
entries=[l for l in registry.splitlines() if 'com.dayu.color.XPC(' in l]
if entries:break
time.sleep(.5)
if len(entries)!=1 or not entries[0].rstrip().endswith('\t'+str(DEST/PLUGIN)):raise RuntimeError('PluginKit发现多份/错误版本: '+registry)
record=dict(status='installed-awaiting-host-verification',path=str(DEST),bundleID='com.dayu.color',version=plistlib.loads((DEST/'Contents/Info.plist').read_bytes())['CFBundleShortVersionString'],build=plistlib.loads((DEST/'Contents/Info.plist').read_bytes())['CFBundleVersion'],team='B3W25FX5H5',notaryID=result['id'],notaryStatus=result['status'],gatekeeper=gate,sourceCommit=args.compiled_source or run('git','rev-parse','HEAD'),deliveryToolCommit=run('git','rev-parse','HEAD'),templatePath=str(TEMPLATE),templateSHA256=template_hashes[str(TEMPLATE.relative_to(TEMPLATE_ROOT))],installedTemplateSHA256=template_hashes,shaderSHA256=hashlib.sha256((DEST/PLUGIN/'Contents/Resources/default.metallib').read_bytes()).hexdigest())
if old:record['previousInstallBackup']=str(old)
output=ROOT/'verification/Delivery.json';output.write_text(json.dumps(record,ensure_ascii=False,indent=2)+'\n')
run('open',str(DEST));print('FORMAL_INSTALLED. Run installed self-test and host verification; type complete only after success. Type leave to preserve pending state.',flush=True)
for decision in sys.stdin:
if decision.strip()=='complete':
if old and old.exists():identity(old,'com.dayu.color');unregister(old/PLUGIN);shutil.rmtree(old);old=None
record.pop('previousInstallBackup',None);record['status']='verified';output.write_text(json.dumps(record,ensure_ascii=False,indent=2)+'\n');break
if decision.strip()=='leave':break
finally:
if stage and stage.exists():shutil.rmtree(stage)
(LOCK/'owner.json').unlink();LOCK.rmdir()
if __name__=='__main__':main()