Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-fs committed Oct 7, 2023
1 parent 22fd9fc commit 8e222d9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ select = [
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = ["D203"]
ignore = ["D203", "D107"]
src = ["src", "tests"]

[tool.ruff.per-file-ignores]
Expand Down
2 changes: 2 additions & 0 deletions src/pybuild_deps/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@click.version_option()
def cli() -> None:
"""Entrypoint for PyBuild Deps."""
log.as_library = False # pragma: no cover



@cli.command()
Expand Down
8 changes: 5 additions & 3 deletions src/pybuild_deps/compile_build_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def get_version(ireq: InstallRequirement):
"""Get version string from InstalledRequirement."""
"""Get version string from InstallRequirement."""
if not is_pinned_requirement(ireq):
raise PyBuildDepsError(
f"requirement '{ireq}' is not exact "
Expand All @@ -25,6 +25,8 @@ def get_version(ireq: InstallRequirement):


class BuildDependencyCompiler:
"""Resolve exact build dependencies."""

def __init__(self, repository: PyPIRepository) -> None:
self.repository = repository
self.resolver = None
Expand All @@ -33,7 +35,7 @@ def resolve(
self,
install_requirements: Iterable[InstallRequirement],
constraints: Iterable[InstallRequirement] | None = None,
) -> tuple[set[InstallRequirement], BacktrackingResolver]:
) -> set[InstallRequirement]:
"""Resolve all build dependencies for a given set of dependencies."""
constraints: list[InstallRequirement] = list(constraints) if constraints else []
constraint_qty = len(constraints)
Expand All @@ -53,7 +55,7 @@ def resolve(
allow_unsafe=True,
)
build_dependencies = self.resolver.resolve()
# dependencies of build dependencies might have their own build dependencies XD
# dependencies of build dependencies might have their own build dependencies,
# so let's recursively search for those.
while len(build_dependencies) != constraint_qty:
constraint_qty = len(build_dependencies)
Expand Down
51 changes: 39 additions & 12 deletions src/pybuild_deps/logger.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
from __future__ import annotations

import logging
import sys
from typing import Any

import click

logging.basicConfig()
_logger = logging.getLogger("pybuild-deps")


class Logger:
stream = sys.stderr
"""Custom logger for pybuild-deps."""

def __init__(self, verbosity: int = 0):
self.verbosity = self._initial_verbosity = verbosity
self._verbosity = verbosity
self.as_library = True

def log(self, message: str, *args: Any, **kwargs: Any) -> None:
@property
def verbosity(self):
return self._verbosity

@verbosity.setter
def verbosity(self, value):
self._verbosity = value
if self._verbosity < 0:
_logger.setLevel(logging.WARNING)
if self._verbosity == 0:
_logger.setLevel(logging.INFO)
if self._verbosity >= 1:
_logger.setLevel(logging.DEBUG)

def log(self, level: int, message: str, *args: Any, **kwargs: Any) -> None:
if self.as_library:
_logger.log(level, message, *args, **kwargs)
else:
self._cli_log(level, message, args, kwargs)

def _cli_log(self, level, message, args, kwargs):
kwargs.setdefault("err", True)
if level >= logging.ERROR:
kwargs.setdefault("fg", "red")
elif level >= logging.WARNING:
kwargs.setdefault("fg", "yellow")
elif level >= logging.INFO and self.verbosity < 0:
return
elif level >= logging.DEBUG and self.verbosity < 1:
return
elif level <= logging.DEBUG and self.verbosity >= 1:
kwargs.setdefault("fg", "blue")
click.secho(message, *args, **kwargs)

def debug(self, message: str, *args: Any, **kwargs: Any) -> None:
if self.verbosity >= 1:
self.log(message, *args, **kwargs)
self.log(logging.DEBUG, message, *args, **kwargs)

def info(self, message: str, *args: Any, **kwargs: Any) -> None:
if self.verbosity >= 0:
self.log(message, *args, **kwargs)
self.log(logging.INFO, message, *args, **kwargs)

def warning(self, message: str, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("fg", "yellow")
self.log(message, *args, **kwargs)
self.log(logging.WARNING, message, *args, **kwargs)

def error(self, message: str, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("fg", "red")
self.log(message, *args, **kwargs)
self.log(logging.ERROR, message, *args, **kwargs)


log = Logger()
5 changes: 4 additions & 1 deletion src/pybuild_deps/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def get_compile_command(click_ctx):
path_type=str,
),
multiple=True,
help="Constrain versions using the given constraints file; may be used more than once.",
help=(
"Constrain versions using the given constraints file; "
"may be used more than once."
),
)
def cli(
ctx: click.Context,
Expand Down

0 comments on commit 8e222d9

Please sign in to comment.