def ls(self, dirname, cheap_remote_ls=False): if not cheap_remote_ls and len(dirname) > len(self.root_dir): local_files = set([ name for name in Shell.run('ls -p {}'.format(dirname)).split('\n') if len(name) > 0 ]) remote_files = set([ name for name in Shell.run('rclone lsf "{}"'.format( self.rpath(dirname))).split('\n') if len(name) > 0 ]) for name in remote_files.difference(local_files): if name[-1] == '/': Shell.mkdir(os.path.join(dirname, name)) else: Shell.touch(os.path.join(dirname, name)) for name in local_files.difference(remote_files): log('rclone copyto "{}" "{}"'.format( os.path.join(dirname, name), os.path.join(self.rpath(dirname), name))) Shell.run('rclone copyto "{}" "{}"'.format( os.path.join(dirname, name), os.path.join(self.rpath(dirname), name))) return super(Rclone, self).ls(dirname)
def do_test(fn=None, fn_remote=None): """ Note on the mecahnism of testing rclone on localhost: 1. Tester run rclone to create a "local" remote named netrtest (must be exact this name) 2. In default.py, the vim variable 'NETRemoteRoots' is set to {test_remote_name: test_remote_dir}, which defaults to {'netrtest', '/tmp/netrtest/remote'}. 3. 'NETRemoteRoots' is passed to Rclone constructor, so that the rpath of the netrtest remote is mapped to '/tmp/netrtest/remote'. 4. This just works in netranger. In cmd line, running 'rclone lsl netranger:/' still shows you the content of the root directory of the localhost. """ old_cwd = os.getcwd() Shell.run('rm -rf {}'.format(test_dir)) def prepare_test_dir(dirname): Shell.mkdir(dirname) os.chdir(dirname) Shell.mkdir('dir/subdir') Shell.mkdir('dir/subdir/subsubdir') Shell.mkdir('dir/subdir2') Shell.touch('dir/a') Shell.touch('.a') Shell.mkdir('dir2/') # The following should be removed when rclone fix "not copying empty directories" bug. Shell.touch('dir/subdir/subsubdir/placeholder') Shell.touch('dir/subdir2/placeholder') Shell.touch('dir/subdir2/placeholder') prepare_test_dir(test_local_dir) if fn is not None: nvim.command('silent tabe {}'.format(test_local_dir)) fn() nvim.command('bwipeout') print('== {} success =='.format(str(fn.__name__))) prepare_test_dir(test_remote_dir) if fn_remote is not None: nvim.command('NETRemoteList') found_remote = False for i, line in enumerate(nvim.current.buffer): if re.findall('.+(netrtest)', line): nvim.command('call cursor({}, 1)'.format(i + 1)) found_remote = True break assert found_remote, 'You must set up an rclone remote named "{}" to test remote function'.format( test_remote_name) nvim.input('l') nvim.command('NETRemotePull') nvim.command('call cursor(2, 1)') fn_remote() nvim.command('bwipeout') print('== {} success =='.format(str(fn_remote.__name__))) os.chdir(old_cwd)
def lsl(self): info = Shell.run('rclone lsl {} --max-depth 1'.format(self.path)) for line in info.split('\n'): line = line.strip() if len(line)>0: name = line.split()[-1] self.child[name] = RcloneFile(os.path.join(self.lpath, name), os.path.join(self.path, name))
def test_sort(): # only test extension, mtime, size # extension: [a, a.a, a.b] # size: [a.b, a.a, a] # mtime: [a, a.b, a.a] Shell.run('echo {} > dir/{}'.format('a' * 3, 'a')) Shell.run('echo {} > dir/{}'.format('a' * 2, 'a.a')) Shell.run('echo {} > dir/{}'.format('a' * 1, 'a.b')) time.sleep(0.01) Shell.run('touch dir/a.a') nvim.input(' Se') assert_content('dir', ind=0, hi='dir', level=0) assert_content('a', ind=3, hi='file', level=1) assert_content('a.a', ind=4, hi='file', level=1) assert_content('a.b', ind=5, hi='file', level=1) assert_content('dir2', ind=6, hi='dir', level=0) nvim.input('SE') assert_content('dir2', ind=0, hi='dir', level=0) assert_content('dir', ind=1, hi='dir', level=0) assert_content('a.b', ind=2, hi='file', level=1) assert_content('a.a', ind=3, hi='file', level=1) assert_content('a', ind=4, hi='file', level=1) nvim.input('Ss') assert_content('dir2', ind=0, hi='dir', level=0) assert_content('dir', ind=1, hi='dir', level=0) assert_content('a.b', ind=4, hi='file', level=1) assert_content('a.a', ind=5, hi='file', level=1) assert_content('a', ind=6, hi='file', level=1) nvim.input('SS') assert_content('dir', ind=0, hi='dir', level=0) assert_content('a', ind=1, hi='file', level=1) assert_content('a.a', ind=2, hi='file', level=1) assert_content('a.b', ind=3, hi='file', level=1) assert_content('dir2', ind=6, hi='dir', level=0) nvim.input('Sm') assert_content('dir2', ind=0, hi='dir', level=0) assert_content('dir', ind=1, hi='dir', level=0) assert_content('a', ind=4, hi='file', level=1) assert_content('a.b', ind=5, hi='file', level=1) assert_content('a.a', ind=6, hi='file', level=1) nvim.input('SM') assert_content('dir', ind=0, hi='dir', level=0) assert_content('a.a', ind=1, hi='file', level=1) assert_content('a.b', ind=2, hi='file', level=1) assert_content('a', ind=3, hi='file', level=1) assert_content('dir2', ind=6, hi='dir', level=0)
def test_size_display(): width = nvim.current.window.width def cLine_ends_with(s): # line[-4:] = [0m return nvim.current.line[:-4].endswith(s) assert cLine_ends_with('3'), 'size display dir fail: {}'.format( 'a' + nvim.current.line[-1] + 'a') Shell.run('echo {} > {}'.format('a' * 1035, 'a' * width + '.pdf')) Shell.run('echo {} > {}'.format('b' * 1024, 'b' * width)) nvim.command('edit .') nvim.input('Gk') assert cLine_ends_with( '~.pdf 1.01 K'), 'size display abbreviation fail: a~.pdf {}'.format( nvim.current.line[-10:]) nvim.input('j') assert cLine_ends_with( 'b~ 1 K'), 'size display abbreviation fail: b~ {}'.format( nvim.current.line[-10:])
def assert_fs(d, expected): """ Test whether 'expected' exists in directory cwd/d, where cwd is /tmp/netrtest/local when testing local functions and cwd is /tmp/netrtest/remote when testing remote functions. """ real = None for i in range(10): real = Shell.run('ls --group-directories-first ' + d).split() if real == expected: return time.sleep(0.05) assert real == expected, 'expected: {}, real: {}'.format(expected, real)
def __init__(self, lpath, path): Shell.mkdir(lpath) self.lpath = lpath self.child = {} self.cached = False self.path = path if path is None: remotes = Shell.run('rclone listremotes').split(':\n') log(remotes) for remote in remotes: if len(remote)==0: continue self.child[remote] = RcloneDir(os.path.join(lpath, remote), remote+':/') self.cached = True
def __init__(self, root_dir, remote_remap): if root_dir[-1] == '/': root_dir = root_dir[:-1] self.root_dir = root_dir self.rplen = len(root_dir) + 1 for remote, root in remote_remap.items(): if root[-1] != '/': remote_remap[remote] += '/' self.remote_remap = remote_remap Shell.mkdir(root_dir) remotes = set(Shell.run('rclone listremotes').split(':\n')) local_remotes = set(super(Rclone, self).ls(root_dir)) for remote in remotes.difference(local_remotes): Shell.mkdir(os.path.join(root_dir, remote)) for remote in local_remotes.difference(remotes): super(Rclone, self).rm(os.path.join(root_dir, remote), True) self.has_remote = len(remotes) > 0 self.ls_time_stamp = {}
def test_bookmark(): bookmarkfile = default.variables['NETRBookmarkFile'] copy = '{}/{}bak'.format(os.path.dirname(bookmarkfile), os.path.basename(bookmarkfile)) if os.path.isfile(bookmarkfile): Shell.run('mv {} {}'.format(bookmarkfile, copy)) Shell.run('rm -f {}'.format(bookmarkfile)) nvim.input('mal') nvim.input("'a") assert_content('dir') nvim.input('lemjrb') nvim.command('exit') nvim.input("'b") assert_content('dir') Shell.run('rm -f {}'.format(bookmarkfile)) if os.path.isfile(copy): Shell.run('mv {} {}'.format(copy, bookmarkfile))
def do_test(fn, wipe_on_done=True): old_cwd = os.getcwd() test_root = os.path.expanduser('~/netranger_test_dir') Shell.run('rm -rf {}'.format(test_root)) Shell.mkdir(test_root) os.chdir(test_root) Shell.mkdir(os.path.join(test_root, 'dir/subdir')) Shell.mkdir(os.path.join(test_root, 'dir/subdir/subsubdir')) Shell.mkdir(os.path.join(test_root, 'dir/subdir2')) Shell.run('touch {}/dir/a'.format(test_root)) Shell.run('touch {}/.a'.format(test_root)) Shell.mkdir(os.path.join(test_root, 'dir2/')) nvim.command('tabe {}'.format(test_root)) fn() if wipe_on_done: nvim.command('bwipeout') os.chdir(old_cwd) print('== {} success =='.format(str(fn.__name__)))
def touch(self, name): Shell.touch(name) Shell.run('rclone copyto "{}" "{}"'.format( name, os.path.join(self.rpath(name), os.path.basename(name))))
def download(self): if self.downloaded: return else: Shell.run('rclone copyto "{}" "{}"'.format(self.path, self.lpath)) self.downloaded = True
def rmf(self, target): Shell.run('rm -r {}'.format(target))
def cp(self, src, dst): if os.path.isdir(src) and src[-1]!='/': src = src+'/' if os.path.isdir(dst) and dst[-1]!='/': dst = dst+'/' Shell.run('cp -r "{}" "{}"'.format(src, dst))
def ensure_downloaded(self, lpath): if os.stat(lpath).st_size == 0: src, dst = self.sync_src_dst(lpath, Rclone.SyncDirection.DOWN) Shell.run('rclone copyto "{}" "{}"'.format(src, dst))
def test_delete(): nvim.input('vD') assert_fs(lambda: Shell.run('ls').split()[0] == 'dir2') nvim.input('XX') assert_fs(lambda: Shell.run('ls') == '')
def test_edit(): nvim.input('iz') nvim.input('') assert_fs(lambda: 'zdir' in Shell.run('ls').split()) assert_content('dir2')
def rename(self, src, dst): Shell.run('rclone moveto "{}" "{}"'.format(self.rpath(src), self.rpath(dst))) super(Rclone, self).rename(src, dst)
def cp(self, src, dst): Shell.run('rclone copyto "{}" "{}"'.format( self.rpath(src), os.path.join(self.rpath(dst), os.path.basename(src)))) super(Rclone, self).cp(src, dst)
def rm(self, target, force=False): cmd = 'purge' if os.path.isdir(target) else 'delete' Shell.run('rclone {} "{}"'.format(cmd, self.rpath(target))) super(Rclone, self).rm(target, force=True)
def rm(self, target, force=False): if force: Shell.run('rm -rf "{}"'.format(target)) else: Shell.run('rm -r "{}"'.format(target))
def sync(self, lpath, direction): src, dst = self.sync_src_dst(lpath, direction) Shell.run('rclone sync "{}" "{}"'.format(src, dst))