bumpver/src/pycalver/__main__.py

178 lines
5.3 KiB
Python
Raw Normal View History

2018-09-02 21:48:12 +02:00
#!/usr/bin/env python
# This file is part of the pycalver project
2018-11-04 21:34:53 +01:00
# https://gitlab.com/mbarkhau/pycalver
2018-09-02 21:48:12 +02:00
#
2018-11-04 21:34:53 +01:00
# Copyright (c) 2018 Manuel Barkhau (@mbarkhau) - MIT License
2018-09-02 21:48:12 +02:00
# SPDX-License-Identifier: MIT
import io
import os
import sys
import click
import logging
from . import DEBUG
from . import vcs
from . import parse
from . import config
from . import version
from . import rewrite
log = logging.getLogger("pycalver.__main__")
def _init_loggers(verbose: bool) -> None:
if DEBUG:
log_formatter = logging.Formatter('%(levelname)s - %(name)s - %(message)s')
2018-11-04 21:11:42 +01:00
log_level = logging.DEBUG
2018-09-02 21:48:12 +02:00
elif verbose:
log_formatter = logging.Formatter('%(levelname)s - %(message)s')
2018-11-04 21:11:42 +01:00
log_level = logging.INFO
2018-09-02 21:48:12 +02:00
else:
log_formatter = logging.Formatter('%(message)s')
2018-11-04 21:11:42 +01:00
log_level = logging.WARNING
2018-09-02 21:48:12 +02:00
loggers = [log, vcs.log, parse.log, config.log, rewrite.log, version.log]
for logger in loggers:
if len(logger.handlers) == 0:
ch = logging.StreamHandler(sys.stderr)
ch.setFormatter(log_formatter)
logger.addHandler(ch)
logger.setLevel(log_level)
log.debug("Loggers initialized.")
@click.group()
def cli():
"""parse and update project versions automatically."""
@cli.command()
def show() -> None:
_init_loggers(verbose=False)
2018-09-03 22:23:51 +02:00
cfg: config.MaybeConfig = config.parse()
2018-09-02 21:48:12 +02:00
if cfg is None:
2018-09-02 23:36:57 +02:00
log.error("Could not parse configuration from setup.cfg")
sys.exit(1)
2018-09-02 21:48:12 +02:00
2018-09-03 22:23:51 +02:00
print(f"Current Version: {cfg.current_version}")
print(f"PEP440 Version: {cfg.pep440_version}")
2018-09-02 21:48:12 +02:00
2018-09-02 23:36:57 +02:00
@cli.command()
@click.argument("old_version")
@click.option(
2018-11-04 21:11:42 +01:00
"--release", default=None, metavar="<name>", help="Override release name of current_version"
2018-09-02 23:36:57 +02:00
)
def incr(old_version: str, release: str = None) -> None:
_init_loggers(verbose=False)
if release and release not in parse.VALID_RELESE_VALUES:
log.error(f"Invalid argument --release={release}")
log.error(f"Valid arguments are: {', '.join(parse.VALID_RELESE_VALUES)}")
sys.exit(1)
2018-11-04 21:11:42 +01:00
new_version = version.bump(old_version, release=release)
2018-09-02 23:36:57 +02:00
new_version_nfo = parse.parse_version_info(new_version)
print("PyCalVer Version:", new_version)
2018-11-04 21:11:42 +01:00
print("PEP440 Version:" , new_version_nfo.pep440_version)
2018-09-02 23:36:57 +02:00
2018-09-02 21:48:12 +02:00
@cli.command()
@click.option(
2018-11-04 21:11:42 +01:00
"--dry", default=False, is_flag=True, help="Display diff of changes, don't rewrite files."
2018-09-02 21:48:12 +02:00
)
def init(dry: bool) -> None:
"""Initialize [pycalver] configuration in setup.cfg"""
_init_loggers(verbose=False)
2018-11-04 21:11:42 +01:00
cfg : config.MaybeConfig = config.parse()
2018-09-02 21:48:12 +02:00
if cfg:
log.error("Configuration already initialized in setup.cfg")
2018-09-02 23:36:57 +02:00
sys.exit(1)
2018-09-02 21:48:12 +02:00
cfg_lines = config.default_config_lines()
if dry:
print("Exiting because of '--dry'. Would have written to setup.cfg:")
print("\n " + "\n ".join(cfg_lines))
return
if os.path.exists("setup.cfg"):
cfg_content = "\n" + "\n".join(cfg_lines)
with io.open("setup.cfg", mode="at", encoding="utf-8") as fh:
fh.write(cfg_content)
print("Updated setup.cfg")
else:
cfg_content = "\n".join(cfg_lines)
with io.open("setup.cfg", mode="at", encoding="utf-8") as fh:
fh.write(cfg_content)
print("Created setup.cfg")
@cli.command()
2018-09-02 23:36:57 +02:00
@click.option(
2018-11-04 21:11:42 +01:00
"--release", default=None, metavar="<name>", help="Override release name of current_version"
2018-09-02 21:48:12 +02:00
)
2018-11-04 21:11:42 +01:00
@click.option("--verbose", default=False, is_flag=True, help="Log applied changes to stderr")
2018-09-02 23:36:57 +02:00
@click.option(
2018-11-04 21:11:42 +01:00
"--dry", default=False, is_flag=True, help="Display diff of changes, don't rewrite files."
2018-09-02 23:36:57 +02:00
)
2018-11-04 21:11:42 +01:00
@click.option("--commit", default=True, is_flag=True, help="Commit after updating version strings.")
@click.option("--tag" , default=True, is_flag=True, help="Tag the commit.")
2018-09-03 00:14:10 +02:00
@click.option(
"--allow-dirty",
default=False,
is_flag=True,
help=(
"Commit even when working directory is has uncomitted changes. "
"(WARNING: The commit will still be aborted if there are uncomitted "
"to files with version strings."
),
)
def bump(
2018-11-04 21:11:42 +01:00
release: str, verbose: bool, dry: bool, commit: bool, tag: bool, allow_dirty: bool
2018-09-03 00:14:10 +02:00
) -> None:
2018-09-02 21:48:12 +02:00
_init_loggers(verbose)
2018-09-02 23:36:57 +02:00
2018-09-02 21:48:12 +02:00
if release and release not in parse.VALID_RELESE_VALUES:
log.error(f"Invalid argument --release={release}")
log.error(f"Valid arguments are: {', '.join(parse.VALID_RELESE_VALUES)}")
2018-09-02 23:36:57 +02:00
sys.exit(1)
2018-09-02 21:48:12 +02:00
2018-09-03 22:23:51 +02:00
cfg: config.MaybeConfig = config.parse()
2018-09-02 21:48:12 +02:00
if cfg is None:
2018-09-02 23:36:57 +02:00
log.error("Could not parse configuration from setup.cfg")
sys.exit(1)
2018-09-02 21:48:12 +02:00
2018-09-03 22:23:51 +02:00
old_version = cfg.current_version
2018-09-02 21:48:12 +02:00
new_version = version.bump(old_version, release=release)
log.info(f"Old Version: {old_version}")
log.info(f"New Version: {new_version}")
2018-09-02 23:36:57 +02:00
if dry:
log.info("Running with '--dry', showing diffs instead of updating files.")
2018-09-03 22:23:51 +02:00
file_patterns = cfg.file_patterns
2018-11-04 21:11:42 +01:00
filepaths = set(file_patterns.keys())
2018-09-03 00:14:10 +02:00
2018-09-03 22:23:51 +02:00
_vcs = vcs.get_vcs()
2018-09-04 20:16:46 +02:00
if _vcs is None:
log.warn("Version Control System not found, aborting commit.")
else:
_vcs.assert_not_dirty(filepaths, allow_dirty)
2018-09-02 23:36:57 +02:00
2018-09-04 20:16:46 +02:00
rewrite.rewrite(new_version, file_patterns, dry, verbose)
2018-09-02 23:36:57 +02:00
2018-09-04 20:16:46 +02:00
if dry or not commit or _vcs is None:
2018-09-02 23:36:57 +02:00
return
2018-09-04 20:16:46 +02:00
# TODO (mb 2018-09-04): add files and commit