コード例 #1
0
ファイル: noder.py プロジェクト: Cloudxtreme/catcli
    def file_node(self, name, path, parent, storagepath):
        '''create a new node representing a file'''
        if not os.path.exists(path):
            Logger.err('File \"{}\" does not exist'.format(path))
            return None
        path = os.path.abspath(path)
        try:
            st = os.lstat(path)
        except OSError as e:
            Logger.err('OSError: {}'.format(e))
            return None
        md5 = None
        if self.hash:
            md5 = utils.md5sum(path)
        relpath = os.path.join(os.path.basename(storagepath),
                               os.path.relpath(path, start=storagepath))

        maccess = os.path.getmtime(path)
        n = self._node(name, self.TYPE_FILE, relpath, parent,
                       size=st.st_size, md5=md5, maccess=maccess)
        if self.arc:
            ext = os.path.splitext(path)[1][1:]
            if ext in self.decomp.get_format():
                names = self.decomp.get_names(path)
                self.list_to_tree(n, names)
        return n
コード例 #2
0
    def file_node(self, name, path, parent, storagepath):
        '''create a new node representing a file'''
        if not os.path.exists(path):
            Logger.err('File \"{}\" does not exist'.format(path))
            return None
        path = os.path.abspath(path)
        try:
            st = os.lstat(path)
        except OSError as e:
            Logger.err('OSError: {}'.format(e))
            return None
        md5 = None
        if self.hash:
            md5 = self._get_hash(path)
        relpath = os.sep.join([storagepath, name])

        maccess = os.path.getmtime(path)
        n = self._node(name,
                       self.TYPE_FILE,
                       relpath,
                       parent,
                       size=st.st_size,
                       md5=md5,
                       maccess=maccess)
        if self.arc:
            ext = os.path.splitext(path)[1][1:]
            if ext.lower() in self.decomp.get_formats():
                self._debug('{} is an archive'.format(path))
                names = self.decomp.get_names(path)
                self.list_to_tree(n, names)
            else:
                self._debug('{} is NOT an archive'.format(path))
        return n
コード例 #3
0
 def get_node(self, top, path):
     ''' get the node at path '''
     r = anytree.resolver.Resolver('name')
     try:
         return r.get(top, path)
     except anytree.resolver.ChildResolverError:
         Logger.err('No node at path \"{}\"'.format(path))
         return None
コード例 #4
0
ファイル: noder.py プロジェクト: Cloudxtreme/catcli
 def get_node(self, top, path, quiet=False):
     '''get the node by internal tree path'''
     r = anytree.resolver.Resolver('name')
     try:
         return r.get(top, path)
     except anytree.resolver.ChildResolverError:
         if not quiet:
             Logger.err('No node at path \"{}\"'.format(path))
         return None
コード例 #5
0
ファイル: noder.py プロジェクト: Cloudxtreme/catcli
 def _print_node(self, node, pre='', withpath=False,
                 withdepth=False, withstorage=False):
     '''print a node'''
     if node.type == self.TYPE_TOP:
         Logger.out('{}{}'.format(pre, node.name))
     elif node.type == self.TYPE_FILE:
         name = node.name
         if withpath:
             name = node.relpath
         if withstorage:
             storage = self._get_storage(node)
         attr = ''
         if node.md5:
             attr = ', md5:{}'.format(node.md5)
         compl = 'size:{}{}'.format(utils.human(node.size), attr)
         if withstorage:
             compl += ', storage:{}'.format(Logger.bold(storage.name))
         Logger.file(pre, name, compl)
     elif node.type == self.TYPE_DIR:
         name = node.name
         if withpath:
             name = node.relpath
         depth = ''
         if withdepth:
             depth = len(node.children)
         if withstorage:
             storage = self._get_storage(node)
         attr = []
         if node.size:
             attr.append(['totsize', utils.human(node.size)])
         if withstorage:
             attr.append(['storage', Logger.bold(storage.name)])
         Logger.dir(pre, name, depth=depth, attr=attr)
     elif node.type == self.TYPE_STORAGE:
         hf = utils.human(node.free)
         ht = utils.human(node.total)
         dt = ''
         if self._has_attr(node, 'ts'):
             dt = ', date:'
             dt += utils.epoch_to_str(node.ts)
         name = '{} (free:{}, total:{}{})'.format(node.name, hf, ht, dt)
         Logger.storage(pre, name, node.attr)
     elif node.type == self.TYPE_ARC:
         if self.arc:
             Logger.arc(pre, node.name, node.archive)
     else:
         Logger.err('Weird node encountered: {}'.format(node))
コード例 #6
0
def md5sum(path):
    ''' calculate md5 sum of a file '''
    p = os.path.realpath(path)
    if not os.path.exists(p):
        Logger.err('\nunable to get md5sum on {}'.format(path))
        return None
    try:
        with open(p, mode='rb') as f:
            d = hashlib.md5()
            while True:
                buf = f.read(4096)
                if not buf:
                    break
                d.update(buf)
            return d.hexdigest()
    except PermissionError:
        pass
    return None
コード例 #7
0
 def save(self, node):
     '''save the catalog'''
     if not self.path:
         Logger.err('Path not defined')
         return False
     d = os.path.dirname(self.path)
     if d and not os.path.exists(d):
         os.makedirs(d)
     elif os.path.exists(self.path) and not self.force:
         if not utils.ask('Update catalog \"{}\"'.format(self.path)):
             Logger.info('Catalog not saved')
             return False
     if d and not os.path.exists(d):
         Logger.err('Cannot write to \"{}\"'.format(d))
         return False
     if self.metanode:
         self.metanode.parent = node
     if self.pickle:
         return self._save_pickle(node)
     return self._save_json(node)
コード例 #8
0
ファイル: noder.py プロジェクト: birt/catcli
 def _print_node(self,
                 node,
                 pre='',
                 withpath=False,
                 withdepth=False,
                 withstorage=False,
                 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:
         # top node
         Logger.out('{}{}'.format(pre, node.name))
     elif node.type == self.TYPE_FILE:
         # node of type file
         name = node.name
         if withpath:
             if recalcparent:
                 name = os.sep.join([self._get_parents(node.parent), name])
             else:
                 name = node.relpath
         name = name.lstrip(os.sep)
         if withstorage:
             storage = self._get_storage(node)
         attr = ''
         if node.md5:
             attr = ', md5:{}'.format(node.md5)
         compl = 'size:{}{}'.format(utils.human(node.size), attr)
         if withstorage:
             compl += ', storage:{}'.format(Logger.bold(storage.name))
         Logger.file(pre, name, compl)
     elif node.type == self.TYPE_DIR:
         # node of type directory
         name = node.name
         if withpath:
             if recalcparent:
                 name = os.sep.join([self._get_parents(node.parent), name])
             else:
                 name = node.relpath
         name = name.lstrip(os.sep)
         depth = ''
         if withdepth:
             depth = len(node.children)
         if withstorage:
             storage = self._get_storage(node)
         attr = []
         if node.size:
             attr.append(['totsize', utils.human(node.size)])
         if withstorage:
             attr.append(['storage', Logger.bold(storage.name)])
         Logger.dir(pre, name, depth=depth, attr=attr)
     elif node.type == self.TYPE_STORAGE:
         # node of type storage
         hf = utils.human(node.free)
         ht = utils.human(node.total)
         nbchildren = len(node.children)
         # get the date
         dt = ''
         if self._has_attr(node, 'ts'):
             dt = 'date:{}'.format(utils.epoch_to_str(node.ts))
         ds = ''
         # the children size
         sz = self._rec_size(node, store=False)
         sz = utils.human(sz)
         ds = 'totsize:{}'.format(sz)
         # format the output
         name = '{}'.format(node.name)
         args = [
             'nbfiles:{}'.format(nbchildren), 'free:{}/{}'.format(hf, ht),
             dt, ds
         ]
         Logger.storage(pre, name, '({})'.format(','.join(args)), node.attr)
     elif node.type == self.TYPE_ARC:
         # archive node
         if self.arc:
             Logger.arc(pre, node.name, node.archive)
     else:
         Logger.err('bad node encountered: {}'.format(node))