Skip to content

Commit 5556550

Browse files
refactor: utils/prepare/upload.py (#1607)
Co-authored-by: Fedor Ignatov <[email protected]>
1 parent c1c1441 commit 5556550

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

utils/prepare/upload.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# limitations under the License.
1414

1515
import argparse
16-
import os
17-
import shutil
16+
import pathlib
1817
import tarfile
1918
from pathlib import Path
2019

@@ -23,45 +22,48 @@
2322
from hashes import main
2423

2524

26-
def upload(config_in_file):
25+
def upload(config_in_file: str, tar_name: str, tar_output_dir: Path):
26+
if not tar_output_dir.exists():
27+
raise RuntimeError(f'A folder {tar_output_dir} does not exist')
28+
29+
print(f'Config: {config_in_file}')
30+
if not Path(config_in_file).exists():
31+
raise RuntimeError(f'A config {config_in_file} does not exist')
2732

28-
print(config_in_file)
2933
config_in = parse_config(config_in_file)
3034
config_in_file = find_config(config_in_file)
3135

3236
model_path = Path(config_in['metadata']['variables']['MODEL_PATH']).expanduser()
33-
models_path = Path(config_in['metadata']['variables']['MODELS_PATH']).expanduser()
3437
model_name, class_name = config_in_file.stem, config_in_file.parent.name
35-
36-
if str(model_name) not in str(model_path):
37-
raise(f'{model_name} is not the path of the {model_path}')
38-
39-
arcname = str(model_path).split("models/")[1]
40-
tar_path = models_path/model_name
41-
tmp_folder = f'/tmp/'
42-
tmp_tar = tmp_folder + f'{model_name}.tar.gz'
4338

44-
print("model_path", model_path)
45-
print("class_name", class_name)
46-
print("model_name", model_name)
47-
48-
print("Start tarring")
49-
archive = tarfile.open(tmp_tar, "w|gz")
50-
archive.add(model_path, arcname=arcname)
51-
archive.close()
39+
if tar_name is None:
40+
tar_name = f'{model_name}'
41+
print(f'tar_name set to {tar_name}')
42+
43+
full_tar_name = tar_output_dir / f'{tar_name}.tar.gz'
44+
if Path(full_tar_name).exists():
45+
raise RuntimeError(f'An archive {Path(full_tar_name)} already exists')
46+
47+
print(f'model_path: {model_path}')
48+
print(f'class_name: {class_name}')
49+
print(f'model_name: {model_name}')
50+
print(f'Start tarring to {full_tar_name}')
51+
with tarfile.open(str(full_tar_name), "w|gz") as archive:
52+
archive.add(model_path, arcname=pathlib.os.sep)
53+
5254
print("Stop tarring")
55+
print(f'Tar archive: {Path(full_tar_name)} has been created')
5356

5457
print("Calculating hash")
55-
main(tmp_tar)
56-
57-
print("tmp_tar", tmp_tar)
58-
command = f'scp -r {tmp_folder}{model_name}* share.ipavlov.mipt.ru:/home/export/v1/{class_name}'
59-
donwload_url = f'http://files.deeppavlov.ai/v1/{class_name}/{model_name}.tar.gz'
60-
print(command, donwload_url, sep='\n')
58+
main(full_tar_name)
6159

6260

6361
if __name__ == '__main__':
6462
parser = argparse.ArgumentParser()
65-
parser.add_argument("config_in", help="path to a config", type=str)
63+
parser.add_argument('-c', '--config_in', help='path to a config', type=str)
64+
parser.add_argument('-n', '--tar_name', help='name of the tar archive (without tar.gz extension)',
65+
default=None, required=False, type=str)
66+
parser.add_argument('-o', '--tar_output_dir', help='dir to save a tar archive', default='./',
67+
required=False, type=Path)
6668
args = parser.parse_args()
67-
upload(args.config_in)
69+
upload(args.config_in, args.tar_name, args.tar_output_dir)

0 commit comments

Comments
 (0)