You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
62 lines
2.1 KiB
# 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'
|
|
EXECUTABLE = True
|
|
|
|
def __init__(self, document_text: str,
|
|
join_before=False):
|
|
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:
|
|
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):
|
|
'''Метод, производящий наложение патча путем запуска процесса patch.'''
|
|
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():
|
|
for line in patch_run:
|
|
if line.startswith('patching file'):
|
|
self._changed_files_list.append(line[13:].strip())
|
|
return patch_run.read()
|
|
else:
|
|
return ''
|