Пример #1
0
    def store(self, xml_files, parsed=None, inventory=None):
        """
        Inserts xml file(s) into database.

        xml_files       -   a list or a single nmap xml output
        parsed          -   a NmapParserSAX object or None
        inventory       -   inventory that scans will be added to, or None
                            to create a new one
        """
        debug("Inserting file(s) into databaseng: %r", xml_files)

        if inventory:
            self.invchanges = UpdateChanges(self)

        if isinstance(xml_files, str):
            # using singe file
            xml_files = [xml_files, ]

        for xml_file in xml_files:
            self.xml_file = xml_file
            if parsed: # used only for single file
                self.parsed = parsed
            else:
                self.parsed = self.parse(xml_file)

            self.scan = self.scan_from_xml()
            self.scaninfo = self.scaninfo_from_xml()
            self.hosts = self.hosts_from_xml()

            if inventory:
                debug("Inserting scan into Inventory %r", inventory)
                inv_id = self.get_inventory_id_from_db(inventory)
                if not inv_id: # create new inventory
                    self.insert_inventory_db(inventory)
                    inv_id = self.get_id_for("inventory")
                self.insert_inventory_scan_db(self.scan["pk"], inv_id)

        if inventory:
            # update list of changes for inventory
            debug("Updating changes for Inventory %r", inventory)
            self.invchanges.do_update(inv_id)

        self.conn.commit()

        debug("%r inserted into database.", xml_files)