Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@_setdoc(Path)
def stem(self):
return self.name.rsplit('.')[0]
@property # type: ignore
@_setdoc(Path)
def root(self):
return '/'
@property # type: ignore
@_setdoc(Path)
def drive(self):
return ''
class RemoteWorkdir(RemotePath):
"""Remote working directory manipulator"""
def __new__(cls, remote):
self = super(RemoteWorkdir, cls).__new__(
cls, remote,
remote._session.run("pwd")[1].strip())
return self
def __hash__(self):
raise TypeError("unhashable type")
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
def join(self, *parts):
return RemotePath(self.remote, self, *parts)
def copy(self, dst, override=None):
if isinstance(dst, RemotePath):
raise TypeError("Cannot copy local path %s to %r" % (self, dst))
dst = LocalPath(dst)
if override is False and dst.exists():
raise TypeError("File exists and override was not specified")
if override:
dst.delete()
if self.is_dir():
shutil.copytree(str(self), str(dst))
else:
dst_dir = LocalPath(dst).dirname
if not dst_dir.exists():
dst_dir.mkdir()
shutil.copy2(str(self), str(dst))
return dst
def getpath(self):
"""Returns the current working directory as a
`remote path ` object"""
return RemotePath(self.remote, self)
def chdir(self, newdir):
"""Changes the current working directory to the given one
:param newdir: The destination director (a string or a ``LocalPath``)
"""
if isinstance(newdir, RemotePath):
raise TypeError("newdir cannot be %r" % (newdir, ))
logger.debug("Chdir to %s", newdir)
os.chdir(str(newdir))
return self.__class__()
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, ))
return self._download(
src if isinstance(src, RemotePath) else self.path(src), dst
if isinstance(dst, LocalPath) else LocalPath(dst))
def _form(self, *parts):
return RemotePath(self.remote, *parts)
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 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, six.string_types):
raise TypeError(
"dst must be a string or a RemotePath (to the same remote machine), "
"got %r" % (dst, ))
if override:
if isinstance(dst, six.string_types):
dst = RemotePath(self.remote, dst)
dst.remove()
else:
if isinstance(dst, six.string_types):
dst = RemotePath(self.remote, dst)
if dst.exists():
raise TypeError("Override not specified and dst exists")
self.remote._path_copy(self, dst)
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)