generated from aboutcode-org/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update query in migrations Signed-off-by: Jono Yang <[email protected]>
- Loading branch information
Showing
5 changed files
with
198 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Generated by Django 4.1.2 on 2023-05-12 17:52 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("packagedb", "0060_remove_package_contains_source_code_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="resource", | ||
name="detected_license_expression", | ||
field=models.TextField(blank=True, help_text="TODO"), | ||
), | ||
migrations.AddField( | ||
model_name="resource", | ||
name="detected_license_expression_spdx", | ||
field=models.TextField(blank=True, help_text="TODO"), | ||
), | ||
migrations.AddField( | ||
model_name="resource", | ||
name="license_clues", | ||
field=models.JSONField( | ||
blank=True, default=list, help_text="List of license clues." | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="resource", | ||
name="license_detections", | ||
field=models.JSONField( | ||
blank=True, default=list, help_text="List of license detection details." | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="resource", | ||
name="percentage_of_license_text", | ||
field=models.FloatField(blank=True, help_text="TODO", null=True), | ||
), | ||
] |
112 changes: 112 additions & 0 deletions
112
packagedb/migrations/0062_compute_resource_license_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Generated by Django 4.1.2 on 2023-05-12 17:56 | ||
|
||
from django.db import migrations | ||
from django.db.models import Q | ||
|
||
|
||
def compute_resource_detected_license_expression(apps, schema_editor): | ||
""" | ||
Compute Resource `detected_license_expression` and | ||
`detected_license_expression_spdx` from old `license_expressions` field. | ||
From scancode.io | ||
""" | ||
from license_expression import combine_expressions | ||
from licensedcode.cache import build_spdx_license_expression | ||
|
||
Resource = apps.get_model("packagedb", "Resource") | ||
resources = Resource.objects.filter(~Q(license_expressions=[]) | Q(license_expressions__isnull=False)) | ||
|
||
for resource in resources: | ||
license_expression = str(combine_expressions(resource.license_expressions)) | ||
license_expression_spdx = build_spdx_license_expression(license_expression) | ||
resource.declared_license_expression = license_expression | ||
resource.declared_license_expression_spdx = license_expression_spdx | ||
resource.save() | ||
|
||
|
||
def _convert_matches_to_detections(license_matches): | ||
""" | ||
Return a list of scancode v32 LicenseDetection mappings from provided | ||
``license_matches``: a list of the scancode v31 LicenseMatch mappings. | ||
From scancode.io | ||
""" | ||
from license_expression import combine_expressions | ||
from licensedcode.detection import get_uuid_on_content | ||
from commoncode.text import python_safe_name | ||
|
||
match_attributes = ["score", "start_line", "end_line", "matched_text"] | ||
rule_attributes = [ | ||
"matched_length", | ||
"match_coverage", | ||
"matcher", | ||
"rule_relevance", | ||
] | ||
license_detection = {} | ||
detection_matches = [] | ||
|
||
for match in license_matches: | ||
detection_match = {} | ||
|
||
for attribute in match_attributes: | ||
detection_match[attribute] = match[attribute] | ||
for attribute in rule_attributes: | ||
detection_match[attribute] = match["matched_rule"][attribute] | ||
|
||
detection_match["rule_identifier"] = match["matched_rule"]["identifier"] | ||
detection_match["license_expression"] = match["matched_rule"][ | ||
"license_expression" | ||
] | ||
detection_match["rule_url"] = None | ||
detection_matches.append(detection_match) | ||
|
||
license_expressions = [match["license_expression"] for match in detection_matches] | ||
hashable_details = tuple( | ||
[ | ||
(match["score"], match["rule_identifier"], match["matched_text"]) | ||
for match in detection_matches | ||
] | ||
) | ||
uuid = get_uuid_on_content(hashable_details) | ||
|
||
license_detection["matches"] = detection_matches | ||
license_detection["license_expression"] = str( | ||
combine_expressions(license_expressions) | ||
) | ||
license_detection["identifier"] = "{}-{}".format( | ||
python_safe_name(license_detection["license_expression"]), uuid | ||
) | ||
|
||
return [license_detection] | ||
|
||
|
||
def compute_resource_license_detections(apps, schema_editor): | ||
""" | ||
Compute Resource `license_detections` from old `licenses` field. | ||
From scancode.io | ||
""" | ||
Resource = apps.get_model("packagedb", "Resource") | ||
resources = Resource.objects.filter(~Q(licenses=[]) | Q(licenses__isnull=False)) | ||
for resource in resources: | ||
detections = _convert_matches_to_detections(resource.licenses) | ||
resource.license_detections = detections | ||
resource.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("packagedb", "0061_add_new_scan_fields"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
compute_resource_detected_license_expression, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
migrations.RunPython( | ||
compute_resource_license_detections, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
] |
20 changes: 20 additions & 0 deletions
20
packagedb/migrations/0063_remove_resource_license_expressions_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 4.1.2 on 2023-05-12 18:28 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("packagedb", "0062_compute_resource_license_data"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="resource", | ||
name="license_expressions", | ||
), | ||
migrations.RemoveField( | ||
model_name="resource", | ||
name="licenses", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters