fix: handle text+bytes in terminal WS; mkdir -p before SFTP upload; robust conda path detection
This commit is contained in:
@@ -66,15 +66,24 @@ class SSHClient:
|
|||||||
self.connected = False
|
self.connected = False
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def _conda_activate_cmd(self) -> str:
|
||||||
|
candidates = [
|
||||||
|
f"/home/{self.username}/miniconda3/etc/profile.d/conda.sh",
|
||||||
|
f"/home/{self.username}/miniforge3/etc/profile.d/conda.sh",
|
||||||
|
f"/home/{self.username}/anaconda3/etc/profile.d/conda.sh",
|
||||||
|
"/opt/conda/etc/profile.d/conda.sh",
|
||||||
|
]
|
||||||
|
checks = " || ".join(
|
||||||
|
f'[ -f "{p}" ] && source "{p}"' for p in candidates
|
||||||
|
)
|
||||||
|
return f'( {checks} ) 2>/dev/null; conda activate synthetic-data 2>/dev/null'
|
||||||
|
|
||||||
def execute(self, command: str, use_conda: bool = True) -> tuple:
|
def execute(self, command: str, use_conda: bool = True) -> tuple:
|
||||||
if not self.is_connected():
|
if not self.is_connected():
|
||||||
raise Exception("Not connected to SSH server")
|
raise Exception("Not connected to SSH server")
|
||||||
|
|
||||||
if use_conda:
|
if use_conda:
|
||||||
full_cmd = (
|
full_cmd = f"{self._conda_activate_cmd()} && {command}"
|
||||||
f"source /home/{self.username}/miniconda3/etc/profile.d/conda.sh && "
|
|
||||||
f"conda activate synthetic-data && {command}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
full_cmd = command
|
full_cmd = command
|
||||||
|
|
||||||
@@ -90,10 +99,7 @@ class SSHClient:
|
|||||||
raise Exception("Not connected to SSH server")
|
raise Exception("Not connected to SSH server")
|
||||||
|
|
||||||
if use_conda:
|
if use_conda:
|
||||||
full_cmd = (
|
full_cmd = f"{self._conda_activate_cmd()} && {command}"
|
||||||
f"source /home/{self.username}/miniconda3/etc/profile.d/conda.sh && "
|
|
||||||
f"conda activate synthetic-data && {command}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
full_cmd = command
|
full_cmd = command
|
||||||
|
|
||||||
@@ -131,17 +137,14 @@ class SSHClient:
|
|||||||
channel.get_pty(term=term, width=width, height=height)
|
channel.get_pty(term=term, width=width, height=height)
|
||||||
channel.invoke_shell()
|
channel.invoke_shell()
|
||||||
|
|
||||||
# Auto-activate conda env
|
channel.send(self._conda_activate_cmd() + "\n")
|
||||||
activate = (
|
|
||||||
f"source /home/{self.username}/miniconda3/etc/profile.d/conda.sh && "
|
|
||||||
f"conda activate synthetic-data\n"
|
|
||||||
)
|
|
||||||
channel.send(activate)
|
|
||||||
return channel
|
return channel
|
||||||
|
|
||||||
def upload_file(self, local_path: str, remote_path: str):
|
def upload_file(self, local_path: str, remote_path: str):
|
||||||
if not self.is_connected():
|
if not self.is_connected():
|
||||||
raise Exception("Not connected to SSH server")
|
raise Exception("Not connected to SSH server")
|
||||||
|
remote_dir = remote_path.rsplit("/", 1)[0]
|
||||||
|
self.execute(f"mkdir -p '{remote_dir}'", use_conda=False)
|
||||||
sftp = self.client.open_sftp()
|
sftp = self.client.open_sftp()
|
||||||
try:
|
try:
|
||||||
sftp.put(local_path, remote_path)
|
sftp.put(local_path, remote_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user