50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--config', required=True)
|
|
parser.add_argument('--job', required=True)
|
|
args = parser.parse_args()
|
|
from config import load_config
|
|
config = load_config(args.config)
|
|
job = json.loads(Path(args.job).read_text())
|
|
artifact_root = Path(job['directory']) / 'artifacts'
|
|
runtime = Path(config['_paths']['runtime']) / 'python'
|
|
sys.path.insert(0, str(runtime))
|
|
from terrain import generate_planet
|
|
from parameters import validate_spec
|
|
generated = generate_planet(job['spec'], config, artifact_root / 'planet-work')
|
|
validated = validate_spec(generated['spec'], config)
|
|
if not validated['ok']:
|
|
raise ValueError('Generated planet spec failed validation: ' + json.dumps(validated['errors'], sort_keys=True))
|
|
dataset = generated['dataset']
|
|
dataset_id = dataset['id']
|
|
source = artifact_root / 'planet-work' / 'preview'
|
|
destination = artifact_root / 'datasets' / dataset_id
|
|
destination.mkdir(parents=True, exist_ok=True)
|
|
for item in source.iterdir():
|
|
if item.is_file():
|
|
os.replace(item, destination / item.name)
|
|
index = json.loads((destination / 'index.json').read_text())
|
|
if index['id'] != dataset_id:
|
|
raise ValueError('Dataset ID does not match its output directory.')
|
|
manifest = json.loads((artifact_root / 'planet-work' / 'planet.json').read_text())
|
|
manifest['recipe'] = validated['spec']
|
|
manifest['surface_files'] = {key: 'planet-work/' + name for key, name in manifest['surface_files'].items()}
|
|
(artifact_root / 'planet.json').write_text(json.dumps(manifest, sort_keys=True))
|
|
used = sum(path.stat().st_size for path in artifact_root.rglob('*') if path.is_file())
|
|
if used > config['max_result_bytes']:
|
|
raise ValueError('Generated output exceeds the configured storage limit.')
|
|
print(json.dumps({'planet': True, 'spec': validated['spec'], 'dataset': dataset_id,
|
|
'cycle': 1, 'cycles': 1}), flush=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|