|
29 | 29 |
|
30 | 30 | from ftplib import FTP |
31 | 31 | from ftplib import FTP_TLS |
| 32 | +from typing import Optional |
32 | 33 |
|
33 | 34 | from paramiko import SSHClient, SSHException |
34 | 35 | from paramiko import MissingHostKeyPolicy |
@@ -350,7 +351,7 @@ def download(self, location: str): |
350 | 351 | raise NotImplementedError("not supported") |
351 | 352 |
|
352 | 353 | @umask(0o077) |
353 | | - def upload(self, location: str): |
| 354 | + def upload(self, location: str, subdirectory: Optional[str] = None): |
354 | 355 | scheme = self.url.scheme |
355 | 356 | _, _, scheme = scheme.partition("+") |
356 | 357 | netloc = self.url.netloc |
@@ -395,10 +396,17 @@ def upload(self, location: str): |
395 | 396 |
|
396 | 397 | # git add |
397 | 398 | filename = Path(Path(self.url.path).name).stem |
398 | | - dst = path_repository / filename |
| 399 | + if subdirectory: |
| 400 | + subdir_path = Path(subdirectory) |
| 401 | + os.mkdir(path_repository / subdir_path) |
| 402 | + dst = path_repository / subdirectory / filename |
| 403 | + dst_file = subdir_path / filename |
| 404 | + else: |
| 405 | + dst = path_repository / filename |
| 406 | + dst_file = filename |
399 | 407 | shutil.copy2(location, dst) |
400 | 408 | rc, out = rc_cmd( |
401 | | - [self.command, "-C", str(path_repository), "add", filename], |
| 409 | + [self.command, "-C", str(path_repository), "add", dst_file], |
402 | 410 | env=env, |
403 | 411 | shell=False, |
404 | 412 | ) |
@@ -458,10 +466,12 @@ def download(local_path, urlstring, progressbar=False, check_space=False, |
458 | 466 | sys.exit(1) |
459 | 467 |
|
460 | 468 | def upload(local_path, urlstring, progressbar=False, |
461 | | - source_host='', source_port=0, timeout=10.0): |
| 469 | + source_host='', source_port=0, timeout=10.0, |
| 470 | + subdirectory: Optional[str] = None): |
462 | 471 | try: |
463 | 472 | progressbar = progressbar and is_interactive() |
464 | | - urlc(urlstring, progressbar, False, source_host, source_port, timeout).upload(local_path) |
| 473 | + urlc(urlstring, progressbar, False, source_host, source_port, |
| 474 | + timeout, subdirectory).upload(local_path) |
465 | 475 | except Exception as err: |
466 | 476 | print_error(f'Unable to upload "{urlstring}": {err}') |
467 | 477 | sys.exit(1) |
|
0 commit comments