Skip to content

Commit

Permalink
feat: use root to determine files to find
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Dec 26, 2022
1 parent b918f2c commit 3527789
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
36 changes: 18 additions & 18 deletions deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from deptry.compat import metadata
from deptry.config import read_configuration_from_pyproject_toml
from deptry.core import Core
from deptry.utils import PYPROJECT_TOML_PATH, run_within_dir
from deptry.utils import PYPROJECT_TOML_PATH


class CommaSeparatedTupleParamType(click.ParamType):
Expand Down Expand Up @@ -211,20 +211,20 @@ def deptry(
"""

with run_within_dir(root):
Core(
ignore_obsolete=ignore_obsolete,
ignore_missing=ignore_missing,
ignore_transitive=ignore_transitive,
ignore_misplaced_dev=ignore_misplaced_dev,
exclude=exclude,
extend_exclude=extend_exclude,
ignore_notebooks=ignore_notebooks,
skip_obsolete=skip_obsolete,
skip_missing=skip_missing,
skip_transitive=skip_transitive,
skip_misplaced_dev=skip_misplaced_dev,
requirements_txt=requirements_txt,
requirements_txt_dev=requirements_txt_dev,
json_output=json_output,
).run()
Core(
root=root,
ignore_obsolete=ignore_obsolete,
ignore_missing=ignore_missing,
ignore_transitive=ignore_transitive,
ignore_misplaced_dev=ignore_misplaced_dev,
exclude=exclude,
extend_exclude=extend_exclude,
ignore_notebooks=ignore_notebooks,
skip_obsolete=skip_obsolete,
skip_missing=skip_missing,
skip_transitive=skip_transitive,
skip_misplaced_dev=skip_misplaced_dev,
requirements_txt=requirements_txt,
requirements_txt_dev=requirements_txt_dev,
json_output=json_output,
).run()
10 changes: 5 additions & 5 deletions deptry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

@dataclass
class Core:
root: Path
ignore_obsolete: tuple[str, ...]
ignore_missing: tuple[str, ...]
ignore_transitive: tuple[str, ...]
Expand All @@ -49,7 +50,7 @@ def run(self) -> None:

all_python_files = PythonFileFinder(
exclude=self.exclude + self.extend_exclude, ignore_notebooks=self.ignore_notebooks
).get_all_python_files_in(Path("."))
).get_all_python_files_in(self.root)

local_modules = self._get_local_modules()

Expand Down Expand Up @@ -96,10 +97,9 @@ def _get_dependencies(self, dependency_management_format: DependencyManagementFo
return RequirementsTxtDependencyGetter(self.requirements_txt, self.requirements_txt_dev).get()
raise ValueError("Incorrect dependency manage format. Only poetry, pdm and requirements.txt are supported.")

@staticmethod
def _get_local_modules() -> set[str]:
directories = [f for f in os.listdir() if Path(f).is_dir()]
return {subdir for subdir in directories if "__init__.py" in os.listdir(subdir)}
def _get_local_modules(self) -> set[str]:
directories = [f for f in os.scandir(self.root) if f.is_dir()]
return {subdirectory.name for subdirectory in directories if "__init__.py" in os.listdir(subdirectory)}

def _log_config(self) -> None:
logging.debug("Running with the following configuration:")
Expand Down
24 changes: 1 addition & 23 deletions deptry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import os
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Generator
from typing import Any

if sys.version_info >= (3, 11):
import tomllib
Expand All @@ -14,27 +13,6 @@
PYPROJECT_TOML_PATH = "./pyproject.toml"


@contextmanager
def run_within_dir(path: Path) -> Generator[None, None, None]:
"""
Utility function to run some code within a directory, and change back to the current directory afterwards.
Example usage:
```
with run_within_dir(directory):
some_code()
```
"""
oldpwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(oldpwd)


def load_pyproject_toml(pyproject_toml_path: str = PYPROJECT_TOML_PATH) -> dict[str, Any]:
try:
with Path(pyproject_toml_path).open("rb") as pyproject_file:
Expand Down

0 comments on commit 3527789

Please sign in to comment.