Publish AI workspace source through the isolated runtime
This commit is contained in:
1 parent
03169bd0f6
commit
d1d14f0f77
21 files changed
+487
-68
No files matched your search
@@ -32,16 +32,22 @@ def review_fingerprint(files):
|
||||
return hashlib.sha256(json.dumps(files, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()).hexdigest()
|
||||
|
||||
|
||||
def review_export(root):
|
||||
def review_export(root, release=False):
|
||||
files, samples, total, sample_chars = [], [], 0, 0
|
||||
excluded = {'.npm', '.cache', '.pytest_cache', '.turbo', '.output', '.nuxt'}
|
||||
def excluded_path(path):
|
||||
return review_secret(path) or (release and (
|
||||
any(part in excluded for part in path.parts) or path.parts[0] == 'data'
|
||||
or path.suffix.lower() in {'.db', '.sqlite', '.sqlite3'}
|
||||
))
|
||||
archive = io.BytesIO()
|
||||
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as output:
|
||||
for directory, dirs, names in os.walk(root, followlinks=False):
|
||||
dirs[:] = sorted(d for d in dirs if not review_secret(pathlib.Path(d)) and not (pathlib.Path(directory) / d).is_symlink())
|
||||
dirs[:] = sorted(d for d in dirs if not excluded_path(pathlib.Path(d)) and not (pathlib.Path(directory) / d).is_symlink())
|
||||
for name in sorted(names):
|
||||
file = pathlib.Path(directory) / name
|
||||
rel = file.relative_to(root)
|
||||
if review_secret(rel) or file.is_symlink() or not file.is_file():
|
||||
if excluded_path(rel) or file.is_symlink() or not file.is_file():
|
||||
continue
|
||||
size = file.stat().st_size
|
||||
if size > 10_000_000 or total + size > 10_000_000 or len(files) >= 2000:
|
||||
@@ -53,7 +59,7 @@ def review_export(root):
|
||||
raise ValueError('Review snapshot exceeds size limit')
|
||||
files.append({'path': rel.as_posix(), 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest()})
|
||||
output.writestr(rel.as_posix(), data)
|
||||
if file.suffix.lower() in REVIEW_TEXT and file.name not in {'package-lock.json', 'yarn.lock'} and sample_chars < 14000:
|
||||
if not release and file.suffix.lower() in REVIEW_TEXT and file.name not in {'package-lock.json', 'yarn.lock'} and sample_chars < 14000:
|
||||
text = data[:64000].decode('utf-8', 'replace')
|
||||
shown = review_redact(text[:min(7000, 14000 - sample_chars)])
|
||||
samples.append({'path': rel.as_posix(), 'content': shown, 'truncated': len(shown) < len(text)})
|
||||
@@ -65,6 +71,41 @@ def review_export(root):
|
||||
'complete': True, 'totalBytes': total}
|
||||
|
||||
|
||||
def release_snapshot(root):
|
||||
snapshot = review_export(root, release=True)
|
||||
data = base64.b64decode(snapshot['archive'])
|
||||
if len(data) > 4_000_000:
|
||||
raise ValueError('Source ZIP exceeds 4 MB')
|
||||
with zipfile.ZipFile(io.BytesIO(data)) as archive:
|
||||
names = set(archive.namelist())
|
||||
package = {}
|
||||
if 'package.json' in names:
|
||||
package = json.loads(archive.read('package.json'))
|
||||
if not isinstance(package, dict) or not isinstance(package.get('scripts', {}), dict):
|
||||
raise ValueError('package.json scripts must be an object')
|
||||
scripts = package.get('scripts', {})
|
||||
start = str(scripts.get('start', '')).strip()
|
||||
build = isinstance(scripts.get('build'), str) and bool(scripts['build'].strip())
|
||||
install = bool(package.get('dependencies') or package.get('devDependencies')
|
||||
or package.get('optionalDependencies'))
|
||||
if start:
|
||||
runtime = 'NODE'
|
||||
simple = re.fullmatch(r'node\s+(?:\./)?([\w./-]+\.(?:mjs|cjs|js))', start)
|
||||
if simple and not build and simple.group(1) not in names:
|
||||
raise ValueError('Start file missing: ' + simple.group(1))
|
||||
elif build:
|
||||
runtime = 'STATIC_BUILD'
|
||||
elif 'index.html' in names:
|
||||
runtime = 'STATIC'
|
||||
install = False
|
||||
else:
|
||||
raise ValueError('Source needs package.json scripts.start, scripts.build, or index.html')
|
||||
if install and not ({'package-lock.json', 'npm-shrinkwrap.json'} & names):
|
||||
raise ValueError('Dependencies require package-lock.json or npm-shrinkwrap.json')
|
||||
return {'archive': snapshot['archive'], 'sha256': snapshot['archiveSha256'],
|
||||
'runtime': runtime, 'install': install, 'build': build}
|
||||
|
||||
|
||||
def review_import(root, values):
|
||||
if any(root.iterdir()):
|
||||
raise ValueError('Review workspace must be empty; snapshots cannot overwrite existing data')
|
||||
|
||||
Reference in new issue
Block a user