#!/usr/bin/python # -*- coding: utf-8 -*- import sys import os patchTemplate = ' %s' if len(sys.argv) == 1: print "usage: %s file.spec" % sys.argv[0] sys.exit(1) srcfile = sys.argv[1] def loadFile(_file): try: f = file(_file) d = [a.lstrip().rstrip("\n") for a in f] d = filter(lambda x: not (x.startswith("#") or x == ""), d) f.close() return d except: return [] gdbFile = loadFile(srcfile) srcpatches = {} sortedpatches = [] for line in gdbFile: if line.startswith("Patch"): a, b = line.split(":", 1) srcpatches[a.replace("Patch", "")] = b.strip() elif line.startswith("%patch"): if "-p" in line: a, b = line.split(" ")[0:2] if a == "%patch": a = "%patch0" sortedpatches.append([a.replace("%patch", ""), b.replace("-p", "")]) else: sortedpatches.append([line.replace("%patch", ""), "0"]) else: continue #for i in srcpatches: # print i, srcpatches[i] for i in sortedpatches: if i[1] == "0": hede = "" else: hede = ' level="%s"' % i[1] print patchTemplate % (hede, srcpatches[i[0]])