log updates to file for #10

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

@ -37,7 +37,7 @@ USAGE = """
Usage: Usage:
{1} index [--catalog=<path>] [--meta=<meta>...] [-acfnV] <name> <path> {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} ls [--catalog=<path>] [-arVS] [<path>]
{1} find [--catalog=<path>] [-abdVP] [--path=<path>] <term> {1} find [--catalog=<path>] [-abdVP] [--path=<path>] <term>
{1} rm [--catalog=<path>] [-fV] <storage> {1} rm [--catalog=<path>] [-fV] <storage>
@ -50,21 +50,22 @@ Usage:
{1} --version {1} --version
Options: Options:
--catalog=<path> Path to the catalog [default: {2}]. --catalog=<path> Path to the catalog [default: {2}].
--meta=<meta> Additional attribute to store [default: ]. --meta=<meta> Additional attribute to store [default: ].
-p --path=<path> Start path. -p --path=<path> Start path.
-n --no-subsize Do not store size of directories [default: False]. -l --lpath=<path> Path where changes are logged [default: ]
-a --archive Handle archive file [default: False]. -n --no-subsize Do not store size of directories [default: False].
-f --force Do not ask when updating the catalog [default: False]. -a --archive Handle archive file [default: False].
-d --directory Only directory (default: False). -f --force Do not ask when updating the catalog [default: False].
-b --script Output script to manage found file(s) [default: False]. -d --directory Only directory (default: False).
-S --sortsize Sort by size, largest first [default: False]. -b --script Output script to manage found file(s) [default: False].
-c --hash Calculate md5 hash [default: False]. -S --sortsize Sort by size, largest first [default: False].
-r --recursive Recursive [default: False]. -c --hash Calculate md5 hash [default: False].
-P --parent Ignore stored relpath [default: True]. -r --recursive Recursive [default: False].
-V --verbose Be verbose [default: False]. -P --parent Ignore stored relpath [default: True].
-v --version Show version. -V --verbose Be verbose [default: False].
-h --help Show this screen. -v --version Show version.
-h --help Show this screen.
""".format(BANNER, NAME, CATALOGPATH) """.format(BANNER, NAME, CATALOGPATH)
@ -103,6 +104,7 @@ def cmd_update(args, noder, catalog, top, debug=False):
path = args['<path>'] path = args['<path>']
name = args['<name>'] name = args['<name>']
hash = args['--hash'] hash = args['--hash']
logpath = args['--lpath']
subsize = not args['--no-subsize'] subsize = not args['--no-subsize']
if not os.path.exists(path): if not os.path.exists(path):
Logger.err('\"{}\" does not exist'.format(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)) Logger.err('storage named \"{}\" does not exist'.format(name))
return return
start = datetime.datetime.now() 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) cnt = walker.reindex(path, root, top)
if subsize: if subsize:
noder.rec_size(root) noder.rec_size(root)

@ -96,3 +96,10 @@ class Logger:
def bold(string): def bold(string):
'''make it bold''' '''make it bold'''
return '{}{}{}'.format(Logger.BOLD, string, Logger.RESET) 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 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 @noder: the noder to use
@hash: calculate hash of nodes @hash: calculate hash of nodes
@debug: debug mode @debug: debug mode
@logpath: path where to log catalog changes on reindex
''' '''
self.noder = noder self.noder = noder
self.hash = hash self.hash = hash
self.noder.set_hashing(self.hash) self.noder.set_hashing(self.hash)
self.debug = debug self.debug = debug
self.lpath = logpath
def index(self, path, parent, name, storagepath=''): def index(self, path, parent, name, storagepath=''):
'''index a directory and store in tree''' '''index a directory and store in tree'''
@ -83,7 +86,7 @@ class Walker:
self._debug('\tskip file {}'.format(sub)) self._debug('\tskip file {}'.format(sub))
self.noder.flag(n) self.noder.flag(n)
continue 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, n = self.noder.file_node(os.path.basename(f), sub,
parent, storagepath) parent, storagepath)
self.noder.flag(n) self.noder.flag(n)
@ -95,7 +98,7 @@ class Walker:
treepath = os.path.join(storagepath, d) treepath = os.path.join(storagepath, d)
reindex, dummy = self._need_reindex(parent, sub, treepath) reindex, dummy = self._need_reindex(parent, sub, treepath)
if reindex: 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) dummy = self.noder.dir_node(base, sub, parent, storagepath)
cnt += 1 cnt += 1
self.noder.flag(dummy) self.noder.flag(dummy)
@ -147,3 +150,10 @@ class Walker:
if len(string) > self.MAXLINE: if len(string) > self.MAXLINE:
string = string[:self.MAXLINE] + '...' string = string[:self.MAXLINE] + '...'
Logger.progr('indexing: {:80}'.format(string)) 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' tmpdirname = 'tmpdir'
args = {'<path>': dirpath, '<name>': tmpdirname, args = {'<path>': dirpath, '<name>': tmpdirname,
'--hash': True, '--meta': ['some meta'], '--hash': True, '--meta': ['some meta'],
'--no-subsize': False, '--verbose': True} '--no-subsize': False, '--verbose': True,
'--lpath': None}
# index the directory # index the directory
unix_tree(dirpath) unix_tree(dirpath)

Loading…
Cancel
Save