-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export license policy to JSON and YAML (#2356)
* feat: export license policy to JSON and YAML * fix: typo in unittest
- Loading branch information
Showing
11 changed files
with
473 additions
and
15 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
83 changes: 83 additions & 0 deletions
83
backend/application/licenses/services/export_license_policy.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,83 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import yaml | ||
|
||
from application.commons.services.export import object_to_json | ||
from application.licenses.models import License_Policy, License_Policy_Item | ||
from application.licenses.services.license_policy import get_ignore_component_type_list | ||
|
||
|
||
@dataclass | ||
class License_Policy_Export_Item: | ||
evaluation_result: str | ||
spdx_license: Optional[str] = None | ||
license_expression: Optional[str] = None | ||
unknown_license: Optional[str] = None | ||
license_group: Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class License_Policy_Export_Ignore_Component_Type: | ||
component_type: str | ||
|
||
|
||
@dataclass | ||
class License_Policy_Export: | ||
name: str | ||
description: str | ||
items: list[License_Policy_Export_Item] | ||
ignore_component_types: list[License_Policy_Export_Ignore_Component_Type] | ||
|
||
|
||
def export_license_policy_yaml(license_policy: License_Policy) -> str: | ||
return yaml.dump(json.loads(export_license_policy_json(license_policy))) | ||
|
||
|
||
def export_license_policy_json(license_policy: License_Policy) -> str: | ||
return object_to_json(_create_license_policy_export(license_policy)) | ||
|
||
|
||
def _create_license_policy_export( | ||
license_policy: License_Policy, | ||
) -> License_Policy_Export: | ||
license_policy_eport = License_Policy_Export( | ||
name=license_policy.name, | ||
description=license_policy.description, | ||
items=[], | ||
ignore_component_types=get_ignore_component_type_list( | ||
license_policy.ignore_component_types | ||
), | ||
) | ||
|
||
license_policy_item: Optional[License_Policy_Item] = None | ||
for license_policy_item in license_policy.license_policy_items.all(): | ||
if license_policy_item.license_group: | ||
for spdx_license in license_policy_item.license_group.licenses.all(): | ||
license_policy_eport_item = License_Policy_Export_Item( | ||
spdx_license=spdx_license.spdx_id, | ||
license_group=license_policy_item.license_group.name, | ||
evaluation_result=license_policy_item.evaluation_result, | ||
) | ||
license_policy_eport.items.append(license_policy_eport_item) | ||
elif license_policy_item.license: | ||
license_policy_eport_item = License_Policy_Export_Item( | ||
spdx_license=license_policy_item.license.spdx_id, | ||
evaluation_result=license_policy_item.evaluation_result, | ||
) | ||
license_policy_eport.items.append(license_policy_eport_item) | ||
elif license_policy_item.license_expression: | ||
license_policy_eport_item = License_Policy_Export_Item( | ||
license_expression=license_policy_item.license_expression, | ||
evaluation_result=license_policy_item.evaluation_result, | ||
) | ||
license_policy_eport.items.append(license_policy_eport_item) | ||
elif license_policy_item.unknown_license: | ||
license_policy_eport_item = License_Policy_Export_Item( | ||
unknown_license=license_policy_item.unknown_license, | ||
evaluation_result=license_policy_item.evaluation_result, | ||
) | ||
license_policy_eport.items.append(license_policy_eport_item) | ||
|
||
return license_policy_eport |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.