Skip to content

Commit

Permalink
Merge pull request #2 from pixee/lazy-manager
Browse files Browse the repository at this point in the history
Added lazyness and moved dry_run flag to write
  • Loading branch information
andrecsilva authored Aug 2, 2023
2 parents a53f424 + 7eb099b commit 73ba36a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
22 changes: 9 additions & 13 deletions src/dependency_manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
from typing import Union
import pkg_resources
from .singleton import Singleton
from functools import cached_property


class DependencyManagerAbstract(Singleton, ABC):
def init(self, dry_run=False):
def init(self):
"""One-time class initialization."""
self.parent_directory = self.get_parent_dir()
self.dependency_file = self._infer_dependency_files()
self.dependencies = self._infer_dependencies()
self.dependencies_changed = False
self.dependency_file_changed = False
self.dry_run = dry_run

@abstractmethod
def get_parent_dir(self) -> Path:
Expand Down Expand Up @@ -46,20 +44,16 @@ def remove(self, dependencies: list[str]):
self.dependencies.remove(dep)
self.dependencies_changed = True

def write(self):
def write(self, dry_run=False):
"""
Write the updated dependency files if any changes were made.
If the dry_run flag is set, print it to stdout instead.
"""
if (
not self.dependency_file
or not self.dependencies
or not self.dependencies_changed
):
if not self.dependencies_changed:
return

dependencies = [str(req) for req in self.dependencies]
if self.dry_run:
if dry_run:
print("\n".join(dependencies))
else:
self._write(dependencies)
Expand All @@ -69,15 +63,17 @@ def _write(self, dependencies):
with open(self.dependency_file, "w", encoding="utf-8") as f:
f.writelines("\n".join(dependencies))

def _infer_dependency_files(self) -> Union[Path, None]:
@cached_property
def dependency_file(self) -> Union[Path, None]:
try:
# For now for simplicity only return the first file
return next(Path(self.parent_directory).rglob("requirements.txt"))
except StopIteration:
pass
return None

def _infer_dependencies(self) -> list[pkg_resources.Requirement]:
@cached_property
def dependencies(self) -> list[pkg_resources.Requirement]:
"""
Extract list of dependencies from requirements.txt file.
Same order of requirements is maintained, no alphabetical sorting is done.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ class DependencyManager(DependencyManagerAbstract):
def get_parent_dir(self):
return path_with_req

manager = DependencyManager(dry_run=True)
manager = DependencyManager()
first_dep = manager.dependencies[0]
manager.remove([str(first_dep)])
manager.write()
manager.write(dry_run=True)
with open(manager.dependency_file, "r", encoding="utf-8") as dep_file:
contents = dep_file.read()
assert contents == original_contents
Expand Down

0 comments on commit 73ba36a

Please sign in to comment.