-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update versionadded:: next (and similar directives) on release
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"""Tests for the update_version_next tool.""" | ||
|
||
from pathlib import Path | ||
import unittest | ||
|
||
from test.support import os_helper | ||
|
||
import update_version_next | ||
|
||
TO_CHANGE = """ | ||
Directives to change | ||
-------------------- | ||
Here, all occurences of NEXT (lowercase) should be changed: | ||
.. versionadded:: next | ||
.. versionchanged:: next | ||
.. deprecated:: next | ||
.. deprecated-removed:: next 4.0 | ||
whitespace: | ||
.. versionchanged:: next | ||
.. versionchanged :: next | ||
.. versionadded:: next | ||
arguments: | ||
.. versionadded:: next | ||
Foo bar | ||
.. versionadded:: next as ``previousname`` | ||
""" | ||
|
||
UNCHANGED = """ | ||
Unchanged | ||
--------- | ||
Here, the word "next" should NOT be changed: | ||
.. versionchanged:: NEXT | ||
..versionchanged:: NEXT | ||
... versionchanged:: next | ||
foo .. versionchanged:: next | ||
.. otherdirective:: next | ||
.. VERSIONCHANGED: next | ||
.. deprecated-removed: 3.0 next | ||
""" | ||
|
||
EXPECTED_CHANGED = TO_CHANGE.replace('next', 'VER') | ||
|
||
|
||
class TestVersionNext(unittest.TestCase): | ||
maxDiff = len(TO_CHANGE + UNCHANGED) * 10 | ||
|
||
def test_freeze_simple_script(self): | ||
with os_helper.temp_dir() as testdir: | ||
path = Path(testdir) | ||
path.joinpath('source.rst').write_text(TO_CHANGE + UNCHANGED) | ||
path.joinpath('subdir').mkdir() | ||
path.joinpath('subdir/change.rst').write_text( | ||
'.. versionadded:: next') | ||
path.joinpath('subdir/keep.not-rst').write_text( | ||
'.. versionadded:: next') | ||
path.joinpath('subdir/keep.rst').write_text( | ||
'nothing to see here') | ||
args = ['VER', testdir] | ||
update_version_next.main(args) | ||
self.assertEqual(path.joinpath('source.rst').read_text(), | ||
EXPECTED_CHANGED + UNCHANGED) | ||
self.assertEqual(path.joinpath('subdir/change.rst').read_text(), | ||
'.. versionadded:: VER') | ||
self.assertEqual(path.joinpath('subdir/keep.not-rst').read_text(), | ||
'.. versionadded:: next') | ||
self.assertEqual(path.joinpath('subdir/keep.rst').read_text(), | ||
'nothing to see here') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Replace `.. versionchanged:: next` lines in docs files by the given version. | ||
Run this at release time to replace `next` with the just-released version | ||
in the sources. | ||
No backups are made; add/commit to Git before running the script. | ||
Applies to all the VersionChange directives. For deprecated-removed, only | ||
handle the first argument (deprecation version, not the removal version). | ||
""" | ||
|
||
import argparse | ||
import re | ||
import sys | ||
from pathlib import Path | ||
|
||
DIRECTIVE_RE = re.compile( | ||
r''' | ||
(?P<before> | ||
\s*\.\.\s+ | ||
(version(added|changed|removed)|deprecated(-removed)?) | ||
\s*::\s* | ||
) | ||
next | ||
(?P<after> | ||
.* | ||
) | ||
''', | ||
re.VERBOSE | re.DOTALL, | ||
) | ||
|
||
doc_dir = (Path(__file__) | ||
.parent # cpython/Doc/tools | ||
.parent # cpython/Doc | ||
.resolve() | ||
) | ||
|
||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument('version', | ||
help='String to replace "next" with. Usually `x.y`, ' | ||
+ 'but can be anything.') | ||
parser.add_argument('directory', type=Path, nargs='?', | ||
help=f'Directory to process. Default: {doc_dir}', | ||
default=doc_dir) | ||
parser.add_argument('--verbose', '-v', action='count', default=0, | ||
help='Increase verbosity. Can be repeated (`-vv`).') | ||
|
||
|
||
def main(argv): | ||
args = parser.parse_args(argv) | ||
version = args.version | ||
if args.verbose: | ||
print( | ||
f'Updating "next" versions in {args.directory} to {version!r}', | ||
file=sys.stderr) | ||
for path in Path(args.directory).glob('**/*.rst'): | ||
num_changed_lines = 0 | ||
lines = [] | ||
with open(path, encoding='utf-8') as file: | ||
for lineno, line in enumerate(file, start=1): | ||
try: | ||
if match := DIRECTIVE_RE.fullmatch(line): | ||
line = match['before'] + version + match['after'] | ||
num_changed_lines += 1 | ||
lines.append(line) | ||
except Exception as exc: | ||
exc.add_note(f'processing line {path}:{lineno}') | ||
raise | ||
if num_changed_lines: | ||
if args.verbose: | ||
print(f'Updating file {path} ({num_changed_lines} changes)', | ||
file=sys.stderr) | ||
with open(path, 'w', encoding='utf-8') as file: | ||
file.writelines(lines) | ||
else: | ||
if args.verbose > 1: | ||
print(f'Unchanged file {path}', file=sys.stderr) | ||
|
||
|
||
if __name__ == '__main__': | ||
main(sys.argv[1:]) |