28
28
from blackduck import Client
29
29
from collections import defaultdict
30
30
31
+ def getlicensesfromprojectversion (subproject ):
32
+ subprojecturl = subproject ['_meta' ]['href' ]
33
+ x = subprojecturl .split ("/" )
34
+ del x [5 ]
35
+ del x [5 ]
36
+ del x [5 ]
37
+ del x [5 ]
38
+ subprojecturl = "/" .join (x )
39
+ version = bd .session .get (subprojecturl ).json ()
40
+ components = bd .get_resource ('components' ,version )
41
+ licenselist = []
42
+ for component in components :
43
+ for license in component ['licenses' ]:
44
+ if license .get ("licenseType" ,None ) :
45
+ for innerlicense in license ['licenses' ]:
46
+ licenselist .append (innerlicense )
47
+ else :
48
+ licenselist .append (license )
49
+ return licenselist
50
+
51
+
31
52
def parse_command_args ():
32
53
parser = argparse .ArgumentParser ("update_project_with_component_licenses.py [-h] -u BASE_URL -t TOKEN_FILE [-nv] " )
33
54
parser .add_argument ("-u" , "--base-url" , required = True , help = "Hub server URL e.g. https://your.blackduck.url" )
@@ -61,73 +82,29 @@ def process_project_version(project_name, version_name, args):
61
82
versions = [v for v in bd .get_resource ('versions' , project , params = params ) if v ['versionName' ] == args .version_name ]
62
83
assert len (versions ) == 1 , f"There should be one, and only one version named { args .version_name } . We found { len (versions )} "
63
84
version = versions [0 ]
64
-
65
-
66
85
67
86
#Return only sub-projects, not components
68
- components = (
87
+ components = [
69
88
c for c in bd .get_resource ('components' ,version ) if c ['componentType' ] == "SUB_PROJECT"
70
- )
71
- for components in components :
72
- #JSON body
73
- body = defaultdict (list )
74
-
75
- existing_licenses = []
89
+ ]
90
+
91
+ for subproject in components :
92
+
93
+ url = subproject ['_meta' ]['href' ]
94
+ subprojectlicenses = getlicensesfromprojectversion (subproject )
95
+ print (subprojectlicenses )
96
+ licenseblock = [
97
+ {
98
+ "licenseType" : "CONJUNCTIVE" ,
99
+ "licenses" : subproject ['licenses' ][0 ]['licenses' ]}]
100
+ for license in subprojectlicenses :
101
+ pprint (license )
102
+ licenseblock [0 ]['licenses' ].append ({"licenseDisplay" :license ['licenseDisplay' ],"license" :license ['license' ]})
103
+ subproject ['licenses' ]= licenseblock
104
+ #pprint(subproject)
105
+ r = bd .session .put (url ,json = subproject )
106
+ print (r )
107
+
76
108
77
- #Defining subcomponents
78
- body ["componentName" ]= components ['componentName' ]
79
- body ["componentVersionName" ]= components ['componentVersionName' ]
80
- body ["component" ]= components ['component' ]
81
- body ["componentVersion" ]= components ['componentVersion' ]
82
- body ["componentType" ]= "SUB_PROJECT"
83
- pprint (components ['componentName' ])
84
- pprint (components ['componentVersionName' ])
85
- url = components ['_meta' ]['href' ]
86
- #Capture current License statement of components to add to
87
- if len (components ['licenses' ][0 ]['licenses' ]) > 0 :
88
- for i , v in enumerate (components ['licenses' ][0 ]['licenses' ]):
89
- parent_license_display = components ['licenses' ][0 ]['licenses' ][i ]['licenseDisplay' ]
90
- parent_license = components ['licenses' ][0 ]['licenses' ][i ]['license' ]
91
- addlicense = {"licenseDisplay" :parent_license_display ,"license" : parent_license }
92
- body ['licenses' ].append (addlicense )
93
- if parent_license_display not in existing_licenses :
94
- existing_licenses .append (parent_license_display )
95
- else :
96
- parent_license_display = components ['licenses' ][0 ]['licenses' ][0 ]['licenseDisplay' ]
97
- parent_license = components ['licenses' ][0 ]['licenses' ][0 ]['license' ]
98
- addlicense = {"licenseDisplay" :parent_license_display ,"license" : parent_license }
99
- body ['licenses' ].append (addlicense )
100
- if parent_license_display not in existing_licenses :
101
- existing_licenses .append (parent_license_display )
102
- #Retrieving componentName values
103
- subprojects = [p for p in bd .get_resource ('projects' ) if p ['name' ] == components ['componentName' ]]
104
- subproject = subprojects [0 ]
105
- subversions = [v for v in bd .get_resource ('versions' , subproject ) if v ['versionName' ] == components ['componentVersionName' ]]
106
- subversion = subversions [0 ]
107
- for subcomponent in bd .get_resource ('components' ,subversion ):
108
- #Parse through multiple licenses
109
- if len (subcomponent ['licenses' ][0 ]['licenses' ]) > 0 :
110
- for i , v in enumerate (subcomponent ['licenses' ][0 ]['licenses' ]):
111
- child_license_display = subcomponent ['licenses' ][0 ]['licenses' ][i ]['licenseDisplay' ]
112
- child_license = subcomponent ['licenses' ][0 ]['licenses' ][i ]['license' ]
113
- addlicense = {"licenseDisplay" :child_license_display ,"license" : child_license }
114
- if child_license_display not in existing_licenses :
115
- body ['licenses' ].append (addlicense )
116
- existing_licenses .append (child_license_display )
117
- #When only one license return it
118
- else :
119
- child_license_display = subcomponent ['licenses' ][0 ]['licenseDisplay' ]
120
- child_license = subcomponent ['licenses' ][0 ]['license' ]
121
- addlicense = {"licenseDisplay" :child_license_display ,"license" : child_license }
122
- if child_license_display not in existing_licenses :
123
- body ['licenses' ].append (addlicense )
124
- existing_licenses .append (child_license_display )
125
- pprint (dict (body ))
126
- try :
127
- r = bd .session .put (url ,json = (dict (body )))
128
- if r .status_code == 200 :
129
- print ("updated project" )
130
- except requests .HTTPError as err :
131
- bd .http_error_handler (err )
132
109
if __name__ == "__main__" :
133
110
sys .exit (main ())
0 commit comments