Skip to content

Commit

Permalink
change: use rich instead of low-level colorama styles, integrate with…
Browse files Browse the repository at this point in the history
… python logging facility (#351)
  • Loading branch information
netomi authored Dec 2, 2024
1 parent b4b2464 commit 0ecfe30
Show file tree
Hide file tree
Showing 68 changed files with 897 additions and 845 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

### Changed

- Integrated existing logging with standard python logging facility.
- Utilized `rich` console formatting instead of low-level colorama styles.
- Improved processing when archiving repositories to process all other requested changes before archiving them. ([#134](https://github.com/eclipse-csi/otterdog/issues/134))
- Split up policies into policies and blueprint and added support for them in the UI
- Improved processing of organization settings `web_commit_signoff_required` and `actions_can_approve_pull_request_reviews` to force update the same settings on repo level as changes will be implicitly performed by GitHub.
Expand Down
7 changes: 5 additions & 2 deletions otterdog/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from decouple import config # type: ignore

from otterdog.logging import init_logging
from otterdog.webapp import create_app
from otterdog.webapp.config import config_dict
from otterdog.webapp.utils import get_temporary_base_directory
Expand Down Expand Up @@ -54,10 +55,9 @@

if DEBUG:
from otterdog.cache import get_github_cache
from otterdog.utils import init

# enable debug outputs
init(2)
init_logging(2)

app.logger.info("DEBUG = " + str(DEBUG))
app.logger.info("Environment = " + config_mode)
Expand All @@ -66,6 +66,9 @@
app.logger.info("APP_ROOT = " + app_config.APP_ROOT)
app.logger.info("GH_CACHE = " + str(get_github_cache()))
app.logger.info("TMP_DIR = " + tmp_dir)
else:
# setup logging to level WARN
init_logging(0)


def run():
Expand Down
7 changes: 4 additions & 3 deletions otterdog/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@

from typing import TYPE_CHECKING

from otterdog.logging import get_logger
from otterdog.providers.github.cache.file import file_cache
from otterdog.utils import print_trace

if TYPE_CHECKING:
from otterdog.providers.github.cache import CacheStrategy

_GITHUB_CACHE = file_cache()
_logger = get_logger(__name__)


def get_github_cache() -> CacheStrategy:
global _GITHUB_CACHE

print_trace(f"Using {_GITHUB_CACHE} as GitHub cache strategy")
_logger.trace("using %s as GitHub cache strategy", _GITHUB_CACHE)
return _GITHUB_CACHE


def set_github_cache(cache: CacheStrategy) -> None:
global _GITHUB_CACHE

print_trace(f"Setting {cache} as GitHub cache strategy")
_logger.trace("setting %s as GitHub cache strategy", cache)
_GITHUB_CACHE = cache
35 changes: 7 additions & 28 deletions otterdog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

import asyncio
import sys
import traceback
from typing import Any

import click
from click.shell_completion import CompletionItem

from otterdog.cache import set_github_cache
from otterdog.logging import CONSOLE_STDOUT, init_logging, print_error, print_exception
from otterdog.providers.github.cache.file import file_cache

from . import __version__
from .config import OtterdogConfig
from .operations import Operation
from .utils import IndentingPrinter, init, is_debug_enabled, print_error, unwrap
from .utils import IndentingPrinter, unwrap

_CONFIG_FILE = "otterdog.json"
_CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"], "max_content_width": 120}
Expand Down Expand Up @@ -86,18 +86,15 @@ def invoke(self, ctx: click.Context) -> Any:
global _CONFIG

verbose = ctx.params.pop("verbose")
init(verbose)
init_logging(verbose)

config_file = ctx.params.pop("config")
local_mode = ctx.params.pop("local")

try:
_CONFIG = OtterdogConfig.from_file(config_file, local_mode)
except Exception as e:
if is_debug_enabled():
traceback.print_exception(e)

print_error(str(e))
except Exception as exc:
print_exception(exc)
sys.exit(2)

return super().invoke(ctx)
Expand Down Expand Up @@ -780,7 +777,7 @@ def install_deps():


def _execute_operation(organizations: list[str], operation: Operation):
printer = IndentingPrinter(sys.stdout)
printer = IndentingPrinter(CONSOLE_STDOUT)

try:
exit_code = 0
Expand All @@ -798,7 +795,6 @@ def _execute_operation(organizations: list[str], operation: Operation):

total_num_orgs = len(organizations)
current_org_number = 1

for organization in organizations:
org_config = config.get_organization_config(organization)
exit_code = max(exit_code, asyncio.run(operation.execute(org_config, current_org_number, total_num_orgs)))
Expand All @@ -808,24 +804,7 @@ def _execute_operation(organizations: list[str], operation: Operation):
sys.exit(exit_code)

except Exception as exc:
if is_debug_enabled():
from rich.traceback import Traceback

rich_tb = Traceback.from_exception(
type(exc),
exc,
exc.__traceback__,
show_locals=True,
suppress=[asyncio],
width=None,
)

from rich.console import Console

console_stderr = Console(stderr=True)
console_stderr.print(rich_tb)
else:
print_error(str(exc))
print_exception(exc)
sys.exit(2)


Expand Down
7 changes: 5 additions & 2 deletions otterdog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
from .credentials.inmemory_provider import InMemoryVault
from .credentials.pass_provider import PassVault
from .jsonnet import JsonnetConfig
from .utils import deep_merge_dict, print_trace, query_json
from .logging import get_logger
from .utils import deep_merge_dict, query_json

if TYPE_CHECKING:
from otterdog.credentials import CredentialProvider, Credentials

_logger = get_logger(__name__)


class OrganizationConfig:
def __init__(
Expand Down Expand Up @@ -150,7 +153,7 @@ def __init__(self, config_file: str, local_mode: bool, working_dir: str | None =
if os.path.exists(override_defaults_file):
with open(override_defaults_file) as defaults_file:
defaults = json.load(defaults_file)
print_trace(f"loading default overrides from '{override_defaults_file}'")
_logger.trace("loading default overrides from '%s'", override_defaults_file)
self._configuration["defaults"] = deep_merge_dict(
defaults, self._configuration.setdefault("defaults")
)
Expand Down
8 changes: 5 additions & 3 deletions otterdog/credentials/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from abc import abstractmethod
from typing import TYPE_CHECKING, Protocol

from otterdog.utils import print_info, print_trace
from otterdog.logging import get_logger

if TYPE_CHECKING:
from typing import Any

_logger = get_logger(__name__)


@dataclasses.dataclass
class Credentials:
Expand Down Expand Up @@ -56,13 +58,13 @@ def totp(self) -> str:

while True:
totp = mintotp.totp(self._totp_secret)
print_trace(f"generated totp '{totp}'")
_logger.trace("generated totp '%s'", totp)

if self._last_totp is None or totp != self._last_totp:
self._last_totp = totp
return totp
else:
print_info("waiting 3s till generating new totp ...")
_logger.info("waiting 3s till generating new totp ...")
time.sleep(3)

@property
Expand Down
6 changes: 4 additions & 2 deletions otterdog/credentials/bitwarden_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import subprocess
from typing import TYPE_CHECKING

from otterdog import utils
from otterdog.credentials import CredentialProvider, Credentials
from otterdog.logging import get_logger

if TYPE_CHECKING:
from typing import Any

_logger = get_logger(__name__)


class BitwardenVault(CredentialProvider):
"""
Expand All @@ -27,7 +29,7 @@ class BitwardenVault(CredentialProvider):
def __init__(self, api_token_key: str):
self._api_token_key = api_token_key

utils.print_debug("unlocking bitwarden vault")
_logger.debug("unlocking bitwarden vault")
self._status, output = subprocess.getstatusoutput("bw unlock --check") # noqa: S605, S607
if self._status != 0:
raise RuntimeError(f"could not access bitwarden vault:\n{output}")
Expand Down
12 changes: 7 additions & 5 deletions otterdog/credentials/pass_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from subprocess import getstatusoutput
from typing import TYPE_CHECKING

from otterdog import utils
from otterdog.credentials import CredentialProvider, Credentials
from otterdog.logging import get_logger

if TYPE_CHECKING:
from typing import Any

_logger = get_logger(__name__)


class PassVault(CredentialProvider):
"""
Expand All @@ -36,15 +38,15 @@ def __init__(
twofa_seed_pattern: str,
api_token_pattern: str,
):
utils.print_debug("accessing pass vault")
_logger.debug("accessing pass vault")
status, output = getstatusoutput("pass ls") # noqa: S605, S607
if status != 0:
raise RuntimeError(f"could not access pass vault:\n{output}")

if password_store_dir:
import os

utils.print_debug(f"setting password store dir to '{password_store_dir}'")
_logger.debug("setting password store dir to '%s'", password_store_dir)
os.environ["PASSWORD_STORE_DIR"] = password_store_dir

self._username_pattern = username_pattern
Expand Down Expand Up @@ -106,9 +108,9 @@ def _retrieve_resolved_key(key: str, strict: bool = True) -> str:
_, output = getstatusoutput(f"pass {key}") # noqa: S605

if strict:
raise RuntimeError(f"{key} could not be retrieved from your pass vault:\n{output}")
raise RuntimeError(f"'{key}' could not be retrieved from your pass vault:\n{output}")
else:
utils.print_warn(f"{key} could not be retrieved from your pass vault:\n{output}")
_logger.warning("'%s' could not be retrieved from your pass vault:\n%s", key, output)
secret = ""

return secret
Expand Down
Loading

0 comments on commit 0ecfe30

Please sign in to comment.