Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/fosslight_util/_get_downloadable_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def extract_name_version_from_link(link):
origin_name = origin_name[:-1]
oss_name = f"go:{origin_name}"
oss_version = match.group(2)
elif key == "cargo":
oss_name = f"cargo:{origin_name}"
oss_version = match.group(2)
except Exception as ex:
logger.info(f"extract_name_version_from_link {key}:{ex}")
if oss_name and (not oss_version):
Expand Down Expand Up @@ -110,8 +113,34 @@ def get_downloadable_url(link):
ret, result_link = get_download_location_for_pub(new_link)
elif pkg_type == "go":
ret, result_link = get_download_location_for_go(new_link)
elif pkg_type == "cargo":
ret, result_link = get_download_location_for_cargo(new_link)
return ret, result_link, oss_name, oss_version, pkg_type


def get_download_location_for_cargo(link):
# get the url for downloading source file: https://crates.io/api/v1/crates/<name>/<version>/download
ret = False
new_link = ''
host = 'https://crates.io/api/v1/crates'

try:
dn_loc_re = re.findall(r'crates.io\/crates\/([^\/]+)\/?([^\/]*)', link)
if dn_loc_re:
oss_name = dn_loc_re[0][0]
oss_version = dn_loc_re[0][1]

return ret, result_link, oss_name, oss_version
new_link = f'{host}/{oss_name}/{oss_version}/download'
res = urlopen(new_link)
if res.getcode() == 200:
ret = True
else:
logger.warning(f'Cannot find the valid link for cargo (url:{new_link}')
except Exception as error:
ret = False
logger.warning(f'Cannot find the link for cargo (url:{link}({(new_link)})): {error}')

return ret, new_link


def get_download_location_for_go(link):
Expand Down
4 changes: 3 additions & 1 deletion src/fosslight_util/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# pub: https://pub.dev/packages/(package)/versions/(version)
# Cocoapods : https://cocoapods.org/(package)
# go : https://pkg.go.dev/(package_name_with_slash)@(version)
# cargo : https://crates.io/crates/(crate_name)/(version)
PKG_PATTERN = {
"pypi": r'https?:\/\/pypi\.org\/project\/([^\/]+)[\/]?([^\/]*)',
"pypi2": r'https?:\/\/files\.pythonhosted\.org\/packages\/source\/[\w]\/([^\/]+)\/[\S]+-([^\-]+)\.tar\.gz',
Expand All @@ -43,5 +44,6 @@
"npm2": r'https?:\/\/www\.npmjs\.com\/package\/(\@[^\/]+\/[^\/]+)(?:\/v\/)?([^\/]*)',
"pub": r'https?:\/\/pub\.dev\/packages\/([^\/]+)(?:\/versions\/)?([^\/]*)',
"cocoapods": r'https?:\/\/cocoapods\.org\/pods\/([^\/]+)',
"go": r'https?:\/\/pkg.go.dev\/([^\@]+)\@?v?([^\/]*)'
"go": r'https?:\/\/pkg.go.dev\/([^\@]+)\@?v?([^\/]*)',
"cargo": r'https?:\/\/crates\.io\/crates\/([^\/]+)\/?([^\/]*)',
}
11 changes: 9 additions & 2 deletions src/fosslight_util/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def download_wget(link, target_dir, compressed_only):

Path(target_dir).mkdir(parents=True, exist_ok=True)

ret, new_link, oss_name, oss_version = get_downloadable_url(link)
ret, new_link, oss_name, oss_version, pkg_type = get_downloadable_url(link)
if ret and new_link:
link = new_link

Expand All @@ -323,14 +323,21 @@ def download_wget(link, target_dir, compressed_only):
if link.endswith(ext):
success = True
break
if not success:
if pkg_type == 'cargo':
success = True
else:
success = True

if not success:
raise Exception('Not supported compression type (link:{0})'.format(link))

logger.info(f"wget: {link}")
downloaded_file = wget.download(link, target_dir)
if pkg_type == 'cargo':
outfile = os.path.join(target_dir, f'{oss_name}.tar.gz')
downloaded_file = wget.download(link, out=outfile)
else:
downloaded_file = wget.download(link, target_dir)
if platform.system() != "Windows":
signal.alarm(0)
else:
Expand Down