1
1
"""Methods for changing files."""
2
- import logging
3
2
import re
4
3
from copy import deepcopy
5
4
from difflib import context_diff
8
7
9
8
from bumpversion .config .models import FileChange , VersionPartConfig
10
9
from bumpversion .exceptions import VersionNotFoundError
10
+ from bumpversion .ui import get_indented_logger
11
11
from bumpversion .version_part import Version , VersionConfig
12
12
13
- logger = logging . getLogger (__name__ )
13
+ logger = get_indented_logger (__name__ )
14
14
15
15
16
16
def contains_pattern (search : re .Pattern , contents : str ) -> bool :
@@ -22,7 +22,7 @@ def contains_pattern(search: re.Pattern, contents: str) -> bool:
22
22
line_no = contents .count ("\n " , 0 , m .start (0 )) + 1
23
23
logger .info (
24
24
"Found '%s' at line %s: %s" ,
25
- search ,
25
+ search . pattern ,
26
26
line_no ,
27
27
m .string [m .start () : m .end (0 )],
28
28
)
@@ -42,8 +42,11 @@ def log_changes(file_path: str, file_content_before: str, file_content_after: st
42
42
"""
43
43
if file_content_before != file_content_after :
44
44
logger .info ("%s file %s:" , "Would change" if dry_run else "Changing" , file_path )
45
+ logger .indent ()
46
+ indent_str = logger .indent_str
47
+
45
48
logger .info (
46
- "\n " .join (
49
+ f "\n { indent_str } " .join (
47
50
list (
48
51
context_diff (
49
52
file_content_before .splitlines (),
@@ -53,8 +56,9 @@ def log_changes(file_path: str, file_content_before: str, file_content_after: st
53
56
lineterm = "" ,
54
57
)
55
58
)
56
- )
59
+ ),
57
60
)
61
+ logger .dedent ()
58
62
else :
59
63
logger .info ("%s file %s" , "Would not change" if dry_run else "Not changing" , file_path )
60
64
@@ -104,12 +108,16 @@ def write_file_contents(self, contents: str) -> None:
104
108
with open (self .file_change .filename , "wt" , encoding = "utf-8" , newline = self ._newlines ) as f :
105
109
f .write (contents )
106
110
107
- def contains_version (self , version : Version , context : MutableMapping ) -> bool :
111
+ def _contains_change_pattern (
112
+ self , search_expression : re .Pattern , raw_search_expression : str , version : Version , context : MutableMapping
113
+ ) -> bool :
108
114
"""
109
- Check whether the version is present in the file.
115
+ Does the file contain the change pattern?
110
116
111
117
Args:
112
- version: The version to check
118
+ search_expression: The compiled search expression
119
+ raw_search_expression: The raw search expression
120
+ version: The version to check, in case it's not the same as the original
113
121
context: The context to use
114
122
115
123
Raises:
@@ -118,17 +126,15 @@ def contains_version(self, version: Version, context: MutableMapping) -> bool:
118
126
Returns:
119
127
True if the version number is in fact present.
120
128
"""
121
- search_expression , raw_search_expression = self .file_change .get_search_pattern (context )
122
129
file_contents = self .get_file_contents ()
123
130
if contains_pattern (search_expression , file_contents ):
124
131
return True
125
132
126
- # the `search` pattern did not match, but the original supplied
133
+ # The `search` pattern did not match, but the original supplied
127
134
# version number (representing the same version part values) might
128
- # match instead.
135
+ # match instead. This is probably the case if environment variables are used.
129
136
130
- # check whether `search` isn't customized, i.e. should match only
131
- # very specific parts of the file
137
+ # check whether `search` isn't customized
132
138
search_pattern_is_default = self .file_change .search == self .version_config .search
133
139
134
140
if search_pattern_is_default and contains_pattern (re .compile (re .escape (version .original )), file_contents ):
@@ -141,19 +147,36 @@ def contains_version(self, version: Version, context: MutableMapping) -> bool:
141
147
return False
142
148
raise VersionNotFoundError (f"Did not find '{ raw_search_expression } ' in file: '{ self .file_change .filename } '" )
143
149
144
- def replace_version (
150
+ def make_file_change (
145
151
self , current_version : Version , new_version : Version , context : MutableMapping , dry_run : bool = False
146
152
) -> None :
147
- """Replace the current version with the new version."""
148
- file_content_before = self .get_file_contents ()
149
-
153
+ """Make the change to the file."""
154
+ logger .info (
155
+ "\n %sFile %s: replace `%s` with `%s`" ,
156
+ logger .indent_str ,
157
+ self .file_change .filename ,
158
+ self .file_change .search ,
159
+ self .file_change .replace ,
160
+ )
161
+ logger .indent ()
162
+ logger .debug ("Serializing the current version" )
163
+ logger .indent ()
150
164
context ["current_version" ] = self .version_config .serialize (current_version , context )
165
+ logger .dedent ()
151
166
if new_version :
167
+ logger .debug ("Serializing the new version" )
168
+ logger .indent ()
152
169
context ["new_version" ] = self .version_config .serialize (new_version , context )
170
+ logger .dedent ()
153
171
154
172
search_for , raw_search_pattern = self .file_change .get_search_pattern (context )
155
173
replace_with = self .version_config .replace .format (** context )
156
174
175
+ if not self ._contains_change_pattern (search_for , raw_search_pattern , current_version , context ):
176
+ return
177
+
178
+ file_content_before = self .get_file_contents ()
179
+
157
180
file_content_after = search_for .sub (replace_with , file_content_before )
158
181
159
182
if file_content_before == file_content_after and current_version .original :
@@ -163,7 +186,7 @@ def replace_version(
163
186
file_content_after = search_for_og .sub (replace_with , file_content_before )
164
187
165
188
log_changes (self .file_change .filename , file_content_before , file_content_after , dry_run )
166
-
189
+ logger . dedent ()
167
190
if not dry_run : # pragma: no-coverage
168
191
self .write_file_contents (file_content_after )
169
192
@@ -209,22 +232,9 @@ def modify_files(
209
232
context: The context used for rendering the version
210
233
dry_run: True if this should be a report-only job
211
234
"""
212
- _check_files_contain_version (files , current_version , context )
213
- for f in files :
214
- f .replace_version (current_version , new_version , context , dry_run )
215
-
216
-
217
- def _check_files_contain_version (
218
- files : List [ConfiguredFile ], current_version : Version , context : MutableMapping
219
- ) -> None :
220
- """Make sure files exist and contain version string."""
221
- logger .info (
222
- "Asserting files %s contain the version string..." ,
223
- ", " .join ({str (f .file_change .filename ) for f in files }),
224
- )
235
+ # _check_files_contain_version(files, current_version, context)
225
236
for f in files :
226
- context ["current_version" ] = f .version_config .serialize (current_version , context )
227
- f .contains_version (current_version , context )
237
+ f .make_file_change (current_version , new_version , context , dry_run )
228
238
229
239
230
240
class FileUpdater :
0 commit comments