Skip to content

Commit

Permalink
Reuse logging for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Oct 28, 2024
1 parent ee705a9 commit e836ed4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ mkdocs:
formats: all

python:
install:
- requirements: requirements.mkdocs.txt
install:
- requirements: requirements.mkdocs.txt
4 changes: 2 additions & 2 deletions mypy_boto3_builder/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ("get_logger",)


def get_logger(level: int | None = None) -> logging.Logger:
def get_logger(level: int | None = None, name: str = LOGGER_NAME) -> logging.Logger:
"""
Get Logger instance.
Expand All @@ -19,7 +19,7 @@ def get_logger(level: int | None = None) -> logging.Logger:
Returns:
Overriden Logger.
"""
logger = logging.getLogger(LOGGER_NAME)
logger = logging.getLogger(name)
if not logger.handlers:
stream_handler = logging.StreamHandler()
formatter = logging.Formatter(
Expand Down
24 changes: 2 additions & 22 deletions scripts/pull_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,11 @@
from pathlib import Path

from mypy_boto3_builder.constants import StaticStubsPath, StaticStubsPullURL
from mypy_boto3_builder.logger import get_logger
from mypy_boto3_builder.utils.github import download_and_extract
from mypy_boto3_builder.utils.path import print_path


def setup_logging(level: int) -> logging.Logger:
"""
Get Logger instance.
Arguments:
level -- Log level
Returns:
Overriden Logger.
"""
logger = logging.getLogger(Path(__file__).stem)
stream_handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(levelname)-7s %(message)s", datefmt="%H:%M:%S")
stream_handler.setFormatter(formatter)
stream_handler.setLevel(level)
logger.addHandler(stream_handler)
logger.setLevel(level)
return logger


logger = setup_logging(logging.DEBUG)
logger = get_logger(logging.DEBUG, Path(__file__).stem)


def pull_static(url: str, output_path: Path, temp_path: Path) -> None:
Expand Down
28 changes: 4 additions & 24 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Sequence, Tuple
from unittest.mock import patch

from mypy_boto3_builder.logger import get_logger
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import HTTPError
from twine.commands.upload import upload
Expand All @@ -36,25 +37,9 @@
]
LOGGER_NAME = "release"


def setup_logging(level: int) -> logging.Logger:
"""
Get Logger instance.
Returns:
Overriden Logger.
"""
logging.getLogger("twine").disabled = True
logging.getLogger("twine.commands.upload").disabled = True

logger = logging.getLogger(LOGGER_NAME)
stream_handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(levelname)-7s %(message)s", datefmt="%H:%M:%S")
stream_handler.setFormatter(formatter)
stream_handler.setLevel(level)
logger.addHandler(stream_handler)
logger.setLevel(level)
return logger
logging.getLogger("twine").disabled = True
logging.getLogger("twine.commands.upload").disabled = True
logger = get_logger(logging.DEBUG, LOGGER_NAME)


@dataclass
Expand Down Expand Up @@ -112,7 +97,6 @@ def check_call(cmd: Sequence[str], print_error: bool = True) -> str:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding="utf-8")
except subprocess.CalledProcessError as e:
if print_error:
logger = logging.getLogger(LOGGER_NAME)
for line in e.output.splitlines():
logger.warning(line)
raise
Expand Down Expand Up @@ -153,7 +137,6 @@ def build(path: Path, max_retries: int = 10) -> Path:
"""
Build package.
"""
logger = logging.getLogger(LOGGER_NAME)
attempt = 1
last_error = Exception("Unknown error")
while attempt <= max_retries:
Expand Down Expand Up @@ -185,7 +168,6 @@ def publish(path: Path, max_retries: int = 10) -> Path:
attempt = 1
dist_path = path / "dist"
packages = [i.as_posix() for i in dist_path.glob("*")]
logger = logging.getLogger(LOGGER_NAME)
last_error = Exception("Unknown error")
while attempt <= max_retries:
try:
Expand Down Expand Up @@ -264,8 +246,6 @@ def main() -> None:
Run main logic.
"""
args = parse_args()
setup_logging(logging.DEBUG)
logger = logging.getLogger(LOGGER_NAME)
paths = [i for i in args.path.absolute().iterdir() if i.is_dir()]
paths.sort(key=lambda x: x.name)
if args.filter:
Expand Down

0 comments on commit e836ed4

Please sign in to comment.