diff --git a/pyporter/utils.py b/pyporter/utils.py index 932e2974b19636da23512049097b5b3b06a482fb..03694309b9931fcf73e88722eac69e6d66281f48 100644 --- a/pyporter/utils.py +++ b/pyporter/utils.py @@ -1,32 +1,35 @@ import re -# TODO: this should be more compatible for https://peps.python.org/pep-0508/ -def transform_module_name(input_str): - match = re.match(r"([a-zA-Z0-9_-]+)", input_str) - if match: - module_name = match.group(1).strip() - - version_names = input_str[len(module_name):].strip().strip("()") - version_constraint = version_names.split(",") - package_name = "python3-" + module_name - if len(version_constraint) > 1: - constraints_string = " with ".join([ - f"{package_name}{constraint}" for constraint in version_constraint - ]) - result_string = f"({constraints_string})" - else: - result_string = f"({package_name}{version_constraint[0]})" - else: - result_string = "Invalid input format" - - return result_string - - -def refine_requires(req: str) -> str: - """ - return only requires without ';' (thus no extra) - """ - ra = req.split(";", 1) - # Do not add requires which has ;, which is often has very complicated precondition - return transform_module_name(ra[0]) +# TODO: this should be more compatible for https://peps.python.org/pep-0508/ +def transform_module_name(input_str): + # Use a more comprehensive regex to match module names and version constraints + match = re.match(r"^([a-zA-Z0-9_-]+)(?:\[([^\[\]]+)\])?$", input_str) + if match: + module_name = match.group(1).strip() + version_constraint = match.group(2) + + package_name = "python3-" + module_name + if version_constraint: + # Clean up and format the version constraint + version_constraint = version_constraint.strip().replace(' ', '') + if ',' in version_constraint: + constraints = [f"{package_name}{vc.strip()}" for vc in version_constraint.split(',')] + result_string = f"({' with '.join(constraints)})" + else: + result_string = f"({package_name}{version_constraint})" + else: + result_string = f"({package_name})" + else: + result_string = "Invalid input format" + + return result_string + +def refine_requires(req: str) -> str: + """ + Return only the requires part without ';' (thus no extra metadata) + """ + # Split the requirement string at the first ';' to remove any extra metadata + requires_part = req.split(";", 1)[0].strip() + # Transform the requires part using the transform_module_name function + return transform_module_name(requires_part)