def start(self, shell): x = call(shell, self.cmd, self.args) if isinstance(x, CommandReturn): x.setid(self.id) else: x = CommandReturn(-1, "No result") x.setid(self.id) return x
def cmd_rm(shell: Shell, args): for w in args: file = absjoin(os.path.abspath(shell.getPwd()), w) if os.path.isfile(file): os.remove(w) elif os.path.isdir(file): return CommandReturn(errors.FILE_NOT_FOUND, "'" + w + "' is not a file'") else: return CommandReturn(errors.FILE_NOT_FOUND, "'" + w + "' no such file or directory")
def cmd_ls(shell: Shell, args): dir = shell.getPwd() if len(args) > 0: dir = absjoin(dir, args[0]) if os.path.isdir(dir): return CommandReturn(errors.OK, "\n".join(os.listdir(dir))) elif os.path.exists(dir): return CommandReturn(errors.FILE_NOT_FOUND, "Not a directory") else: return CommandReturn(errors.FILE_NOT_FOUND, "No such file or directory")
def cmd_cd(shell: Shell, args): np = absjoin(shell.getPwd(), args[0]) if os.path.isdir(np): if Conf.isWindows() and np[-1] == ":": np += "\\" shell.setPwd(np) return CommandReturn(errors.OK) elif os.path.exists(np): return CommandReturn(errors.FILE_NOT_FOUND, "'" + args[0] + "' not a directory") else: return CommandReturn(errors.FILE_NOT_FOUND, "'" + args[0] + "' no file or directory")
def cmd_env(shell: Shell, args): out = "" if len(args) == 0: for k in shell._env: out += k + " = '" + shell._env[k] + "'\n" out = out[:-1] elif len(args) == 1: if not args[0] in shell._env: return CommandReturn(errors.BAD_PARAMETER, "'" + args[0] + "' is not defined") out = shell._env[args[0]] elif len(args) == 3 and args[1] == "=": shell._env[args[0]] = args[2] else: print("Error ", len(args), '"' + args[1] + '"', "'") return CommandReturn(errors.OK, out)
def cmd_upload(shell: Shell, data): path = data[0] filename = "" if len(data) > 1: filename = data[1] else: filename = os.path.basename(path) ret = shell.getAgent().sendFile(path, filename) return CommandReturn(ret["code"], ret["data"])
def execSystem(cmd, pipe=True, input=None): if pipe: x = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) return CommandReturn(x.returncode, x.stdout) else: #x = subprocess.Popen(cmd) print(cmd) if Conf.isWindows(): os.system(argsToString(cmd)) else: pid = os.fork() if pid: os.waitpid(pid, 0) else: os.execv(cmd[0], cmd) return CommandReturn(0)
def _print(shell, title, message): os.environ["ZENITY_DATADIR"] = os.path.abspath(Conf.ZENITY + "\\..\\..\\share\\zenity\\") if Conf.isWindows(): wd = os.getcwd() os.chdir(os.path.abspath(Conf.ZENITY + "\\..\\..")) try: execSystem( [Conf.ZENITY, '--error', '--text=' + message, '--title=' + title]) except FileNotFoundError as err: if Conf.isWindows(): os.chdir(wd) return CommandReturn(errors.MALFORMED_REQUEST, "Zenity Not found") if Conf.isWindows(): os.chdir(wd)
def cmd_print(shell, text): title, message = tuple(text) start_thread(_print, (shell, title, message)) return CommandReturn(errors.OK, "")
def cmd_mkdir(shell: Shell, args): if isinstance(args, str): args = [args] for x in args: os.mkdir(x) return CommandReturn(errors.OK, "")
def cmd_pwd(shell: Shell, args): return CommandReturn(errors.OK, shell.getPwd())
def cmd_wget(shell, data): try: filename = wget.download(*data) return CommandReturn(errors.OK, filename) except Exception as err: return CommandReturn(errors.UNKNOWWN, str(err))