Skip to content

Commit 83c9147

Browse files
committed
Merge branch 'azcli'
2 parents 2cb13ff + c3cf20e commit 83c9147

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

setup.py

+5
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ def get_version(rel_path):
4141
extras_require={
4242
'full': ['torch>=1.7.1'],
4343
'docs': ['sphinx'],
44+
},
45+
entry_points={
46+
'console_scripts': [
47+
'uazcli = utilsd.az.cli:main',
48+
]
4449
}
4550
)

utilsd/az/__init__.py

Whitespace-only changes.

utilsd/az/cli.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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()

utilsd/az/utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
5+
6+
def find_secret_file(rel_path) -> Path:
7+
paths = [Path('~/.secrets').expanduser() / rel_path]
8+
current_dir = Path.cwd()
9+
while not current_dir.parent == current_dir:
10+
paths.append(current_dir / '.secrets' / rel_path)
11+
current_dir = current_dir.parent
12+
for path in paths:
13+
if path.exists():
14+
return path
15+
raise FileNotFoundError(f'Secret key "{rel_path}" not found.')
16+
17+
18+
def run_command(command):
19+
print('+', command)
20+
if os.system(command) != 0:
21+
sys.exit(1)

0 commit comments

Comments
 (0)