mirror of
https://github.com/TECHNOFAB11/bumpver.git
synced 2025-12-12 14:30:09 +01:00
WIP: diffs and stuff
This commit is contained in:
parent
3471560eaa
commit
ce9083ef85
7 changed files with 246 additions and 64 deletions
|
|
@ -6,6 +6,6 @@
|
|||
|
||||
import os
|
||||
|
||||
__version__ = "v201809.0001-beta"
|
||||
__version__ = "v201809.0002-beta"
|
||||
|
||||
DEBUG = os.environ.get("PYCALVER_DEBUG", "0") == "1"
|
||||
DEBUG = os.environ.get("PYCALVER_DEBUG", "0") == "1"
|
||||
|
|
@ -10,6 +10,7 @@ import os
|
|||
import sys
|
||||
import click
|
||||
import logging
|
||||
import difflib
|
||||
import typing as typ
|
||||
|
||||
from . import DEBUG
|
||||
|
|
@ -57,12 +58,36 @@ def show() -> None:
|
|||
|
||||
cfg = config.parse()
|
||||
if cfg is None:
|
||||
return
|
||||
log.error("Could not parse configuration from setup.cfg")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Current Version: {cfg['current_version']}")
|
||||
print(f"PEP440 Version: {cfg['pep440_version']}")
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument("old_version")
|
||||
@click.option(
|
||||
"--release",
|
||||
default=None,
|
||||
metavar="<name>",
|
||||
help="Override release name of current_version",
|
||||
)
|
||||
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)
|
||||
|
||||
new_version = version.bump(old_version, release=release)
|
||||
new_version_nfo = parse.parse_version_info(new_version)
|
||||
|
||||
print("PyCalVer Version:", new_version)
|
||||
print("PEP440 Version:", new_version_nfo["pep440_version"])
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
"--dry",
|
||||
|
|
@ -77,7 +102,7 @@ def init(dry: bool) -> None:
|
|||
cfg = config.parse()
|
||||
if cfg:
|
||||
log.error("Configuration already initialized in setup.cfg")
|
||||
return
|
||||
sys.exit(1)
|
||||
|
||||
cfg_lines = config.default_config_lines()
|
||||
|
||||
|
|
@ -99,6 +124,12 @@ def init(dry: bool) -> None:
|
|||
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
"--release",
|
||||
default=None,
|
||||
metavar="<name>",
|
||||
help="Override release name of current_version",
|
||||
)
|
||||
@click.option(
|
||||
"--verbose",
|
||||
default=False,
|
||||
|
|
@ -112,35 +143,78 @@ def init(dry: bool) -> None:
|
|||
help="Display diff of changes, don't rewrite files.",
|
||||
)
|
||||
@click.option(
|
||||
"--release",
|
||||
default=None,
|
||||
metavar="<name>",
|
||||
help="Override release name of current_version",
|
||||
"--commit",
|
||||
default=True,
|
||||
is_flag=True,
|
||||
help="Tag the commit.",
|
||||
)
|
||||
def bump(verbose: bool, dry: bool, release: typ.Optional[str] = None) -> None:
|
||||
@click.option(
|
||||
"--tag",
|
||||
default=True,
|
||||
is_flag=True,
|
||||
help="Tag the commit.",
|
||||
)
|
||||
def bump(release: str, verbose: bool, dry: bool, commit: bool, tag: bool) -> None:
|
||||
_init_loggers(verbose)
|
||||
|
||||
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)}")
|
||||
return
|
||||
sys.exit(1)
|
||||
|
||||
cfg = config.parse()
|
||||
|
||||
if cfg is None:
|
||||
log.error("Unable to parse pycalver configuration from setup.cfg")
|
||||
return
|
||||
log.error("Could not parse configuration from setup.cfg")
|
||||
sys.exit(1)
|
||||
|
||||
old_version = cfg["current_version"]
|
||||
new_version = version.bump(old_version, release=release)
|
||||
new_version_nfo = parse.parse_version_info(new_version)
|
||||
new_version_fmt_kwargs = new_version_nfo._asdict()
|
||||
|
||||
log.info(f"Old Version: {old_version}")
|
||||
log.info(f"New Version: {new_version}")
|
||||
|
||||
if dry:
|
||||
log.info("Running with '--dry', showing diffs instead of updating files.")
|
||||
|
||||
matches: typ.List[parse.PatternMatch]
|
||||
for filepath, patterns in cfg["file_patterns"].items():
|
||||
with io.open(filepath, mode="rt", encoding="utf-8") as fh:
|
||||
content = fh.read()
|
||||
lines = content.splitlines()
|
||||
matches = parse.parse_patterns(lines, patterns)
|
||||
|
||||
old_lines = content.splitlines()
|
||||
new_lines = old_lines.copy()
|
||||
|
||||
matches = parse.parse_patterns(old_lines, patterns)
|
||||
for m in matches:
|
||||
print(m)
|
||||
replacement = m.pattern.format(**new_version_fmt_kwargs)
|
||||
span_l, span_r = m.span
|
||||
new_line = m.line[:span_l] + replacement + m.line[span_r:]
|
||||
new_lines[m.lineno] = new_line
|
||||
|
||||
if dry or verbose:
|
||||
print("\n".join(difflib.unified_diff(
|
||||
old_lines,
|
||||
new_lines,
|
||||
lineterm="",
|
||||
fromfile="a/" + filepath,
|
||||
tofile="b/" + filepath,
|
||||
)))
|
||||
|
||||
# if not dry:
|
||||
# new_content = "\n".join(new_lines)
|
||||
# with io.open(filepath, mode="wt", encoding="utf-8") as fh:
|
||||
# fh.write(new_content)
|
||||
|
||||
if dry:
|
||||
return
|
||||
|
||||
if not commit:
|
||||
return
|
||||
|
||||
v = vcs.get_vcs()
|
||||
if v is None:
|
||||
log.warn("Version Control System not found, aborting commit.")
|
||||
return
|
||||
|
|
|
|||
|
|
@ -11,8 +11,7 @@ import sys
|
|||
import logging
|
||||
import typing as typ
|
||||
import datetime as dt
|
||||
|
||||
from pkg_resources import parse_version
|
||||
import pkg_resources
|
||||
|
||||
from . import lex_id
|
||||
|
||||
|
|
@ -22,6 +21,7 @@ log = logging.getLogger("pycalver.parse")
|
|||
VALID_RELESE_VALUES = ("alpha", "beta", "dev", "rc", "post")
|
||||
|
||||
|
||||
# https://regex101.com/r/fnj60p/10
|
||||
PYCALVER_RE: typ.re.Pattern[str] = re.compile(r"""
|
||||
\b
|
||||
(?P<version>
|
||||
|
|
@ -30,22 +30,20 @@ PYCALVER_RE: typ.re.Pattern[str] = re.compile(r"""
|
|||
(?P<year>\d{4})
|
||||
(?P<month>\d{2})
|
||||
)
|
||||
(?:
|
||||
(?P<build>
|
||||
\. # "." build nr prefix
|
||||
(?P<build>\d{4,})
|
||||
\d{4,}
|
||||
)
|
||||
(?:
|
||||
(?P<release>
|
||||
\- # "-" release prefix
|
||||
(?P<release>
|
||||
alpha|beta|dev|rc|post
|
||||
)
|
||||
(?:alpha|beta|dev|rc|post)
|
||||
)?
|
||||
)(?:\s|$)
|
||||
""", flags=re.VERBOSE)
|
||||
|
||||
|
||||
RE_PATTERN_PARTS = {
|
||||
"pep440_version" : r"\d{6}\.\d+(a|b|dev|rc|post)?\d*",
|
||||
"pep440_version" : r"\b\d{6}\.[1-9]\d*(a|b|dev|rc|post)?\d*(?:\s|$)",
|
||||
"version" : r"v\d{6}\.\d{4,}\-(?:alpha|beta|dev|rc|post)",
|
||||
"calver" : r"v\d{6}",
|
||||
"build" : r"\.\d{4,}",
|
||||
|
|
@ -55,15 +53,28 @@ RE_PATTERN_PARTS = {
|
|||
|
||||
class PatternMatch(typ.NamedTuple):
|
||||
|
||||
lineno : int
|
||||
lineno : int # zero based
|
||||
line : str
|
||||
pattern : str
|
||||
span : typ.Tuple[int, int]
|
||||
match : str
|
||||
|
||||
|
||||
MaybeMatch = typ.Optional[typ.re.Match[str]]
|
||||
PyCalVerInfo = typ.Dict[str, str]
|
||||
class VersionInfo(typ.NamedTuple):
|
||||
|
||||
pep440_version : str
|
||||
version : str
|
||||
calver : str
|
||||
year : str
|
||||
month : str
|
||||
build : str
|
||||
release : typ.Optional[str]
|
||||
|
||||
|
||||
def parse_version_info(version: str) -> VersionInfo:
|
||||
match = PYCALVER_RE.match(version)
|
||||
pep440_version = pkg_resources.parse_version(version)
|
||||
return VersionInfo(pep440_version=pep440_version, **match.groupdict())
|
||||
|
||||
|
||||
def iter_pattern_matches(lines: typ.List[str], pattern: str) -> typ.Iterable[PatternMatch]:
|
||||
|
|
@ -80,10 +91,9 @@ def iter_pattern_matches(lines: typ.List[str], pattern: str) -> typ.Iterable[Pat
|
|||
.replace("(", "\\(")
|
||||
.format(**RE_PATTERN_PARTS)
|
||||
)
|
||||
for i, line in enumerate(lines):
|
||||
for lineno, line in enumerate(lines):
|
||||
match = pattern_re.search(line)
|
||||
if match:
|
||||
lineno = i + 1
|
||||
yield PatternMatch(lineno, line, pattern, match.span(), match.group(0))
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ import logging
|
|||
import datetime as dt
|
||||
|
||||
from . import lex_id
|
||||
from . import parse
|
||||
|
||||
log = logging.getLogger("pycalver.version")
|
||||
|
||||
|
||||
|
|
@ -18,15 +20,16 @@ def current_calver() -> str:
|
|||
def bump(old_version: str, release: str=None) -> str:
|
||||
# old_version is assumed to be a valid calver string,
|
||||
# validated in pycalver.config.parse.
|
||||
|
||||
old_calver, rest = old_version.split(".")
|
||||
old_build, old_release = rest.split("-")
|
||||
old_ver = parse.parse_version_info(old_version)
|
||||
|
||||
new_calver = current_calver()
|
||||
new_build = lex_id.next_id(old_build)
|
||||
new_build = lex_id.next_id(old_ver.build[1:])
|
||||
if release is None:
|
||||
# preserve existing release
|
||||
new_release = old_release
|
||||
if old_ver.release:
|
||||
# preserve existing release
|
||||
new_release = old_ver.release[1:]
|
||||
else:
|
||||
new_release = None
|
||||
else:
|
||||
new_release = release
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue