# vim: fileencoding=utf-8 # from calculate.utils.files import Process from os import path class DiffFormat(): def __init__(self, document_text: str): self._patch_text = document_text self._root_path = '' self._last_level = 0 # вынести в более общий класс или куда-то еще. self._changed_files_list = [] def execute_format(self, root_path): if path.exists(root_path): self._root_path = root_path else: # Какая-то обработка ошибки. error_message = 'Root path does not exist.' print(error_message) return False if self._patch_text: return self._patch_document() else: # Какая-то обработка ошибки. error_message = 'Empty patch file.' print(error_message) return False def _patch_document(self): for level in range(0, 4): patch_dry_run = Process('patch', '--dry-run', '-p{}'.format(level), cwd=self._root_path) patch_dry_run.write(self._patch_text) if patch_dry_run.success(): break patch_dry_run = Process('patch', '-R', '--dry-run', '-p{}'.format(level), cwd=self._root_path) patch_dry_run.write(self._patch_text) if patch_dry_run.success(): return '' else: # Какая-то обработка ошибки. error_message = 'Correction failed.' print(error_message) return False self._last_level = level patch_run = Process('patch', '-p{}'.format(level), cwd=self._root_path) patch_run.write(self._patch_text) if patch_run.success(): for line in patch_run: if line.startswith('patching file'): self._changed_files_list.append(line[13:].strip()) return patch_run.read() else: return ''