def putTree(self, targetPath, exists=False, overwrite=True): _, dirName = os.path.split(targetPath) dirNameToCreate = dirName if not overwrite and exists: timestamp = getTimestamp() dirNameToCreate = "{time}_{dir}".format(time=timestamp, dir=dirName) remoteDirPath = getNextPath(self.pwd(), dirNameToCreate) if not exists or (exists and not overwrite): self.mkd(remoteDirPath) self.cwd(remoteDirPath) for el in os.listdir(targetPath): elPath = getNextPath(targetPath, el) remoteElPath = getNextPath(self.pwd(), el) if os.path.isdir(elPath): if not self.isBrokenSymlink(elPath): innerExists = False if self.exists(el, self.pwd()): innerExists = True self.putTree(elPath, innerExists) if self.isBrokenSymlink(elPath): continue if os.path.isfile(elPath): self.put(elPath, el) backPath = getNextPath(self.pwd(), "..") self.cwd(backPath)
def getTree(self, targetPath, exists=False, overwrite=True): _, dirName = os.path.split(targetPath) dirNameToCreate = dirName if not overwrite and exists: timestamp = getTimestamp() dirNameToCreate = "{time}_{dir}".format(time=timestamp, dir=dirName) localDirPath = getNextPath(os.getcwd(), dirNameToCreate) if not exists or (exists and not overwrite): os.mkdir(localDirPath) os.chdir(localDirPath) targetList = self.list(targetPath, extra=True) for el in targetList: elPath = getNextPath(targetPath, el["name"]) localElPath = getNextPath(os.getcwd(), el["name"]) if el["directory"] == "d": innerExists = False if os.path.exists(localElPath): innerExists = True self.getTree(elPath, innerExists) else: localFileName = getNextPath(os.getcwd(), el["name"]) self.get(elPath, localFileName) backPath = getNextPath(os.getcwd(), "..") os.chdir(backPath) # ----------------------------------------------------
def download(self): self.pingServer(message=False) if self.connected: overwrite = False pathExists = False target = getSingleActionParam(act["Download"], self.action) targetPath = getNextPath(self.pathRemote, target) absTargetPath, targetName = os.path.split(targetPath) localTargetPath = getNextPath(self.pathLocal, targetName) if self.ftp.exists(targetName, absTargetPath): if os.path.exists(localTargetPath): pathExists = True confirmOverwrite = interact.confirm(ns.interact["data_exists"].format(target=targetName)) if confirmOverwrite: overwrite = True downloadingMsg = ns.common["downloading"].format(target=targetPath) try: spinner.start(downloadingMsg) if self.ftp.isDir(targetPath): self.ftp.getTree(targetPath, pathExists, overwrite) else: self.ftp.getFile(targetPath, self.pathLocal, pathExists, overwrite) spinner.success(downloadingMsg) msg.success(ns.common["success"]) except Exception as e: spinner.fail(downloadingMsg) msg.default(e) msg.error(ns.errors["transfer_error"]) else: msg.error(ns.errors["invalid_path"]) else: msg.error(ns.errors["no_connection"])
def mkdir(self): dirName = getSingleActionParam(act["Mkdir"], self.action) try: if self.env == envs["Local"]: localDirPath = getNextPath(self.pathLocal, dirName) os.mkdir(localDirPath) else: self.pingServer() if self.connected: remoteDirPath = getNextPath(self.pathRemote, dirName) self.ftp.mkd(remoteDirPath) except Exception as e: msg.default(e) msg.error(ns.errors["mkdir_error"].format(dirName=dirName))
def cd(self): dest = getSingleActionParam(act["Cd"], self.action) if self.env == envs["Local"]: nextLocalPath = getNextPath(self.pathLocal, dest) if os.path.exists(nextLocalPath): self.changeLocalPath(nextLocalPath) else: msg.error(ns.errors["cd_error"].format(dest=nextLocalPath)) else: self.pingServer() if self.connected: try: nextRemotePath = getNextPath(self.pathRemote, dest) self.changeRemotePath(nextRemotePath) except Exception as e: msg.default(e) msg.error(ns.errors["cd_error"].format(dest=nextRemotePath))
def rmTree(self, path): pathList = self.list(path, extra=True) for target in pathList: preparedTargetPath = getNextPath(path, target["name"]) if target["directory"] == "d": self.rmTree(preparedTargetPath) else: self.delete(preparedTargetPath) self.rmd(path)
def getFile(self, targetPath, saveToPath, exists=False, overwrite=True): _, fileName = os.path.split(targetPath) fileNameToCreate = fileName if not overwrite and exists: timestamp = getTimestamp() fileNameToCreate = "{time}_{file}".format(time=timestamp, file=fileName) localFileName = getNextPath(saveToPath, fileNameToCreate) self.get(targetPath, localFileName)
def delete(self): target = getSingleActionParam(act["Delete"], self.action) try: if self.env == envs["Local"]: localTargetPath = getNextPath(self.pathLocal, target) if os.path.exists(localTargetPath): deletingMsg = ns.common["deleting"].format(target=localTargetPath) confirmMsg = ns.interact["confirm_delete"].format(fileName=localTargetPath) confirmDel = interact.confirm(confirmMsg) if confirmDel: spinner.start(deletingMsg) if os.path.isdir(localTargetPath): shutil.rmtree(localTargetPath) else: os.remove(localTargetPath) spinner.success(deletingMsg) msg.success(ns.common["success"]) else: msg.error(ns.errors["invalid_path"]) else: self.pingServer() if self.connected: remoteTargetPath = getNextPath(self.pathRemote, target) absTargetPath, absTargetName = os.path.split(remoteTargetPath) if self.ftp.exists(absTargetName, absTargetPath): deletingMsg = ns.common["deleting"].format(target=remoteTargetPath) confirmMsg = ns.interact["confirm_delete"].format(fileName=remoteTargetPath) confirmDel = interact.confirm(confirmMsg) if confirmDel: spinner.start(deletingMsg) if self.ftp.isDir(remoteTargetPath): self.ftp.rmTree(remoteTargetPath) else: self.ftp.delete(remoteTargetPath) spinner.success(deletingMsg) msg.success(ns.common["success"]) else: msg.error(ns.errors["invalid_path"]) except Exception as e: spinner.fail(deletingMsg) msg.default(e) msg.error(ns.errors["delete_error"].format(target=target))