Skip to content

Commit

Permalink
FIX: learn how to handle cwd/cd for put/get connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLLentz committed Mar 20, 2024
1 parent c8c4af2 commit 601fa2d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pmpsdb_client/ssh_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from contextlib import contextmanager
from dataclasses import dataclass
from io import StringIO
from pathlib import Path
from typing import Iterator, TypeVar

from fabric import Connection
Expand Down Expand Up @@ -68,6 +69,8 @@ def ssh(
result = conn.run(f"mkdir -p {directory}")
if result.exited != 0:
raise RuntimeError(f"Failed to create directory {directory}")
# Note: conn.cd only affects calls to conn.run, not conn.get or conn.put
# Use conn.cwd property to check this live
with conn.cd(directory):
yield conn
if not connected:
Expand Down Expand Up @@ -168,7 +171,9 @@ def upload_filename(
if dest_filename is None:
dest_filename = filename
with ssh(hostname=hostname, directory=directory) as conn:
conn.put(local=filename, remote=dest_filename)
if directory is None:
directory = conn.cwd
conn.put(local=filename, remote=str(Path(directory) / dest_filename))


def download_file_text(
Expand Down Expand Up @@ -200,5 +205,7 @@ def download_file_text(
logger.debug("download_file_text(%s, %s, %s)", hostname, filename, directory)
stringio = StringIO()
with ssh(hostname=hostname, directory=directory) as conn:
conn.get(remote=filename, local=stringio)
if directory is None:
directory = conn.cwd
conn.get(remote=str(Path(directory) / filename), local=stringio)
return stringio.getvalue()

0 comments on commit 601fa2d

Please sign in to comment.