diff --git a/catcli/catcli.py b/catcli/catcli.py index 7aa6a98..9f41c9c 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -37,7 +37,7 @@ USAGE = """ Usage: {1} index [--catalog=] [--meta=...] [-acfnV] - {1} update [--catalog=] [-acfnV] + {1} update [--catalog=] [-acfnV] [--lpath=] {1} ls [--catalog=] [-arVS] [] {1} find [--catalog=] [-abdVP] [--path=] {1} rm [--catalog=] [-fV] @@ -50,21 +50,22 @@ Usage: {1} --version Options: - --catalog= Path to the catalog [default: {2}]. - --meta= Additional attribute to store [default: ]. - -p --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 to the catalog [default: {2}]. + --meta= Additional attribute to store [default: ]. + -p --path= Start path. + -l --lpath= 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[''] name = args[''] 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) diff --git a/catcli/logger.py b/catcli/logger.py index a056d01..64ded23 100644 --- a/catcli/logger.py +++ b/catcli/logger.py @@ -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) diff --git a/catcli/walker.py b/catcli/walker.py index 45f0bad..ebc6fbb 100644 --- a/catcli/walker.py +++ b/catcli/walker.py @@ -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) diff --git a/tests/test_update.py b/tests/test_update.py index c881862..7bcdb66 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -60,7 +60,8 @@ class TestIndexing(unittest.TestCase): tmpdirname = 'tmpdir' args = {'': dirpath, '': tmpdirname, '--hash': True, '--meta': ['some meta'], - '--no-subsize': False, '--verbose': True} + '--no-subsize': False, '--verbose': True, + '--lpath': None} # index the directory unix_tree(dirpath)