Пример #1
0
 def _restore_json(self, string):
     '''restore the tree from json'''
     imp = JsonImporter()
     root = imp.import_(string)
     if self.verbose:
         Logger.info('Catalog imported from json \"{}\"'.format(self.path))
     return root
Пример #2
0
 def _rec_size(self, node, store=True):
     '''
     recursively traverse tree and return size
     @store: store the size in the node
     '''
     if self.verbose:
         Logger.info('getting node size recursively')
     if node.type == self.TYPE_FILE:
         return node.size
     size = 0
     for i in node.children:
         if node.type == self.TYPE_DIR:
             sz = self._rec_size(i, store=store)
             if store:
                 i.size = sz
             size += sz
         if node.type == self.TYPE_STORAGE:
             sz = self._rec_size(i, store=store)
             if store:
                 i.size = sz
             size += sz
         else:
             continue
     if store:
         node.size = size
     return size
Пример #3
0
 def _restore_pickle(self):
     '''restore the pickled tree'''
     root = pickle.load(open(self.path, 'rb'))
     if self.verbose:
         m = 'Catalog imported from pickle \"{}\"'.format(self.path)
         Logger.info(m)
     return root
Пример #4
0
 def _save_json(self, node):
     '''export the catalog in json'''
     exp = JsonExporter(indent=2, sort_keys=True)
     with open(self.path, 'w') as f:
         exp.write(node, f)
     if self.verbose:
         Logger.info('Catalog saved to json \"{}\"'.format(self.path))
     return True
Пример #5
0
    def find_name(self,
                  root,
                  key,
                  script=False,
                  directory=False,
                  startpath=None,
                  parentfromtree=False,
                  fmt='native'):
        '''
        find files based on their names
        @script: output script
        @directory: only search for directories
        @startpath: node to start with
        @parentfromtree: get path from parent instead of stored relpath
        @fmt: output format
        '''
        self._debug('searching for \"{}\"'.format(key))
        start = root
        if startpath:
            start = self.get_node(root, startpath)
        self.term = key
        found = anytree.findall(start, filter_=self._find_name)
        paths = []
        for f in found:
            if f.type == self.TYPE_STORAGE:
                # ignore storage nodes
                continue
            if directory and f.type != self.TYPE_DIR:
                # ignore non directory
                continue

            # print the node
            if fmt == 'native':
                self._print_node(f,
                                 withpath=True,
                                 withdepth=True,
                                 withstorage=True,
                                 recalcparent=parentfromtree)
            elif fmt == 'csv':
                self._node_to_csv(f)

            if parentfromtree:
                paths.append(self._get_parents(f))
            else:
                paths.append(f.relpath)

        if script:
            tmp = ['${source}/' + x for x in paths]
            cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp))
            Logger.info(cmd)

        return found
Пример #6
0
 def rec_size(self, node):
     '''recursively traverse tree and store dir size'''
     if self.verbose:
         Logger.info('getting directory size recursively')
     if node.type == self.TYPE_FILE:
         return node.size
     size = 0
     for i in node.children:
         if node.type == self.TYPE_DIR:
             size += self.rec_size(i)
         if node.type == self.TYPE_STORAGE:
             self.rec_size(i)
         else:
             continue
     node.size = size
     return size
Пример #7
0
 def find_name(self, root, key, script=False):
     '''find files based on their names'''
     if self.verbose:
         Logger.info('searching for \"{}\"'.format(key))
     self.term = key
     found = anytree.findall(root, filter_=self._find_name)
     paths = []
     for f in found:
         if f.type == self.TYPE_STORAGE:
             # ignore storage nodes
             continue
         self._print_node(f, withpath=True, withdepth=True,
                          withstorage=True)
         paths.append(f.relpath)
     if script:
         tmp = ['${source}/' + x for x in paths]
         cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp))
         Logger.info(cmd)
     return found
Пример #8
0
 def walk(self, root, path, rec=False):
     '''walk the tree for ls based on names'''
     if self.verbose:
         Logger.info('walking path: \"{}\"'.format(path))
     r = anytree.resolver.Resolver('name')
     found = []
     try:
         found = r.glob(root, path)
         if len(found) < 1:
             return []
         if rec:
             self.print_tree(found[0].parent)
             return found
         found = sorted(found, key=self._sort, reverse=self.sortsize)
         self._print_node(found[0].parent, withpath=False, withdepth=True)
         for f in found:
             self._print_node(f, withpath=False, pre='- ', withdepth=True)
     except anytree.resolver.ChildResolverError:
         pass
     return found
Пример #9
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)
Пример #10
0
 def find_name(self,
               root,
               key,
               script=False,
               directory=False,
               startpath=None,
               parentfromtree=False):
     '''find files based on their names'''
     if self.verbose:
         Logger.info('searching for \"{}\"'.format(key))
     start = root
     if startpath:
         start = self.get_node(root, startpath)
     self.term = key
     found = anytree.findall(start, filter_=self._find_name)
     paths = []
     for f in found:
         if f.type == self.TYPE_STORAGE:
             # ignore storage nodes
             continue
         if directory and f.type != self.TYPE_DIR:
             # ignore non directory
             continue
         self._print_node(f,
                          withpath=True,
                          withdepth=True,
                          withstorage=True,
                          recalcparent=parentfromtree)
         if parentfromtree:
             paths.append(self._get_parents(f))
         else:
             paths.append(f.relpath)
     if script:
         tmp = ['${source}/' + x for x in paths]
         cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp))
         Logger.info(cmd)
     return found
Пример #11
0
 def to_dot(self, node, path='tree.dot'):
     '''export to dot for graphing'''
     anytree.exporter.DotExporter(node).to_dotfile(path)
     Logger.info('dot file created under \"{}\"'.format(path))
     return 'dot {} -T png -o /tmp/tree.png'.format(path)
Пример #12
0
 def _save_pickle(self, node):
     '''pickle the catalog'''
     pickle.dump(node, open(self.path, 'wb'))
     if self.verbose:
         Logger.info('Catalog saved to pickle \"{}\"'.format(self.path))
     return True