Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more docstrings, fixed some typos #89

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pip install deptry

### Prerequisites

_deptry_ should be run withing the root directory of the project to be scanned, and the proejct should be running in its own dedicated virtual environment.
_deptry_ should be run withing the root directory of the project to be scanned, and the project should be running in its own dedicated virtual environment.

### Usage

Expand Down
4 changes: 2 additions & 2 deletions deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def log_obsolete_dependencies(dependencies: List[str], sep="\n\t") -> None:
logging.info("\n-----------------------------------------------------\n")
logging.info(f"The project contains obsolete dependencies:\n{sep}{sep.join(sorted(dependencies))}\n")
logging.info(
"""Consider removing them from your projects dependencies. If a package is used for development purposes, you should add
"""Consider removing them from your project's dependencies. If a package is used for development purposes, you should add
it to your development dependencies instead."""
)

Expand All @@ -233,7 +233,7 @@ def log_missing_dependencies(dependencies: List[str], sep="\n\t") -> None:
def log_transitive_dependencies(dependencies: List[str], sep="\n\t") -> None:
logging.info("\n-----------------------------------------------------\n")
logging.info(
f"There are transitive dependencies that should be explicitly defined as dependencies in pyproject.toml:\n{sep}{sep.join(sorted(dependencies))}\n"
f"There are transitive dependencies that should be explicitly defined as dependencies:\n{sep}{sep.join(sorted(dependencies))}\n"
)
logging.info("""They are currently imported but not specified directly as your project's dependencies.""")

Expand Down
2 changes: 1 addition & 1 deletion deptry/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Dependency:
def __init__(self, name: str, conditional: bool = False, optional: bool = False) -> None:
"""
Args:
name: Name of the dependency, as shown in pyproject.toml or requirements.txt
name: Name of the dependency, as shown in pyproject.toml
conditional: boolean to indicate if the dependency is conditional, e.g. 'importlib-metadata': {'version': '*', 'python': '<=3.7'}
"""
self.name = name
Expand Down
15 changes: 12 additions & 3 deletions deptry/dependency_getter/requirements_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ def __init__(

def get(self):
if not self.dev:
dependencies = self._get_dependencies_from_requirements_txt(self.requirements_txt)
dependencies = self._get_dependencies_from_requirements_file(self.requirements_txt)
else:
dev_requirements_files = self._scan_for_dev_requirements_files()
if dev_requirements_files:
dependencies = []
for file_name in dev_requirements_files:
dependencies += self._get_dependencies_from_requirements_txt(file_name)
dependencies += self._get_dependencies_from_requirements_file(file_name)
else:
return []

self._log_dependencies(dependencies=dependencies)
return dependencies

def _scan_for_dev_requirements_files(self):
"""
Check if any of the files passed as requirements_txt_dev exist, and if so; return them.
"""
dev_requirements_files = [file_name for file_name in self.requirements_txt_dev if file_name in os.listdir()]
if dev_requirements_files:
logging.debug(f"Found files with development requirements! {dev_requirements_files}")
return dev_requirements_files

def _get_dependencies_from_requirements_txt(self, file_name: str):
def _get_dependencies_from_requirements_file(self, file_name: str):
logging.debug(f"Scanning {file_name} for {'dev-' if self.dev else ''}dependencies")
dependencies = []

Expand All @@ -60,6 +63,9 @@ def _get_dependencies_from_requirements_txt(self, file_name: str):
return dependencies

def _extract_dependency_from_line(self, line: str) -> Dependency:
"""
Extract a dependency from a single line of a requirements.txt file.
"""
line = self._remove_comments_from(line)
line = self._remove_newlines_from(line)
name = self._find_dependency_name_in(line)
Expand All @@ -73,6 +79,9 @@ def _extract_dependency_from_line(self, line: str) -> Dependency:

@staticmethod
def _find_dependency_name_in(line):
"""
Find the dependency name of a dependency specified according to the pip-standards for requirement.txt
"""
match = re.search("^[a-zA-Z0-9-_]+", line)
if match and not match.group(0)[0] == "-":
name = match.group(0)
Expand Down