def move(self, dst): if isinstance(dst, RemotePath) and dst.remote is not self.remote: raise TypeError("dst points to a different remote machine") elif not isinstance(dst, six.string_types): raise TypeError( "dst must be a string or a RemotePath (to the same remote machine)" ) self.remote._session.run("mv %s %s" % (shquote(self), shquote(dst)))
def copy(self, dst, override = False): if isinstance(dst, RemotePath): if dst.remote is not self.remote: raise TypeError("dst points to a different remote machine") elif not isinstance(dst, str): raise TypeError("dst must be a string or a RemotePath (to the same remote machine)", repr(dst)) if override: if isinstance(dst, str): dst = RemotePath(self.remote, dst) dst.remove() self.remote._session.run("cp -r %s %s" % (shquote(self), shquote(dst)))
def popen(self, args, ssh_opts=(), **kwargs): cmdline = [] cmdline.extend(ssh_opts) cmdline.append(self._fqhost) remote_cmdline = [] if shell_cmd: formulate = arg.formulate() if isinstance( arg, BaseCommand) else (shquote(arg), ) quote = shquote else: formulate = quote = lambda x: x if args: if hasattr(self, "env"): envdelta = self.env.getdelta() remote_cmdline.extend(["cd", quote(str(self.cwd)), "&&"]) if envdelta: remote_cmdline.append("env") remote_cmdline.extend("%s=%s" % (quote(k), quote(v)) for k, v in envdelta.items()) if isinstance(args, (tuple, list)): for arg in args: remote_cmdline.extend(formulate(arg)) else: remote_cmdline.extend(formulate(args)) if self._remote_shell: cmdline.append(self._remote_shell) if remote_cmdline: cmdline.append('-c') cmdline.append(quote(' '.join(remote_cmdline))) else: cmdline.extend(remote_cmdline) return self._ssh_command[tuple(cmdline)].popen(**kwargs)
def _path_getgid(self, fn): stat_cmd = ( "stat -c '%g,%G' " if self.uname not in ("Darwin", "FreeBSD") else "stat -f '%g,%Sg' " ) return self._session.run(stat_cmd + shquote(fn))[1].strip().split(",")
def _stat(self, path): rc, out, _ = self.remote._session.run( "stat -c '%F,%f,%i,%d,%h,%u,%g,%s,%X,%Y,%Z' " + shquote(path), retcode = None) if rc != 0: return None statres = out.strip().split(",") mode = statres.pop(0).lower() return mode, os.stat_result(statres)
def upload(self, src, dst): if isinstance(src, RemotePath): raise TypeError("src of upload cannot be %r" % (src,)) if isinstance(dst, LocalPath): raise TypeError("dst of upload cannot be %r" % (dst,)) if isinstance(dst, RemotePath) and dst.remote != self: raise TypeError("dst %r points to a different remote machine" % (dst,)) self._scp_command(src, "%s:%s" % (self._fqhost, shquote(dst)))
def upload(self, src, dst): if isinstance(src, RemotePath): raise TypeError("src of upload cannot be %r" % (src, )) if isinstance(dst, LocalPath): raise TypeError("dst of upload cannot be %r" % (dst, )) if isinstance(dst, RemotePath) and dst.remote != self: raise TypeError("dst %r points to a different remote machine" % (dst, )) self._scp_command(src, "%s:%s" % (self._fqhost, shquote(dst)))
def _path_stat(self, fn): rc, out, _ = self._session.run( "stat -c '%F,%f,%i,%d,%h,%u,%g,%s,%X,%Y,%Z' " + shquote(fn), retcode=None) if rc != 0: return None statres = out.strip().split(",") text_mode = statres.pop(0).lower() res = StatRes(statres) res.text_mode = text_mode return res
def download(self, src, dst): if isinstance(src, LocalPath): raise TypeError("src of download cannot be %r" % (src,)) if isinstance(src, RemotePath) and src.remote != self: raise TypeError("src %r points to a different remote machine" % (src,)) if isinstance(dst, RemotePath): raise TypeError("dst of download cannot be %r" % (dst,)) if IS_WIN32: src = self._translate_drive_letter(src) dst = self._translate_drive_letter(dst) self._scp_command("%s:%s" % (self._fqhost, shquote(src)), dst)
def _path_chown(self, fn, owner, group, recursive): args = ["chown"] if recursive: args.append("-R") if owner is not None and group is not None: args.append("%s:%s" % (owner, group)) elif owner is not None: args.append(str(owner)) elif group is not None: args.append(":%s" % (group,)) args.append(shquote(fn)) self._session.run(" ".join(args))
def upload(self, src, dst): if isinstance(src, RemotePath): raise TypeError("src of upload cannot be {!r}".format(src)) if isinstance(dst, LocalPath): raise TypeError("dst of upload cannot be {!r}".format(dst)) if isinstance(dst, RemotePath) and dst.remote != self: raise TypeError( "dst {!r} points to a different remote machine".format(dst)) if IS_WIN32: src = self._translate_drive_letter(src) dst = self._translate_drive_letter(dst) self._scp_command(src, "{}:{}".format(self._fqhost, shquote(dst)))
def _path_chown(self, fn, owner, group, recursive): args = ["chown"] if recursive: args.append("-R") if owner is not None and group is not None: args.append("%s:%s" % (owner, group)) elif owner is not None: args.append(str(owner)) elif group is not None: args.append(":%s" % (group, )) args.append(shquote(fn)) self._session.run(" ".join(args))
def _path_stat(self, fn): if self.uname not in ('Darwin', 'FreeBSD'): stat_cmd = "stat -c '%F,%f,%i,%d,%h,%u,%g,%s,%X,%Y,%Z' " else: stat_cmd = "stat -f '%HT,%Xp,%i,%d,%l,%u,%g,%z,%a,%m,%c' " rc, out, _ = self._session.run(stat_cmd + shquote(fn), retcode = None) if rc != 0: return None statres = out.strip().split(",") text_mode = statres.pop(0).lower() res = StatRes((int(statres[0], 16),) + tuple(int(sr) for sr in statres[1:])) res.text_mode = text_mode return res
def chown(self, owner=None, group=None, recursive=None): args = ["chown"] if recursive is None: recursive = self.isdir() if recursive: args.append("-R") if owner is not None and group is not None: args.append("%s:%s" % (owner, group)) elif owner is not None: args.append(str(owner)) elif group is not None: args.append(":%s" % (group,)) args.append(shquote(self)) self.remote._session.run(" ".join(args))
def chown(self, owner=None, group=None, recursive=None): args = ["chown"] if recursive is None: recursive = self.isdir() if recursive: args.append("-R") if owner is not None and group is not None: args.append("%s:%s" % (owner, group)) elif owner is not None: args.append(str(owner)) elif group is not None: args.append(":%s" % (group, )) args.append(shquote(self)) self.remote._session.run(" ".join(args))
def popen(self, args, ssh_opts=(), **kwargs): cmdline = [] cmdline.extend(ssh_opts) cmdline.append(self._fqhost) if args and hasattr(self, "env"): envdelta = self.env.getdelta() cmdline.extend(["cd", str(self.cwd), "&&"]) if envdelta: cmdline.append("env") cmdline.extend("%s=%s" % (k, shquote(v)) for k, v in envdelta.items()) if isinstance(args, (tuple, list)): cmdline.extend(args) else: cmdline.append(args) return self._ssh_command[tuple(cmdline)].popen(**kwargs)
def which(self, progname): """Looks up a program in the ``PATH``. If the program is not found, raises :class:`CommandNotFound <plumbum.commands.CommandNotFound>` :param progname: The program's name. Note that if underscores (``_``) are present in the name, and the exact name is not found, they will be replaced by hyphens (``-``) and the name will be looked up again :returns: A :class:`RemotePath <plumbum.local_machine.RemotePath>` """ alternatives = [progname] if "_" in progname: alternatives.append(progname.replace("_", "-")) for name in alternatives: rc, out, _ = self._session.run("which %s" % (shquote(name),), retcode = None) if rc == 0: return self.path(out.strip()) raise CommandNotFound(progname, self.env.path)
def which(self, progname): """Looks up a program in the ``PATH``. If the program is not found, raises :class:`CommandNotFound <plumbum.commands.CommandNotFound>` :param progname: The program's name. Note that if underscores (``_``) are present in the name, and the exact name is not found, they will be replaced by hyphens (``-``) and the name will be looked up again :returns: A :class:`RemotePath <plumbum.local_machine.RemotePath>` """ alternatives = [progname] if "_" in progname: alternatives.append(progname.replace("_", "-")) for name in alternatives: rc, out, _ = self._session.run("which %s" % (shquote(name), ), retcode=None) if rc == 0: return self.path(out.strip()) raise CommandNotFound(progname, self.env.path)
def popen(self, args, ssh_opts=(), env=None, cwd=None, **kwargs): cmdline = [] cmdline.extend(ssh_opts) cmdline.append(self._fqhost) if args: envdelta = {} if hasattr(self, "env"): envdelta.update(self.env.getdelta()) if env: envdelta.update(env) if cwd is None: cwd = getattr(self, "cwd", None) if cwd: cmdline.extend(["cd", str(cwd), "&&"]) if envdelta: cmdline.append("env") cmdline.extend("{}={}".format(k, shquote(v)) for k, v in envdelta.items()) if isinstance(args, (tuple, list)): cmdline.extend(args) else: cmdline.append(args) return self._ssh_command[tuple(cmdline)].popen(**kwargs)
def _path_getgid(self, fn): return self._session.run("stat -c '%g,%G' " + shquote(fn))[1].strip().split(",")
def __setitem__(self, name, value): BaseEnv.__setitem__(self, name, value) self.remote._session.run("export %s=%s" % (name, shquote(value)))
def mkdir(self): self.remote._session.run("mkdir -p %s" % (shquote(self),))
def move(self, dst): if isinstance(dst, RemotePath) and dst.remote is not self.remote: raise TypeError("dst points to a different remote machine") elif not isinstance(dst, str): raise TypeError("dst must be a string or a RemotePath (to the same remote machine)") self.remote._session.run("mv %s %s" % (shquote(self), shquote(dst)))
def _path_chmod(self, mode, fn): self._session.run("chmod %o %s" % (mode, shquote(fn)))
def _path_delete(self, fn): self._session.run("rm -rf %s" % (shquote(fn), ))
def _path_getgid(self, fn): stat_cmd = "stat -c '%g,%G' " if self.uname != 'Darwin' else "stat -f '%g,%Sg' " return self._session.run(stat_cmd + shquote(fn))[1].strip().split(",")
def _path_listdir(self, fn): files = self._session.run("ls -a %s" % (shquote(fn), ))[1].splitlines() files.remove(".") files.remove("..") return files
def chdir(self, newdir): """Changes the current working directory to the given one""" self.remote._session.run("cd %s" % (shquote(newdir), )) self._path = self.remote._session.run("pwd")[1].strip()
def _path_getuid(self, fn): stat_cmd = "stat -c '%u,%U' " if self.uname not in ( 'Darwin', 'FreeBSD') else "stat -f '%u,%Su' " return self._session.run(stat_cmd + shquote(fn))[1].strip().split(",")
def _path_delete(self, fn): self._session.run("rm -rf %s" % (shquote(fn),))
def _path_copy(self, src, dst): self._session.run("cp -r %s %s" % (shquote(src), shquote(dst)))
def _path_move(self, src, dst): self._session.run("mv %s %s" % (shquote(src), shquote(dst)))
def update(self, *args, **kwargs): BaseEnv.update(self, *args, **kwargs) self.remote._session.run("export " + " ".join("%s=%s" % (k, shquote(v)) for k, v in self.getdict().items()))
def delete(self): if not self.exists(): return self.remote._session.run("rm -rf %s" % (shquote(self),))
def _path_mkdir(self, fn): self._session.run("mkdir -p %s" % (shquote(fn), ))
def _path_mkdir(self, fn, mode=None, minus_p=True): p_str = "-p " if minus_p else "" cmd = "mkdir %s%s" % (p_str, shquote(fn)) self._session.run(cmd)
def chdir(self, newdir): """Changes the current working directory to the given one""" self.remote._session.run("cd %s" % (shquote(newdir),)) self._path = self.remote._session.run("pwd")[1].strip()
def _path_link(self, src, dst, symlink): self._session.run( "ln %s %s %s" % ("-s" if symlink else "", shquote(src), shquote(dst)))
def _path_listdir(self, fn): files = self._session.run("ls -a %s" % (shquote(fn),))[1].splitlines() files.remove(".") files.remove("..") return files
def _path_stat(self, fn): rc, out, _ = self._session.run("stat -c '%F,%f,%i,%d,%h,%u,%g,%s,%X,%Y,%Z' " + shquote(fn), retcode = None) if rc != 0: return None statres = out.strip().split(",") text_mode = statres.pop(0).lower() res = StatRes((int(statres[0], 16),) + tuple(int(sr) for sr in statres[1:])) res.text_mode = text_mode return res
def chdir(self, newdir): """Changes the current working directory to the given one""" self.remote._session.run("cd %s" % (shquote(newdir), )) if hasattr(self.remote, '_cwd'): del self.remote._cwd return self.__class__(self.remote)
def _path_getgid(self, fn): stat_cmd = "stat -c '%g,%G' " if self.uname not in ('Darwin', 'FreeBSD') else "stat -f '%g,%Sg' " return self._session.run(stat_cmd + shquote(fn))[1].strip().split(",")
def _path_mkdir(self, fn): self._session.run("mkdir -p %s" % (shquote(fn),))
def gid(self): gid, name = self.remote._session.run("stat -c '%g,%G' " + shquote(self))[1].strip().split(",") return FSUser(int(gid), name)
def chdir(self, newdir): """Changes the current working directory to the given one""" self.remote._session.run("cd %s" % (shquote(newdir),)) del self.remote._cwd return self.__class__(self.remote)
def chmod(self, mode): args = ["chmod", '%o' % mode, shquote(self)] self.remote._session.run(" ".join(args))
def _path_link(self, src, dst, symlink): self._session.run("ln -s %s %s" % ("-s" if symlink else "", shquote(src), shquote(dst)))
def uid(self): uid, name = self.remote._session.run("stat -c '%u,%U' " + shquote(self))[1].strip().split(",") return FSUser(int(uid), name)
def _path_getuid(self, fn): stat_cmd = "stat -c '%u,%U' " if self.uname != "Darwin" else "stat -f '%u,%Su' " return self._session.run(stat_cmd + shquote(fn))[1].strip().split(",")