fix relpath bug for #7

pull/11/head
deadc0de6 6 years ago
parent 01729ec7db
commit 60993abf29

@ -39,7 +39,7 @@ Usage:
{1} index [--catalog=<path>] [--meta=<meta>...] [-acfuV] <name> <path> {1} index [--catalog=<path>] [--meta=<meta>...] [-acfuV] <name> <path>
{1} update [--catalog=<path>] [-acfuV] <name> <path> {1} update [--catalog=<path>] [-acfuV] <name> <path>
{1} ls [--catalog=<path>] [-arVS] [<path>] {1} ls [--catalog=<path>] [-arVS] [<path>]
{1} find [--catalog=<path>] [-abV] <term> {1} find [--catalog=<path>] [-abVP] <term>
{1} rm [--catalog=<path>] [-fV] <storage> {1} rm [--catalog=<path>] [-fV] <storage>
{1} tree [--catalog=<path>] [-aVS] [<path>] {1} tree [--catalog=<path>] [-aVS] [<path>]
{1} rename [--catalog=<path>] [-fV] <storage> <name> {1} rename [--catalog=<path>] [-fV] <storage> <name>
@ -59,6 +59,7 @@ Options:
-S --sortsize Sort by size, largest first [default: False]. -S --sortsize Sort by size, largest first [default: False].
-c --hash Calculate md5 hash [default: False]. -c --hash Calculate md5 hash [default: False].
-r --recursive Recursive [default: False]. -r --recursive Recursive [default: False].
-P --parent Ignore stored relpath [default: True].
-V --verbose Be verbose [default: False]. -V --verbose Be verbose [default: False].
-v --version Show version. -v --version Show version.
-h --help Show this screen. -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)) Logger.err('\"{}\" does not exist'.format(path))
return return
if name in noder.get_storage_names(top): if name in noder.get_storage_names(top):
if not ask('Overwrite storage \"{}\"'.format(name)): try:
Logger.err('storage named \"{}\" already exist'.format(name)) if not ask('Overwrite storage \"{}\"'.format(name)):
Logger.err('storage named \"{}\" already exist'.format(name))
return
except KeyboardInterrupt:
Logger.err('aborted')
return return
node = noder.get_storage_node(top, name) node = noder.get_storage_node(top, name)
node.parent = None node.parent = None
@ -144,7 +149,9 @@ def cmd_rm(args, noder, catalog, top):
def cmd_find(args, noder, top): def cmd_find(args, noder, top):
return noder.find_name(top, args['<term>'], script=args['--script']) fromtree = args['--parent']
return noder.find_name(top, args['<term>'], script=args['--script'],
parentfromtree=fromtree)
def cmd_tree(args, noder, top): def cmd_tree(args, noder, top):

@ -155,8 +155,7 @@ class Noder:
md5 = None md5 = None
if self.hash: if self.hash:
md5 = utils.md5sum(path) md5 = utils.md5sum(path)
relpath = os.path.join(os.path.basename(storagepath), relpath = os.sep.join([storagepath, name])
os.path.relpath(path, start=storagepath))
maccess = os.path.getmtime(path) maccess = os.path.getmtime(path)
n = self._node(name, self.TYPE_FILE, relpath, parent, n = self._node(name, self.TYPE_FILE, relpath, parent,
@ -171,7 +170,7 @@ class Noder:
def dir_node(self, name, path, parent, storagepath): def dir_node(self, name, path, parent, storagepath):
'''create a new node representing a directory''' '''create a new node representing a directory'''
path = os.path.abspath(path) path = os.path.abspath(path)
relpath = os.path.relpath(path, start=storagepath) relpath = os.sep.join([storagepath, name])
maccess = os.path.getmtime(path) maccess = os.path.getmtime(path)
return self._node(name, self.TYPE_DIR, relpath, return self._node(name, self.TYPE_DIR, relpath,
parent, maccess=maccess) parent, maccess=maccess)
@ -224,14 +223,26 @@ class Noder:
# printing # printing
############################################################### ###############################################################
def _print_node(self, node, pre='', withpath=False, def _print_node(self, node, pre='', withpath=False,
withdepth=False, withstorage=False): withdepth=False, withstorage=False,
'''print a node''' 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: if node.type == self.TYPE_TOP:
Logger.out('{}{}'.format(pre, node.name)) Logger.out('{}{}'.format(pre, node.name))
elif node.type == self.TYPE_FILE: elif node.type == self.TYPE_FILE:
name = node.name name = node.name
if withpath: if withpath:
name = node.relpath if recalcparent:
name = os.sep.join([self._get_parents(node.parent), name])
else:
name = node.relpath
if withstorage: if withstorage:
storage = self._get_storage(node) storage = self._get_storage(node)
attr = '' attr = ''
@ -244,7 +255,10 @@ class Noder:
elif node.type == self.TYPE_DIR: elif node.type == self.TYPE_DIR:
name = node.name name = node.name
if withpath: if withpath:
name = node.relpath if recalcparent:
name = os.sep.join([self._get_parents(node.parent), name])
else:
name = node.relpath
depth = '' depth = ''
if withdepth: if withdepth:
depth = len(node.children) depth = len(node.children)
@ -287,7 +301,9 @@ class Noder:
############################################################### ###############################################################
# searching # 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''' '''find files based on their names'''
if self.verbose: if self.verbose:
Logger.info('searching for \"{}\"'.format(key)) Logger.info('searching for \"{}\"'.format(key))
@ -299,8 +315,11 @@ class Noder:
# ignore storage nodes # ignore storage nodes
continue continue
self._print_node(f, withpath=True, withdepth=True, self._print_node(f, withpath=True, withdepth=True,
withstorage=True) withstorage=True, recalcparent=parentfromtree)
paths.append(f.relpath) if parentfromtree:
paths.append(self._get_parents(f))
else:
paths.append(f.relpath)
if script: if script:
tmp = ['${source}/' + x for x in paths] tmp = ['${source}/' + x for x in paths]
cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp)) cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp))
@ -395,3 +414,12 @@ class Noder:
def _has_attr(self, node, attr): def _has_attr(self, node, attr):
return attr in node.__dict__.keys() 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

@ -22,7 +22,7 @@ class Walker:
self.noder.set_hashing(not nohash) self.noder.set_hashing(not nohash)
self.debug = debug self.debug = debug
def index(self, path, parent, name): def index(self, path, parent, name, storagepath=''):
'''index a directory and store in tree''' '''index a directory and store in tree'''
self._debug('indexing starting at {}'.format(path)) self._debug('indexing starting at {}'.format(path))
if not parent: if not parent:
@ -36,16 +36,19 @@ class Walker:
self._log(f) self._log(f)
self._debug('index file {}'.format(sub)) self._debug('index file {}'.format(sub))
self.noder.file_node(os.path.basename(f), sub, self.noder.file_node(os.path.basename(f), sub,
parent, path) parent, storagepath)
cnt += 1 cnt += 1
for d in dirs: for d in dirs:
self._debug('found dir {} under {}'.format(d, path)) self._debug('found dir {} under {}'.format(d, path))
base = os.path.basename(d) base = os.path.basename(d)
sub = os.path.join(root, d) sub = os.path.join(root, d)
self._debug('index directory {}'.format(sub)) 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 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 cnt += cnt2
break break
self._log(None) self._log(None)
@ -53,11 +56,11 @@ class Walker:
def reindex(self, path, parent, top): def reindex(self, path, parent, top):
'''reindex a directory and store in tree''' '''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) cnt += self.noder.clean_not_flagged(top)
return cnt return cnt
def _reindex(self, path, parent, top): def _reindex(self, path, parent, top, storagepath):
'''reindex a directory and store in tree''' '''reindex a directory and store in tree'''
self._debug('reindexing starting at {}'.format(path)) self._debug('reindexing starting at {}'.format(path))
cnt = 0 cnt = 0
@ -74,7 +77,7 @@ class Walker:
self._debug('\tre-index file {}'.format(sub)) self._debug('\tre-index file {}'.format(sub))
self._log(f) self._log(f)
n = self.noder.file_node(os.path.basename(f), sub, n = self.noder.file_node(os.path.basename(f), sub,
parent, path) parent, storagepath)
self.noder.flag(n) self.noder.flag(n)
cnt += 1 cnt += 1
for d in dirs: for d in dirs:
@ -85,11 +88,14 @@ class Walker:
reindex, dummy = self._need_reindex(parent, base, maccess) reindex, dummy = self._need_reindex(parent, base, maccess)
if reindex: if reindex:
self._debug('\tre-index directory {}'.format(sub)) 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 cnt += 1
self.noder.flag(dummy) self.noder.flag(dummy)
self._debug('reindexing deeper under {}'.format(sub)) 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 cnt += cnt2
break break
self._log(None) self._log(None)

@ -28,7 +28,7 @@ class TestFind(unittest.TestCase):
# create fake args # create fake args
args = {'<term>': '7544G', '--script': True, args = {'<term>': '7544G', '--script': True,
'--verbose': True} '--verbose': True, '--parent': False}
# try to find something # try to find something
found = cmd_find(args, noder, top) found = cmd_find(args, noder, top)

Loading…
Cancel
Save