Skip to content

Commit

Permalink
symlink refresh improve log messages
Browse files Browse the repository at this point in the history
- style: pipenv-unlock refresh improve log messages
  • Loading branch information
msftcangoblowm committed Sep 6, 2024
1 parent e3ed0a7 commit 1df5af8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ Changelog

.. scriv-start-here
.. _changes_1-6-9:

Version 1.6.9 — 2024-09-06
--------------------------

- style: pipenv-unlock refresh improve log messages

.. _changes_1-6-8:

Version 1.6.8 — 2024-09-06
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# @@@ editable
copyright = "2024–2024, Dave Faulkmore"
# The short X.Y.Z version.
version = "1.6.8"
version = "1.6.9"
# The full version, including alpha/beta/rc tags.
release = "1.6.8"
release = "1.6.9"
# The date of release, in "monthname day, year" format.
release_date = "September 6, 2024"
# @@@ end
Expand Down
4 changes: 2 additions & 2 deletions src/drain_swamp/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
__version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE

__version__ = version = '1.6.8'
__version_tuple__ = version_tuple = (1, 6, 8)
__version__ = version = '1.6.9'
__version_tuple__ = version_tuple = (1, 6, 9)
2 changes: 1 addition & 1 deletion src/drain_swamp/backend_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

_logger = logging.getLogger(f"{g_app_name}.backend_abc")

is_module_debug = False
is_module_debug = True

# taken from pyproject.toml
entrypoint_name = "pipenv-unlock" # noqa: F401
Expand Down
8 changes: 7 additions & 1 deletion src/drain_swamp/cli_unlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ def create_links(path, is_set_lock, snippet_co):
"""
try:
refresh_links(inst, is_set_lock=is_set_lock)
except MissingRequirementsFoldersFiles:
except (MissingRequirementsFoldersFiles, OSError):
# OSError On Windows malformed path --> PermissionError
msg_exc = (
"Missing requirements folders and files. Prepare these "
f"{traceback.format_exc()}"
Expand All @@ -819,6 +820,11 @@ def create_links(path, is_set_lock, snippet_co):
click.secho(msg_exc, fg="red", err=True)
sys.exit(7)

if __debug__: # pragma: no cover
_logger.info(f"{modpath} update snippet...")
else: # pragma: no cover
pass

path_config = inst.path_config
err_or_none = snippet_replace_suffixes(path_config, snippet_co=snippet_co)
if err_or_none == ReplaceResult.VALIDATE_FAIL:
Expand Down
47 changes: 32 additions & 15 deletions src/drain_swamp/lock_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
)

_logger = logging.getLogger(f"{g_app_name}.lock_toggle")
is_module_debug = False
is_module_debug = True


def _create_symlinks_relative(src, dest, cwd_path):
Expand Down Expand Up @@ -1156,6 +1156,8 @@ def refresh_links(inst, is_set_lock=None):
- :py:exc:`TypeError` -- is_set_lock unsupported type expecting None or bool
- :py:exc:`OSError` -- on Windows, malformed in file path --> PermissionError
"""
is_invalid_set_lock = is_set_lock is not None and not isinstance(is_set_lock, bool)
if is_invalid_set_lock:
Expand Down Expand Up @@ -1224,11 +1226,14 @@ def refresh_links(inst, is_set_lock=None):
else: # pragma: no cover
pass

msg_warn = (
msg_warn_lock_files = (
"{}. No corresponding .unlock / .lock files"
"Cannot make symlink. "
"In {}, prepare the missing folders and files"
)
msg_warn_paths_malformed = (
"Malformed .in file path. {exc} cwd: {path_cwd} abspath: {abspath}"
)
for in_ in files.zeroes:
if in_.stem == "pins":
# pins.in is used as-is
Expand All @@ -1244,24 +1249,36 @@ def refresh_links(inst, is_set_lock=None):
abspath = abspath_zero.parent.joinpath(file_name)
is_dest_file_exists = abspath.exists() and abspath.is_file()
if not is_dest_file_exists:
# No dest file, so skip creating a symlink
msg_warn_missing_dest = msg_warn.format(abspath, path_cwd)
raise MissingRequirementsFoldersFiles(msg_warn_missing_dest)
# No lock/unlock file, so skip creating a symlink
msg_warn = msg_warn_lock_files.format(abspath, path_cwd)
raise MissingRequirementsFoldersFiles(msg_warn)
else:
if is_module_debug: # pragma: no cover
msg_info = f"{mod_path} path_cwd: {path_cwd}"
_logger.info(msg_info)
msg_info = f"{mod_path} abspath: {abspath}"
_logger.info(msg_info)
else: # pragma: no cover
pass

try:
_maintain_symlink(path_cwd, abspath)
except (NotADirectoryError, FileNotFoundError) as e:
msg_warn_missing_dest = msg_warn.format(abspath, path_cwd)
raise MissingRequirementsFoldersFiles(msg_warn_missing_dest) from e
# cwd not a folder or lock/unlock file not exists
msg_warn = msg_warn_lock_files.format(
abspath,
path_cwd,
)
raise MissingRequirementsFoldersFiles(msg_warn) from e
except OSError as e:
# On Windows, malformed path to .in file
d_args = {
"exc": str(e),
"abspath": abspath,
"path_cwd": path_cwd,
}
msg_warn = msg_warn_paths_malformed.format(**d_args)
raise OSError(msg_warn) from e
except Exception as e: # pragma: no cover
# Sad, but not the end of the world
if is_module_debug: # pragma: no cover
msg_info = f"{mod_path} path_cwd: {path_cwd}"
_logger.info(msg_info)
msg_info = f"{mod_path} abspath: {abspath}"
_logger.info(msg_info)
else: # pragma: no cover
pass
msg_exc = str(e)
_logger.warning(msg_exc)
2 changes: 1 addition & 1 deletion src/drain_swamp/snip.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
)

_logger = logging.getLogger(f"{g_app_name}.snip")
is_module_debug = True
is_module_debug = False


class ReplaceResult(Enum):
Expand Down
28 changes: 28 additions & 0 deletions src/drain_swamp/snippet_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@
:type: tuple[str, str, str]
:value: ("SNIPPET_NO_MATCH", "SNIPPET_VALIDATE_FAIL", "snippet_replace_suffixes")
.. py:data:: is_module_debug
:type: bool
Module debugging messages flag
.. py:data:: _logger
:type: logging.Logger
Module level logger
"""

import logging

from .constants import (
SUFFIX_LOCKED,
SUFFIX_SYMLINK,
SUFFIX_UNLOCKED,
g_app_name,
)
from .snip import (
ReplaceResult,
Expand All @@ -54,6 +67,8 @@
"SNIPPET_VALIDATE_FAIL",
"snippet_replace_suffixes",
)
is_module_debug = True
_logger = logging.getLogger(f"{g_app_name}.snippet_pyproject_toml")


def snippet_replace_suffixes(path_config, snippet_co=None):
Expand All @@ -76,6 +91,13 @@ def snippet_replace_suffixes(path_config, snippet_co=None):
:rtype: drain_swamp.snip.ReplaceResult | None
"""

modpath = f"{g_app_name}.snippet_pyproject_toml.snippet_replace_suffixes"
if is_module_debug: # pragma: no cover
msg_info = f"{modpath} path containing snippet {path_config!r}"
_logger.info(msg_info)
else: # pragma: no cover
pass

snip = Snip(path_config, is_quiet=True)

# get
Expand All @@ -102,6 +124,12 @@ def snippet_replace_suffixes(path_config, snippet_co=None):
# replace
is_not_same = contents_orig != contents
if is_not_same:
if is_module_debug: # pragma: no cover
msg_info = f"{modpath} snippet contents: {contents}"
_logger.info(msg_info)
else: # pragma: no cover
pass

snip.replace(contents, id_=snippet_co_actual)
else:
# Nothing to do. in pyproject.toml snippet. suffixes already SUFFIX_SYMLINK
Expand Down
3 changes: 3 additions & 0 deletions src/drain_swamp/snippet_pyproject_toml.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
from pathlib import Path
from typing import Final

from .snip import ReplaceResult

SNIPPET_NO_MATCH: Final[str] # noqa: F401
SNIPPET_VALIDATE_FAIL: Final[str] # noqa: F401
is_module_debug: bool
_logger: logging.Logger

__all__ = (
"SNIPPET_VALIDATE_FAIL",
Expand Down

0 comments on commit 1df5af8

Please sign in to comment.