|
| 1 | +import functools |
| 2 | +import os |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import utilsd.fileio |
| 7 | +from .utils import find_secret_file, run_command |
| 8 | + |
| 9 | + |
| 10 | +print = functools.partial(print, flush=True) |
| 11 | + |
| 12 | + |
| 13 | +@click.group() |
| 14 | +def main(): |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +@main.command() |
| 19 | +@click.argument('geo') |
| 20 | +@click.argument('dest') |
| 21 | +@click.argument('partition') |
| 22 | +@click.option('--delete/--no-delete', help='Sync latest deletions.', default=False) |
| 23 | +def download(geo, dest, partition, delete): |
| 24 | + """ |
| 25 | + Download PARTITION sub-folder from storage and store it under DEST. |
| 26 | + """ |
| 27 | + geo_lib = utilsd.fileio.load(find_secret_file('storage.json')) |
| 28 | + assert geo in geo_lib, f'{geo} not found in {geo_lib.keys()}' |
| 29 | + assert '/' not in partition and '..' not in partition, 'Illegal partition name.' |
| 30 | + geo = geo_lib[geo] |
| 31 | + dest_folder = os.path.join(dest, partition) |
| 32 | + os.makedirs(dest_folder, exist_ok=True) |
| 33 | + run_command('azcopy sync "{}/{}/{}{}" "{}" --delete-destination={}'.format( |
| 34 | + geo['host'], geo['default_container'], partition, geo['secret'][geo['default_container']], |
| 35 | + dest_folder, 'true' if delete else 'false')) |
| 36 | + |
| 37 | + |
| 38 | +@main.command() |
| 39 | +@click.argument('geo') |
| 40 | +@click.argument('src') |
| 41 | +@click.argument('partition') |
| 42 | +@click.option('--delete/--no-delete', help='Sync latest deletions.', default=False) |
| 43 | +def upload(geo, src, partition, delete): |
| 44 | + """ |
| 45 | + Copy PARTITION sub-folder in SRC dir to the storage. |
| 46 | + """ |
| 47 | + geo_lib = utilsd.fileio.load(find_secret_file('storage.json')) |
| 48 | + assert geo in geo_lib, f'{geo} not found in {geo_lib.keys()}' |
| 49 | + assert '/' not in partition and '..' not in partition, 'Illegal partition name.' |
| 50 | + geo = geo_lib[geo] |
| 51 | + source_folder = os.path.join(src, partition) |
| 52 | + assert os.path.exists(source_folder), f'Source folder {source_folder} does not exist!' |
| 53 | + run_command('azcopy sync "{}" "{}/{}/{}{}" --delete-destination={}'.format( |
| 54 | + source_folder, geo['host'], geo['default_container'], partition, geo['secret'][geo['default_container']], |
| 55 | + 'true' if delete else 'false')) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + main() |
0 commit comments