"""Create a new work with portable input paths, without generating any images.""" from pathlib import Path import argparse,json,os,re,sys WORKSPACE=Path(__file__).resolve().parents[1] def supplied(value): path=Path(value) if not path.is_absolute():path=WORKSPACE/path path=path.resolve(strict=True) if not path.is_file():raise ValueError('Input must be a file: '+str(path)) if not path.is_relative_to(WORKSPACE/'input'): raise ValueError('Place supplied original materials inside this project input/ first.') return path def main(): p=argparse.ArgumentParser(description=__doc__) p.add_argument('name');p.add_argument('--source',required=True) p.add_argument('--design',action='append',required=True) p.add_argument('--reference',action='append',default=[]) args=p.parse_args() if (args.name in ('.','..') or not args.name.strip() or len(args.name)>80 or re.search(r'[\x00-\x1f<>:"/\\|?*]',args.name) or args.name.endswith(('.', ' ')) or re.fullmatch(r'(?i:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\..*)?',args.name)): p.error('Use a work name without path separators or reserved device names.') root=(WORKSPACE/'output'/args.name).resolve() if root.parent!=(WORKSPACE/'output').resolve():p.error('Invalid output location.') try: source=supplied(args.source);designs=[supplied(s) for s in args.design] refs=[supplied(s) for s in args.reference] if len(set(designs))!=len(designs):raise ValueError('Duplicate design input.') config={'source':os.path.relpath(source,root).replace('\\','/'), 'designs':[os.path.relpath(s,root).replace('\\','/') for s in designs], 'judgmentMaterials':[os.path.relpath(s,root).replace('\\','/') for s in refs]} root.mkdir(parents=True,exist_ok=False) for folder in ('page-specs','candidates','records','pages'):(root/folder).mkdir() (root/'production-inputs.json').write_text(json.dumps(config,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') except (OSError,ValueError) as e:p.exit(1,str(e)+'\n') print(json.dumps({'work':root.relative_to(WORKSPACE).as_posix(),'config':config, 'imagesGenerated':False},ensure_ascii=False)) if __name__=='__main__': sys.stdout.reconfigure(encoding='utf-8') main()