|
5 | 5 | from pathlib import Path |
6 | 6 | from subprocess import STDOUT, CalledProcessError, call, check_output |
7 | 7 | from tempfile import TemporaryDirectory |
8 | | -from typing import Dict, List |
| 8 | +from typing import Dict, List, Optional, Tuple |
9 | 9 |
|
10 | 10 | from . import __version__ |
11 | 11 |
|
|
17 | 17 | MERGE_BRANCH = "skeleton-merge-branch" |
18 | 18 | # Extensions to change |
19 | 19 | CHANGE_SUFFIXES = [".py", ".rst", ".cfg", "", ".toml"] |
20 | | -# Files not to change where IGNORE_FILES[x] is a list containing line numbers |
21 | | -# in the file x to be ignored. An empty list will ignore the whole file. |
22 | | -IGNORE_FILES: Dict[str, List[int]] = { |
| 20 | +# Files not to change where IGNORE_FILES[x] is a list of tuples where substitutions |
| 21 | +# will be ignored in that file in any substring between the two strings. |
| 22 | +# An empty list will ignore the whole file. |
| 23 | +IGNORE_FILES: Dict[str, List[Optional[Tuple[str, str]]]] = { |
23 | 24 | "update-tools.rst": [], |
24 | 25 | "test_boilerplate_removed.py": [], |
25 | 26 | "pin-requirements.rst": [], |
26 | 27 | "0002-switched-to-pip-skeleton.rst:": [], |
27 | | - "README.rst": [10], |
| 28 | + "README.rst": [ |
| 29 | + ("adopt this skeleton project see", "that describes what your module does") |
| 30 | + ], |
28 | 31 | } |
29 | 32 |
|
30 | 33 | SKELETON_ROOT_COMMIT = "ededf00035e6ccfac78946213009c1ecd7c110a9" |
@@ -108,11 +111,29 @@ def replace_text(text: str) -> str: |
108 | 111 | and IGNORE_FILES[child.name] |
109 | 112 | ): |
110 | 113 | original_text = child.read_text() |
111 | | - original_lines = original_text.splitlines() |
112 | | - replaced_lines = replace_text(original_text).splitlines() |
113 | | - for ignored_line in IGNORE_FILES[child.name]: |
114 | | - replaced_lines[ignored_line - 1] = original_lines[ignored_line - 1] |
115 | | - child.write_text("\n".join(replaced_lines)) |
| 114 | + replaced_text = replace_text(original_text) |
| 115 | + for sub_strings in IGNORE_FILES[child.name]: |
| 116 | + assert isinstance(sub_strings, tuple) |
| 117 | + pre_sub_string, post_sub_string = sub_strings |
| 118 | + regex = rf"(?s){pre_sub_string}(.*?){post_sub_string}" |
| 119 | + # finditer so we can allow for different contents between ignore |
| 120 | + # substrings, e.g: `a foo b` and then later in the file `a bar b` |
| 121 | + original_substrings = list(re.finditer(regex, original_text)) |
| 122 | + replaced_substrings = list(re.finditer(regex, replaced_text)) |
| 123 | + assert len(original_substrings) == len(replaced_substrings), ( |
| 124 | + "different number of ignored substrings between " |
| 125 | + f"{pre_sub_string} and {post_sub_string} found in " |
| 126 | + f"{child.name}, did you include replaced " |
| 127 | + "text inside the substrings?" |
| 128 | + ) |
| 129 | + for original_substring, replaced_substring in zip( |
| 130 | + original_substrings, |
| 131 | + replaced_substrings, |
| 132 | + ): |
| 133 | + replaced_text = replaced_text.replace( |
| 134 | + replaced_substring.group(0), original_substring.group(0) |
| 135 | + ) |
| 136 | + child.write_text(replaced_text) |
116 | 137 |
|
117 | 138 | # Commit what we have and push to the original repo |
118 | 139 | git_tmp("commit", "-a", "-m", f"Rename python3-pip-skeleton -> {repo}") |
|
0 commit comments