Skip to content

Commit e943c8b

Browse files
author
Ethan Lee
committed
Fix -m option, notice screen, dinamic graph size
Signed-off-by: Ethan Lee <[email protected]>
1 parent f4cf0c1 commit e943c8b

File tree

1 file changed

+87
-7
lines changed

1 file changed

+87
-7
lines changed

src/fosslight_dependency/run_dependency_scanner.py

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import warnings
1212
from datetime import datetime
1313
import logging
14+
import shutil
1415
import fosslight_dependency.constant as const
1516
from collections import defaultdict
1617
from fosslight_util.set_log import init_log
@@ -33,6 +34,23 @@
3334
_exclude_dir = ['node_moduels', 'venv']
3435

3536

37+
def get_terminal_size():
38+
size = shutil.get_terminal_size()
39+
return size.lines
40+
41+
42+
def paginate_file(file_path):
43+
lines_per_page = get_terminal_size() - 1
44+
with open(file_path, 'r', encoding='utf8') as file:
45+
lines = file.readlines()
46+
47+
for i in range(0, len(lines), lines_per_page):
48+
os.system('clear' if os.name == 'posix' else 'cls')
49+
print(''.join(lines[i: i + lines_per_page]))
50+
if i + lines_per_page < len(lines):
51+
input("Press Enter to see the next page...")
52+
53+
3654
def find_package_manager(input_dir, abs_path_to_exclude=[]):
3755
ret = True
3856
manifest_file_name = []
@@ -86,6 +104,50 @@ def find_package_manager(input_dir, abs_path_to_exclude=[]):
86104
return ret, found_package_manager, input_dir
87105

88106

107+
def find_manifest_for_package_manager(package_manager, input_dir, abs_path_to_exclude=[]):
108+
ret = True
109+
manifest_file_name = []
110+
111+
value = const.SUPPORT_PACKAE[package_manager]
112+
if isinstance(value, list):
113+
manifest_file_name.extend(value)
114+
else:
115+
manifest_file_name.append(value)
116+
117+
found_manifest_file = []
118+
for (parent, _, files) in os.walk(input_dir):
119+
if len(files) < 1:
120+
continue
121+
if os.path.basename(parent) in _exclude_dir:
122+
continue
123+
if os.path.abspath(parent) in abs_path_to_exclude:
124+
continue
125+
for file in files:
126+
file_path = os.path.join(parent, file)
127+
file_abs_path = os.path.abspath(file_path)
128+
if any(os.path.commonpath([file_abs_path, exclude_path]) == exclude_path
129+
for exclude_path in abs_path_to_exclude):
130+
continue
131+
if file in manifest_file_name:
132+
found_manifest_file.append(file)
133+
if len(found_manifest_file) > 0:
134+
input_dir = parent
135+
break
136+
137+
found_package_manager = defaultdict(list)
138+
for f_idx in found_manifest_file:
139+
found_package_manager[package_manager].append(f_idx)
140+
141+
if len(found_package_manager) >= 1:
142+
manifest_file_w_path = map(lambda x: os.path.join(input_dir, x), found_manifest_file)
143+
logger.info(f"Found the manifest file({','.join(manifest_file_w_path)}) automatically.")
144+
else:
145+
ret = False
146+
logger.info("It cannot find the manifest file.")
147+
148+
return ret, found_package_manager, input_dir
149+
150+
89151
def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='', pip_activate_cmd='',
90152
pip_deactivate_cmd='', output_custom_dir='', app_name=const.default_app_name,
91153
github_token='', formats=[], direct=True, path_to_exclude=[], graph_path='',
@@ -183,8 +245,17 @@ def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='',
183245
if not ret:
184246
logger.warning("Dependency scanning terminated because the package manager was not found.")
185247
ret = False
186-
else:
187-
found_package_manager[package_manager] = ["manual detect ('-m option')"]
248+
else:
249+
try:
250+
ret, found_package_manager, input_dir = find_manifest_for_package_manager(package_manager, input_dir, abs_path_to_exclude)
251+
os.chdir(input_dir)
252+
except Exception as e:
253+
logger.error(f'Fail to find manifest file: {e}')
254+
ret = False
255+
finally:
256+
if not ret:
257+
logger.warning("Dependency scanning terminated because any manifest file was not found.")
258+
ret = False
188259

189260
pass_key = 'PASS'
190261
success_pm = []
@@ -227,7 +298,14 @@ def run_dependency_scanner(package_manager='', input_dir='', output_dir_file='',
227298
graph_path = os.path.abspath(graph_path)
228299
try:
229300
converter = GraphConvertor(scan_item.file_items[_PKG_NAME])
230-
converter.save(graph_path, graph_size)
301+
growth_factor_per_node = 10
302+
node_count_threshold = 20
303+
node_count = len(scan_item.file_items[_PKG_NAME])
304+
if node_count > node_count_threshold:
305+
new_size = graph_size + (node_count * growth_factor_per_node)
306+
else:
307+
new_size = graph_size
308+
converter.save(graph_path, new_size)
231309
logger.info(f"Output graph image file: {graph_path}")
232310
except Exception as e:
233311
logger.error(f'Fail to make graph image: {e}')
@@ -324,9 +402,9 @@ def main():
324402
if args.graph_size:
325403
graph_size = args.graph_size
326404
if args.direct: # --direct option
327-
if args.direct == 'true':
405+
if args.direct == 'true' or args.direct == 'True':
328406
direct = True
329-
elif args.direct == 'false':
407+
elif args.direct == 'false' or args.direct == 'False':
330408
direct = False
331409
if args.notice: # --notice option
332410
try:
@@ -337,8 +415,10 @@ def main():
337415
data_path = os.path.join(base_path, 'LICENSES')
338416
print(f"*** {_PKG_NAME} open source license notice ***")
339417
for ff in os.listdir(data_path):
340-
f = open(os.path.join(data_path, ff), 'r', encoding='utf8')
341-
print(f.read())
418+
source_file = os.path.join(data_path, ff)
419+
destination_file = os.path.join(base_path, ff)
420+
paginate_file(source_file)
421+
shutil.copyfile(source_file, destination_file)
342422
sys.exit(0)
343423

344424
run_dependency_scanner(package_manager, input_dir, output_dir, pip_activate_cmd, pip_deactivate_cmd,

0 commit comments

Comments
 (0)