-
Notifications
You must be signed in to change notification settings - Fork 0
/
purl.py
145 lines (97 loc) · 4.39 KB
/
purl.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
Copyright 2022 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Author : sgeary
Created On : Fri May 20 2022
File : purl.py
'''
import logging
import common.api.component.get_component_details
logger = logging.getLogger(__name__)
##############################
def get_purl_string(inventoryItem, baseURL, authToken):
logger.info("entering get_purl_string")
purlString = "pkg:" # Default value
componentId = inventoryItem["componentId"]
# Since the summary does not have the forge grab that plus title from component lookup
componentDetails = common.api.component.get_component_details.get_component_details_v3_summary(baseURL, componentId, authToken)
forge = componentDetails["data"]["forge"]
componentTitle = componentDetails["data"]["title"]
componentName = inventoryItem["componentName"]
componentVersionName = inventoryItem["componentVersionName"]
# Ensure there are no spaces in the version name
if " " in componentVersionName:
logger.debug(" Remove space from version name: %s" %componentVersionName)
componentVersionName = componentVersionName.replace(" ", "")
logger.debug(" is now: %s" %componentVersionName)
inventoryItemName = inventoryItem["name"]
logger.info(" Forge: %s Inventory Item: %s" %(forge, inventoryItemName))
# Create the purl based on the forge
if forge in ["apache", "crates", "nuget gallery", "pypi", "rubygems", "sourceforge"]:
if forge == "rubygems":
purlRepo = "gem"
elif forge == "crates":
purlRepo = "cargo"
elif forge == "nuget gallery":
purlRepo = "nuget"
else:
purlRepo = forge
if forge in ["pypi"]:
purlName = componentName.replace("_", "-")
else:
purlName = componentName
purlVersion = componentVersionName
purlNameSpace = ""
elif forge in ["centos", "fedora-koji"]:
purlRepo = "rpm"
purlName = componentName
purlVersion = componentVersionName
if forge == "centos":
purlNameSpace = forge
else:
purlNameSpace = "fedora"
elif forge in ["clojars", "maven-google", "maven2-ibiblio"]:
if forge == "clojars":
purlRepo = forge
else:
purlRepo = "maven"
purlName = componentName
purlVersion = componentVersionName
purlNameSpace = componentTitle.split("/")[0] # parse groupId from component title (start of string to forward slash "/")
elif forge in ["cpan", "cran", "hackage"]:
purlRepo = forge
purlNameSpace = ""
purlVersion = componentVersionName
purlName = componentTitle.split(" - ")[0] # parse case-sensitive name from component title (start of string to dash "-" minus 1)
elif forge in ["npm"]:
purlRepo = forge
purlNameSpace = ""
purlVersion = componentVersionName
purlName = componentName
elif forge in ["packagist"]:
purlRepo = "composer"
purlNameSpace = ""
purlName = componentTitle.split(" - ")[0] # parse case-sensitive name from component title (start of string to dash "-" minus 1)
purlVersion = componentVersionName
elif forge in ["github", "gitlab"]:
purlRepo = forge
purlVersion = componentVersionName
componentName = componentTitle.split(" - ")[0] # parse case-sensitive name from component title (start of string to dash "-" minus 1)
purlNameSpace, purlName = componentName.split("/") # parse groupId from component title (start of string to forward slash "/")
elif forge in ["fsf-directory", "codeplex", "gnu", "java.net", "kernel.org", "mozilla", "mysqlab", "savannah", "googlecode"]:
logger.warning(" No purl string for repository %s." %forge)
purlString = ""
else:
logger.error(" Unsupported forge")
purlString = ""
# Is there a value
if purlString != "":
if purlNameSpace == "":
purlString = "pkg:" + purlRepo + "/" + purlName + "@" + purlVersion
else:
purlString = "pkg:" + purlRepo + "/" + purlNameSpace +"/" + purlName + "@" + purlVersion
if purlVersion == "N/A":
purlString = purlString[:-4]
logger.info(" purlString: %s" %(purlString))
return purlString