__init__.py 4.01 KB
Newer Older
1 2
# vim: set fileencoding=utf-8 :
#
3
# (C) 2006,2007,2011,2017 Guido Günther <agx@sigxcpu.org>
4 5 6 7 8 9 10 11 12 13 14
#    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
15 16
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>
17
"""provides some debian source package related helpers"""
18

19
import os
Guido Günther's avatar
Guido Günther committed
20
import subprocess
21 22 23

import gbp.command_wrappers as gbpc
from gbp.errors import GbpError
24
from gbp.git import GitRepositoryError
25 26

# Make sure these are available with 'import gbp.deb'
Guido Günther's avatar
Guido Günther committed
27
from gbp.deb.changelog import ChangeLog, NoChangeLogError
28
from gbp.deb.policy import DebianPkgPolicy                # noqa: F401
29

Guido Günther's avatar
Guido Günther committed
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
Releases = ("buzz",
            "rez",
            "bo",
            "hamm",
            "slink",
            "potato",
            "woody",
            "sarge",
            "etch",
            "lenny",
            "squeeze",
            "wheezy",
            "jessie",
            "stretch",
            "buster",
            "bullseye",
            "sid")


50
class DpkgCompareVersions(gbpc.Command):
51
    dpkg = '/usr/bin/dpkg'
52 53

    def __init__(self):
54 55 56
        if not os.access(self.dpkg, os.X_OK):
            raise GbpError("%s not found - cannot use compare versions" % self.dpkg)
        gbpc.Command.__init__(self, self.dpkg, ['--compare-versions'], capture_stderr=True)
57 58

    def __call__(self, version1, version2):
59 60 61 62 63 64
        """
        Compare two package versions. Return 0 if the versions are equal, -1 1 if version1 < version2,
        and 1 oterwise.

        @raises CommandExecFailed: if the version comparison fails
        """
65
        self.run_error = "Couldn't compare %s with %s" % (version1, version2)
Guido Günther's avatar
Guido Günther committed
66 67
        res = self.call([version1, 'lt', version2])
        if res not in [0, 1]:
68 69
            if self.stderr:
                self.run_error += ' (%s)' % self.stderr
70
            raise gbpc.CommandExecFailed("%s: bad return code %d" % (self.run_error, res))
71 72 73
        if res == 0:
            return -1
        elif res == 1:
Guido Günther's avatar
Guido Günther committed
74 75
            res = self.call([version1, 'gt', version2])
            if res not in [0, 1]:
76 77
                if self.stderr:
                    self.run_error += ' (%s)' % self.stderr
78
                raise gbpc.CommandExecFailed("%s: bad return code %d" % (self.run_error, res))
79 80 81 82 83
            if res == 0:
                return 1
        return 0


84 85 86 87
def parse_changelog_repo(repo, branch, filename):
    """
    Parse the changelog file from given branch in the git
    repository.
88 89

    FIXME: this should use *Vfs methods
90
    """
91 92 93 94 95
    try:
        # Note that we could just pass in the branch:filename notation
        # to show as well, but we want to check if the branch / filename
        # exists first, so we can give a separate error from other
        # repository errors.
96
        sha = repo.rev_parse("%s:%s" % (branch, filename))
97
    except GitRepositoryError:
98
        raise NoChangeLogError("Changelog %s not found in branch %s" % (filename, branch))
99

100
    return ChangeLog(repo.show(sha))
101

102

Guido Günther's avatar
Guido Günther committed
103 104 105
def get_arch():
    pipe = subprocess.Popen(["dpkg", "--print-architecture"], shell=False, stdout=subprocess.PIPE)
    arch = pipe.stdout.readline().strip()
Guido Günther's avatar
Guido Günther committed
106
    return arch.decode('ascii')
Guido Günther's avatar
Guido Günther committed
107 108


109
def compare_versions(version1, version2):
Guido Günther's avatar
Guido Günther committed
110
    """compares to Debian version numbers suitable for sort()"""
111 112 113
    return DpkgCompareVersions()(version1, version2)


114 115
def get_vendor():
    pipe = subprocess.Popen(["dpkg-vendor", "--query", "Vendor"], shell=False, stdout=subprocess.PIPE)
Guido Günther's avatar
Guido Günther committed
116 117
    vendor = pipe.stdout.readline().strip()
    return vendor.decode('ascii')
118

119
# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: