cleanup bad naming

This commit is contained in:
Manuel Barkhau 2020-07-19 13:18:42 +00:00
parent 6660b3b815
commit e70c45403c
6 changed files with 81 additions and 79 deletions

View file

@ -42,7 +42,7 @@ click.disable_unicode_literals_warning = True
VALID_RELEASE_VALUES = ("alpha", "beta", "dev", "rc", "post", "final") VALID_RELEASE_VALUES = ("alpha", "beta", "dev", "rc", "post", "final")
log = logging.getLogger("pycalver.cli") logger = logging.getLogger("pycalver.cli")
def _configure_logging(verbose: int = 0) -> None: def _configure_logging(verbose: int = 0) -> None:
@ -57,15 +57,15 @@ def _configure_logging(verbose: int = 0) -> None:
log_level = logging.INFO log_level = logging.INFO
logging.basicConfig(level=log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S") logging.basicConfig(level=log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S")
log.debug("Logging configured.") logger.debug("Logging configured.")
def _validate_release_tag(release: str) -> None: def _validate_release_tag(release: str) -> None:
if release in VALID_RELEASE_VALUES: if release in VALID_RELEASE_VALUES:
return return
log.error(f"Invalid argument --release={release}") logger.error(f"Invalid argument --release={release}")
log.error(f"Valid arguments are: {', '.join(VALID_RELEASE_VALUES)}") logger.error(f"Valid arguments are: {', '.join(VALID_RELEASE_VALUES)}")
sys.exit(1) sys.exit(1)
@ -108,7 +108,7 @@ def test(
old_version, pattern=pattern, release=release, major=major, minor=minor, patch=patch old_version, pattern=pattern, release=release, major=major, minor=minor, patch=patch
) )
if new_version is None: if new_version is None:
log.error(f"Invalid version '{old_version}' and/or pattern '{pattern}'.") logger.error(f"Invalid version '{old_version}' and/or pattern '{pattern}'.")
sys.exit(1) sys.exit(1)
pep440_version = version.to_pep440(new_version) pep440_version = version.to_pep440(new_version)
@ -119,29 +119,31 @@ def test(
def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config: def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config:
try: try:
_vcs = vcs.get_vcs() vcs_api = vcs.get_vcs_api()
log.debug(f"vcs found: {_vcs.name}") logger.debug(f"vcs found: {vcs_api.name}")
if fetch: if fetch:
log.info("fetching tags from remote (to turn off use: -n / --no-fetch)") logger.info("fetching tags from remote (to turn off use: -n / --no-fetch)")
_vcs.fetch() vcs_api.fetch()
version_tags = [tag for tag in _vcs.ls_tags() if version.is_valid(tag, cfg.version_pattern)] version_tags = [
tag for tag in vcs_api.ls_tags() if version.is_valid(tag, cfg.version_pattern)
]
if version_tags: if version_tags:
version_tags.sort(reverse=True) version_tags.sort(reverse=True)
log.debug(f"found {len(version_tags)} tags: {version_tags[:2]}") logger.debug(f"found {len(version_tags)} tags: {version_tags[:2]}")
latest_version_tag = version_tags[0] latest_version_tag = version_tags[0]
latest_version_pep440 = version.to_pep440(latest_version_tag) latest_version_pep440 = version.to_pep440(latest_version_tag)
if latest_version_tag > cfg.current_version: if latest_version_tag > cfg.current_version:
log.info(f"Working dir version : {cfg.current_version}") logger.info(f"Working dir version : {cfg.current_version}")
log.info(f"Latest version from {_vcs.name:>3} tag: {latest_version_tag}") logger.info(f"Latest version from {vcs_api.name:>3} tag: {latest_version_tag}")
cfg = cfg._replace( cfg = cfg._replace(
current_version=latest_version_tag, pep440_version=latest_version_pep440 current_version=latest_version_tag, pep440_version=latest_version_pep440
) )
else: else:
log.debug("no vcs tags found") logger.debug("no vcs tags found")
except OSError: except OSError:
log.debug("No vcs found") logger.debug("No vcs found")
return cfg return cfg
@ -159,7 +161,7 @@ def show(verbose: int = 0, fetch: bool = True) -> None:
cfg: config.MaybeConfig = config.parse(ctx) cfg: config.MaybeConfig = config.parse(ctx)
if cfg is None: if cfg is None:
log.error("Could not parse configuration. Perhaps try 'pycalver init'.") logger.error("Could not parse configuration. Perhaps try 'pycalver init'.")
sys.exit(1) sys.exit(1)
cfg = _update_cfg_from_vcs(cfg, fetch=fetch) cfg = _update_cfg_from_vcs(cfg, fetch=fetch)
@ -181,7 +183,7 @@ def init(verbose: int = 0, dry: bool = False) -> None:
cfg: config.MaybeConfig = config.parse(ctx) cfg: config.MaybeConfig = config.parse(ctx)
if cfg: if cfg:
log.error(f"Configuration already initialized in {ctx.config_filepath}") logger.error(f"Configuration already initialized in {ctx.config_filepath}")
sys.exit(1) sys.exit(1)
if dry: if dry:
@ -193,67 +195,70 @@ def init(verbose: int = 0, dry: bool = False) -> None:
config.write_content(ctx) config.write_content(ctx)
def _assert_not_dirty(_vcs: vcs.VCS, filepaths: typ.Set[str], allow_dirty: bool) -> None: def _assert_not_dirty(vcs_api: vcs.VCSAPI, filepaths: typ.Set[str], allow_dirty: bool) -> None:
dirty_files = _vcs.status(required_files=filepaths) dirty_files = vcs_api.status(required_files=filepaths)
if dirty_files: if dirty_files:
log.warning(f"{_vcs.name} working directory is not clean. Uncomitted file(s):") logger.warning(f"{vcs_api.name} working directory is not clean. Uncomitted file(s):")
for dirty_file in dirty_files: for dirty_file in dirty_files:
log.warning(" " + dirty_file) logger.warning(" " + dirty_file)
if not allow_dirty and dirty_files: if not allow_dirty and dirty_files:
sys.exit(1) sys.exit(1)
dirty_pattern_files = set(dirty_files) & filepaths dirty_pattern_files = set(dirty_files) & filepaths
if dirty_pattern_files: if dirty_pattern_files:
log.error("Not commiting when pattern files are dirty:") logger.error("Not commiting when pattern files are dirty:")
for dirty_file in dirty_pattern_files: for dirty_file in dirty_pattern_files:
log.warning(" " + dirty_file) logger.warning(" " + dirty_file)
sys.exit(1) sys.exit(1)
def _commit(
cfg: config.Config, new_version: str, vcs_api: vcs.VCSAPI, filepaths: typ.Set[str]
) -> None:
for filepath in filepaths:
vcs_api.add(filepath)
vcs_api.commit(f"bump version to {new_version}")
if cfg.commit and cfg.tag:
vcs_api.tag(new_version)
if cfg.commit and cfg.tag and cfg.push:
vcs_api.push(new_version)
def _bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> None: def _bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> None:
_vcs: typ.Optional[vcs.VCS] vcs_api: typ.Optional[vcs.VCSAPI] = None
if cfg.commit: if cfg.commit:
try: try:
_vcs = vcs.get_vcs() vcs_api = vcs.get_vcs_api()
except OSError: except OSError:
log.warning("Version Control System not found, aborting commit.") logger.warning("Version Control System not found, aborting commit.")
_vcs = None
filepaths = set(cfg.file_patterns.keys()) filepaths = set(cfg.file_patterns.keys())
if _vcs: if vcs_api:
_assert_not_dirty(_vcs, filepaths, allow_dirty) _assert_not_dirty(vcs_api, filepaths, allow_dirty)
try: try:
new_vinfo = version.parse_version_info(new_version, cfg.version_pattern) new_vinfo = version.parse_version_info(new_version, cfg.version_pattern)
rewrite.rewrite(cfg.file_patterns, new_vinfo) rewrite.rewrite(cfg.file_patterns, new_vinfo)
except Exception as ex: except Exception as ex:
log.error(str(ex)) logger.error(str(ex))
sys.exit(1) sys.exit(1)
if _vcs is None or not cfg.commit: if vcs_api:
return _commit(cfg, new_version, vcs_api, filepaths)
for filepath in filepaths:
_vcs.add(filepath)
_vcs.commit(f"bump version to {new_version}")
if cfg.commit and cfg.tag:
_vcs.tag(new_version)
if cfg.commit and cfg.tag and cfg.push:
_vcs.push(new_version)
def _try_bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> None: def _try_bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> None:
try: try:
_bump(cfg, new_version, allow_dirty) _bump(cfg, new_version, allow_dirty)
except sp.CalledProcessError as ex: except sp.CalledProcessError as ex:
log.error(f"Error running subcommand: {ex.cmd}") logger.error(f"Error running subcommand: {ex.cmd}")
if ex.stdout: if ex.stdout:
sys.stdout.write(ex.stdout.decode('utf-8')) sys.stdout.write(ex.stdout.decode('utf-8'))
if ex.stderr: if ex.stderr:
@ -285,7 +290,7 @@ def _try_print_diff(cfg: config.Config, new_version: str) -> None:
try: try:
_print_diff(cfg, new_version) _print_diff(cfg, new_version)
except Exception as ex: except Exception as ex:
log.error(str(ex)) logger.error(str(ex))
sys.exit(1) sys.exit(1)
@ -340,7 +345,7 @@ def bump(
cfg: config.MaybeConfig = config.parse(ctx) cfg: config.MaybeConfig = config.parse(ctx)
if cfg is None: if cfg is None:
log.error("Could not parse configuration. Perhaps try 'pycalver init'.") logger.error("Could not parse configuration. Perhaps try 'pycalver init'.")
sys.exit(1) sys.exit(1)
cfg = _update_cfg_from_vcs(cfg, fetch=fetch) cfg = _update_cfg_from_vcs(cfg, fetch=fetch)
@ -358,13 +363,13 @@ def bump(
is_semver = "{semver}" in cfg.version_pattern is_semver = "{semver}" in cfg.version_pattern
has_semver_inc = major or minor or patch has_semver_inc = major or minor or patch
if is_semver and not has_semver_inc: if is_semver and not has_semver_inc:
log.warning("bump --major/--minor/--patch required when using semver.") logger.warning("bump --major/--minor/--patch required when using semver.")
else: else:
log.error(f"Invalid version '{old_version}' and/or pattern '{cfg.version_pattern}'.") logger.error(f"Invalid version '{old_version}' and/or pattern '{cfg.version_pattern}'.")
sys.exit(1) sys.exit(1)
log.info(f"Old Version: {old_version}") logger.info(f"Old Version: {old_version}")
log.info(f"New Version: {new_version}") logger.info(f"New Version: {new_version}")
if dry or verbose >= 2: if dry or verbose >= 2:
_try_print_diff(cfg, new_version) _try_print_diff(cfg, new_version)

View file

@ -17,7 +17,7 @@ import pathlib2 as pl
from . import version from . import version
log = logging.getLogger("pycalver.config") logger = logging.getLogger("pycalver.config")
Patterns = typ.List[str] Patterns = typ.List[str]
PatternsByGlob = typ.Dict[str, Patterns] PatternsByGlob = typ.Dict[str, Patterns]
@ -203,7 +203,7 @@ def _normalize_file_patterns(raw_cfg: RawConfig) -> FilePatterns:
for filepath, patterns in list(file_patterns.items()): for filepath, patterns in list(file_patterns.items()):
if not os.path.exists(filepath): if not os.path.exists(filepath):
log.warning(f"Invalid config, no such file: {filepath}") logger.warning(f"Invalid config, no such file: {filepath}")
normalized_patterns: typ.List[str] = [] normalized_patterns: typ.List[str] = []
for pattern in patterns: for pattern in patterns:
@ -215,8 +215,8 @@ def _normalize_file_patterns(raw_cfg: RawConfig) -> FilePatterns:
elif version_pattern == "{semver}": elif version_pattern == "{semver}":
normalized_pattern = normalized_pattern.replace("{pep440_version}", "{semver}") normalized_pattern = normalized_pattern.replace("{pep440_version}", "{semver}")
elif "{pep440_version}" in pattern: elif "{pep440_version}" in pattern:
log.warning(f"Invalid config, cannot match '{pattern}' for '{filepath}'.") logger.warning(f"Invalid config, cannot match '{pattern}' for '{filepath}'.")
log.warning(f"No mapping of '{version_pattern}' to '{pep440_version}'") logger.warning(f"No mapping of '{version_pattern}' to '{pep440_version}'")
normalized_patterns.append(normalized_pattern) normalized_patterns.append(normalized_pattern)
file_patterns[filepath] = normalized_patterns file_patterns[filepath] = normalized_patterns
@ -268,7 +268,7 @@ def _parse_config(raw_cfg: RawConfig) -> Config:
push=push, push=push,
file_patterns=file_patterns, file_patterns=file_patterns,
) )
log.debug(_debug_str(cfg)) logger.debug(_debug_str(cfg))
return cfg return cfg
@ -288,7 +288,7 @@ def _parse_current_version_default_pattern(cfg: Config, raw_cfg_text: str) -> st
def parse(ctx: ProjectContext) -> MaybeConfig: def parse(ctx: ProjectContext) -> MaybeConfig:
"""Parse config file if available.""" """Parse config file if available."""
if not ctx.config_filepath.exists(): if not ctx.config_filepath.exists():
log.warning(f"File not found: {ctx.config_filepath}") logger.warning(f"File not found: {ctx.config_filepath}")
return None return None
fh: typ.IO[str] fh: typ.IO[str]
@ -322,7 +322,7 @@ def parse(ctx: ProjectContext) -> MaybeConfig:
return cfg return cfg
except ValueError as ex: except ValueError as ex:
log.warning(f"Couldn't parse {cfg_path}: {str(ex)}") logger.warning(f"Couldn't parse {cfg_path}: {str(ex)}")
return None return None

View file

@ -6,12 +6,9 @@
"""Parse PyCalVer strings from files.""" """Parse PyCalVer strings from files."""
import typing as typ import typing as typ
import logging
from . import patterns from . import patterns
log = logging.getLogger("pycalver.parse")
class PatternMatch(typ.NamedTuple): class PatternMatch(typ.NamedTuple):
"""Container to mark a version string in a file.""" """Container to mark a version string in a file."""

View file

@ -18,7 +18,7 @@ from . import config
from . import version from . import version
from . import patterns from . import patterns
log = logging.getLogger("pycalver.rewrite") logger = logging.getLogger("pycalver.rewrite")
def detect_line_sep(content: str) -> str: def detect_line_sep(content: str) -> str:
@ -44,7 +44,7 @@ def detect_line_sep(content: str) -> str:
class NoPatternMatch(Exception): class NoPatternMatch(Exception):
"""Pattern not found in content. """Pattern not found in content.
log.error is used to show error info about the patterns so logger.error is used to show error info about the patterns so
that users can debug what is wrong with them. The class that users can debug what is wrong with them. The class
itself doesn't capture that info. This approach is used so itself doesn't capture that info. This approach is used so
that all patter issues can be shown, rather than bubbling that all patter issues can be shown, rather than bubbling
@ -80,9 +80,9 @@ def rewrite_lines(
non_matched_patterns = set(pattern_strs) - found_patterns non_matched_patterns = set(pattern_strs) - found_patterns
if non_matched_patterns: if non_matched_patterns:
for non_matched_pattern in non_matched_patterns: for non_matched_pattern in non_matched_patterns:
log.error(f"No match for pattern '{non_matched_pattern}'") logger.error(f"No match for pattern '{non_matched_pattern}'")
compiled_pattern = patterns._compile_pattern(non_matched_pattern) compiled_pattern = patterns._compile_pattern(non_matched_pattern)
log.error(f"Pattern compiles to regex '{compiled_pattern}'") logger.error(f"Pattern compiles to regex '{compiled_pattern}'")
raise NoPatternMatch("Invalid pattern(s)") raise NoPatternMatch("Invalid pattern(s)")
else: else:
return new_lines return new_lines

View file

@ -20,7 +20,7 @@ import logging
import tempfile import tempfile
import subprocess as sp import subprocess as sp
log = logging.getLogger("pycalver.vcs") logger = logging.getLogger("pycalver.vcs")
VCS_SUBCOMMANDS_BY_NAME = { VCS_SUBCOMMANDS_BY_NAME = {
@ -52,8 +52,8 @@ VCS_SUBCOMMANDS_BY_NAME = {
Env = typ.Dict[str, str] Env = typ.Dict[str, str]
class VCS: class VCSAPI:
"""VCS absraction for git and mercurial.""" """Absraction for git and mercurial."""
def __init__(self, name: str, subcommands: typ.Dict[str, str] = None): def __init__(self, name: str, subcommands: typ.Dict[str, str] = None):
self.name = name self.name = name
@ -67,9 +67,9 @@ class VCS:
cmd_tmpl = self.subcommands[cmd_name] cmd_tmpl = self.subcommands[cmd_name]
cmd_str = cmd_tmpl.format(**kwargs) cmd_str = cmd_tmpl.format(**kwargs)
if cmd_name in ("commit", "tag", "push_tag"): if cmd_name in ("commit", "tag", "push_tag"):
log.info(cmd_str) logger.info(cmd_str)
else: else:
log.debug(cmd_str) logger.debug(cmd_str)
output_data: bytes = sp.check_output(cmd_str.split(), env=env, stderr=sp.STDOUT) output_data: bytes = sp.check_output(cmd_str.split(), env=env, stderr=sp.STDOUT)
# TODO (mb 2018-11-15): Detect encoding of output? # TODO (mb 2018-11-15): Detect encoding of output?
@ -122,7 +122,7 @@ class VCS:
def ls_tags(self) -> typ.List[str]: def ls_tags(self) -> typ.List[str]:
"""List vcs tags on all branches.""" """List vcs tags on all branches."""
ls_tag_lines = self('ls_tags').splitlines() ls_tag_lines = self('ls_tags').splitlines()
log.debug(f"ls_tags output {ls_tag_lines}") logger.debug(f"ls_tags output {ls_tag_lines}")
return [line.strip().split(" ", 1)[0] for line in ls_tag_lines] return [line.strip().split(" ", 1)[0] for line in ls_tag_lines]
def add(self, path: str) -> None: def add(self, path: str) -> None:
@ -164,17 +164,17 @@ class VCS:
def __repr__(self) -> str: def __repr__(self) -> str:
"""Generate string representation.""" """Generate string representation."""
return f"VCS(name='{self.name}')" return f"VCSAPI(name='{self.name}')"
def get_vcs() -> VCS: def get_vcs_api() -> VCSAPI:
"""Detect the appropriate VCS for a repository. """Detect the appropriate VCS for a repository.
raises OSError if the directory doesn't use a supported VCS. raises OSError if the directory doesn't use a supported VCS.
""" """
for vcs_name in VCS_SUBCOMMANDS_BY_NAME.keys(): for vcs_name in VCS_SUBCOMMANDS_BY_NAME.keys():
vcs = VCS(name=vcs_name) vcs_api = VCSAPI(name=vcs_name)
if vcs.is_usable: if vcs_api.is_usable:
return vcs return vcs_api
raise OSError("No such directory .git/ or .hg/ ") raise OSError("No such directory .git/ or .hg/ ")

View file

@ -14,7 +14,7 @@ import pkg_resources
from . import lex_id from . import lex_id
from . import patterns from . import patterns
log = logging.getLogger("pycalver.version") logger = logging.getLogger("pycalver.version")
# The test suite may replace this. # The test suite may replace this.
@ -467,7 +467,7 @@ def incr(
try: try:
old_vinfo = parse_version_info(old_version, pattern) old_vinfo = parse_version_info(old_version, pattern)
except PatternError as ex: except PatternError as ex:
log.error(str(ex)) logger.error(str(ex))
return None return None
cur_vinfo = old_vinfo cur_vinfo = old_vinfo
@ -480,7 +480,7 @@ def incr(
if old_date <= cur_date: if old_date <= cur_date:
cur_vinfo = cur_vinfo._replace(**cur_cal_nfo._asdict()) cur_vinfo = cur_vinfo._replace(**cur_cal_nfo._asdict())
else: else:
log.warning(f"Version appears to be from the future '{old_version}'") logger.warning(f"Version appears to be from the future '{old_version}'")
cur_vinfo = cur_vinfo._replace(bid=lex_id.next_id(cur_vinfo.bid)) cur_vinfo = cur_vinfo._replace(bid=lex_id.next_id(cur_vinfo.bid))
@ -496,7 +496,7 @@ def incr(
new_version = format_version(cur_vinfo, pattern) new_version = format_version(cur_vinfo, pattern)
if new_version == old_version: if new_version == old_version:
log.error("Invalid arguments or pattern, version did not change.") logger.error("Invalid arguments or pattern, version did not change.")
return None return None
else: else:
return new_version return new_version