mirror of
https://github.com/TECHNOFAB11/bumpver.git
synced 2025-12-12 22:40:09 +01:00
wip: refuctoring on the road to v201812
This commit is contained in:
parent
70f4e01104
commit
fe06833764
5 changed files with 468 additions and 302 deletions
|
|
@ -13,6 +13,7 @@ import typing as typ
|
|||
from . import parse
|
||||
from . import config
|
||||
|
||||
|
||||
log = logging.getLogger("pycalver.rewrite")
|
||||
|
||||
|
||||
|
|
@ -46,12 +47,12 @@ def rewrite_lines(
|
|||
>>> new_lines = rewrite_lines(patterns, "v201811.0123-beta", old_lines)
|
||||
>>> assert new_lines == ['__version__ = "v201811.0123-beta"']
|
||||
"""
|
||||
new_version_nfo = parse.VersionInfo.parse(new_version)
|
||||
new_version_nfo = parse.parse_version_info(new_version)
|
||||
new_version_fmt_kwargs = new_version_nfo._asdict()
|
||||
|
||||
new_lines = old_lines.copy()
|
||||
|
||||
for m in parse.PatternMatch.iter_matches(old_lines, patterns):
|
||||
for m in parse.iter_matches(old_lines, patterns):
|
||||
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:]
|
||||
|
|
@ -68,99 +69,99 @@ class RewrittenFileData(typ.NamedTuple):
|
|||
old_lines: typ.List[str]
|
||||
new_lines: typ.List[str]
|
||||
|
||||
@property
|
||||
def diff_lines(self) -> typ.List[str]:
|
||||
r"""Generate unified diff.
|
||||
|
||||
>>> rwd = RewrittenFileData(
|
||||
... path = "<path>",
|
||||
... line_sep = "\n",
|
||||
... old_lines = ["foo"],
|
||||
... new_lines = ["bar"],
|
||||
... )
|
||||
>>> rwd.diff_lines
|
||||
['--- <path>', '+++ <path>', '@@ -1 +1 @@', '-foo', '+bar']
|
||||
"""
|
||||
return list(
|
||||
difflib.unified_diff(
|
||||
a=self.old_lines,
|
||||
b=self.new_lines,
|
||||
lineterm="",
|
||||
fromfile=self.path,
|
||||
tofile=self.path,
|
||||
)
|
||||
)
|
||||
def rfd_from_content(patterns: typ.List[str], new_version: str, content: str) -> RewrittenFileData:
|
||||
r"""Rewrite pattern occurrences with version string.
|
||||
|
||||
@staticmethod
|
||||
def from_content(
|
||||
patterns: typ.List[str], new_version: str, content: str
|
||||
) -> 'RewrittenFileData':
|
||||
r"""Rewrite pattern occurrences with version string.
|
||||
>>> patterns = ['__version__ = "{version}"']
|
||||
>>> content = '__version__ = "v201809.0001-alpha"'
|
||||
>>> rfd = rfd_from_content(patterns, "v201809.0123", content)
|
||||
>>> assert rfd.new_lines == ['__version__ = "v201809.0123"']
|
||||
"""
|
||||
line_sep = detect_line_sep(content)
|
||||
old_lines = content.split(line_sep)
|
||||
new_lines = rewrite_lines(patterns, new_version, old_lines)
|
||||
return RewrittenFileData("<path>", line_sep, old_lines, new_lines)
|
||||
|
||||
>>> patterns = ['__version__ = "{version}"']
|
||||
>>> content = '__version__ = "v201809.0001-alpha"'
|
||||
>>> rwd = RewrittenFileData.from_content(patterns, "v201809.0123", content)
|
||||
>>> assert rwd.new_lines == ['__version__ = "v201809.0123"']
|
||||
"""
|
||||
line_sep = detect_line_sep(content)
|
||||
old_lines = content.split(line_sep)
|
||||
new_lines = rewrite_lines(patterns, new_version, old_lines)
|
||||
return RewrittenFileData("<path>", line_sep, old_lines, new_lines)
|
||||
|
||||
@staticmethod
|
||||
def iter_rewritten(
|
||||
file_patterns: config.PatternsByFilePath, new_version: str
|
||||
) -> typ.Iterable['RewrittenFileData']:
|
||||
r'''Iterate over files with version string replaced.
|
||||
def iter_rewritten(
|
||||
file_patterns: config.PatternsByFilePath, new_version: str
|
||||
) -> typ.Iterable[RewrittenFileData]:
|
||||
r'''Iterate over files with version string replaced.
|
||||
|
||||
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{version}"']}
|
||||
>>> rewritten_datas = RewrittenFileData.iter_rewritten(file_patterns, "v201809.0123")
|
||||
>>> rwd = list(rewritten_datas)[0]
|
||||
>>> assert rwd.new_lines == [
|
||||
... '# This file is part of the pycalver project',
|
||||
... '# https://gitlab.com/mbarkhau/pycalver',
|
||||
... '#',
|
||||
... '# Copyright (c) 2018 Manuel Barkhau (@mbarkhau) - MIT License',
|
||||
... '# SPDX-License-Identifier: MIT',
|
||||
... '"""PyCalVer: Automatic CalVer Versioning for Python Packages."""',
|
||||
... '',
|
||||
... '__version__ = "v201809.0123"',
|
||||
... '',
|
||||
... ]
|
||||
>>>
|
||||
'''
|
||||
for filepath, patterns in file_patterns.items():
|
||||
with io.open(filepath, mode="rt", encoding="utf-8") as fh:
|
||||
content = fh.read()
|
||||
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{version}"']}
|
||||
>>> rewritten_datas = iter_rewritten(file_patterns, "v201809.0123")
|
||||
>>> rfd = list(rewritten_datas)[0]
|
||||
>>> assert rfd.new_lines == [
|
||||
... '# This file is part of the pycalver project',
|
||||
... '# https://gitlab.com/mbarkhau/pycalver',
|
||||
... '#',
|
||||
... '# Copyright (c) 2018 Manuel Barkhau (@mbarkhau) - MIT License',
|
||||
... '# SPDX-License-Identifier: MIT',
|
||||
... '"""PyCalVer: Automatic CalVer Versioning for Python Packages."""',
|
||||
... '',
|
||||
... '__version__ = "v201809.0123"',
|
||||
... '',
|
||||
... ]
|
||||
>>>
|
||||
'''
|
||||
for filepath, patterns in file_patterns.items():
|
||||
with io.open(filepath, mode="rt", encoding="utf-8") as fh:
|
||||
content = fh.read()
|
||||
|
||||
rfd = RewrittenFileData.from_content(patterns, new_version, content)
|
||||
yield rfd._replace(path=filepath)
|
||||
rfd = rfd_from_content(patterns, new_version, content)
|
||||
yield rfd._replace(path=filepath)
|
||||
|
||||
|
||||
def diff_lines(rfd: RewrittenFileData) -> typ.List[str]:
|
||||
r"""Generate unified diff.
|
||||
|
||||
>>> rfd = RewrittenFileData(
|
||||
... path = "<path>",
|
||||
... line_sep = "\n",
|
||||
... old_lines = ["foo"],
|
||||
... new_lines = ["bar"],
|
||||
... )
|
||||
>>> diff_lines(rfd)
|
||||
['--- <path>', '+++ <path>', '@@ -1 +1 @@', '-foo', '+bar']
|
||||
"""
|
||||
lines = difflib.unified_diff(
|
||||
a=rfd.old_lines, b=rfd.new_lines, lineterm="", fromfile=rfd.path, tofile=rfd.path
|
||||
)
|
||||
return list(lines)
|
||||
|
||||
|
||||
def diff(new_version: str, file_patterns: config.PatternsByFilePath) -> str:
|
||||
r"""Generate diffs of rewritten files.
|
||||
|
||||
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{version}"']}
|
||||
>>> diff_lines = diff("v201809.0123", file_patterns).split("\n")
|
||||
>>> diff_lines[:2]
|
||||
>>> diff_str = diff("v201809.0123", file_patterns)
|
||||
>>> lines = diff_str.split("\n")
|
||||
>>> lines[:2]
|
||||
['--- src/pycalver/__init__.py', '+++ src/pycalver/__init__.py']
|
||||
>>> assert diff_lines[6].startswith('-__version__ = "v2')
|
||||
>>> assert not diff_lines[6].startswith('-__version__ = "v201809.0123"')
|
||||
>>> diff_lines[7]
|
||||
>>> assert lines[6].startswith('-__version__ = "v2')
|
||||
>>> assert not lines[6].startswith('-__version__ = "v201809.0123"')
|
||||
>>> lines[7]
|
||||
'+__version__ = "v201809.0123"'
|
||||
"""
|
||||
diff_lines: typ.List[str] = []
|
||||
|
||||
for rwd in RewrittenFileData.iter_rewritten(file_patterns, new_version):
|
||||
diff_lines += rwd.diff_lines
|
||||
full_diff = ""
|
||||
file_path: str
|
||||
for file_path, patterns in file_patterns.items():
|
||||
with io.open(file_path, mode="rt", encoding="utf-8") as fh:
|
||||
content = fh.read()
|
||||
|
||||
return "\n".join(diff_lines)
|
||||
rfd = rfd_from_content(patterns, new_version, content)
|
||||
full_diff += "\n".join(diff_lines(rfd)) + "\n"
|
||||
|
||||
full_diff = full_diff.rstrip("\n")
|
||||
return full_diff
|
||||
|
||||
|
||||
def rewrite(new_version: str, file_patterns: config.PatternsByFilePath) -> None:
|
||||
"""Rewrite project files, updating each with the new version."""
|
||||
|
||||
for file_data in RewrittenFileData.iter_rewritten(file_patterns, new_version):
|
||||
for file_data in iter_rewritten(file_patterns, new_version):
|
||||
new_content = file_data.line_sep.join(file_data.new_lines)
|
||||
with io.open(file_data.path, mode="wt", encoding="utf-8") as fh:
|
||||
fh.write(new_content)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue