-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcollectpackagesdata.py
More file actions
62 lines (50 loc) · 2.02 KB
/
collectpackagesdata.py
File metadata and controls
62 lines (50 loc) · 2.02 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.
# DejaCode is a trademark of nexB Inc.
# SPDX-License-Identifier: AGPL-3.0-only
# See https://github.com/nexB/dejacode for support or download.
# See https://aboutcode.org for more information about AboutCode FOSS projects.
#
from django.db.models import Q
from component_catalog.models import Package
from dje.management.commands import DataspacedCommand
class Command(DataspacedCommand):
help = (
"Collects and saves md5, sha1, and size values where one of those "
"are missing in the given Dataspace on Package instances."
)
def add_arguments(self, parser):
super().add_arguments(parser)
parser.add_argument(
"--save",
action="store_true",
dest="save",
default=False,
help="Use save() in place of update() (default) to trigger all "
"associated logic and signals. Fields such as last_modified_date "
"will be updated.",
)
def handle(self, *args, **options):
super().handle(*args, **options)
packages = (
Package.objects.scope(self.dataspace)
.exclude(download_url="")
.filter(Q(md5="") | Q(sha1="") | Q(size__isnull=True))
)
self.stdout.write(f"{packages.count()} Packages in the queue.")
update_count = 0
for package in packages:
self.stdout.write(f"Collecting: {package.download_url}")
update_fields = package.collect_data(save=False)
if not update_fields:
continue
if options["save"]:
package.save()
else:
Package.objects.filter(pk=package.pk).update(
**{field: getattr(package, field) for field in update_fields}
)
self.stdout.write(f"{', '.join(update_fields)} updated")
update_count += 1
msg = f"{update_count} Package(s) updated."
self.stdout.write(self.style.SUCCESS(msg))