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.
calculate-utils-3-lib/pym/calculate/lib/format/diff.py

68 lines
2.4 KiB

9 years ago
# -*- coding: utf-8 -*-
11 years ago
# Copyright 2008-2016 Mir Calculate. http://www.calculate-linux.org
11 years ago
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
9 years ago
import sys
import re
from ..cl_template import TemplateFormat
from ..utils.files import process
11 years ago
from ..cl_lang import setLocalTranslate
9 years ago
_ = lambda x: x
setLocalTranslate('cl_lib3', sys.modules[__name__])
11 years ago
class diff(TemplateFormat):
11 years ago
"""
Format using diff
"""
text = ""
p1_token = re.compile(r'\n--- (.*)\n\+\+\+ ')
11 years ago
def textToXML(self):
return self.text
def processingFile(self, textConfigFile, rootPath=None, nameFile=None):
# определить, что патч содержит только добавление новых файлов
# удаляем b/ из целевого пути
if all(x.group(1) == '/dev/null'
for x in self.p1_token.finditer(self.text)):
self.text = self.text.replace("--- /dev/null\n+++ b/",
"--- /dev/null\n+++ ")
9 years ago
for i in range(0, 4):
patchDryRun = process('/usr/bin/patch', '--dry-run',
'-p%d' % i, cwd=rootPath, )
11 years ago
patchDryRun.write(self.text)
if patchDryRun.success():
break
9 years ago
patchDryRun = process('/usr/bin/patch', '-R', '--dry-run',
'-p%d' % i, cwd=rootPath, )
11 years ago
patchDryRun.write(self.text)
if patchDryRun.success():
return ""
11 years ago
else:
self.setError(_("Correction failed"))
11 years ago
return False
patchRun = process('/usr/bin/patch',
9 years ago
'-p%d' % i, cwd=rootPath)
11 years ago
patchRun.write(self.text)
if patchRun.success():
for line in patchRun:
if line.startswith("patching file"):
self.changed_files.append(line[13:].strip())
11 years ago
return patchRun.read()
return ""