Skip to content

Commit 975507d

Browse files
authored
Add func to get downlodable url for go (#218)
Signed-off-by: jiyeong.seok <[email protected]>
1 parent b5942da commit 975507d

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/fosslight_util/_get_downloadable_url.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ def extract_name_version_from_link(link):
4242
oss_version = match.group(2)
4343
elif key == "cocoapods":
4444
oss_name = f"cocoapods:{origin_name}"
45+
elif key == "go":
46+
if origin_name.endswith('/'):
47+
origin_name = origin_name[:-1]
48+
oss_name = f"go:{origin_name}"
49+
oss_version = match.group(2)
4550
except Exception as ex:
4651
logger.info(f"extract_name_version_from_link {key}:{ex}")
4752
if oss_name and (not oss_version):
48-
if key in ["pypi", "maven", "npm", "npm2", "pub"]:
53+
if key in ["pypi", "maven", "npm", "npm2", "pub", "go"]:
4954
oss_version, link = get_latest_package_version(link, key, origin_name)
50-
logger.debug(f'Try to download with the latest version:{link}')
55+
logger.info(f'Try to download with the latest version:{link}')
5156
break
5257
return oss_name, oss_version, link, key
5358

@@ -76,8 +81,13 @@ def get_latest_package_version(link, pkg_type, oss_name):
7681
if pub_response.status_code == 200:
7782
find_version = pub_response.json().get('latest').get('version')
7883
link_with_version = f'https://pub.dev/packages/{oss_name}/versions/{find_version}'
84+
elif pkg_type == 'go':
85+
go_response = requests.get(f'https://proxy.golang.org/{oss_name}/@latest')
86+
if go_response.status_code == 200:
87+
find_version = go_response.json().get('Version')
88+
link_with_version = f'https://pkg.go.dev/{oss_name}@{find_version}'
7989
except Exception as e:
80-
logger.debug(f'Fail to get latest package version({link}:{e})')
90+
logger.info(f'Fail to get latest package version({link}:{e})')
8191
return find_version, link_with_version
8292

8393

@@ -98,10 +108,42 @@ def get_downloadable_url(link):
98108
ret, result_link = get_download_location_for_npm(new_link)
99109
elif pkg_type == "pub":
100110
ret, result_link = get_download_location_for_pub(new_link)
111+
elif pkg_type == "go":
112+
ret, result_link = get_download_location_for_go(new_link)
101113

102114
return ret, result_link, oss_name, oss_version
103115

104116

117+
def get_download_location_for_go(link):
118+
# get the url for downloading source file: https://proxy.golang.org/<module>/@v/VERSION.zip
119+
ret = False
120+
new_link = ''
121+
host = 'https://proxy.golang.org'
122+
123+
try:
124+
dn_loc_re = re.findall(r'pkg.go.dev\/([^\@]+)\@?([^\/]*)', link)
125+
if dn_loc_re:
126+
oss_name = dn_loc_re[0][0]
127+
if oss_name.endswith('/'):
128+
oss_name = oss_name[:-1]
129+
oss_version = dn_loc_re[0][1]
130+
131+
new_link = f'{host}/{oss_name}/@v/{oss_version}.zip'
132+
try:
133+
res = urlopen(new_link)
134+
if res.getcode() == 200:
135+
ret = True
136+
else:
137+
logger.warning(f'Cannot find the valid link for go (url:{new_link}')
138+
except Exception as e:
139+
logger.warning(f'Fail to find the valid link for go (url:{new_link}: {e}')
140+
except Exception as error:
141+
ret = False
142+
logger.warning(f'Cannot find the link for go (url:{link}({(new_link)})): {error}')
143+
144+
return ret, new_link
145+
146+
105147
def get_download_location_for_pypi(link):
106148
# get the url for downloading source file: https://docs.pypi.org/api/ Predictable URLs
107149
ret = False

0 commit comments

Comments
 (0)