Example #1
0
def main():
    orphans = {}

    elements_index = lib_search.ElementsIndex(message)

    for elempath in lib_repo.get_element_paths_iterator():
        oval_id = lib_repo.path_to_oval_id(elempath)
        oval_ids = { oval_id }

        upstream_ids = elements_index.find_upstream_ids(oval_ids, set())
        downstream_ids = elements_index.find_downstream_ids(oval_ids, set())

        #message("INFO", "OVAL ID '%s' had %i upstream ids and %i downstream ids" % (oval_id, len(upstream_ids), len(downstream_ids)))

        if len(upstream_ids) == 0 and len(downstream_ids) == 0:
            message("INFO", "Found Orphan '%s'" % oval_id)
            orphans[oval_id] = elempath

    response = input("\n :::: Remove all orphans? (N[o] / y[es]): ")
    if response != 'y' and response != 'Y':
        return

    message("INFO", "\n\n")
    
    for ok in orphans.keys():
        message("INFO", "Deleting Orphan '%s'" % ok)
        os.remove(orphans[ok])
Example #2
0
 def get_last(self):
     for xml_file in lib_repo.get_element_paths_iterator():
         if (self.tst_or_obj_or_ste in xml_file) and (self.family in xml_file):
             current_id = int(lib_repo.path_to_oval_id(xml_file).split(':')[3])
             if current_id > self.new_id:
                 self.new_id = current_id
                 self.path = os.path.split(xml_file)[0]
                 self.new_filename = os.path.split(xml_file)[1]
     return
Example #3
0
 def get_last_def(self):
     for xml_file in lib_repo.get_element_paths_iterator():
         if 'vulnerability' in xml_file:
             current_id = int(lib_repo.path_to_oval_id(xml_file).split(':')[3])
             if current_id > self.new_id:
                 self.new_id = current_id
                 self.path = os.path.split(xml_file)[0]
                 self.new_filename = os.path.split(xml_file)[1]
     return
 def get_last_def(self):
     for xml_file in lib_repo.get_element_paths_iterator():
         if 'vulnerability' in xml_file:
             current_id = int(
                 lib_repo.path_to_oval_id(xml_file).split(':')[3])
             if current_id > self.new_id:
                 self.new_id = current_id
                 self.path = os.path.split(xml_file)[0]
                 self.new_filename = os.path.split(xml_file)[1]
     return
 def get_last(self):
     for xml_file in lib_repo.get_element_paths_iterator():
         if (self.tst_or_obj_or_ste in xml_file) and (self.family
                                                      in xml_file):
             current_id = int(
                 lib_repo.path_to_oval_id(xml_file).split(':')[3])
             if current_id > self.new_id:
                 self.new_id = current_id
                 self.path = os.path.split(xml_file)[0]
                 self.new_filename = os.path.split(xml_file)[1]
     return
Example #6
0
    def document_iterator(self, paths=False):
        """ Iterator yielding elements files in repo as indexable documents. """

        if not paths:
            # get all in repo
            paths = lib_repo.get_element_paths_iterator()

        for path in paths:
            try:
                document = lib_xml.get_element_metadata(path)
            except lib_xml.InvalidPathError as e:
                yield { 'path': e.path, 'deleted': True }
            else:
                document = self.whoosh_escape_document(document)
                yield document
Example #7
0
    def document_iterator(self, paths=False):
        """ Iterator yielding elements files in repo as indexable documents. """

        if not paths:
            # get all in repo
            paths = lib_repo.get_element_paths_iterator()

        for path in paths:
            try:
                document = lib_xml.get_element_metadata(path)
            except lib_xml.InvalidPathError as e:
                yield {'path': e.path, 'deleted': True}
            except lib_xml.InvalidXmlError as e:
                raise
            else:
                document = self.whoosh_escape_document(document)
                yield document
Example #8
0
def main():
    orphans = {}

    elements_index = lib_search.ElementsIndex(message)

    for elempath in lib_repo.get_element_paths_iterator():
        oval_id = lib_repo.path_to_oval_id(elempath)
        oval_ids = { oval_id }

        upstream_ids = elements_index.find_upstream_ids(oval_ids, set())
        downstream_ids = elements_index.find_downstream_ids(oval_ids, set())

        message("INFO", "OVAL ID '%s' had %i upstream ids and %i downstream ids" % (oval_id, len(upstream_ids), len(downstream_ids)))

        if len(upstream_ids) == 0 and len(downstream_ids) == 0:
            orphans[oval_id] = elempath

    for ok in orphans.keys():
        message("INFO", "Found Orphan '%s'" % ok)
        os.remove(orphans[ok])
Example #9
0
def main():
    orphans = {}

    elements_index = lib_search.ElementsIndex(message)

    for elempath in lib_repo.get_element_paths_iterator():
        oval_id = lib_repo.path_to_oval_id(elempath)
        oval_ids = {oval_id}

        upstream_ids = elements_index.find_upstream_ids(oval_ids, set())
        downstream_ids = elements_index.find_downstream_ids(oval_ids, set())

        message(
            "INFO", "OVAL ID '%s' had %i upstream ids and %i downstream ids" %
            (oval_id, len(upstream_ids), len(downstream_ids)))

        if len(upstream_ids) == 0 and len(downstream_ids) == 0:
            orphans[oval_id] = elempath

    for ok in orphans.keys():
        message("INFO", "Found Orphan '%s'" % ok)
        os.remove(orphans[ok])
def main():
    start_time = time.time()

    tracking = dict()
    elements_index = lib_search.ElementsIndex(message)

    # getting all definitions
    all_def_ids = set()
    message('info', 'getting all definitions')
    for defpath in lib_repo.get_definition_paths_iterator():
        all_def_ids.add(lib_repo.path_to_oval_id(defpath))
    message('info', 'found {0} definitions'.format(len(all_def_ids)))

    # getting all element ids downstream from any definition
    message('info', 'getting all downstream element ids')
    all_downstream_ids = elements_index.find_downstream_ids(all_def_ids)
    message('info',
            'found {0} downstream element ids'.format(len(all_downstream_ids)))

    # get elements that aren't in all_downstream_ids
    message('info', 'checking all elements')
    cur_element_type = None
    for elempath in lib_repo.get_element_paths_iterator():
        oval_id = lib_repo.path_to_oval_id(elempath)
        element_type = lib_repo.get_element_type_from_oval_id(oval_id)

        # skip definitions... we're only pruning child elements
        if element_type == 'definition':
            continue

        # write status msg
        if element_type != cur_element_type:
            cur_element_type = element_type
            i_element_type = 0
        i_element_type = i_element_type + 1
        sys.stdout.write(
            'Analyzing {0}s: {1}                            \r'.format(
                cur_element_type, i_element_type))
        sys.stdout.flush()

        # it's an orphan if it's not downsteam of a definition
        track_as = 'orphan' if oval_id not in all_downstream_ids else 'in_use'
        if not track_as in tracking:
            tracking[track_as] = dict()
        if not element_type in tracking[track_as]:
            tracking[track_as][element_type] = set()
        tracking[track_as][element_type].add(oval_id)

    sys.stdout.write(
        '                                                               \r'.
        format(cur_element_type, i_element_type))
    sys.stdout.flush()

    # generate report
    report = []
    for track_as, elements_by_type in tracking.items():
        report.append('\t{0}:'.format(track_as.replace('_', ' ').capitalize()))
        for element_type, oval_ids in elements_by_type.items():
            report.append('\t\t{0} {1}s'.format(len(oval_ids), element_type))
    message('found', '\n'.join(report))

    response = input("\n :::: Remove all orphans? (N[o] / y[es]): ")
    if response.lower() == 'y':
        orphan_ids = set()
        for element_type, oval_ids in tracking['orphan'].items():
            orphan_ids.update(oval_ids)

        file_paths = elements_index.get_paths_from_ids(orphan_ids)
        for file_path in file_paths:
            message("INFO",
                    "Deleting Orphan '%s'" % os.path.basename(file_path))
            os.remove(file_path)

    seconds_elapsed = time.time() - start_time
    message('info',
            'Completed in {0}!'.format(format_duration(seconds_elapsed)))
Example #11
0
def main():
    start_time = time.time()

    tracking = dict()
    elements_index = lib_search.ElementsIndex(message)
    
    # getting all definitions
    all_def_ids = set()
    message('info', 'getting all definitions')
    for defpath in lib_repo.get_definition_paths_iterator():
        all_def_ids.add(lib_repo.path_to_oval_id(defpath))
    message('info', 'found {0} definitions'.format(len(all_def_ids)))

    # getting all element ids downstream from any definition
    message('info', 'getting all downstream element ids')
    all_downstream_ids = elements_index.find_downstream_ids(all_def_ids)
    message('info', 'found {0} downstream element ids'.format(len(all_downstream_ids)))

    # get elements that aren't in all_downstream_ids
    message('info', 'checking all elements')
    cur_element_type = None
    for elempath in lib_repo.get_element_paths_iterator():
        oval_id = lib_repo.path_to_oval_id(elempath)
        element_type = lib_repo.get_element_type_from_oval_id(oval_id)

        # skip definitions... we're only pruning child elements
        if element_type == 'definition':
            continue

        # write status msg
        if element_type != cur_element_type:
            cur_element_type = element_type
            i_element_type = 0
        i_element_type = i_element_type + 1
        sys.stdout.write('Analyzing {0}s: {1}                            \r'.format(cur_element_type, i_element_type))
        sys.stdout.flush()

        # it's an orphan if it's not downsteam of a definition
        track_as = 'orphan' if oval_id not in all_downstream_ids else 'in_use'
        if not track_as in tracking:
            tracking[track_as] = dict()
        if not element_type in tracking[track_as]:
            tracking[track_as][element_type] = set()
        tracking[track_as][element_type].add(oval_id)

    sys.stdout.write('                                                               \r'.format(cur_element_type, i_element_type))
    sys.stdout.flush()

    # generate report
    report = []
    for track_as, elements_by_type in tracking.items():
        report.append('\t{0}:'.format(track_as.replace('_', ' ').capitalize()))
        for element_type, oval_ids in elements_by_type.items():
            report.append('\t\t{0} {1}s'.format(len(oval_ids), element_type))
    message('found', '\n'.join(report))

    response = input("\n :::: Remove all orphans? (N[o] / y[es]): ")
    if response.lower() == 'y':
        orphan_ids = set()
        for element_type, oval_ids in tracking['orphan'].items():
            orphan_ids.update(oval_ids)
        
        file_paths = elements_index.get_paths_from_ids(orphan_ids)
        for file_path in file_paths:
            message("INFO", "Deleting Orphan '%s'" % os.path.basename(file_path))
            os.remove(file_path)

    seconds_elapsed = time.time() - start_time
    message('info','Completed in {0}!'.format(format_duration(seconds_elapsed)))