Skip to content

Commit

Permalink
Move dependency parsing to PackageStore
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Dec 5, 2023
1 parent 1f6770f commit 0c8b51a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
11 changes: 2 additions & 9 deletions src/codemodder/project_analysis/file_parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/codemodder/project_analysis/file_parsers/package_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 [],
)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [],
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down

0 comments on commit 0c8b51a

Please sign in to comment.