From 60993abf29187903e451a44818b6e104ca435a1a Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 21 Nov 2018 14:51:57 +0100 Subject: [PATCH] fix relpath bug for #7 --- catcli/catcli.py | 15 +++++++++++---- catcli/noder.py | 48 ++++++++++++++++++++++++++++++++++++---------- catcli/walker.py | 24 ++++++++++++++--------- tests/test_find.py | 2 +- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index 43f28fd..e26d93b 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -39,7 +39,7 @@ Usage: {1} index [--catalog=] [--meta=...] [-acfuV] {1} update [--catalog=] [-acfuV] {1} ls [--catalog=] [-arVS] [] - {1} find [--catalog=] [-abV] + {1} find [--catalog=] [-abVP] {1} rm [--catalog=] [-fV] {1} tree [--catalog=] [-aVS] [] {1} rename [--catalog=] [-fV] @@ -59,6 +59,7 @@ Options: -S --sortsize Sort by size, largest first [default: False]. -c --hash Calculate md5 hash [default: False]. -r --recursive Recursive [default: False]. + -P --parent Ignore stored relpath [default: True]. -V --verbose Be verbose [default: False]. -v --version Show version. -h --help Show this screen. @@ -74,8 +75,12 @@ def cmd_index(args, noder, catalog, top, debug=False): Logger.err('\"{}\" does not exist'.format(path)) return if name in noder.get_storage_names(top): - if not ask('Overwrite storage \"{}\"'.format(name)): - Logger.err('storage named \"{}\" already exist'.format(name)) + try: + if not ask('Overwrite storage \"{}\"'.format(name)): + Logger.err('storage named \"{}\" already exist'.format(name)) + return + except KeyboardInterrupt: + Logger.err('aborted') return node = noder.get_storage_node(top, name) node.parent = None @@ -144,7 +149,9 @@ def cmd_rm(args, noder, catalog, top): def cmd_find(args, noder, top): - return noder.find_name(top, args[''], script=args['--script']) + fromtree = args['--parent'] + return noder.find_name(top, args[''], script=args['--script'], + parentfromtree=fromtree) def cmd_tree(args, noder, top): diff --git a/catcli/noder.py b/catcli/noder.py index 930de4d..88dba76 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -155,8 +155,7 @@ class Noder: md5 = None if self.hash: md5 = utils.md5sum(path) - relpath = os.path.join(os.path.basename(storagepath), - os.path.relpath(path, start=storagepath)) + relpath = os.sep.join([storagepath, name]) maccess = os.path.getmtime(path) n = self._node(name, self.TYPE_FILE, relpath, parent, @@ -171,7 +170,7 @@ class Noder: def dir_node(self, name, path, parent, storagepath): '''create a new node representing a directory''' path = os.path.abspath(path) - relpath = os.path.relpath(path, start=storagepath) + relpath = os.sep.join([storagepath, name]) maccess = os.path.getmtime(path) return self._node(name, self.TYPE_DIR, relpath, parent, maccess=maccess) @@ -224,14 +223,26 @@ class Noder: # printing ############################################################### def _print_node(self, node, pre='', withpath=False, - withdepth=False, withstorage=False): - '''print a node''' + withdepth=False, withstorage=False, + recalcparent=False): + ''' + print a node + @node: the node to print + @pre: string to print before node + @withpath: print the node path + @withdepth: print the node depth info + @withstorage: print the node storage it belongs to + @recalcparent: get relpath from tree instead of relpath field + ''' if node.type == self.TYPE_TOP: Logger.out('{}{}'.format(pre, node.name)) elif node.type == self.TYPE_FILE: name = node.name if withpath: - name = node.relpath + if recalcparent: + name = os.sep.join([self._get_parents(node.parent), name]) + else: + name = node.relpath if withstorage: storage = self._get_storage(node) attr = '' @@ -244,7 +255,10 @@ class Noder: elif node.type == self.TYPE_DIR: name = node.name if withpath: - name = node.relpath + if recalcparent: + name = os.sep.join([self._get_parents(node.parent), name]) + else: + name = node.relpath depth = '' if withdepth: depth = len(node.children) @@ -287,7 +301,9 @@ class Noder: ############################################################### # searching ############################################################### - def find_name(self, root, key, script=False): + def find_name(self, root, key, + script=False, + parentfromtree=False): '''find files based on their names''' if self.verbose: Logger.info('searching for \"{}\"'.format(key)) @@ -299,8 +315,11 @@ class Noder: # ignore storage nodes continue self._print_node(f, withpath=True, withdepth=True, - withstorage=True) - paths.append(f.relpath) + withstorage=True, recalcparent=parentfromtree) + if parentfromtree: + paths.append(self._get_parents(f)) + else: + paths.append(f.relpath) if script: tmp = ['${source}/' + x for x in paths] cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp)) @@ -395,3 +414,12 @@ class Noder: def _has_attr(self, node, attr): return attr in node.__dict__.keys() + + def _get_parents(self, node): + '''get all parents recursively''' + if node.type == self.TYPE_STORAGE: + return '' + parent = self._get_parents(node.parent) + if parent: + return os.sep.join([parent, node.name]) + return node.name diff --git a/catcli/walker.py b/catcli/walker.py index 19e84f7..1522159 100644 --- a/catcli/walker.py +++ b/catcli/walker.py @@ -22,7 +22,7 @@ class Walker: self.noder.set_hashing(not nohash) self.debug = debug - def index(self, path, parent, name): + def index(self, path, parent, name, storagepath=''): '''index a directory and store in tree''' self._debug('indexing starting at {}'.format(path)) if not parent: @@ -36,16 +36,19 @@ class Walker: self._log(f) self._debug('index file {}'.format(sub)) self.noder.file_node(os.path.basename(f), sub, - parent, path) + parent, storagepath) cnt += 1 for d in dirs: self._debug('found dir {} under {}'.format(d, path)) base = os.path.basename(d) sub = os.path.join(root, d) self._debug('index directory {}'.format(sub)) - dummy = self.noder.dir_node(base, sub, parent, path) + dummy = self.noder.dir_node(base, sub, parent, storagepath) cnt += 1 - _, cnt2 = self.index(sub, dummy, base) + nstoragepath = os.sep.join([storagepath, base]) + if not storagepath: + nstoragepath = base + _, cnt2 = self.index(sub, dummy, base, nstoragepath) cnt += cnt2 break self._log(None) @@ -53,11 +56,11 @@ class Walker: def reindex(self, path, parent, top): '''reindex a directory and store in tree''' - cnt = self._reindex(path, parent, top) + cnt = self._reindex(path, parent, top, '') cnt += self.noder.clean_not_flagged(top) return cnt - def _reindex(self, path, parent, top): + def _reindex(self, path, parent, top, storagepath): '''reindex a directory and store in tree''' self._debug('reindexing starting at {}'.format(path)) cnt = 0 @@ -74,7 +77,7 @@ class Walker: self._debug('\tre-index file {}'.format(sub)) self._log(f) n = self.noder.file_node(os.path.basename(f), sub, - parent, path) + parent, storagepath) self.noder.flag(n) cnt += 1 for d in dirs: @@ -85,11 +88,14 @@ class Walker: reindex, dummy = self._need_reindex(parent, base, maccess) if reindex: self._debug('\tre-index directory {}'.format(sub)) - dummy = self.noder.dir_node(base, sub, parent, path) + dummy = self.noder.dir_node(base, sub, parent, storagepath) cnt += 1 self.noder.flag(dummy) self._debug('reindexing deeper under {}'.format(sub)) - cnt2 = self._reindex(sub, dummy, top) + nstoragepath = os.sep.join([storagepath, base]) + if not storagepath: + nstoragepath = base + cnt2 = self._reindex(sub, dummy, top, nstoragepath) cnt += cnt2 break self._log(None) diff --git a/tests/test_find.py b/tests/test_find.py index 86d51b8..229ab3e 100644 --- a/tests/test_find.py +++ b/tests/test_find.py @@ -28,7 +28,7 @@ class TestFind(unittest.TestCase): # create fake args args = {'': '7544G', '--script': True, - '--verbose': True} + '--verbose': True, '--parent': False} # try to find something found = cmd_find(args, noder, top)