# vim: fileencoding=utf-8 # from .base_format import BaseFormat from calculate.utils.files import Process from calculate.templates.format.base_format import FormatError from os import path class DiffFormat(BaseFormat): FORMAT = 'diff' def __init__(self, document_text: str, comment_symbol=''): self._patch_text = document_text self._root_path = '' self._last_level = 0 # вынести в более общий класс или куда-то еще. self._changed_files_list = [] def execute_format(self, root_path): print if path.exists(root_path): self._root_path = root_path else: raise FormatError('Root path does not exist.') if self._patch_text: return self._patch_document() else: raise FormatError('Empty patch file.') 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: raise FormatError('Correction failed.') 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(): print('patch run is successful...') for line in patch_run: if line.startswith('patching file'): self._changed_files_list.append(line[13:].strip()) return patch_run.read() else: print('patch run is no successful...') print(patch_run.read_error()) return ''