diff options
author | Guido Günther <agx@sigxcpu.org> | 2011-07-29 17:06:55 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2011-07-29 17:55:30 +0200 |
commit | dbe68fbdba2efa3655560bb364e87fa775bb30ca (patch) | |
tree | 9139f0eae453c0eeac8bdfe2457d8bc412f2b0ee | |
parent | 3fb7fd628efa60f12218c7ee7e9f0f4b3e2239e3 (diff) |
Split out GbpPatchQueue and GbpPatch
to handle "-p<num>" without munging the code further
Closes: #635873
-rwxr-xr-x | gbp-pq | 140 |
1 files changed, 121 insertions, 19 deletions
@@ -35,6 +35,123 @@ PQ_BRANCH_PREFIX = "patch-queue/" PATCH_DIR = "debian/patches/" SERIES_FILE = os.path.join(PATCH_DIR,"series") + +class GbpPatch(object): + """ + A patch in a patchqueue + + @ivar path: path to the patch + @type path: string + @ivar topic: the topic of the patch + @type topic: string + @ivar strip: path components to strip (think patch -p<strip>) + @type strip: integer + """ + def __init__(self, path, topic=None, strip=None): + self.path = path + self.topic = topic + self.strip = strip + + def __repr__(self): + repr = "<gbp_pq.GbpPatch path='%s' " % self.path + if self.topic: + repr += "topic='%s' " % self.topic + if self.strip != None: + repr += "strip=%d " % self.strip + repr += ">" + return repr + + +class GbpPatchQueue(list): + @classmethod + def read_series_file(klass, seriesfile): + """Read a series file into GbpPatch objects""" + patch_dir = os.path.dirname(seriesfile) + try: + s = file(seriesfile) + except Exception, err: + raise GbpError("Cannot open series file: %s" % err) + + queue = klass._read_series(s, patch_dir) + s.close() + return queue + + @classmethod + def _read_series(klass, series, patch_dir): + """ + Read patch series + @param series: series of patches in quilt format + @type series: iterable of strings + @param patch_dir: path prefix to prepend to each patch path + @type patch_dir: string + + >>> GbpPatchQueue._read_series(['a/b', \ + 'a -p1', \ + 'a/b -p2'], '.') # doctest:+NORMALIZE_WHITESPACE + [<gbp_pq.GbpPatch path='./a/b' topic='a' >, + <gbp_pq.GbpPatch path='./a' strip=1 >, + <gbp_pq.GbpPatch path='./a/b' topic='a' strip=2 >] + """ + + queue = GbpPatchQueue() + for line in series: + queue.append(klass._parse_line(line, patch_dir)) + return queue + + @staticmethod + def _get_topic(line): + """ + Get the topic from the path's path + >>> GbpPatchQueue._get_topic("a/b c") + 'a' + >>> GbpPatchQueue._get_topic("asdf") + >>> GbpPatchQueue._get_topic("/asdf") + """ + topic = os.path.dirname(line) + if topic in [ '', '/' ]: + topic = None + return topic + + @staticmethod + def _split_strip(line): + """ + Separate the -p<num> option from the patch name + + >>> GbpPatchQueue._split_strip("asdf -p1") + ('asdf', 1) + >>> GbpPatchQueue._split_strip("a/nice/patch") + ('a/nice/patch', None) + >>> GbpPatchQueue._split_strip("asdf foo") + ('asdf foo', None) + """ + patch = line + strip = None + + split = line.rsplit(None, 1) + if len(split) > 1: + m = re.match('-p(?P<level>[0-9]+)', split[1]) + if m: + patch = split[0] + strip = int(m.group('level')) + + return (patch, strip) + + @classmethod + def _parse_line(klass, line, patch_dir): + """ + Parse a single line from a patch file + + >>> GbpPatchQueue._parse_line("a/b -p1", '/tmp/patches') + <gbp_pq.GbpPatch path='/tmp/patches/a/b' topic='a' strip=1 > + >>> GbpPatchQueue._parse_line("a/b", '.') + <gbp_pq.GbpPatch path='./a/b' topic='a' > + """ + line = line.rstrip() + topic = klass._get_topic(line) + (patch, split) = klass._split_strip(line) + return GbpPatch(os.path.join(patch_dir, patch), topic, split) + + def is_pq_branch(branch): """ is branch a patch-queue branch? @@ -181,25 +298,10 @@ def import_quilt_patches(repo, branch, series): % pq_branch) repo.set_branch(pq_branch) - if not os.path.exists(series): - gbp.log.info("Found no series file at '%s'. No patches to add to patch-queue branch." - % series) - return - else: - patch_dir = os.path.dirname(series) - - try: - s = file(series) - except Exception, err: - raise GbpError("Cannot open series file: %s" % err) - - for patch in s: - patch = patch[:-1] - topic = os.path.dirname(patch) - if topic == '' or topic == '/': - topic = None - apply_and_commit_patch(repo, os.path.join(patch_dir, patch), topic) - s.close() + queue = GbpPatchQueue.read_series_file(series) + for patch in queue: + gbp.log.debug("Applying %s" % patch.path) + apply_and_commit_patch(repo, patch.path, patch.topic) def get_mailinfo(patch): |