-
-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathcompute_advisory_content_hash.py
More file actions
62 lines (46 loc) · 1.95 KB
/
compute_advisory_content_hash.py
File metadata and controls
62 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from aboutcode.pipeline import LoopProgress
from vulnerabilities.models import AdvisoryV2
from vulnerabilities.pipelines import VulnerableCodePipeline
from vulnerabilities.utils import compute_advisory_content
class ComputeAdvisoryContentHash(VulnerableCodePipeline):
"""Compute Advisory Content Hash for Advisory."""
pipeline_id = "compute_advisory_content_hash_v2"
@classmethod
def steps(cls):
return (cls.compute_advisory_content_hash,)
def compute_advisory_content_hash(self):
"""Compute Advisory Content Hash for Advisory."""
advisories = AdvisoryV2.objects.filter(advisory_content_hash__isnull=True)
advisories_count = advisories.count()
progress = LoopProgress(
total_iterations=advisories_count,
logger=self.log,
progress_step=1,
)
to_update = []
batch_size = 5000
for advisory in progress.iter(advisories.iterator(chunk_size=batch_size)):
advisory.advisory_content_hash = compute_advisory_content(advisory)
to_update.append(advisory)
if len(to_update) >= batch_size:
AdvisoryV2.objects.bulk_update(
to_update,
["advisory_content_hash"],
batch_size=batch_size,
)
to_update.clear()
if to_update:
AdvisoryV2.objects.bulk_update(
to_update,
["advisory_content_hash"],
batch_size=batch_size,
)
self.log("Finished computing advisory_content_hash")