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

Refactor legacy code #26

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v5.0.0 (08/09/2024)
## Changes
## 🔧 Maintenance

- Refactoring related to FOSSLight Util @soimkim (#21)
- Revert android_binary_analysis @soimkim (#20)
- Remove the Unused Option from Run Parameters @cjho0316 (#18)
- Limit version of fosslight packages @soimkim (#19)

---

## v4.1.19 (10/06/2024)
## Changes
## 🚀 Features
Expand Down
4 changes: 2 additions & 2 deletions src/fosslight_android/_binary_db_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_oss_info_from_db(platform_version, bin_info_list, return_list):
else: # In case more than 2 OSS is used for this bin.
item.set_additional_oss_items(row['ossname'] + '\t' + row['ossversion'] + '\t' + row['license'])
except Exception as error:
logger.warn(f"READ OSS :{error}")
logger.warning(f"READ OSS :{error}")

disconnect_lge_bin_db(conn, cur)
return_list.extend(bin_info_list)
Expand All @@ -94,7 +94,7 @@ def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value, sou

# Match checksum and fileName
df_result = get_list_by_using_query(sql_statement + sql_statement_checksum, sql_checksum_params, columns, conn, cur)
if df_result is not None and len(df_result) > 0: # Found a file with the same checksum.
if df_result: # Found a file with the same checksum.
final_result_item = df_result
else: # Can't find files that have same name and checksum
# Match tlsh and fileName
Expand Down
2 changes: 1 addition & 1 deletion src/fosslight_android/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def check_empty_column(license, oss_name, directory):
def get_comment(default_comment, license_to_notice, notice_value, empty_columns):
comment = ""

if empty_columns is not None and len(empty_columns) > 0:
if empty_columns:
comment = "Fill in " + ",".join(empty_columns) + "."

if notice_value == 'nok' and license_to_notice:
Expand Down
2 changes: 1 addition & 1 deletion src/fosslight_android/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def read_file(file_name_with_path, read_as_one_line=False):
file = open(file_name_with_path, encoding=encoding_option)
read_line = file.read() if read_as_one_line else file.readlines()
file.close()
if read_line is not None and len(read_line) > 0:
if read_line:
read_success = True
break
except Exception:
Expand Down
81 changes: 52 additions & 29 deletions src/fosslight_android/android_binary_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,33 @@ def get_module_json_obj_by_installed_path(module_name, binary_name_with_path, bi
js_value[MODULE_TYPE_NAME] = binary_name_only
return js_value
else: # Find binary by installed path
for key in module_info_json_obj:
js_value = module_info_json_obj[key]
output_files = js_value["installed"]
if output_files is not None:
for output_file in output_files:
if output_file == binary_name_with_path:
js_value[MODULE_TYPE_NAME] = key
return js_value
else:
path_with_out_dir = os.path.join(build_out_path, binary_name_with_path)
if path_with_out_dir == output_file:
js_value[MODULE_TYPE_NAME] = key
return js_value

# for key in module_info_json_obj:
# js_value = module_info_json_obj[key]
# output_files = js_value["installed"]
# if output_files is not None:

for key, js_value in module_info_json_obj.items():
output_files = js_value.get("installed", [])
if not output_files:
continue

# for output_file in output_files:
# if output_file == binary_name_with_path:
# js_value[MODULE_TYPE_NAME] = key
# return js_value
# else:
# path_with_out_dir = os.path.join(build_out_path, binary_name_with_path)
# if path_with_out_dir == output_file:
# js_value[MODULE_TYPE_NAME] = key
# return js_value
# 반복문과 중복 코드 제거

path_with_out_dir = os.path.join(build_out_path, binary_name_with_path)
if (binary_name_with_path in output_files) or (path_with_out_dir in output_files):
js_value[MODULE_TYPE_NAME] = key
return js_value

return ""


Expand All @@ -176,9 +190,9 @@ def read_module_info_from_build_output_file():
# Only in case upper 7.0.0 versions have a module-info.mk at build/core/tasks.
# Lower versions sould copy module-info.mk to build/core/tasks than build it again.
if not os.path.isfile(os.path.join(build_out_path, MODULE_INFO_FILE_NAME)):
logger.warn("BUILD OUTPUT PATH :", build_out_path)
logger.warn("Can't find a module-info.json file at build output path.")
logger.warn("Please copy module-info.mk file to build/core/tasks than build it again.")
logger.warning("BUILD OUTPUT PATH :", build_out_path)
logger.warning("Can't find a module-info.json file at build output path.")
logger.warning("Please copy module-info.mk file to build/core/tasks than build it again.")
sys.exit(1)

try:
Expand All @@ -188,7 +202,7 @@ def read_module_info_from_build_output_file():
f.close()

except IOError:
logger.warn("[ERROR] Cannot read ", MODULE_INFO_FILE_NAME)
logger.warning("[ERROR] Cannot read ", MODULE_INFO_FILE_NAME)


def set_env_variables_from_result_log():
Expand Down Expand Up @@ -306,12 +320,23 @@ def find_tag_file(bin_info_list, return_bin_list):
dir_path = item.source_code_path
item.license = CONST_NULL
try:
module_license_files = [x for x in os.listdir(dir_path) if x.startswith('MODULE_LICENSE_')]
if module_license_files is not None and len(module_license_files) > 0:
for module_license_file in module_license_files:
item.license = module_license_file.replace('MODULE_LICENSE_', '')
if item.license in _TAG_FILE_TABLE:
item.license = _TAG_FILE_TABLE[item.license]
module_license_files = [
_TAG_FILE_TABLE.get(x.replace('MODULE_LICENSE_', ''), x.replace('MODULE_LICENSE_', ''))
for x in os.listdir(dir_path) if x.startswith('MODULE_LICENSE_')
]
# 파일 추가와 'MODULE_LICENSE_' 대체를 한 번에 처리

# module_license_files = [x for x in os.listdir(dir_path) if x.startswith('MODULE_LICENSE_')]
# if module_license_files is not None and len(module_license_files) > 0:
# for module_license_file in module_license_files:
# item.license = module_license_file.replace('MODULE_LICENSE_', '')
# if item.license in _TAG_FILE_TABLE:
# item.license = _TAG_FILE_TABLE[item.license]

if module_license_files:
item.license = module_license_files[0]
# 만약 빈값이 아니라면, 라이센스 업데이트

except Exception:
item.license = CONST_NULL
if item.license == CONST_NULL:
Expand Down Expand Up @@ -384,11 +409,9 @@ def map_binary_module_name_and_path(installed_file_list):
bin_info = AndroidBinary(file_name_with_relative_path)
bin_info.set_bin_name_with_installed_path(out_binary)
if found_json_obj != "":
if found_json_obj["path"] != "" and len(found_json_obj["path"]) > 0:
bin_info.set_module_name(found_json_obj[MODULE_TYPE_NAME])
if found_json_obj.get("path"):
bin_info.set_source_code_path(found_json_obj["path"][0])
bin_info.set_module_name(found_json_obj[MODULE_TYPE_NAME])
else:
bin_info.set_module_name(found_json_obj[MODULE_TYPE_NAME])
else:
bin_info.set_module_name(module_name)

Expand Down Expand Up @@ -662,7 +685,7 @@ def remove_duplicated_binaries_by_checking_checksum(remove_list_file):

if bin_with_path.startswith('system/'):
priority[0] = return_shorter_installed_path_data(priority[0], bin_same_name)
if src_path is not None and len(src_path) > 0:
if src_path:
priority[1] = return_shorter_installed_path_data(priority[1], bin_same_name)
if notice_check == "ok(NA)" or notice_check == "ok":
value_notice = notice_check
Expand Down Expand Up @@ -755,7 +778,7 @@ def find_meta_lic_files():
if matched:
lic_list.append(matched)
except Exception as error:
logger.warn(f"meta_lic_files:{error}")
logger.warning(f"meta_lic_files:{error}")
if lic_list:
lic = ','.join(lic_list)
meta_lic_files[key] = lic
Expand Down
14 changes: 7 additions & 7 deletions src/fosslight_android/check_notice_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def run_notice_html_checklist(binary_file, check_type, notice_file):
if output != "":
if find_bin_in_notice(output, notice_file_list): # ok
if not check_type:
logger.warn(f"{output} ok")
logger.warning(f"{output} ok")
else: # nok
if check_type:
logger.warn(f"{output} nok")
logger.warning(f"{output} nok")
except Exception as error:
logger.info(f"Find bin in NOTICE :{error}")
except IOError:
Expand Down Expand Up @@ -286,16 +286,16 @@ def divide_notice_files_by_binary(notice_file_to_divide, result_file_path, now):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
else:
logger.warn(dir_name + " folder already exists.")
logger.warning(f"{dir_name} folder already exists.")
except OSError:
logger.warn("Cannot create " + dir_name + " folder.")
logger.warning(f"Cannot create {dir_name} folder.")
return
os.chdir(dir_name)
items = parsing_notice_html_for_license_text(contents)
create_license_txt_files(items, "")
logger.warn(dir_name + " folder has been created.")
logger.warning(f"{dir_name} folder has been created.")
else:
logger.warn("Failed to read :" + notice_file_to_divide)
logger.warning(f"Failed to read: {notice_file_to_divide}")


def create_license_txt_files(file_list, result_file_path):
Expand All @@ -308,7 +308,7 @@ def create_license_txt_files(file_list, result_file_path):
os.makedirs(dir)
write_txt_file(file_name, file['license_txt'], "")
except Exception as error:
logger.warn(f"Error: Cannot create a notice file.:{error}")
logger.warning(f"Error: Cannot create a notice file.:{error}")


def parsing_notice_html_for_license_text(notice_file_content):
Expand Down
16 changes: 8 additions & 8 deletions src/fosslight_android/check_package_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def check_packaging_files(root_path):
check_pkg_files_recursively(root_path)

# Print result
logger.warn("1. Prohibited file names :" + str(len(prohibited_file_names)))
logger.warn('\n'.join(map(str, prohibited_file_names)))
logger.warn("2. Prohibited file extension :" + str(len(prohibited_file_extensions)))
logger.warn('\n'.join(map(str, prohibited_file_extensions)))
logger.warn("3. Prohibited Path :" + str(len(prohibited_path_list)))
logger.warn('\n'.join(map(str, prohibited_path_list)))
logger.warn("4. Fail to read :" + str(len(failed_to_read_path)))
logger.warn('\n'.join(map(str, failed_to_read_path)))
logger.warning("1. Prohibited file names :" + str(len(prohibited_file_names)))
logger.warning('\n'.join(map(str, prohibited_file_names)))
logger.warning("2. Prohibited file extension :" + str(len(prohibited_file_extensions)))
logger.warning('\n'.join(map(str, prohibited_file_extensions)))
logger.warning("3. Prohibited Path :" + str(len(prohibited_path_list)))
logger.warning('\n'.join(map(str, prohibited_path_list)))
logger.warning("4. Fail to read :" + str(len(failed_to_read_path)))
logger.warning('\n'.join(map(str, failed_to_read_path)))


def read_packaging_config():
Expand Down