The ImportRunner is responsible for inserting and updating data about vulnerabilities and affected/unaffected/fixed packages in the database. The main goal for the implementation is correctness Correctness: - There must be no duplicates in the database (should be enforced b
| 38 | |
| 39 | |
| 40 | class ImportRunner: |
| 41 | """ |
| 42 | The ImportRunner is responsible for inserting and updating data about vulnerabilities and |
| 43 | affected/unaffected/fixed packages in the database. The main goal for the implementation |
| 44 | is correctness |
| 45 | |
| 46 | Correctness: |
| 47 | - There must be no duplicates in the database (should be enforced by the schema). |
| 48 | - No valid data from the data source must be skipped or truncated. |
| 49 | """ |
| 50 | |
| 51 | def __init__(self, importer_class: Importer): |
| 52 | self.importer_class = importer_class |
| 53 | |
| 54 | def run(self) -> None: |
| 55 | """ |
| 56 | Create a data source for the given importer and store the data retrieved in the database. |
| 57 | """ |
| 58 | importer_name = self.importer_class.qualified_name |
| 59 | importer_class = self.importer_class |
| 60 | logger.info(f"Starting import for {importer_name}") |
| 61 | advisory_datas = importer_class().advisory_data() |
| 62 | count = self.process_advisories(advisory_datas=advisory_datas, importer_name=importer_name) |
| 63 | logger.info(f"Finished import for {importer_name}. Imported {count} advisories.") |
| 64 | |
| 65 | def do_import(self, advisories) -> None: |
| 66 | advisory_importer = DefaultImporter(advisories=advisories) |
| 67 | logger.info(f"Running importer: {advisory_importer.qualified_name}") |
| 68 | importer_name = advisory_importer.qualified_name |
| 69 | advisories = [] |
| 70 | for advisory in advisory_importer.interesting_advisories: |
| 71 | if advisory.date_imported: |
| 72 | continue |
| 73 | logger.info(f"Processing advisory: {advisory!r}") |
| 74 | advisory_data = None |
| 75 | inferences = None |
| 76 | try: |
| 77 | advisory_data = advisory.to_advisory_data() |
| 78 | inferences = advisory_importer.get_inferences(advisory_data=advisory_data) |
| 79 | process_inferences( |
| 80 | inferences=inferences, |
| 81 | advisory=advisory, |
| 82 | improver_name=importer_name, |
| 83 | ) |
| 84 | except Exception: |
| 85 | from pprint import pformat |
| 86 | |
| 87 | logger.warning( |
| 88 | f"Failed to process advisory:\n{pformat(advisory_data.to_dict())}\n\n" |
| 89 | f"with error:\n{traceback_format_exc()}\n\n" |
| 90 | ) |
| 91 | logger.info("Finished importing using %s.", advisory_importer.__class__.qualified_name) |
| 92 | |
| 93 | def process_advisories( |
| 94 | self, advisory_datas: Iterable[AdvisoryData], importer_name: str |
| 95 | ) -> List: |
| 96 | """ |
| 97 | Insert advisories into the database |
no outgoing calls