aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp/deb/dscfile.py
blob: 76676fc67a331d1446b0b3152f2370a58745c8f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# vim: set fileencoding=utf-8 :
#
# (C) 2006,2007,2011,2013 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, please see
#    <http://www.gnu.org/licenses/>
"""provides some debian source package related helpers"""

import os
import re

from gbp.errors import GbpError
from gbp.deb.upstreamsource import DebianUpstreamSource
from gbp.deb.policy import DebianPkgPolicy


class DscFile(object):
    """Keeps data read from a dscfile"""
    compressions = r"(%s)" % '|'.join(DebianUpstreamSource.known_compressions())
    pkg_re = re.compile(r'Source:\s+(?P<pkg>.+)\s*')
    version_re = re.compile(r'Version:\s((?P<epoch>\d+)\:)?'
                            '(?P<version>[%s]+)\s*$'
                            % DebianPkgPolicy.debianversion_chars)
    tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                        '(\.orig)?\.tar\.%s)$' % compressions)
    add_tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                            '\.orig-(?P<dir>[a-zA-Z0-9-]+)\.tar\.%s)$' % compressions)
    diff_re = re.compile(r'^\s\w+\s\d+\s+(?P<diff>[^_]+_[^_]+'
                         '\.diff.(gz|bz2))$')
    deb_tgz_re = re.compile(r'^\s\w+\s\d+\s+(?P<deb_tgz>[^_]+_[^_]+'
                            '\.debian.tar.%s)$' % compressions)
    format_re = re.compile(r'Format:\s+(?P<format>[0-9.]+)\s*')
    sig_re = re.compile(r'^\s\w+\s\d+\s+(?P<sig>[^_]+_[^_]+'
                        '\.orig(-[a-z0-9-]+)?\.tar\.%s.asc)$' % compressions)

    def __init__(self, dscfile):
        self.pkg = ""
        self.tgz = ""
        self.diff = ""
        self.deb_tgz = ""
        self.pkgformat = "1.0"
        self.debian_version = ""
        self.upstream_version = ""
        self.native = False
        self.dscfile = os.path.abspath(dscfile)
        sigs = []
        add_tars = []

        f = open(self.dscfile, encoding='utf-8')
        fromdir = os.path.dirname(os.path.abspath(dscfile))
        for line in f:
            m = self.version_re.match(line)
            if m and not self.upstream_version:
                if '-' in m.group('version'):
                    self.debian_version = m.group('version').split("-")[-1]
                    self.upstream_version = "-".join(m.group('version').split("-")[0:-1])
                    self.native = False
                else:
                    self.native = True  # Debian native package
                    self.upstream_version = m.group('version')
                if m.group('epoch'):
                    self.epoch = m.group('epoch')
                else:
                    self.epoch = ""
                continue
            m = self.pkg_re.match(line)
            if m:
                self.pkg = m.group('pkg')
                continue
            m = self.deb_tgz_re.match(line)
            if m:
                self.deb_tgz = os.path.join(fromdir, m.group('deb_tgz'))
                continue
            m = self.add_tar_re.match(line)
            if m:
                add_tars.append((m.group('dir'),
                                 os.path.join(fromdir, m.group('tar'))))
                continue
            m = self.tar_re.match(line)
            if m:
                self.tgz = os.path.join(fromdir, m.group('tar'))
                continue
            m = self.sig_re.match(line)
            if m:
                sigs.append(os.path.join(fromdir, m.group('sig')))
                continue
            m = self.diff_re.match(line)
            if m:
                self.diff = os.path.join(fromdir, m.group('diff'))
                continue
            m = self.format_re.match(line)
            if m:
                self.pkgformat = m.group('format')
                continue
        f.close()

        # Source format 1.0 can have non-native packages without a Debian revision:
        # e.g. http://snapshot.debian.org/archive/debian/20090801T192339Z/pool/main/l/latencytop/latencytop_0.5.dsc
        if self.pkgformat == "1.0" and self.diff:
            self.native = False
        elif not self.native and not self.debian_version:
            raise GbpError("Cannot parse Debian version number from '%s'" % self.dscfile)

        if not self.pkg:
            raise GbpError("Cannot parse package name from '%s'" % self.dscfile)
        elif not self.tgz:
            raise GbpError("Cannot parse archive name from '%s'" % self.dscfile)
        if not self.upstream_version:
            raise GbpError("Cannot parse version number from '%s'" % self.dscfile)
        self.additional_tarballs = dict(add_tars)
        self.sigs = list(set(sigs))

    @property
    def version(self):
        version = ["", self.epoch + ":"][len(self.epoch) > 0]
        if self.native:
            version += self.upstream_version
        else:
            if self.debian_version != '':
                version += "%s-%s" % (self.upstream_version, self.debian_version)
            else:   # possible in 1.0
                version += "%s" % self.upstream_version
        return version

    def __str__(self):
        return "<%s object %s>" % (self.__class__.__name__, self.dscfile)

    @classmethod
    def parse(cls, filename):
        try:
            dsc = cls(filename)
        except IOError as err:
            raise GbpError("Error reading dsc file: %s" % err)
        return dsc

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