Skip to content

Commit 7442413

Browse files
authored
Merge pull request #73 from fosslight/develop
Modify to analyze the license name for carthage
2 parents b2632a8 + 10c64d6 commit 7442413

File tree

2 files changed

+83
-31
lines changed

2 files changed

+83
-31
lines changed

src/fosslight_dependency/_package_manager.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,29 @@ def get_github_license(g, github_repo, platform, license_scanner_bin):
7777
try:
7878
repository = g.get_repo(github_repo)
7979
except Exception:
80-
logger.error("It cannot find the license name. Please use '-t' option with github token.")
81-
logger.error("{0}{1}".format("refer:https://docs.github.com/en/github/authenticating-to-github/",
82-
"keeping-your-account-and-data-secure/creating-a-personal-access-token"))
80+
logger.info("It cannot find the license name. Please use '-t' option with github token.")
81+
logger.info("{0}{1}".format("refer:https://docs.github.com/en/github/authenticating-to-github/",
82+
"keeping-your-account-and-data-secure/creating-a-personal-access-token"))
8383
repository = ''
8484

85-
if repository is not None:
85+
if repository != '':
8686
try:
8787
license_name = repository.get_license().license.spdx_id
88+
if license_name == "" or license_name == "NOASSERTION":
89+
try:
90+
license_txt_data = base64.b64decode(repository.get_license().content).decode('utf-8')
91+
tmp_license_txt = open(tmp_license_txt_file_name, 'w', encoding='utf-8')
92+
tmp_license_txt.write(license_txt_data)
93+
tmp_license_txt.close()
94+
license_name = check_and_run_license_scanner(platform, license_scanner_bin, tmp_license_txt_file_name)
95+
except Exception:
96+
logger.info("Cannot find the license name with license scanner binary.")
97+
98+
if os.path.isfile(tmp_license_txt_file_name):
99+
os.remove(tmp_license_txt_file_name)
88100
except Exception:
89101
logger.info("Cannot find the license name with github api.")
90102

91-
if license_name == "" or license_name == "NOASSERTION":
92-
try:
93-
license_txt_data = base64.b64decode(repository.get_license().content).decode('utf-8')
94-
tmp_license_txt = open(tmp_license_txt_file_name, 'w', encoding='utf-8')
95-
tmp_license_txt.write(license_txt_data)
96-
tmp_license_txt.close()
97-
license_name = check_and_run_license_scanner(platform, license_scanner_bin, tmp_license_txt_file_name)
98-
except Exception:
99-
logger.info("Cannot find the license name with license scanner binary.")
100-
101-
if os.path.isfile(tmp_license_txt_file_name):
102-
os.remove(tmp_license_txt_file_name)
103-
104103
return license_name
105104

106105

src/fosslight_dependency/package_manager/Carthage.py

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55

66
import logging
77
import re
8+
import os
89
import fosslight_util.constant as constant
910
import fosslight_dependency.constant as const
1011
from fosslight_dependency._package_manager import PackageManager
1112
from fosslight_dependency._package_manager import connect_github
1213
from fosslight_dependency._package_manager import get_github_license
14+
from fosslight_dependency._package_manager import check_and_run_license_scanner
1315

1416
logger = logging.getLogger(constant.LOGGER_NAME)
1517

1618

19+
checkout_dir = os.path.join("Carthage", 'Checkouts')
20+
license_file_regs = ['licen[cs]e[s]?', 'notice[s]?', 'copying*', 'copyright[s]?',
21+
'mit', 'bsd[-]?[0-4]?', 'bsd[-]?[0-4][-]?clause[s]?', 'apache[-,_]?[1-2]?[.,-,_]?[0-2]?',
22+
'unlicen[cs]e', '[a,l]?gpl[-]?[1-3]?[.,-,_]?[0-1]?', 'legal']
23+
24+
1725
class Carthage(PackageManager):
1826
package_manager_name = const.CARTHAGE
1927

@@ -26,30 +34,75 @@ def __init__(self, input_dir, output_dir, github_token):
2634
self.append_input_package_list_file(self.input_file_name)
2735

2836
def parse_oss_information(self, f_name):
37+
github = "github"
38+
checkout_dir_list = get_checkout_dirname()
2939
with open(f_name, 'r', encoding='utf8') as input_fp:
3040
sheet_list = []
31-
32-
g = connect_github(self.github_token)
41+
g = ''
42+
if not checkout_dir_list:
43+
g = connect_github(self.github_token)
3344

3445
for i, line in enumerate(input_fp.readlines()):
35-
36-
re_result = re.findall(r'github[\s]\"(\S*)\"[\s]\"(\S*)\"', line)
46+
# Cartfile origin : github, git
47+
# Ref. https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md
48+
re_result = re.findall(r'(github|git)[\s]\"(\S*)\"[\s]\"(\S*)\"', line)
3749
try:
38-
github_repo = re_result[0][0]
39-
oss_origin_name = github_repo.split('/')[1]
50+
repo = re_result[0][0]
51+
oss_path = re_result[0][1]
52+
if oss_path.endswith('.git'):
53+
oss_path = oss_path[:-4]
54+
oss_origin_name = oss_path.split('/')[-1]
4055
oss_name = self.package_manager_name + ":" + oss_origin_name
41-
oss_version = re_result[0][1]
42-
homepage = self.dn_url + github_repo
56+
57+
if repo == github:
58+
homepage = self.dn_url + oss_path
59+
else:
60+
homepage = oss_path
4361
dn_loc = homepage
4462

63+
oss_version = re_result[0][2]
64+
4565
license_name = ''
46-
license_name = get_github_license(g, github_repo, self.platform, self.license_scanner_bin)
66+
find_license = False
67+
if oss_origin_name in checkout_dir_list:
68+
oss_path_in_checkout = os.path.join(checkout_dir, oss_origin_name)
69+
for filename_in_dir in os.listdir(oss_path_in_checkout):
70+
if find_license:
71+
break
72+
filename_with_checkout_path = os.path.join(oss_path_in_checkout, filename_in_dir)
73+
if os.path.isfile(filename_with_checkout_path):
74+
for license_file_reg in license_file_regs:
75+
match_result = re.match(license_file_reg, filename_in_dir.lower())
76+
if match_result is not None:
77+
license_name = check_and_run_license_scanner(self.platform,
78+
self.license_scanner_bin,
79+
filename_with_checkout_path)
80+
find_license = True
81+
break
82+
if license_name == '':
83+
if repo == github:
84+
try:
85+
if not g:
86+
g = connect_github(self.github_token)
87+
license_name = get_github_license(g, oss_path, self.platform, self.license_scanner_bin)
88+
except Exception as e:
89+
logger.warning("Failed to get license with github api:" + str(e))
90+
license_name == ''
4791

48-
except Exception:
49-
logger.error("It cannot find the github oss information. So skip it.")
50-
break
92+
sheet_list.append([const.SUPPORT_PACKAE.get(self.package_manager_name),
93+
oss_name, oss_version, license_name, dn_loc, homepage, '', '', ''])
5194

52-
sheet_list.append([const.SUPPORT_PACKAE.get(self.package_manager_name),
53-
oss_name, oss_version, license_name, dn_loc, homepage, '', '', ''])
95+
except Exception as e:
96+
logger.warning("Failed to parse oss information:" + str(e))
5497

5598
return sheet_list
99+
100+
101+
def get_checkout_dirname():
102+
checkout_dir_list = []
103+
if os.path.isdir(checkout_dir):
104+
for item in os.listdir(checkout_dir):
105+
if os.path.isdir(os.path.join(checkout_dir, item)):
106+
checkout_dir_list.append(item)
107+
108+
return checkout_dir_list

0 commit comments

Comments
 (0)