From f81864efd59c83aa62335d204a36252da44152e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Safa=20Ar=C4=B1man?= Date: Mon, 5 Aug 2019 21:39:38 +0300 Subject: [PATCH] detect_patch_level patch applied --- pisi/util.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/pisi/util.py b/pisi/util.py index 6768064c..1b9c6414 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -516,6 +516,17 @@ def uncompress(patchFile, compressType="gz", targetDir=""): return filePath.split(".%s" % extension)[0] +def check_patch_level(workdir, path): + level = 0 + while path: + if os.path.isfile("%s/%s" % (workdir, path)): + return level + if path.find("/") == -1: + return None + level += 1 + path = path[path.find("/")+1:] + + def do_patch(sourceDir, patchFile, level=0, name=None, reverse=False): """Apply given patch to the sourceDir.""" cwd = os.getcwd() @@ -524,14 +535,42 @@ def do_patch(sourceDir, patchFile, level=0, name=None, reverse=False): else: raise Error(_("ERROR: WorkDir (%s) does not exist\n") % (sourceDir)) - if level == None: + check_file(patchFile) + + if level is None: + with open(patchFile, "r") as patchfile: + lines = patchfile.readlines() + try: + paths_m = [l.strip().split()[1] for l in lines if l.startswith("---") and "/" in l] + try: + paths_p = [l.strip().split()[1] for l in lines if l.startswith("+++")] + except IndexError: + paths_p = [] + except IndexError: + pass + else: + if not paths_p: + paths_p = paths_m[:] + try: + paths_m = [l.strip().split()[1] for l in lines if l.startswith("***") and "/" in l] + except IndexError: + pass + + for path_p, path_m in zip(paths_p, paths_m): + if "/dev/null" in path_m and not len(paths_p) -1 == paths_p.index(path_p): continue + level = check_patch_level(sourceDir, path_p) + if level is None and len(paths_m) - 1 == paths_m.index(path_m): + level = check_patch_level(sourceDir, path_m) + if level is not None: + ctx.ui.debug("Detected patch level=%s for %s" % (level, os.path.basename(patchFile))) + break + + if level is None: level = 0 if name is None: name = os.path.basename(patchFile) - check_file(patchFile) - if ctx.get_option('use_quilt'): patchesDir = join_path(sourceDir, ctx.const.quilt_dir_suffix) # Make sure sourceDir/patches directory exists and if not create one!