#!/bin/env python3
from time import time
start = time()
from sys import argv
from pathlib import Path
from math import log10
from . import get_steps
from .util import die
usage = f'''
usage: {argv[0]} <SRCDIR> <OUTDIR>
recurses into SRCDIR, parsing each .gloss file as YAML and running the
requested parser. if a directory does not contain a '.gloss' file, or
said file's 'mode' key is missing, the mode defaults to 'copy'.
.gloss may include the following fields:
mode: sets the mode. may be one of 'copy', 'ignore', or
'gloss'. if mode is 'ignore', the directory and all
subdirectories will be skipped.
out: sets the output directory, relative to OUTDIR. if
omitted, it defaults to the current path relative to
SRCDIR.
'''[1:]
argv = argv[1:]
if len(argv) != 2 or argv[0][0] == '-' or argv[1][0] == '-':
die(usage)
srcdir = Path(argv[0]).absolute()
outdir = Path(argv[1]).absolute()
print('collecting steps...')
steps = get_steps(srcdir, outdir)
numlen = int(log10(len(steps))+1)
nfmt = f'[{{0:{numlen}}}/{len(steps)}]' # calculates the correct width for e.g. [ 5/52]
sfmt = f'<{{0:^{numlen*2+1}}}> {{1}}' # and for the status, e.g. < END >
print(sfmt.format('...', 'testing!'))
for n, step in enumerate(steps):
print(f'{nfmt} testing {{1}}'.format(n+1, Path('/', step.reldir)))
step.test()
print(sfmt.format('...', 'building!'))
for n, step in enumerate(steps):
print(f'{nfmt} building {{1}}'.format(n+1, Path('/', step.reldir)))
step.exec(sfmt)
stop = time()
print(sfmt.format('END', f'build complete in {stop-start:.2}s! :3'))