def path_executable_match(self, input, file_path): """This function is a hack for Windows; essentially we allow using "python" as an exact match for "python.exe". This implementation is suitable for Unix systems where executability is determined by permissions mode and not extension.""" return unix_basename(input) == unix_basename(file_path)
def path_executable_match(self, input, file_path): """This function is a hack for Windows; essentially we allow using "python" as an exact match for "python.exe". This implementation is suitable for Unix systems where executability is determined by permissions mode and not extension.""" return unix_basename(input) == unix_basename(file_path)
def path_executable_match(self, input, file_path): """On Windows; we want to allow for e.g. using "python" as an exact match for "python.exe".""" input_basename = unix_basename(input) file_basename = unix_basename(file_path) if input_basename == file_basename: return True (pfx, ext) = os.path.splitext(file_basename) return input_basename == pfx
def path_executable_match(self, input, file_path): """On Windows; we want to allow for e.g. using "python" as an exact match for "python.exe".""" input_basename = unix_basename(input) file_basename = unix_basename(file_path) if input_basename == file_basename: return True (pfx, ext) = os.path.splitext(file_basename) return input_basename == pfx
def testLs(self): self._setupTree1() p = Pipeline.parse("ls *test*", self._context) p.execute_sync() results = list(p.get_output()) results.sort() self.assertEquals(len(results), 2) self.assertEquals(os.path.dirname(results[0].path), self._tmpd) self.assertEquals(unix_basename(results[0].path), 'testdir') self.assertEquals(os.path.dirname(results[1].path), self._tmpd) self.assertEquals(unix_basename(results[1].path), 'testf')
def testLs(self): self._setupTree1() p = Pipeline.parse("ls *test*", self._context) p.execute_sync() results = list(p.get_output()) results.sort() self.assertEqual(len(results), 2) self.assertEqual(os.path.dirname(results[0].path), self._tmpd) self.assertEqual(unix_basename(results[0].path), 'testdir') self.assertEqual(os.path.dirname(results[1].path), self._tmpd) self.assertEqual(unix_basename(results[1].path), 'testf')
class MvBuiltin(FileOpBuiltin): __doc__ = _("""Rename initial arguments to destination.""") def __init__(self): super(MvBuiltin, self).__init__('mv', aliases=['move'], hasstatus=True, argspec=MultiArgSpec('paths', min=2)) def execute(self, context, args): target = FilePath(args[-1], context.cwd) try: target_is_dir = stat.S_ISDIR(os.stat(target).st_mode) target_exists = True except OSError, e: target_is_dir = False target_exists = False sources = args[:-1] if (not target_is_dir) and len(sources) > 1: raise ValueError(_("Can't move multiple items to non-directory")) sources_total = len(sources) self._status_notify(context, sources_total, 0) if target_is_dir: for i, source in enumerate(sources): target_path = FilePath(unix_basename(source), target) shutil.move(FilePath(source, context.cwd), target_path) self._status_notify(context, sources_total, i + 1) else: shutil.move(FilePath(sources[0], context.cwd), target) self._status_notify(context, sources_total, 1) return []
def completions(self, text, cwd, context=None): bc = BuiltinCompleter() for completion in bc.completions(text, cwd, context=context): yield completion aliases = AliasRegistry.getInstance() for alias in aliases: compl = self._match(alias.name, text, alias) if compl: yield compl textpath = FilePath(text, cwd) expanded_textpath = path_expanduser(textpath) (text_dpath, text_prefix) = os.path.split(expanded_textpath) if text.find('/') >= 0 or text.startswith('.' + os.sep): pc = PathCompleter() for completion in pc.completions(text, cwd): fobj = completion.target if fobj.is_directory or fobj.is_executable: yield completion else: fs = Filesystem.getInstance() for dpath in fs.get_path_generator(): if not os.access(dpath, os.X_OK): continue for fpath in iterd_sorted(dpath): fname = unix_basename(fpath) if not fname.startswith(text_prefix): continue fobj = fs.get_file_sync(fpath) if fobj.is_executable: yield _mkfile_completion(text, fpath, fobj)
def move_to_trash(self, path): bn = unix_basename(path) newf = os.path.join(self._trashdir, bn) try: statbuf = os.stat(newf) except OSError, e: statbuf = None
def completions(self, text, cwd, context=None): bc = BuiltinCompleter() for completion in bc.completions(text, cwd, context=context): yield completion aliases = AliasRegistry.getInstance() for alias in aliases: compl = self._match(alias.name, text, alias) if compl: yield compl textpath = FilePath(text, cwd) expanded_textpath = path_expanduser(textpath) (text_dpath, text_prefix) = os.path.split(expanded_textpath) if text.find('/') >= 0 or text.startswith('.' + os.sep): pc = PathCompleter() for completion in pc.completions(text, cwd): fobj = completion.target if fobj.is_directory or fobj.is_executable: yield completion else: fs = Filesystem.getInstance() for dpath in fs.get_path_generator(): if not os.access(dpath, os.X_OK): continue for fpath in iterd_sorted(dpath): fname = unix_basename(fpath) if not fname.startswith(text_prefix): continue fobj = fs.get_file_sync(fpath) if fobj.is_executable: yield _mkfile_completion(text, fpath, fobj)
def move_to_trash(self, path): bn = unix_basename(path) newf = os.path.join(self._trashdir, bn) try: statbuf = os.stat(newf) except OSError, e: statbuf = None
class PathCompleter(Completer): def __init__(self): super(PathCompleter, self).__init__() def completions(self, text, cwd): expanded = path_expanduser(text) fullpath = FilePath(expanded, cwd) try: isdir = stat.S_ISDIR(os.stat(fullpath).st_mode) except OSError, e: isdir = False fs = Filesystem.getInstance() if isdir and fullpath.endswith('/'): for fpath in iterd_sorted(fullpath, fpath=True): yield _mkfile_completion(text, fpath) return (src_dpath, src_prefix) = os.path.split(fullpath) try: for fpath in iterd_sorted(src_dpath, fpath=True): fname = unix_basename(fpath) if fname.startswith(src_prefix): try: yield _mkfile_completion(text, fpath) except OSError, e: pass except OSError, e: pass
def execute(self, context, args): target = FilePath(args[-1], context.cwd) try: target_is_dir = stat.S_ISDIR(os.stat(target).st_mode) target_exists = True except OSError as e: target_is_dir = False target_exists = False sources = args[:-1] if (not target_is_dir) and len(sources) > 1: raise ValueError(_("Can't move multiple items to non-directory")) sources_total = len(sources) self._status_notify(context, sources_total, 0) if target_is_dir: for i, source in enumerate(sources): target_path = FilePath(unix_basename(source), target) shutil.move(FilePath(source, context.cwd), target_path) self._status_notify(context, sources_total, i + 1) else: shutil.move(FilePath(sources[0], context.cwd), target) self._status_notify(context, sources_total, 1) return []
def execute(self, context, args): target = FilePath(args[-1], context.cwd) try: target_is_dir = stat.S_ISDIR(os.stat(target).st_mode) target_exists = True except OSError as e: target_is_dir = False target_exists = False sources = args[:-1] if (not target_is_dir) and len(sources) > 1: raise ValueError(_("Can't move multiple items to non-directory")) sources_total = len(sources) self._status_notify(context, sources_total, 0) if target_is_dir: for i,source in enumerate(sources): target_path = FilePath(unix_basename(source), target) shutil.move(FilePath(source, context.cwd), target_path) self._status_notify(context, sources_total, i+1) else: shutil.move(FilePath(sources[0], context.cwd), target) self._status_notify(context,sources_total,1) return []
def testLs4(self): self._setupTree1() p = Pipeline.parse("ls | filter spac path", self._context) p.execute_sync() results = list(p.get_output()) self.assertEquals(len(results), 1) self.assertEquals(os.path.dirname(results[0].path), self._tmpd) self.assertEquals(unix_basename(results[0].path), 'dir with spaces')
def _mkfile_completion(text, fpath, fileobj=None): if not isinstance(text, unicode): text = unicode(text, "utf-8") if not isinstance(fpath, unicode): fpath = unicode(fpath, "utf-8") fs = Filesystem.getInstance() fname = unix_basename(fpath) if text.endswith("/"): textbase = "" else: textbase = unix_basename(text) fobj = fileobj or fs.get_file_sync(fpath) startidx = fpath.rindex(fname) suffix = quote_arg(fpath[startidx + len(textbase) :]) if fobj.test_directory(follow_link=True): suffix += "/" return Completion(suffix, fobj, fname)
def testLs4(self): self._setupTree1() p = Pipeline.parse("ls | filter spac path", self._context) p.execute_sync() results = list(p.get_output()) self.assertEqual(len(results), 1) self.assertEqual(os.path.dirname(results[0].path), self._tmpd) self.assertEqual(unix_basename(results[0].path), 'dir with spaces')
def _mkfile_completion(text, fpath, fileobj=None): if not isinstance(text, unicode): text = unicode(text, 'utf-8') if not isinstance(fpath, unicode): fpath = unicode(fpath, 'utf-8') fs = Filesystem.getInstance() fname = unix_basename(fpath) if text.endswith('/'): textbase = '' else: textbase = unix_basename(text) fobj = fileobj or fs.get_file_sync(fpath) startidx = fpath.rindex(fname) suffix = quote_arg(fpath[startidx + len(textbase):]) if fobj.test_directory(follow_link=True): suffix += '/' return Completion(suffix, fobj, fname)
def _mkfile_completion(text, fpath, fileobj=None): if not isinstance(text, str): text = str(text, 'utf-8') if not isinstance(fpath, str): fpath = str(fpath, 'utf-8') fs = Filesystem.getInstance() fname = unix_basename(fpath) if text.endswith('/'): textbase = '' else: textbase = unix_basename(text) fobj = fileobj or fs.get_file_sync(fpath) startidx = fpath.rindex(fname) suffix = quote_arg(fpath[startidx+len(textbase):]) if fobj.test_directory(follow_link=True): suffix += '/' return Completion(suffix, fobj, fname)
def move_to_trash(self, path): bn = unix_basename(path) newf = os.path.join(self._trashdir, bn) try: statbuf = os.stat(newf) except OSError as e: statbuf = None if statbuf: _logger.debug("Removing from trash: %s", newf) if stat.S_ISDIR(statbuf.st_mode): shutil.rmtree(newf, onerror=lambda f,p,e:_logger.exception("Failed to delete '%s' from trash", newf)) shutil.move(path, newf)
def move_to_trash(self, path): bn = unix_basename(path) newf = os.path.join(self._trashdir, bn) try: statbuf = os.stat(newf) except OSError as e: statbuf = None if statbuf: _logger.debug("Removing from trash: %s", newf) if stat.S_ISDIR(statbuf.st_mode): shutil.rmtree(newf, onerror=lambda f, p, e: _logger.exception( "Failed to delete '%s' from trash", newf)) shutil.move(path, newf)
def __init__(self, path, fs=None): super(File, self).__init__() if not isinstance(path, unicode): path = unicode(path, 'utf-8') self._path = path self._uri = 'file://' + urllib.pathname2url(path.encode(sys.getfilesystemencoding())) self._basename = unix_basename(path) self.fs = fs self.stat = None self.xaccess = None self._hidden = None self._icon = None self.icon_error = False self._permstring = None self.target_stat = None self.stat_error = None
def __init__(self, path, fs=None): super(File, self).__init__() if not isinstance(path, unicode): path = unicode(path, 'utf-8') self._path = path self._uri = 'file://' + urllib.pathname2url( path.encode(sys.getfilesystemencoding())) self._basename = unix_basename(path) self.fs = fs self.stat = None self.xaccess = None self._hidden = None self._icon = None self.icon_error = False self._permstring = None self.target_stat = None self.stat_error = None
def completions(self, text, cwd): expanded = path_expanduser(text) fullpath = FilePath(expanded, cwd) try: isdir = stat.S_ISDIR(os.stat(fullpath).st_mode) except OSError as e: isdir = False fs = Filesystem.getInstance() if isdir and fullpath.endswith('/'): for fpath in iterd_sorted(fullpath, fpath=True): yield _mkfile_completion(text, fpath) return (src_dpath, src_prefix) = os.path.split(fullpath) try: for fpath in iterd_sorted(src_dpath, fpath=True): fname = unix_basename(fpath) if fname.startswith(src_prefix): try: yield _mkfile_completion(text, fpath) except OSError as e: pass except OSError as e: pass
def undo_trashed(self, args): for arg in args: trashed = self.get_trash_item(unix_basename(arg)) if trashed: shutil.move(trashed, arg)
def undo_trashed(self, args): for arg in args: trashed = self.get_trash_item(unix_basename(arg)) if trashed: shutil.move(trashed, arg)