Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Al-Saffar committed Nov 12, 2023
1 parent f06806e commit c2847f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
10 changes: 6 additions & 4 deletions myresources/crocodile/file_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ def to_cloud(self, cloud: str, remotepath: OPLike = None, zip: bool = False, enc
if remotepath is None:
rp = localpath.get_remote_path(root=root, os_specific=os_specific, rel2home=rel2home, strict=strict) # if rel2home else (P(root) / localpath if root is not None else localpath)
else: rp = P(remotepath)
from crocodile.meta import Terminal, subprocess; _ = print(f"{'⬆️'*5} UPLOADING {localpath.as_posix()} to {cloud}:{rp.as_posix()}") if verbose else None
res = Terminal(stdout=None if verbose else subprocess.PIPE).run(f"""rclone copyto '{localpath.as_posix()}' '{cloud}:{rp.as_posix()}' {'--progress' if verbose else ''} --transfers={transfers}""", shell="powershell").capture()
rclone_cmd = f"""rclone copyto '{localpath.as_posix()}' '{cloud}:{rp.as_posix()}' {'--progress' if verbose else ''} --transfers={transfers}"""
from crocodile.meta import Terminal, subprocess; _ = print(f"{'⬆️'*5} UPLOADING with `{rclone_cmd}`") if verbose else None
res = Terminal(stdout=None if verbose else subprocess.PIPE).run(rclone_cmd, shell="powershell").capture()
_ = [item.delete(sure=True) for item in to_del]; _ = print(f"{'⬆️'*5} UPLOAD COMPLETED.") if verbose else None
assert res.is_successful(strict_err=False, strict_returcode=True), res.print(capture=False)
if share:
Expand All @@ -542,8 +543,9 @@ def from_cloud(self, cloud: str, remotepath: OPLike = None, decrypt: bool = Fals
else: remotepath = P(remotepath)
localpath = self.expanduser().absolute()
localpath += ".zip" if unzip else ""; localpath += ".enc" if decrypt else ""
from crocodile.meta import Terminal, subprocess; _ = print(f"{'⬇️' * 5} DOWNLOADING {cloud}:{remotepath.as_posix()} ==> {localpath.as_posix()}") if verbose else None
res = Terminal(stdout=None if verbose else subprocess.PIPE).run(f"""rclone copyto '{cloud}:{remotepath.as_posix()}' '{localpath.as_posix()}' {'--progress' if verbose else ''} --transfers={transfers}""", shell="powershell")
rclone_cmd = f"""rclone copyto '{cloud}:{remotepath.as_posix()}' '{localpath.as_posix()}' {'--progress' if verbose else ''} --transfers={transfers}"""
from crocodile.meta import Terminal, subprocess; _ = print(f"{'⬇️' * 5} DOWNLOADING with `{rclone_cmd}`") if verbose else None
res = Terminal(stdout=None if verbose else subprocess.PIPE).run(rclone_cmd, shell="powershell")
assert res.is_successful(strict_err=False, strict_returcode=True), res.print(capture=False)
if decrypt: localpath = localpath.decrypt(key=key, pwd=pwd, inplace=True)
if unzip: localpath = localpath.unzip(inplace=True, verbose=True, overwrite=overwrite, content=True, merge=merge)
Expand Down
9 changes: 3 additions & 6 deletions myresources/crocodile/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,15 @@ def run_py(self, cmd: str, desc: str = "", return_obj: bool = False, verbose: bo
source_file = self.run_py(f"""{cmd}\npath = tb.Save.pickle(obj=obj, path=tb.P.tmpfile(suffix='.pkl'))\nprint(path)""", desc=desc, verbose=verbose, strict_err=True, strict_returncode=True).op.split('\n')[-1]
return self.copy_to_here(source=source_file, target=P.tmpfile(suffix='.pkl')).readit()
def copy_from_here(self, source: PLike, target: OPLike = None, z: bool = False, r: bool = False, overwrite: bool = False, init: bool = True) -> Union[P, list[P]]:
if init: print(f"{'>'*15} SFTP SENDING FROM `{source}` TO `{target}`") # TODO: using return_obj do all tests required in one go.
if init: print(f"{'⬆️' * 5} SFTP UPLOADING FROM `{source}` TO `{target}`") # TODO: using return_obj do all tests required in one go.
source_obj = P(source).expanduser().absolute()
if not z and source_obj.is_dir():
if r is True:
tmp = source_obj.search("*", folders=False, r=True)
tmp.apply(lambda file: self.copy_from_here(source=file, target=target))
return list(tmp)
print(f"tb.Meta.SSH Error: source is a directory! either set r=True for recursive sending or raise zip_first flag."); raise RuntimeError
if z: print(f"ZIPPING ..."); source_obj = P(source_obj).expanduser().zip(content=True) # .append(f"_{randstr()}", inplace=True) # eventually, unzip will raise content flag, so this name doesn't matter.
if z: print(f"🗜️ ZIPPING ..."); source_obj = P(source_obj).expanduser().zip(content=True) # .append(f"_{randstr()}", inplace=True) # eventually, unzip will raise content flag, so this name doesn't matter.
if target is None: target = P(source_obj).expanduser().absolute().collapseuser(strict=True); assert target.is_relative_to("~"), f"If target is not specified, source must be relative to home."
remotepath = self.run_py(f"path=tb.P(r'{P(target).as_posix()}').expanduser()\n{'path.delete(sure=True)' if overwrite else ''}\nprint(path.parent.create())", desc=f"Creating Target directory `{P(target).parent.as_posix()}` @ {self.get_remote_repr()}", verbose=False).op or ''
remotepath = P(remotepath.split("\n")[-1]).joinpath(P(target).name)
Expand All @@ -350,10 +350,7 @@ def copy_from_here(self, source: PLike, target: OPLike = None, z: bool = False,
source_obj.delete(sure=True); print("\n")
return source_obj
def copy_to_here(self, source: PLike, target: OPLike = None, z: bool = False, r: bool = False, init: bool = True) -> P:
if source is None:
assert target is not None, f"If source is not specified, target must be specified."

if init: print(f"{'<'*15} SFTP RECEIVING FROM `{source}` TO `{target}`")
if init: print(f"{'⬇️' * 5} SFTP DOWNLOADING FROM `{source}` TO `{target}`")
if not z and self.run_py(f"print(tb.P(r'{source}').expanduser().absolute().is_dir())", desc=f"Check if source `{source}` is a dir", verbose=False, strict_returncode=True, strict_err=True).op.split("\n")[-1] == 'True':
if r:
tmp11 = self.run_py(f"obj=tb.P(r'{source}').search(folders=False, r=True).collapseuser(strict=False)", desc="Searching for files in source", return_obj=True, verbose=False)
Expand Down

0 comments on commit c2847f9

Please sign in to comment.