You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
catcli/catcli/walker.py

102 lines
3.1 KiB
Python

7 years ago
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
Catcli filesystem indexer
"""
import os
import anytree
# local imports
from catcli.noder import Noder
from catcli.logger import Logger
class Walker:
MAXLINE = 80 - 15
def __init__(self, noder, nohash=False, debug=False):
7 years ago
self.noder = noder
self.noder.set_hashing(not nohash)
self.debug = debug
7 years ago
def index(self, path, name, parent):
6 years ago
'''index a directory and store in tree'''
if not parent:
parent = noder.dir_node(name, path, parent)
cnt = 0
for (root, dirs, files) in os.walk(path):
for f in files:
sub = os.path.join(root, f)
self._log(f)
self.noder.file_node(os.path.basename(f), sub,
parent, path)
cnt += 1
for d in dirs:
base = os.path.basename(d)
sub = os.path.join(root, d)
dummy = self.noder.dir_node(base, sub, parent, path)
6 years ago
_, cnt2 = self.index(sub, base, dummy)
cnt += cnt2
break
self._log(None)
6 years ago
return parent, cnt
7 years ago
6 years ago
def reindex(self, path, parent, top):
'''reindex a directory and store in tree'''
7 years ago
cnt = 0
for (root, dirs, files) in os.walk(path):
for f in files:
sub = os.path.join(root, f)
6 years ago
if not self._need_reindex(top, sub):
self._debug('ignore {}'.format(sub))
continue
self._debug('re-index {}'.format(sub))
self._log(f)
7 years ago
self.noder.file_node(os.path.basename(f), sub,
parent, path)
7 years ago
cnt += 1
for d in dirs:
base = os.path.basename(d)
sub = os.path.join(root, d)
6 years ago
if not self._need_reindex(top, sub):
self._debug('ignore {}'.format(sub))
continue
self._debug('re-index {}'.format(sub))
dummy = self.noder.dir_node(base, sub, parent, path)
6 years ago
cnt2 = self.reindex(sub, dummy, top)
7 years ago
cnt += cnt2
break
self._log(None)
6 years ago
return cnt
def _need_reindex(self, top, path):
'''test if node needs re-indexing'''
cnode, newer = self.noder.get_node_if_newer(top, path)
if cnode and not newer:
# ignore this node
return False
if cnode and newer:
# remove this node and re-add
cnode.parent = None
return True
def _debug(self, string):
if not self.debug:
return
Logger.info(string)
def _log(self, string):
if self.debug:
return
if not string:
# clean
Logger.progr('{:80}'.format(' '))
return
if len(string) > self.MAXLINE:
string = string[:self.MAXLINE] + '...'
Logger.progr('indexing: {:80}'.format(string))