Skip to content

Commit 174d8c7

Browse files
committed
remote: T7940: Initial work on adding subdir support for git
1 parent 2a9b37a commit 174d8c7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

python/vyos/remote.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from ftplib import FTP
3131
from ftplib import FTP_TLS
32+
from typing import Optional
3233

3334
from paramiko import SSHClient, SSHException
3435
from paramiko import MissingHostKeyPolicy
@@ -350,7 +351,7 @@ def download(self, location: str):
350351
raise NotImplementedError("not supported")
351352

352353
@umask(0o077)
353-
def upload(self, location: str):
354+
def upload(self, location: str, subdirectory: Optional[str] = None):
354355
scheme = self.url.scheme
355356
_, _, scheme = scheme.partition("+")
356357
netloc = self.url.netloc
@@ -395,10 +396,17 @@ def upload(self, location: str):
395396

396397
# git add
397398
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
399407
shutil.copy2(location, dst)
400408
rc, out = rc_cmd(
401-
[self.command, "-C", str(path_repository), "add", filename],
409+
[self.command, "-C", str(path_repository), "add", dst_file],
402410
env=env,
403411
shell=False,
404412
)
@@ -458,10 +466,12 @@ def download(local_path, urlstring, progressbar=False, check_space=False,
458466
sys.exit(1)
459467

460468
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):
462471
try:
463472
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)
465475
except Exception as err:
466476
print_error(f'Unable to upload "{urlstring}": {err}')
467477
sys.exit(1)

0 commit comments

Comments
 (0)