def patch(conanfile, base_path=None, patch_file=None, patch_string=None, strip=0, fuzz=False, **kwargs): """ Applies a diff from file (patch_file) or string (patch_string) in base_path directory or current dir if None :param base_path: Base path where the patch should be applied. :param patch_file: Patch file that should be applied. :param patch_string: Patch string that should be applied. :param strip: Number of folders to be stripped from the path. :param output: Stream object. :param fuzz: Should accept fuzzy patches. :param kwargs: Extra parameters that can be added and will contribute to output information """ patch_type = kwargs.get('patch_type') patch_description = kwargs.get('patch_description') if patch_type or patch_description: patch_type_str = ' ({})'.format(patch_type) if patch_type else '' patch_description_str = ': {}'.format(patch_description) if patch_description else '' conanfile.output.info('Apply patch{}{}'.format(patch_type_str, patch_description_str)) patchlog = logging.getLogger("patch_ng") patchlog.handlers = [] patchlog.addHandler(PatchLogHandler(conanfile, patch_file)) if patch_file: patchset = patch_ng.fromfile(patch_file) else: patchset = patch_ng.fromstring(patch_string.encode()) if not patchset: raise ConanException("Failed to parse patch: %s" % (patch_file if patch_file else "string")) if not patchset.apply(root=base_path, strip=strip, fuzz=fuzz): raise ConanException("Failed to apply patch: %s" % patch_file)
def test_fromstring(self): try: f = open(join(TESTS, "01uni_multi/01uni_multi.patch"), "rb") readstr = f.read() finally: f.close() pst = patch_ng.fromstring(readstr) self.assertEqual(len(pst), 5)
def patch(base_path=None, patch_file=None, patch_string=None, strip=0, output=None, fuzz=False): """ Applies a diff from file (patch_file) or string (patch_string) in base_path directory or current dir if None :param base_path: Base path where the patch should be applied. :param patch_file: Patch file that should be applied. :param patch_string: Patch string that should be applied. :param strip: Number of folders to be stripped from the path. :param output: Stream object. :param fuzz: Should accept fuzzy patches. """ class PatchLogHandler(logging.Handler): def __init__(self): logging.Handler.__init__(self, logging.DEBUG) self.output = output or ConanOutput( sys.stdout, sys.stderr, color=True) self.patchname = patch_file if patch_file else "patch_ng" def emit(self, record): logstr = self.format(record) if record.levelno == logging.WARN: self.output.warn("%s: %s" % (self.patchname, logstr)) else: self.output.info("%s: %s" % (self.patchname, logstr)) patchlog = logging.getLogger("patch_ng") if patchlog: patchlog.handlers = [] patchlog.addHandler(PatchLogHandler()) if not patch_file and not patch_string: return if patch_file: patchset = fromfile(patch_file) else: patchset = fromstring(patch_string.encode()) if not patchset: raise ConanException("Failed to parse patch: %s" % (patch_file if patch_file else "string")) if not patchset.apply(root=base_path, strip=strip, fuzz=fuzz): raise ConanException("Failed to apply patch: %s" % patch_file)
def patch(base_path=None, patch_file=None, patch_string=None, strip=0, output=None): """Applies a diff from file (patch_file) or string (patch_string) in base_path directory or current dir if None""" class PatchLogHandler(logging.Handler): def __init__(self): logging.Handler.__init__(self, logging.DEBUG) self.output = output or ConanOutput( sys.stdout, sys.stderr, color=True) self.patchname = patch_file if patch_file else "patch_ng" def emit(self, record): logstr = self.format(record) if record.levelno == logging.WARN: self.output.warn("%s: %s" % (self.patchname, logstr)) else: self.output.info("%s: %s" % (self.patchname, logstr)) patchlog = logging.getLogger("patch_ng") if patchlog: patchlog.handlers = [] patchlog.addHandler(PatchLogHandler()) if not patch_file and not patch_string: return if patch_file: patchset = fromfile(patch_file) else: patchset = fromstring(patch_string.encode()) if not patchset: raise ConanException("Failed to parse patch: %s" % (patch_file if patch_file else "string")) if not patchset.apply(root=base_path, strip=strip): raise ConanException("Failed to apply patch: %s" % patch_file)