Skip to content

Commit

Permalink
added the directory argument to the CLI (#13)
Browse files Browse the repository at this point in the history
* added the directory argument to the CLI
  • Loading branch information
Florian Maas authored Sep 3, 2022
1 parent 572f670 commit 0a2ee4c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 255 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ check: ## Check code formatting using isort, black, flake8 and mypy.
@echo "🚀 Checking code formatting: Running flake8"
@flake8 .
@echo "🚀 Checking for obsolete dependencies: Running deptry"
@deptry check
@deptry check .

test: ## Test the code with pytest
@echo "🚀 Testing code: Running pytest"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

---

__deptry__ is a command line tool to check for unused dependencies in a poetry managed Python project. It does so by scanning the imported modules within all Python files in
_deptry_ is a command line tool to check for unused dependencies in a poetry managed Python project. It does so by scanning the imported modules within all Python files in
a directory and it's subdirectories, and comparing those to the dependencies listed in `pyproject.toml`.

---
Expand Down Expand Up @@ -40,13 +40,13 @@ In order to check for obsolete imports, __deptry__ should be run directly within
To scan your project for obsolete imports, run

```sh
deptry check
deptry check .
```

or for a more verbose version

```sh
deptry check -v
deptry check . -v
```

__deptry__ can be configured by using additional command line arguments, or
Expand Down
47 changes: 25 additions & 22 deletions deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from deptry.config import Config
from deptry.core import Core
from deptry.utils import run_within_dir


@click.group()
Expand All @@ -13,6 +14,7 @@ def deptry():


@click.command()
@click.argument("directory", type=click.Path(exists=True))
@click.option(
"--verbose",
"-v",
Expand All @@ -38,31 +40,32 @@ def deptry():
is_flag=True,
help="Boolean flag to specify if notebooks should be ignored while scanning for imports.",
)
def check(verbose, ignore_dependencies, ignore_directories, ignore_notebooks):
def check(directory, verbose, ignore_dependencies, ignore_directories, ignore_notebooks):

log_level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(level=log_level, handlers=[logging.StreamHandler()], format="%(message)s")
with run_within_dir(directory):
log_level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(level=log_level, handlers=[logging.StreamHandler()], format="%(message)s")

cli_arguments = {} # a dictionary with the cli arguments, if they are used.
if len(ignore_dependencies) > 0:
cli_arguments["ignore_dependencies"] = list(ignore_dependencies)
if len(ignore_directories) > 0:
cli_arguments["ignore_directories"] = list(ignore_directories)
if ignore_notebooks:
cli_arguments["ignore_notebooks"] = True
config = Config(cli_arguments)
cli_arguments = {} # a dictionary with the cli arguments, if they are used.
if len(ignore_dependencies) > 0:
cli_arguments["ignore_dependencies"] = list(ignore_dependencies)
if len(ignore_directories) > 0:
cli_arguments["ignore_directories"] = list(ignore_directories)
if ignore_notebooks:
cli_arguments["ignore_notebooks"] = True
config = Config(cli_arguments)

obsolete_dependencies = Core(
ignore_dependencies=config.config["ignore_dependencies"],
ignore_directories=config.config["ignore_directories"],
ignore_notebooks=config.config["ignore_notebooks"],
).run()
if len(obsolete_dependencies):
logging.info(f"pyproject.toml contains obsolete dependencies: {obsolete_dependencies}")
sys.exit(1)
else:
logging.info("Succes! No obsolete dependencies found.")
sys.exit(0)
obsolete_dependencies = Core(
ignore_dependencies=config.config["ignore_dependencies"],
ignore_directories=config.config["ignore_directories"],
ignore_notebooks=config.config["ignore_notebooks"],
).run()
if len(obsolete_dependencies):
logging.info(f"pyproject.toml contains obsolete dependencies: {obsolete_dependencies}")
sys.exit(1)
else:
logging.info("Succes! No obsolete dependencies found.")
sys.exit(0)


deptry.add_command(check)
24 changes: 24 additions & 0 deletions deptry/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from contextlib import contextmanager
from pathlib import Path


@contextmanager
def run_within_dir(path: Path):
"""
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)
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In order to check for obsolete imports, _deptry_ should be run directly within t
To scan your project for obsolete imports, run

```sh
deptry check
deptry check .
```

which might output:
Expand Down
12 changes: 6 additions & 6 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In order to check for obsolete imports, _deptry_ should be run directly within t
_deptry_ can be run with

```sh
deptry check
deptry check .
```

which might output the following:
Expand All @@ -23,7 +23,7 @@ pyproject.toml contains obsolete dependencies: ['pandas', 'numpy']
To show more details about the scanned python files, the imported modules found, and how deptry determined which dependencies are obsolete, add the `-v` flag:

```sh
deptry check -v
deptry check . -v
```

## Ignore dependencies
Expand All @@ -32,13 +32,13 @@ Sometimes, you might want _deptry_ to ignore certain dependencies, for example w
incorrectly marks a dependency as obsolete. Dependencies can be ignore with the `-i` flag:

```sh
deptry check -i pandas
deptry check . -i pandas
```

Multiple dependencies can be ignored by using the flag multiple times:

```sh
deptry check -i pandas -i numpy
deptry check . -i pandas -i numpy
```

## Ignore directories
Expand All @@ -48,15 +48,15 @@ the `.venv` directory is ignored. To ignore other directories, use the `-id` fla
both the `.venv` directory and another directory, use the flag twice:

```sh
deptry check -id .venv -id other_directory
deptry check . -id .venv -id other_directory
```

## Ignore notebooks

By default, _deptry_ scans the working directory for `.py` and `.ipynb` files to check for import statements. To ignore `.ipynb` files, use the `--ignore-notebooks` flag:

```sh
deptry check --ignore-notebooks
deptry check . --ignore-notebooks
```

## pyproject.toml
Expand Down
101 changes: 0 additions & 101 deletions notebooks/Untitled-Copy1.ipynb

This file was deleted.

103 changes: 0 additions & 103 deletions notebooks/Untitled.ipynb

This file was deleted.

Loading

0 comments on commit 0a2ee4c

Please sign in to comment.