log updates to file for #10

pull/19/head
deadc0de6 4 years ago
parent 4af351d144
commit c2510924b1

@ -37,7 +37,7 @@ USAGE = """
Usage:
{1} index [--catalog=<path>] [--meta=<meta>...] [-acfnV] <name> <path>
{1} update [--catalog=<path>] [-acfnV] <name> <path>
{1} update [--catalog=<path>] [-acfnV] [--lpath=<path>] <name> <path>
{1} ls [--catalog=<path>] [-arVS] [<path>]
{1} find [--catalog=<path>] [-abdVP] [--path=<path>] <term>
{1} rm [--catalog=<path>] [-fV] <storage>
@ -50,21 +50,22 @@ Usage:
{1} --version
Options:
--catalog=<path> Path to the catalog [default: {2}].
--meta=<meta> Additional attribute to store [default: ].
-p --path=<path> Start path.
-n --no-subsize Do not store size of directories [default: False].
-a --archive Handle archive file [default: False].
-f --force Do not ask when updating the catalog [default: False].
-d --directory Only directory (default: False).
-b --script Output script to manage found file(s) [default: False].
-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.
--catalog=<path> Path to the catalog [default: {2}].
--meta=<meta> Additional attribute to store [default: ].
-p --path=<path> Start path.
-l --lpath=<path> Path where changes are logged [default: ]
-n --no-subsize Do not store size of directories [default: False].
-a --archive Handle archive file [default: False].
-f --force Do not ask when updating the catalog [default: False].
-d --directory Only directory (default: False).
-b --script Output script to manage found file(s) [default: False].
-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.
""".format(BANNER, NAME, CATALOGPATH)
@ -103,6 +104,7 @@ def cmd_update(args, noder, catalog, top, debug=False):
path = args['<path>']
name = args['<name>']
hash = args['--hash']
logpath = args['--lpath']
subsize = not args['--no-subsize']
if not os.path.exists(path):
Logger.err('\"{}\" does not exist'.format(path))
@ -112,7 +114,8 @@ def cmd_update(args, noder, catalog, top, debug=False):
Logger.err('storage named \"{}\" does not exist'.format(name))
return
start = datetime.datetime.now()
walker = Walker(noder, hash=hash, debug=debug)
walker = Walker(noder, hash=hash, debug=debug,
logpath=logpath)
cnt = walker.reindex(path, root, top)
if subsize:
noder.rec_size(root)

@ -96,3 +96,10 @@ class Logger:
def bold(string):
'''make it bold'''
return '{}{}{}'.format(Logger.BOLD, string, Logger.RESET)
def flog(path, string, append=True):
mode = 'w'
if append:
mode = 'a'
with open(path, mode) as f:
f.write(string)

@ -15,16 +15,19 @@ class Walker:
MAXLINE = 80 - 15
def __init__(self, noder, hash=True, debug=False):
def __init__(self, noder, hash=True, debug=False,
logpath=None):
'''
@noder: the noder to use
@hash: calculate hash of nodes
@debug: debug mode
@logpath: path where to log catalog changes on reindex
'''
self.noder = noder
self.hash = hash
self.noder.set_hashing(self.hash)
self.debug = debug
self.lpath = logpath
def index(self, path, parent, name, storagepath=''):
'''index a directory and store in tree'''
@ -83,7 +86,7 @@ class Walker:
self._debug('\tskip file {}'.format(sub))
self.noder.flag(n)
continue
Logger.out('- update catalag for \"{}\"'.format(sub))
self._log2file('update catalog for \"{}\"'.format(sub))
n = self.noder.file_node(os.path.basename(f), sub,
parent, storagepath)
self.noder.flag(n)
@ -95,7 +98,7 @@ class Walker:
treepath = os.path.join(storagepath, d)
reindex, dummy = self._need_reindex(parent, sub, treepath)
if reindex:
Logger.out('- update catalog for \"{}\"'.format(sub))
self._log2file('update catalog for \"{}\"'.format(sub))
dummy = self.noder.dir_node(base, sub, parent, storagepath)
cnt += 1
self.noder.flag(dummy)
@ -147,3 +150,10 @@ class Walker:
if len(string) > self.MAXLINE:
string = string[:self.MAXLINE] + '...'
Logger.progr('indexing: {:80}'.format(string))
def _log2file(self, string):
'''log to file'''
if not self.lpath:
return
line = '{}\n'.format(string)
Logger.flog(self.lpath, line, append=True)

@ -60,7 +60,8 @@ class TestIndexing(unittest.TestCase):
tmpdirname = 'tmpdir'
args = {'<path>': dirpath, '<name>': tmpdirname,
'--hash': True, '--meta': ['some meta'],
'--no-subsize': False, '--verbose': True}
'--no-subsize': False, '--verbose': True,
'--lpath': None}
# index the directory
unix_tree(dirpath)

Loading…
Cancel
Save