diff --git a/src/codemodder/project_analysis/file_parsers/base_parser.py b/src/codemodder/project_analysis/file_parsers/base_parser.py index 58190f2f..014f9b23 100644 --- a/src/codemodder/project_analysis/file_parsers/base_parser.py +++ b/src/codemodder/project_analysis/file_parsers/base_parser.py @@ -2,12 +2,13 @@ from pathlib import Path from typing import List -from codemodder.dependency import Requirement from codemodder.logging import logger from .package_store import FileType, PackageStore class BaseParser(ABC): + parent_directory: Path + def __init__(self, parent_directory: Path): self.parent_directory = parent_directory @@ -16,14 +17,6 @@ def __init__(self, parent_directory: Path): def file_type(self) -> FileType: pass - def _parse_dependencies(self, dependencies: List[str]): - return [ - Requirement(line) - for x in dependencies - # Skip empty lines and comments - if (line := x.strip()) and not line.startswith("#") - ] - @abstractmethod def _parse_file(self, file: Path) -> PackageStore | None: pass diff --git a/src/codemodder/project_analysis/file_parsers/package_store.py b/src/codemodder/project_analysis/file_parsers/package_store.py index 42048ab9..e28090f8 100644 --- a/src/codemodder/project_analysis/file_parsers/package_store.py +++ b/src/codemodder/project_analysis/file_parsers/package_store.py @@ -12,9 +12,24 @@ class FileType(Enum): SETUP_CFG = "setup.cfg" -@dataclass +@dataclass(init=False) class PackageStore: type: FileType file: Path dependencies: set[Requirement] py_versions: list[str] + + def __init__( + self, + type: FileType, # pylint: disable=redefined-builtin + file: Path, + dependencies: set[str | Requirement], + py_versions: list[str], + ): + self.type = type + self.file = file + self.dependencies = { + dep if isinstance(dep, Requirement) else Requirement(dep) + for dep in dependencies + } + self.py_versions = py_versions diff --git a/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py b/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py index b3e66124..b0d59b78 100644 --- a/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py @@ -26,6 +26,6 @@ def _parse_file(self, file: Path) -> PackageStore | None: return PackageStore( type=self.file_type, file=file, - dependencies=set(dependencies) if dependencies else set(), + dependencies=set(dependencies), py_versions=[version] if version else [], ) diff --git a/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py b/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py index 2ae9d67e..be30ae11 100644 --- a/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py @@ -28,10 +28,12 @@ def _parse_file(self, file: Path) -> PackageStore | None: logger.debug("Unknown encoding for file: %s", file) return None + dependencies = set(line.strip() for line in lines if not line.startswith("#")) + return PackageStore( type=self.file_type, file=file, - dependencies=set(self._parse_dependencies(lines)), + dependencies=dependencies, # requirements.txt files do not declare py versions explicitly # though we could create a heuristic by analyzing each dependency # and extracting py versions from them. diff --git a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py index 9a600acc..5fe84e75 100644 --- a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py @@ -26,6 +26,6 @@ def _parse_file(self, file: Path) -> PackageStore | None: return PackageStore( type=self.file_type, file=file, - dependencies=set(self._parse_dependencies(dependency_lines)), + dependencies=set(line for line in dependency_lines if line), py_versions=[python_requires] if python_requires else [], ) diff --git a/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py b/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py index 3a9d8b83..057a7a7d 100644 --- a/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py @@ -26,7 +26,7 @@ def _parse_file(self, file: Path) -> PackageStore | None: return PackageStore( type=self.file_type, file=file, - dependencies=set(self._parse_dependencies(visitor.install_requires)), + dependencies=set(visitor.install_requires), py_versions=visitor.python_requires, )