Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
) -> List[Patch]:
old_string = change_request.old_string
new_string = change_request.new_string
search = change_request.search
patches = []
file_path_glob = self.working_path / change_request.src
for file_path_str in glob.glob(file_path_glob, recursive=True):
file_path = Path(file_path_str)
expanded_src = file_path.relpath(self.working_path)
old_lines = file_path.lines(retain=False)
for i, old_line in enumerate(old_lines):
if should_replace(old_line, old_string, search):
new_line = old_line.replace(old_string, new_string)
patch = Patch(
self.working_path, expanded_src, i, old_line, new_line
)
patches.append(patch)
if not patches:
raise CurrentVersionNotFound(
src=change_request.src, current_version_string=old_string
)
return patches
def apply(self) -> None:
file_path = self.working_path / self.src
contents = file_path.bytes()
lines = contents.splitlines(keepends=True)
old_line = lines[self.lineno]
lines[self.lineno] = self.new_line.encode() + Patch.get_ending(old_line)
file_path.write_lines(lines, linesep=None)