in update-crt.py [0:0]
def update(*, filepath, preceded_by, followed_by, force_version=None):
"""
Args:
filepath: File containing hard-coded CRT version numbers.
preceded_by: Regex pattern for text preceding the CRT version number.
followed_by: Regex pattern for text following the CRT version number.
"""
with open(filepath, 'r+') as f:
txt_old = f.read()
full_pattern = rf'({preceded_by})({VERSION_PATTERN})({followed_by})'
full_replacement = rf'\g<1>{args.version}\g<3>'
if force_version != None:
full_replacement = rf'\g<1>{force_version}\g<3>'
matches = re.findall(full_pattern, txt_old)
if len(matches) == 0:
exit(f'Version not found in: {filepath}\n' +
f'Preceded by: "{preceded_by}"')
if args.check_consistency:
# in --check-consistency mode we remember the version from the first
# file we scan, and then ensure all subsequent files use that version too
for match in matches:
found_version = match[1]
global consistency_version
if consistency_version is None:
print(f'Found version {found_version} in: {filepath}')
consistency_version = found_version
elif found_version != consistency_version:
exit(f'Found different version {found_version} in: {filepath}')
else:
# running in normal mode, update the file
txt_new = re.sub(full_pattern, full_replacement, txt_old)
f.seek(0)
f.write(txt_new)
f.truncate()