diff options
author | Guido Günther <agx@sigxcpu.org> | 2012-08-02 11:27:22 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2012-08-03 19:07:41 +0200 |
commit | 83577af21ebfbd3b94fb51f89627c2c7dd5dbb88 (patch) | |
tree | 91ed09b8ee8ae497ca8b46d5d2de658ecbe800e4 /gbp/deb | |
parent | aa2ba858bd6afe4c8ddcb0ce2b1e6112a461d257 (diff) |
Move uscan to separate class
Diffstat (limited to 'gbp/deb')
-rw-r--r-- | gbp/deb/__init__.py | 65 | ||||
-rw-r--r-- | gbp/deb/uscan.py | 126 |
2 files changed, 126 insertions, 65 deletions
diff --git a/gbp/deb/__init__.py b/gbp/deb/__init__.py index 1f52c5a..f9dfc94 100644 --- a/gbp/deb/__init__.py +++ b/gbp/deb/__init__.py @@ -222,7 +222,6 @@ def parse_changelog_repo(repo, branch, filename): lines = repo.show(sha) return ChangeLog('\n'.join(lines)) - def orig_file(cp, compression): """ The name of the orig file belonging to changelog cp @@ -236,70 +235,6 @@ def orig_file(cp, compression): cp['Upstream-Version'], compression) - -def parse_uscan(out): - """ - Parse the uscan output return (True, tarball) if a new version was - downloaded and could be located. If the tarball can't be located it returns - (True, None). Returns (False, None) if the current version is up to date. - - >>> parse_uscan("<status>up to date</status>") - (False, None) - >>> parse_uscan("<target>virt-viewer_0.4.0.orig.tar.gz</target>") - (True, '../virt-viewer_0.4.0.orig.tar.gz') - - @param out: uscan output - @type out: string - @return: status and tarball name - @rtype: tuple - """ - source = None - if "<status>up to date</status>" in out: - return (False, None) - else: - # Check if uscan downloaded something - for row in out.split("\n"): - # uscan >= 2.10.70 has a target element: - m = re.match(r"<target>(.*)</target>", row) - if m: - source = '../%s' % m.group(1) - break - elif row.startswith('<messages>'): - m = re.match(r".*symlinked ([^\s]+) to it", row) - if m: - source = "../%s" % m.group(1) - break - m = re.match(r"Successfully downloaded updated package ([^<]+)", row) - if m: - source = "../%s" % m.group(1) - break - # try to determine the already downloaded sources name - else: - d = {} - for row in out.split("\n"): - for n in ('package', 'upstream-version', 'upstream-url'): - m = re.match("<%s>(.*)</%s>" % (n,n), row) - if m: - d[n] = m.group(1) - d["ext"] = os.path.splitext(d['upstream-url'])[1] - # We want the name of the orig tarball if possible - source = "../%(package)s_%(upstream-version)s.orig.tar%(ext)s" % d - if not os.path.exists(source): - # Fall back to the sources name otherwise - source = "../%s" % d['upstream-url'].rsplit('/',1)[1] - print source - if not os.path.exists(source): - source = None - return (True, source) - - -def do_uscan(): - """invoke uscan to fetch a new upstream version""" - p = subprocess.Popen(['uscan', '--symlink', '--destdir=..', '--dehs'], stdout=subprocess.PIPE) - out = p.communicate()[0] - return parse_uscan(out) - - def get_arch(): pipe = subprocess.Popen(["dpkg", "--print-architecture"], shell=False, stdout=subprocess.PIPE) arch = pipe.stdout.readline().strip() diff --git a/gbp/deb/uscan.py b/gbp/deb/uscan.py new file mode 100644 index 0000000..76bde65 --- /dev/null +++ b/gbp/deb/uscan.py @@ -0,0 +1,126 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2012 Guido Günther <agx@sigxcpu.org> +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +"""Interface to uscan""" + +import os, re, subprocess + +class UscanError(Exception): + pass + +class Uscan(object): + cmd = '/usr/bin/uscan' + + def __init__(self, dir='.'): + self._uptodate = False + self._tarball = None + self._dir = os.path.abspath(dir) + + @property + def uptodate(self): + return self._uptodate + + @property + def tarball(self): + return self._tarball + + def _parse(self, out): + r""" + Parse the uscan output return and update the object's properties + + @param out: uscan output + @type out: string + + >>> u = Uscan('http://example.com/') + >>> u._parse('<status>up to date</status>') + >>> u.tarball + >>> u.uptodate + True + >>> u._parse('<target>virt-viewer_0.4.0.orig.tar.gz</target>') + >>> u.tarball + '../virt-viewer_0.4.0.orig.tar.gz' + >>> u.uptodate + False + >>> u._parse('') + Traceback (most recent call last): + ... + UscanError: Couldn't find 'upstream-url' in uscan output + """ + source = None + + if "<status>up to date</status>" in out: + self._uptodate = True + self._tarball = None + return + else: + self._uptodate = False + + # Check if uscan downloaded something + for row in out.split("\n"): + # uscan >= 2.10.70 has a target element: + m = re.match(r"<target>(.*)</target>", row) + if m: + source = '../%s' % m.group(1) + break + elif row.startswith('<messages>'): + m = re.match(r".*symlinked ([^\s]+) to it", row) + if m: + source = "../%s" % m.group(1) + break + m = re.match(r"Successfully downloaded updated package " + "([^<]+)", row) + if m: + source = "../%s" % m.group(1) + break + + # Try to determine the already downloaded sources name + else: + d = {} + + try: + for row in out.split("\n"): + for n in ('package', + 'upstream-version', + 'upstream-url'): + m = re.match("<%s>(.*)</%s>" % (n,n), row) + if m: + d[n] = m.group(1) + d["ext"] = os.path.splitext(d['upstream-url'])[1] + # We want the name of the orig tarball if possible + source = ("../%(package)s_%(upstream-version)s." + "orig.tar%(ext)s" % d) + + # Fall back to the upstream source name otherwise + if not os.path.exists(source): + source = "../%s" % d['upstream-url'].rsplit('/',1)[1] + if not os.path.exists(source): + raise UscanError("Couldn't find tarball at '%s'" % + source) + except KeyError as e: + raise UscanError("Couldn't find '%s' in uscan output" % + e.args[0]) + self._tarball = source + + def scan(self, destdir='..'): + """Invoke uscan to fetch a new upstream version""" + p = subprocess.Popen(['uscan', '--symlink', '--destdir=%s' % destdir, + '--dehs'], + cwd=self._dir, + stdout=subprocess.PIPE) + out = p.communicate()[0] + return self._parse(out) + +# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: |