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

Apply f-string format #45

Merged
merged 1 commit into from
Mar 3, 2022
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
52 changes: 26 additions & 26 deletions src/fosslight_reuse/_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_licenses_from_json():
with open(file_withpath, 'r') as f:
licenses = json.load(f)
except Exception as ex:
print_error('Error to get license from json file :' + str(ex))
print_error(f"Error to get license from json file : {ex}")

return licenses

Expand All @@ -61,11 +61,11 @@ def check_file_extension(file_list):
try:
file_extension = os.path.splitext(file)[1].lower()
if file_extension == "":
logger.info(" No extension file(s) : " + file)
logger.info(f" No extension file(s) : {file}")
if file_extension in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE:
files_filtered.append(file)
except Exception as ex:
print_error("Error - Unknown error to check file extension - " + str(ex))
print_error(f"Error - Unknown error to check file extension: {ex}")

return files_filtered

Expand All @@ -90,7 +90,7 @@ def check_license_and_copyright(path_to_find, all_files, missing_license, missin

def convert_to_spdx_style(input_string):
input_string = input_string.replace(" ", "-")
input_converted = "LicenseRef-" + input_string
input_converted = f"LicenseRef-{input_string}"
return input_converted


Expand All @@ -103,7 +103,7 @@ def check_input_license_format(input_license):

licensesfromJson = get_licenses_from_json()
if licensesfromJson == "":
print_error(" Error - Return Value to get license from Json is none ")
print_error(" Error - Return Value to get license from Json is none")

try:
# Get frequetly used license from json file
Expand Down Expand Up @@ -161,13 +161,13 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
try:
main_parser = reuse_arg_parser()
except Exception as ex:
print_error('Error_get_arg_parser :' + str(ex))
print_error(f"Error_get_arg_parser : {ex}")

# Print missing License
if missing_license_filtered is not None and len(missing_license_filtered) > 0:
missing_license_list = []

logger.info("# Missing license File(s) ")
logger.info("# Missing license File(s)")
for lic_file in sorted(missing_license_filtered):
logger.info(f" * {lic_file}")
missing_license_list.append(lic_file)
Expand All @@ -184,7 +184,7 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
try:
reuse_header(parsed_args, project)
except Exception as ex:
print_error('Error_call_run_in_license :' + str(ex))
print_error(f"Error_call_run_in_license : {ex}")
else:
logger.info("# There is no missing license file\n")

Expand All @@ -203,20 +203,20 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
input_copyright = copyright

if input_copyright != "":
input_copyright = 'Copyright ' + input_copyright
input_copyright = f"Copyright {input_copyright}"

input_ok = check_input_copyright_format(input_copyright)
if input_ok is False:
return

logger.warning(f" * Your input Copyright : {input_copyright}")
parsed_args = main_parser.parse_args(['addheader', '--copyright',
'SPDX-FileCopyrightText: ' + str(input_copyright),
f'SPDX-FileCopyrightText: {input_copyright}',
'--exclude-year'] + missing_copyright_list)
try:
reuse_header(parsed_args, project)
except Exception as ex:
print_error('Error_call_run_in_copyright :' + str(ex))
print_error(f"Error_call_run_in_copyright : {ex}")
else:
logger.info("\n# There is no missing copyright file\n")

Expand All @@ -236,7 +236,7 @@ def get_allfiles_list(path):
file_rel_path = os.path.relpath(file_abs_path, path)
all_files.append(file_rel_path)
except Exception as ex:
print_error('Error_Get_AllFiles : ' + str(ex))
print_error(f"Error_Get_AllFiles : {ex}")

return all_files

Expand All @@ -246,17 +246,17 @@ def save_result_log():
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
logger.info(_str_final_result_log)
except Exception as ex:
logger.warning("Failed to print add result log. " + str(ex))
logger.warning(f"Failed to print add result log. : {ex}")


def copy_to_root(input_license):
lic_file = str(input_license) + '.txt'
lic_file = f"{input_license}.txt"
try:
source = os.path.join('LICENSES', f'{lic_file}')
destination = 'LICENSE'
shutil.copyfile(source, destination)
except Exception as ex:
print_error("Error - Can't copy license file: " + str(ex))
print_error(f"Error - Can't copy license file: {ex}")


def find_representative_license(path_to_find, input_license):
Expand Down Expand Up @@ -284,7 +284,7 @@ def find_representative_license(path_to_find, input_license):
input_license = check_input_license_format(input_license)

logger.info(f" Input License : {input_license}")
parsed_args = main_parser.parse_args(['download', str(input_license)])
parsed_args = main_parser.parse_args(['download', f"{input_license}"])

try:
logger.warning(" # There is no representative license file")
Expand All @@ -294,7 +294,7 @@ def find_representative_license(path_to_find, input_license):
logger.warning(f" # Created Representative License File : {input_license}.txt")

except Exception as ex:
print_error('Error - download representative license text:' + str(ex))
print_error(f"Error - download representative license text: {ex}")


def is_exclude_dir(dir_path):
Expand Down Expand Up @@ -338,7 +338,7 @@ def download_oss_info_license(base_path, input_license=""):
try:
reuse_download(parsed_args, prj)
except Exception as ex:
print_error('Error - download license text in OSS-pkg-info.yml :' + str(ex))
print_error(f"Error - download license text in OSS-pkg-info.yml : {ex}")
else:
logger.info(" # There is no license in the path \n")

Expand All @@ -356,7 +356,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):

now = datetime.now().strftime('%Y%m%d_%H-%M-%S')
output_dir = os.getcwd()
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_reuse_add_log_"+now+".txt"),
logger, _result_log = init_log(os.path.join(output_dir, f"fosslight_reuse_add_log_{now}.txt"),
True, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find)

if file != "":
Expand All @@ -367,7 +367,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
try:
success, error_msg, licenses = get_spdx_licenses_json()
if success is False:
print_error('Error to get SPDX Licesens : ' + str(error_msg))
print_error(f"Error to get SPDX Licesens : {error_msg}")

licenseInfo = licenses.get("licenses")
for info in licenseInfo:
Expand All @@ -376,7 +376,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
if isDeprecated is False:
spdx_licenses.append(shortID)
except Exception as ex:
print_error('Error access to get_spdx_licenses_json : ' + str(ex))
print_error(f"Error access to get_spdx_licenses_json : {ex}")

if input_license != "":
find_representative_license(path_to_find, input_license)
Expand All @@ -396,11 +396,11 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
if input_license != "":
converted_license = check_input_license_format(input_license)
logger.warning(f" * Your input license : {converted_license}")
parsed_args = main_parser.parse_args(['addheader', '--license', str(converted_license)] + missing_license_list)
parsed_args = main_parser.parse_args(['addheader', '--license', f"{converted_license}"] + missing_license_list)
try:
reuse_header(parsed_args, project)
except Exception as ex:
print_error('Error_call_run_in_license_file_only :' + str(ex))
print_error(f"Error_call_run_in_license_file_only : {ex}")
else:
logger.info("# There is no missing license file")

Expand All @@ -409,20 +409,20 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
input_copyright = input_copyright_while_running()

if input_copyright != "":
input_copyright = 'Copyright ' + input_copyright
input_copyright = f"Copyright {input_copyright}"

input_ok = check_input_copyright_format(input_copyright)
if input_ok is False:
return

logger.warning(f" * Your input Copyright : {input_copyright}")
parsed_args = main_parser.parse_args(['addheader', '--copyright',
'SPDX-FileCopyrightText: ' + str(input_copyright),
f"SPDX-FileCopyrightText: {input_copyright}",
'--exclude-year'] + missing_copyright_list)
try:
reuse_header(parsed_args, project)
except Exception as ex:
print_error('Error_call_run_in_copyright_file_only :' + str(ex))
print_error(f"Error_call_run_in_copyright_file_only : {ex}")
else:
logger.info("# There is no missing copyright file\n")
# Path mode (-p option)
Expand Down
34 changes: 17 additions & 17 deletions src/fosslight_reuse/_fosslight_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def find_oss_pkg_info(path):
_DEFAULT_EXCLUDE_EXTENSION_FILES.append(file_rel_path)

except Exception as ex:
print_error('Error_FIND_OSS_PKG :' + str(ex))
print_error(f"Error_FIND_OSS_PKG : {ex}")

return oss_pkg_info

Expand All @@ -84,20 +84,20 @@ def create_reuse_dep5_file(path):
else:
dir_to_remove = ""
if os.path.exists(reuse_config_file):
file_to_remove = reuse_config_file + "_" + _start_time + ".bk"
file_to_remove = f"{reuse_config_file}_{_start_time}.bk"
shutil.copy2(reuse_config_file, file_to_remove)
need_rollback = True

_DEFAULT_EXCLUDE_EXTENSION_FILES.extend(_DEFAULT_EXCLUDE_FOLDERS)
for file_to_exclude in _DEFAULT_EXCLUDE_EXTENSION_FILES:
str_contents += "\nFiles: " + file_to_exclude + "\nCopyright: -\nLicense: -\n"
str_contents += f"\nFiles: {file_to_exclude} \nCopyright: -\nLicense: -\n"

with open(reuse_config_file, "a") as f:
if not need_rollback:
f.write(_DEFAULT_CONFIG_PREFIX)
f.write(str_contents)
except Exception as ex:
print_error('Error_Create_Dep5 :' + str(ex))
print_error(f"Error_Create_Dep5 : {ex}")

return need_rollback, file_to_remove, dir_to_remove

Expand All @@ -114,7 +114,7 @@ def remove_reuse_dep5_file(rollback, file_to_remove, temp_dir_name):
os.rmdir(temp_dir_name)

except Exception as ex:
print_error('Error_Remove_Dep5 :' + str(ex))
print_error(f"Error_Remove_Dep5 : {ex}")


def reuse_for_files(path, files):
Expand All @@ -137,22 +137,22 @@ def reuse_for_files(path, files):
if extension in _DEFAULT_EXCLUDE_EXTENSION:
_DEFAULT_EXCLUDE_EXTENSION_FILES.append(file)
else:
logger.info("# " + file)
logger.info(f"# {file}")
rep = report.FileReport.generate(prj, file_abs_path)

logger.info("* License: " + ", ".join(rep.spdxfile.licenses_in_file))
logger.info("* Copyright: " + rep.spdxfile.copyright + "\n")
logger.info(f"* License: {', '.join(rep.spdxfile.licenses_in_file)}")
logger.info(f"* Copyright: {rep.spdxfile.copyright}\n")

if rep.spdxfile.licenses_in_file is None or len(rep.spdxfile.licenses_in_file) == 0:
missing_license_list.append(file)
if rep.spdxfile.copyright is None or len(rep.spdxfile.copyright) == 0:
missing_copyright_list.append(file)

except Exception as ex:
print_error('Error_Reuse_for_file_to_read :' + str(ex))
print_error(f"Error_Reuse_for_file_to_read : {ex}")

except Exception as ex:
print_error('Error_Reuse_for_file :' + str(ex))
print_error(f"Error_Reuse_for_file : {ex}")
error_occurred = True

return missing_license_list, missing_copyright_list, error_occurred, prj
Expand Down Expand Up @@ -236,15 +236,15 @@ def result_for_summary(str_lint_result, oss_pkg_info, path, msg_missing_files):
reuse_compliant = True
str_oss_pkg += ", ".join(oss_pkg_info)
except Exception as ex:
print_error('Error_Print_OSS_PKG_INFO:' + str(ex))
print_error(f"Error_Print_OSS_PKG_INFO: {ex}")

if msg_missing_files == "":
reuse_compliant = True

# Add Summary Comment
_SUMMARY_PREFIX = '# SUMMARY\n'
_SUMMARY_SUFFIX = '\n\n' + _MSG_REFERENCE
str_summary = _SUMMARY_PREFIX + str_oss_pkg + '\n' + str_lint_result + _SUMMARY_SUFFIX
str_summary = f"{_SUMMARY_PREFIX}{str_oss_pkg}\n{str_lint_result}{_SUMMARY_SUFFIX}"
items = ET.Element('error')
items.set('id', 'rule_key_osc_checker_01')
items.set('line', '0')
Expand Down Expand Up @@ -275,7 +275,7 @@ def result_for_missing_license_and_copyright_files(files_without_license, copyri
items.set('msg', _MSG_FOLLOW_LIC_TXT)
if _check_only_file_mode:
_root_xml_item.append(items)
str_missing_lic_files += ("* " + file_name + "\n")
str_missing_lic_files += (f"* {file_name}\n")

for file_name in copyright_without_files:
items = ET.Element('error')
Expand All @@ -285,7 +285,7 @@ def result_for_missing_license_and_copyright_files(files_without_license, copyri
items.set('msg', _MSG_FOLLOW_LIC_TXT)
if _check_only_file_mode:
_root_xml_item.append(items)
str_missing_cop_files += ("* " + file_name + "\n")
str_missing_cop_files += (f"* {file_name}\n")

if _check_only_file_mode and _DEFAULT_EXCLUDE_EXTENSION_FILES is not None and len(
_DEFAULT_EXCLUDE_EXTENSION_FILES) > 0:
Expand All @@ -311,13 +311,13 @@ def write_xml_and_exit(result_file: str, exit_code: int) -> None:
for xml_item in error_items:
logger.warning(xml_item.text)
except Exception as ex:
logger.error('Error_to_write_xml:', ex)
logger.error(f"Error_to_write_xml: {ex}")
exit_code = os.EX_IOERR
try:
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
logger.info(_str_final_result_log)
except Exception as ex:
logger.warning("Failed to print result log. " + str(ex))
logger.warning(f"Failed to print result log. {ex}")
sys.exit(exit_code)


Expand All @@ -326,7 +326,7 @@ def init(path_to_find, result_file, file_list):

_start_time = datetime.now().strftime('%Y%m%d_%H-%M-%S')
output_dir = os.path.dirname(os.path.abspath(result_file))
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_reuse_log_"+_start_time+".txt"),
logger, _result_log = init_log(os.path.join(output_dir, f"fosslight_reuse_log_{_start_time}.txt"),
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_find)
if file_list:
_result_log["File list to check"] = file_list
Expand Down