2018-09-02 21:48:12 +02:00
|
|
|
# This file is part of the pycalver project
|
|
|
|
|
# https://github.com/mbarkhau/pycalver
|
|
|
|
|
#
|
|
|
|
|
# (C) 2018 Manuel Barkhau (@mbarkhau)
|
|
|
|
|
# SPDX-License-Identifier: MIT
|
2018-11-15 22:16:16 +01:00
|
|
|
"""Rewrite files, updating occurences of version strings."""
|
2018-09-02 21:48:12 +02:00
|
|
|
|
2018-09-03 22:23:51 +02:00
|
|
|
import io
|
2018-09-02 21:48:12 +02:00
|
|
|
import difflib
|
2018-09-03 22:23:51 +02:00
|
|
|
import logging
|
|
|
|
|
import typing as typ
|
|
|
|
|
|
|
|
|
|
from . import parse
|
2018-11-15 22:16:16 +01:00
|
|
|
from . import config
|
2018-12-09 14:49:13 +01:00
|
|
|
from . import version
|
2018-09-02 21:48:12 +02:00
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
|
2018-09-02 21:48:12 +02:00
|
|
|
log = logging.getLogger("pycalver.rewrite")
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 22:16:16 +01:00
|
|
|
def detect_line_sep(content: str) -> str:
|
|
|
|
|
r"""Parse line separator from content.
|
|
|
|
|
|
|
|
|
|
>>> detect_line_sep('\r\n')
|
|
|
|
|
'\r\n'
|
|
|
|
|
>>> detect_line_sep('\r')
|
|
|
|
|
'\r'
|
|
|
|
|
>>> detect_line_sep('\n')
|
|
|
|
|
'\n'
|
|
|
|
|
>>> detect_line_sep('')
|
|
|
|
|
'\n'
|
|
|
|
|
"""
|
2018-11-11 15:40:16 +01:00
|
|
|
if "\r\n" in content:
|
|
|
|
|
return "\r\n"
|
|
|
|
|
elif "\r" in content:
|
|
|
|
|
return "\r"
|
|
|
|
|
else:
|
|
|
|
|
return "\n"
|
|
|
|
|
|
|
|
|
|
|
2018-11-04 21:11:42 +01:00
|
|
|
def rewrite_lines(
|
2018-11-15 22:16:16 +01:00
|
|
|
patterns: typ.List[str], new_version: str, old_lines: typ.List[str]
|
2018-11-04 21:11:42 +01:00
|
|
|
) -> typ.List[str]:
|
2018-11-15 22:16:16 +01:00
|
|
|
"""Replace occurances of patterns in old_lines with new_version.
|
|
|
|
|
|
2019-01-06 14:38:20 +01:00
|
|
|
>>> patterns = ['__version__ = "{pycalver}"']
|
|
|
|
|
>>> rewrite_lines(patterns, "v201811.0123-beta", ['__version__ = "v201809.0002-beta"'])
|
|
|
|
|
['__version__ = "v201811.0123-beta"']
|
|
|
|
|
|
|
|
|
|
>>> patterns = ['__version__ = "{pep440_version}"']
|
|
|
|
|
>>> rewrite_lines(patterns, "v201811.0123-beta", ['__version__ = "201809.2b0"'])
|
|
|
|
|
['__version__ = "201811.123b0"']
|
2018-11-15 22:16:16 +01:00
|
|
|
"""
|
2019-01-06 14:38:20 +01:00
|
|
|
new_version_nfo = version.parse_version_info(new_version)
|
2018-09-03 22:23:51 +02:00
|
|
|
|
2018-12-09 14:59:21 +01:00
|
|
|
new_lines = old_lines[:]
|
2018-11-04 21:11:42 +01:00
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
for m in parse.iter_matches(old_lines, patterns):
|
2019-01-06 14:38:20 +01:00
|
|
|
replacement = version.format_version(new_version_nfo, m.pattern)
|
2018-11-04 21:11:42 +01:00
|
|
|
span_l, span_r = m.span
|
|
|
|
|
new_line = m.line[:span_l] + replacement + m.line[span_r:]
|
|
|
|
|
new_lines[m.lineno] = new_line
|
|
|
|
|
|
|
|
|
|
return new_lines
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 22:16:16 +01:00
|
|
|
class RewrittenFileData(typ.NamedTuple):
|
|
|
|
|
"""Container for line-wise content of rewritten files."""
|
|
|
|
|
|
|
|
|
|
path : str
|
|
|
|
|
line_sep : str
|
|
|
|
|
old_lines: typ.List[str]
|
|
|
|
|
new_lines: typ.List[str]
|
|
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
|
|
|
|
|
def rfd_from_content(patterns: typ.List[str], new_version: str, content: str) -> RewrittenFileData:
|
|
|
|
|
r"""Rewrite pattern occurrences with version string.
|
|
|
|
|
|
2019-01-06 14:38:20 +01:00
|
|
|
>>> patterns = ['__version__ = "{pycalver}"']
|
2018-12-05 09:38:27 +01:00
|
|
|
>>> content = '__version__ = "v201809.0001-alpha"'
|
|
|
|
|
>>> rfd = rfd_from_content(patterns, "v201809.0123", content)
|
2019-01-06 14:38:20 +01:00
|
|
|
>>> rfd.new_lines
|
|
|
|
|
['__version__ = "v201809.0123"']
|
2018-12-05 09:38:27 +01:00
|
|
|
"""
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def iter_rewritten(
|
|
|
|
|
file_patterns: config.PatternsByFilePath, new_version: str
|
|
|
|
|
) -> typ.Iterable[RewrittenFileData]:
|
|
|
|
|
r'''Iterate over files with version string replaced.
|
|
|
|
|
|
2019-01-06 14:38:20 +01:00
|
|
|
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']}
|
2018-12-05 09:38:27 +01:00
|
|
|
>>> 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',
|
|
|
|
|
... '#',
|
2018-12-09 18:09:35 +01:00
|
|
|
... '# Copyright (c) 2018 Manuel Barkhau (mbarkhau@gmail.com) - MIT License',
|
2018-12-05 09:38:27 +01:00
|
|
|
... '# 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 = 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)
|
2018-11-15 22:16:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def diff(new_version: str, file_patterns: config.PatternsByFilePath) -> str:
|
|
|
|
|
r"""Generate diffs of rewritten files.
|
|
|
|
|
|
2019-01-06 14:38:20 +01:00
|
|
|
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']}
|
2018-12-05 09:38:27 +01:00
|
|
|
>>> diff_str = diff("v201809.0123", file_patterns)
|
|
|
|
|
>>> lines = diff_str.split("\n")
|
|
|
|
|
>>> lines[:2]
|
2018-11-15 22:16:16 +01:00
|
|
|
['--- src/pycalver/__init__.py', '+++ src/pycalver/__init__.py']
|
2018-12-05 09:38:27 +01:00
|
|
|
>>> assert lines[6].startswith('-__version__ = "v2')
|
|
|
|
|
>>> assert not lines[6].startswith('-__version__ = "v201809.0123"')
|
|
|
|
|
>>> lines[7]
|
2018-11-15 22:16:16 +01:00
|
|
|
'+__version__ = "v201809.0123"'
|
|
|
|
|
"""
|
|
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
full_diff = ""
|
|
|
|
|
file_path: str
|
2018-12-21 23:48:48 +01:00
|
|
|
for file_path, patterns in sorted(file_patterns.items()):
|
2018-12-05 09:38:27 +01:00
|
|
|
with io.open(file_path, mode="rt", encoding="utf-8") as fh:
|
|
|
|
|
content = fh.read()
|
|
|
|
|
|
|
|
|
|
rfd = rfd_from_content(patterns, new_version, content)
|
2018-12-08 19:18:47 +01:00
|
|
|
rfd = rfd._replace(path=file_path)
|
2018-12-05 09:38:27 +01:00
|
|
|
full_diff += "\n".join(diff_lines(rfd)) + "\n"
|
2018-11-15 22:16:16 +01:00
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
full_diff = full_diff.rstrip("\n")
|
|
|
|
|
return full_diff
|
2018-11-15 22:16:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def rewrite(new_version: str, file_patterns: config.PatternsByFilePath) -> None:
|
|
|
|
|
"""Rewrite project files, updating each with the new version."""
|
|
|
|
|
|
2018-12-05 09:38:27 +01:00
|
|
|
for file_data in iter_rewritten(file_patterns, new_version):
|
2018-11-15 22:16:16 +01:00
|
|
|
new_content = file_data.line_sep.join(file_data.new_lines)
|
|
|
|
|
with io.open(file_data.path, mode="wt", encoding="utf-8") as fh:
|
2018-09-03 22:23:51 +02:00
|
|
|
fh.write(new_content)
|