|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import argparse
|
16 |
| -import os |
17 |
| -import shutil |
| 16 | +import pathlib |
18 | 17 | import tarfile
|
19 | 18 | from pathlib import Path
|
20 | 19 |
|
|
23 | 22 | from hashes import main
|
24 | 23 |
|
25 | 24 |
|
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') |
27 | 32 |
|
28 |
| - print(config_in_file) |
29 | 33 | config_in = parse_config(config_in_file)
|
30 | 34 | config_in_file = find_config(config_in_file)
|
31 | 35 |
|
32 | 36 | model_path = Path(config_in['metadata']['variables']['MODEL_PATH']).expanduser()
|
33 |
| - models_path = Path(config_in['metadata']['variables']['MODELS_PATH']).expanduser() |
34 | 37 | 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' |
43 | 38 |
|
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 | + |
52 | 54 | print("Stop tarring")
|
| 55 | + print(f'Tar archive: {Path(full_tar_name)} has been created') |
53 | 56 |
|
54 | 57 | 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) |
61 | 59 |
|
62 | 60 |
|
63 | 61 | if __name__ == '__main__':
|
64 | 62 | 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) |
66 | 68 | args = parser.parse_args()
|
67 |
| - upload(args.config_in) |
| 69 | + upload(args.config_in, args.tar_name, args.tar_output_dir) |
0 commit comments