Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get license name from Maven homepage #222

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ requirements-parser
defusedxml
packageurl-python
igraph
matplotlib
matplotlib
requests
cloudscraper
pyuseragents
42 changes: 41 additions & 1 deletion src/fosslight_dependency/package_manager/Gradle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
import json
import fosslight_util.constant as constant
import fosslight_dependency.constant as const
import cloudscraper
from fosslight_dependency._package_manager import PackageManager
from fosslight_dependency._package_manager import version_refine, get_url_to_purl
from requests import exceptions
from bs4 import BeautifulSoup as bs
from pyuseragents import random as random_user_agent

logger = logging.getLogger(constant.LOGGER_NAME)

NO_LICENSE_MESSAGE = "NO_LICENSE_DETECTED"


class Gradle(PackageManager):
package_manager_name = const.GRADLE
Expand Down Expand Up @@ -62,7 +68,9 @@ def parse_oss_information(self, f_name):
purl = ''
try:
for licenses in d['licenses']:
if licenses['name'] != '':
if licenses['name'] == 'No license found':
license_names.append(parse_oss_name_version_in_maven(self.dn_url, group_id, artifact_id))
elif licenses['name'] != '':
license_names.append(licenses['name'].replace(",", ""))
license_name = ', '.join(license_names)
except Exception:
Expand Down Expand Up @@ -115,3 +123,35 @@ def parse_oss_name_version_in_artifactid(name):
oss_version = artifact_comp[2]

return group_id, artifact_id, oss_version


def parse_oss_name_version_in_maven(dn_url, group_id, artifact_id):
url = f"{dn_url}{group_id}/{artifact_id}"
headers = {
'Content-Type': 'text/html;',
'User-Agent': random_user_agent()
}
try:
scraper = cloudscraper.create_scraper()
res = scraper.get(url=url, headers=headers)
res.raise_for_status()
except exceptions.HTTPError as http_err:
logger.debug(f"HTTP error occurred: {http_err}")
return NO_LICENSE_MESSAGE
except exceptions.RequestException as req_err:
logger.debug(f"Request exception occurred: {req_err}")
return NO_LICENSE_MESSAGE
except Exception as e:
logger.debug(f"Unexpected error occurred: {e}")
return NO_LICENSE_MESSAGE

soup = bs(res.content, 'html.parser')
license_th = soup.find('th', text='License')
if not license_th:
return NO_LICENSE_MESSAGE

license_td = license_th.find_next_sibling('td')
license_span = license_td.find('span', {'class': 'b lic'}) if license_td else None
license_name = license_span.text.strip()

return license_name
Loading