Skip to content

Commit 1127988

Browse files
author
Glenn Snyder
authored
Merge pull request #28 from blackducksoftware/gsnyder/license-info
Gsnyder/license info
2 parents 1f11e25 + 885190e commit 1127988

File tree

5 files changed

+120
-5
lines changed

5 files changed

+120
-5
lines changed

blackduck/HubRestApi.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,41 @@ def download_report(self, report_id):
544544
url = self.get_urlbase() + "/api/reports/{}".format(report_id)
545545
return self.execute_get(url, {'Content-Type': 'application/zip', 'Accept':'application/zip'})
546546

547+
##
548+
#
549+
# License stuff
550+
#
551+
##
552+
def _get_license_info(self, license_obj):
553+
if 'license' in license_obj:
554+
license_info = {}
555+
text_json = {}
556+
logging.debug("license: {}".format(license_obj))
557+
response = self.execute_get(license_obj['license'])
558+
if response.status_code == 200:
559+
license_info = response.json()
560+
text_url = self.get_link(license_info, 'text')
561+
response = self.execute_get(text_url)
562+
if response.status_code == 200:
563+
text_json = response.text
564+
yield {"license_info": license_info,
565+
"license_text_info": text_json}
566+
elif 'licenses' in license_obj and isinstance(license_obj['licenses'], list):
567+
for license in license_obj['licenses']:
568+
self._get_license_info(license)
569+
570+
def get_license_info_for_bom_component(self, bom_component, limit=1000):
571+
self._check_version_compatibility()
572+
all_licenses = {}
573+
logging.debug("gathering license info for bom component {}, version {}".format(
574+
bom_component['componentName'], bom_component['componentVersionName']))
575+
for license in bom_component.get('licenses', []):
576+
for license_info_obj in self._get_license_info(license):
577+
all_licenses.update({
578+
license['licenseDisplay']: license_info_obj
579+
})
580+
return all_licenses
581+
547582
##
548583
#
549584
# Files and Snippet matching
@@ -564,11 +599,10 @@ def get_file_bom_entries(self, hub_release_id, limit=100):
564599
jsondata = response.json()
565600
return jsondata
566601

567-
def get_file_matches_for_component_with_version(self, project_id, version_id, component_id, component_version_id, limit=1000):
602+
def get_file_matches_for_bom_component(self, bom_component, limit=1000):
568603
self._check_version_compatibility()
604+
url = self.get_link(bom_component, "matched-files")
569605
paramstring = self.get_limit_paramstring(limit)
570-
url = "{}/projects/{}/versions/{}/components/{}/versions/{}/matched-files".format(
571-
self.get_apibase(), project_id, version_id, component_id, component_version_id)
572606
logging.debug("GET {}".format(url))
573607
response = self.execute_get(url)
574608
jsondata = response.json()

blackduck/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
VERSION = (0, 0, 12)
2+
VERSION = (0, 0, 14)
33

44
__version__ = '.'.join(map(str, VERSION))

examples/create_policy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"operation":"EQ",
1717
"parameters":{"values":["0"]}}]},"wait":True}
1818

19-
import pdb; pdb.set_trace()
2019
result = hub.create_policy(policy_data)
2120

2221
print(result)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
6+
from blackduck.HubRestApi import HubInstance
7+
8+
9+
parser = argparse.ArgumentParser("Retreive BOM component file matches for the given project and version")
10+
parser.add_argument("project_name")
11+
parser.add_argument("version")
12+
13+
args = parser.parse_args()
14+
15+
16+
hub = HubInstance()
17+
18+
project = hub.get_project_by_name(args.project_name)
19+
version = hub.get_version_by_name(project, args.version)
20+
21+
bom_components = hub.get_version_components(version)
22+
23+
project_id = project['_meta']['href'].split("/")[-1]
24+
version_id = version['_meta']['href'].split("/")[-1]
25+
26+
all_matches = dict()
27+
28+
for bom_component in bom_components['items']:
29+
# Retrieve the file matches for this bom component and insert those matches along with the bom component info
30+
# into the all_matches dictionary
31+
component_file_matches = hub.get_file_matches_for_bom_component(bom_component)
32+
all_matches.update({"{}, {}".format(bom_component['componentName'], bom_component['componentVersionName']): {
33+
"component_info": bom_component,
34+
"component_file_matches": component_file_matches
35+
}
36+
})
37+
38+
print(json.dumps(all_matches))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
import logging
6+
import sys
7+
8+
from blackduck.HubRestApi import HubInstance
9+
10+
11+
parser = argparse.ArgumentParser("Retreive BOM component license information for the given project and version")
12+
parser.add_argument("project_name")
13+
parser.add_argument("version")
14+
15+
args = parser.parse_args()
16+
17+
18+
hub = HubInstance()
19+
20+
project = hub.get_project_by_name(args.project_name)
21+
version = hub.get_version_by_name(project, args.version)
22+
23+
bom_components = hub.get_version_components(version)
24+
25+
project_id = project['_meta']['href'].split("/")[-1]
26+
version_id = version['_meta']['href'].split("/")[-1]
27+
28+
all_licenses = dict()
29+
30+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
31+
logging.getLogger("requests").setLevel(logging.WARNING)
32+
logging.getLogger("urllib3").setLevel(logging.WARNING)
33+
34+
for bom_component in bom_components['items']:
35+
# Retrieve the licenses and license text for this bom component and insert the license info along with the bom component info
36+
# into the all_licenses dictionary
37+
component_licenses = hub.get_license_info_for_bom_component(bom_component)
38+
all_licenses.update({"{}, {}".format(bom_component['componentName'], bom_component['componentVersionName']): {
39+
"component_info": bom_component,
40+
"component_licenses": component_licenses
41+
}
42+
})
43+
44+
print(json.dumps(all_licenses))

0 commit comments

Comments
 (0)