3030from typing import Set
3131from typing import Tuple
3232from typing import Optional
33+ from tqdm import tqdm
3334
3435import packageurl
3536from django .db import DataError
3637from django .core import serializers
3738
39+ from django .core .management .base import BaseCommand
3840from vulnerabilities import models
3941from vulnerabilities .data_source import Advisory , DataSource
4042from vulnerabilities .data_source import PackageURL
@@ -78,7 +80,7 @@ def __init__(self, importer: models.Importer, batch_size: int):
7880 self .importer = importer
7981 self .batch_size = batch_size
8082
81- def run (self , cutoff_date : datetime .datetime = None ) -> None :
83+ def run (self , cutoff_date : datetime .datetime = None , command : BaseCommand = None ) -> None :
8284 """
8385 Create a data source for the given importer and store the data retrieved in the database.
8486
@@ -92,7 +94,7 @@ def run(self, cutoff_date: datetime.datetime = None) -> None:
9294 logger .info (f"Starting import for { self .importer .name } ." )
9395 data_source = self .importer .make_data_source (self .batch_size , cutoff_date = cutoff_date )
9496 with data_source :
95- process_advisories (data_source )
97+ process_advisories (data_source , command = command )
9698 self .importer .last_run = datetime .datetime .now (tz = datetime .timezone .utc )
9799 self .importer .data_source_cfg = dataclasses .asdict (data_source .config )
98100 self .importer .save ()
@@ -113,13 +115,17 @@ def get_vuln_pkg_refs(vulnerability, package):
113115 )
114116
115117
116- def process_advisories (data_source : DataSource ) -> None :
118+ def process_advisories (data_source : DataSource , command : BaseCommand = None ) -> None :
117119 bulk_create_vuln_pkg_refs = set ()
118120 # Treat updated_advisories and added_advisories as same. Eventually
119121 # we want to refactor all data sources to provide advisories via a
120122 # single method.
121- advisory_batches = chain (data_source .updated_advisories (), data_source .added_advisories ())
122- for batch in advisory_batches :
123+ if command :
124+ command .stdout .write ("Collecting the data from the source .." )
125+ advisory_batches = list (chain (data_source .updated_advisories (), data_source .added_advisories ()))
126+ if command :
127+ command .stdout .write ("installing the data on database ..." )
128+ for batch in tqdm (advisory_batches ):
123129 for advisory in batch :
124130 try :
125131 vuln , vuln_created = _get_or_create_vulnerability (advisory )
0 commit comments