File size: 5,966 Bytes
7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 850b319 7c94a98 | 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | """Update a dedicated server checkout from the public HF source channel, fast-forward only."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
REPO = 'codemaivanngu/apsp-lite-b200-cu128'
GITHUB = 'https://github.com/NguyenNgocMinh30012005/DMRL.git'
BRANCH = 'apsp/lite2'
def git(root, *args):
return subprocess.check_output(['git', *args], cwd=root, text=True).strip()
def download(url, target, limit):
proxy = os.environ.get('PROXY') or os.environ.get('HTTPS_PROXY') or os.environ.get('HTTP_PROXY')
if not proxy and os.environ.get('APSP_ALLOW_DIRECT') != '1':
raise ValueError('Proxy env missing; follow PROXY_DOWNLOAD_NOTE.md')
cmd = ['curl', '--silent', '--fail', '--location', '--retry', '3', '--connect-timeout', '20',
'--max-time', '600', '--max-filesize', str(limit)]
cmd += ['--proxy', proxy, '--noproxy', ''] if proxy else ['--noproxy', '*']
result = subprocess.run([*cmd, url, '-o', str(target)], stderr=subprocess.DEVNULL)
if result.returncode:
raise RuntimeError('Source download failed: curl rc=' + str(result.returncode))
def verify_checkout(target):
if target.is_symlink() or Path(git(target, 'rev-parse', '--show-toplevel')).resolve() != target.resolve():
raise ValueError('Expected dedicated non-symlink source checkout')
if git(target, 'branch', '--show-current') != BRANCH or git(target, 'status', '--porcelain'):
raise ValueError('Dirty or non-' + BRANCH + ' checkout preserved; use a dedicated checkout')
if git(target, 'remote', 'get-url', 'origin') != GITHUB:
raise ValueError('Unexpected checkout origin preserved; use a dedicated DMRL checkout')
def apply_bundle(bundle, manifest, target):
"""No resets: preserve dirty/diverged state and reject unexpected bundle refs."""
head = manifest['source_commit']
if manifest['source_branch'] != BRANCH or not re.fullmatch('[0-9a-f]{40}', head):
raise ValueError('Unexpected source identity')
if manifest['github'] != GITHUB or manifest['bundle_file'] != 'apsp.bundle':
raise ValueError('Unexpected distribution identity')
if bundle.stat().st_size != manifest['bundle_bytes'] or hashlib.sha256(bundle.read_bytes()).hexdigest() != manifest['bundle_sha256']:
raise ValueError('Bundle hash/size mismatch')
ref = 'refs/heads/' + BRANCH
if git(target.parent, 'bundle', 'list-heads', str(bundle)) != head + ' ' + ref:
raise ValueError('Bundle ref mismatch')
if target.exists():
verify_checkout(target)
git(target, 'bundle', 'verify', str(bundle))
git(target, 'fetch', '--no-tags', str(bundle), ref)
if git(target, 'rev-parse', 'FETCH_HEAD') != head:
raise ValueError('Fetched source mismatch')
if subprocess.run(['git', 'merge-base', '--is-ancestor', 'HEAD', head], cwd=target).returncode:
raise ValueError('Diverged or older source; checkout preserved')
git(target, 'merge', '--ff-only', head)
else:
# Clone beside destination, then publish atomically; keep failed staging for diagnosis.
stage = Path(tempfile.mkdtemp(prefix='.'+target.name+'-clone-', dir=target.parent))/'checkout'
git(target.parent, 'clone', '--branch', BRANCH, str(bundle), str(stage))
git(stage, 'remote', 'set-url', 'origin', GITHUB)
if git(stage, 'rev-parse', 'HEAD') != head:
raise ValueError('Clone source mismatch')
stage.rename(target)
stage.parent.rmdir()
verify_checkout(target)
if git(target, 'rev-parse', 'HEAD') != head:
raise ValueError('Final source mismatch')
return head
def main():
import fcntl # server runs Linux; no dependency installation needed
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--target', type=Path, default=Path('/workspace/storage-shared/nlp/tungks/DMRL-apsp-lite2'))
parser.add_argument('--revision', help='Immutable HF commit; omitted resolves main once, then pins all reads')
args = parser.parse_args()
target = args.target.absolute()
if target.is_symlink() or target.name in ('', '.', '..') or target == Path(target.anchor):
raise ValueError('Unsafe checkout destination')
target.parent.mkdir(parents=True, exist_ok=True)
with (target.parent/('.'+target.name+'.source-update.lock')).open('a+') as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
if target.exists():
verify_checkout(target)
work = Path(tempfile.mkdtemp(prefix='apsp-source-', dir=target.parent))
revision = args.revision
if revision is None:
download(f'https://huggingface.co/api/datasets/{REPO}/revision/main', work/'head.json', 2<<20)
revision = json.loads((work/'head.json').read_text())['sha']
if not re.fullmatch('[0-9a-f]{40}', revision):
raise ValueError('HF revision must be an immutable commit')
base = f'https://huggingface.co/datasets/{REPO}/resolve/{revision}/source/'
download(base+'source-manifest.json', work/'source-manifest.json', 2<<20)
manifest = json.loads((work/'source-manifest.json').read_text())
if not isinstance(manifest['bundle_bytes'], int) or not 0 < manifest['bundle_bytes'] <= 128<<20:
raise ValueError('Source bundle exceeds download bound')
download(base+'apsp.bundle', work/'apsp.bundle', 128<<20)
head = apply_bundle(work/'apsp.bundle', manifest, target)
receipt = {'pass': True, 'source_commit': head, 'source_branch': BRANCH,
'github': GITHUB, 'hf_revision': revision,
'target': str(target), 'bundle_sha256': manifest['bundle_sha256']}
(work/'receipt.json').write_text(json.dumps(receipt, indent=2)+'\n')
print('UPDATE_PASS ' + json.dumps(receipt))
if __name__ == '__main__':
main()
|