summaryrefslogtreecommitdiff
path: root/gitbuildrecipe/deb_version.py
blob: f70bc4916b87ca78b13506b5939c3b4f5106862f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright 2015 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.

"""Handle deb-version and related substitutions."""

import re
import subprocess

from debian.changelog import Changelog

from gitbuildrecipe.recipe import (
    BranchSubstitutionVariable,
    DateVariable,
    FormattedError,
    GitCommitVariable,
    LatestTagVariable,
    RevdateVariable,
    RevnoVariable,
    RevtimeVariable,
    SubstitutionUnavailable,
    TimeVariable,
    branch_vars,
    simple_vars,
    )


class DebUpstreamVariable(BranchSubstitutionVariable):

    basename = "debupstream"

    minimum_format = 0.1

    def __init__(self, branch_name, version):
        super(DebUpstreamVariable, self).__init__(branch_name)
        self._version = version

    @classmethod
    def from_changelog(cls, branch_name, changelog):
        if len(changelog._blocks) > 0:
            return cls(branch_name, changelog._blocks[0].version)
        else:
            return cls(branch_name, None)

    def get(self):
        if self._version is None:
            raise SubstitutionUnavailable(
                self.name,
                "No previous changelog to take the upstream version from")
        # Should we include the epoch?
        return self._version.upstream_version


class DebVersionVariable(BranchSubstitutionVariable):

    basename = "debversion"

    minimum_format = 0.3

    def __init__(self, branch_name, version):
        super(DebVersionVariable, self).__init__(branch_name)
        self._version = version

    @classmethod
    def from_changelog(cls, branch_name, changelog):
        if len(changelog._blocks) > 0:
            return cls(branch_name, changelog._blocks[0].version)
        else:
            return cls(branch_name, None)

    def get(self):
        if self._version is None:
            raise SubstitutionUnavailable(self.name,
                "No previous changelog to take the version from")
        return str(self._version)


dfsg_regex = re.compile(r"[+.]*dfsg[.]*[0-9]+")

version_regex = re.compile(r"([~+])(svn[0-9]+|bzr[0-9]+|git[0-9a-f]+)")

def version_extract_base(version):
    version = dfsg_regex.sub("", version)
    return version_regex.sub("\\1", version)


class DebUpstreamBaseVariable(DebUpstreamVariable):

    basename = "debupstream-base"
    minimum_format = 0.3

    def get(self):
        version = super(DebUpstreamBaseVariable, self).get()
        version = version_extract_base(version)
        if version[-1] not in ("~", "+"):
            version += "+"
        return version


ok_to_preserve = [
    DebUpstreamVariable,
    DebUpstreamBaseVariable,
    DebVersionVariable,
    ]
deb_branch_vars = [
    DebVersionVariable,
    DebUpstreamBaseVariable,
    DebUpstreamVariable,
    ]


class NotFullyExpanded(FormattedError):

    _fmt = (
        "deb-version not fully expanded: %(deb_version)s.  Valid "
        "substitutions in recipe format %(format_version)s are: "
        "%(available_tokens)s")

    def __init__(self, deb_version, format_version, available_tokens):
        super().__init__(
            deb_version=deb_version, format_version=format_version,
            available_tokens=available_tokens)


def check_expanded_deb_version(base_branch):
    checked_version = base_branch.deb_version
    if checked_version is None:
        return
    for token in ok_to_preserve:
        if issubclass(token, BranchSubstitutionVariable):
            for name in base_branch.list_branch_names():
                checked_version = checked_version.replace(
                    token.determine_name(name), "")
            checked_version = checked_version.replace(
                    token.determine_name(None), "")
        else:
            checked_version = checked_version.replace(
                token.name, "")
    if "{" in checked_version:
        available_tokens = [var.name for var in simple_vars if
                            var.available_in(base_branch.format_version)]
        for var_kls in branch_vars + deb_branch_vars:
            if not var_kls.available_in(base_branch.format_version):
                continue
            for name in base_branch.list_branch_names():
                available_tokens.append(var_kls.determine_name(name))
            available_tokens.append(var_kls.determine_name(None))
        raise NotFullyExpanded(
            base_branch.deb_version, base_branch.format_version,
            available_tokens)


def substitute_changelog_vars(base_branch, branch_name, cl):
    """Substitute variables related to a changelog.

    :param base_branch: The base `RecipeBranch` to operate on.
    :param branch_name: The name of the `RecipeBranch` to substitute (None
        for the root branch).
    :param cl: The `Changelog` object to use.
    """
    debupstream_var = DebUpstreamVariable.from_changelog(branch_name, cl)
    base_branch.deb_version = debupstream_var.replace(base_branch.deb_version)
    if base_branch.format_version < 0.3:
        # The other variables were introduced in recipe format 0.3
        return
    debupstreambase_var = DebUpstreamBaseVariable.from_changelog(
        branch_name, cl)
    base_branch.deb_version = debupstreambase_var.replace(
        base_branch.deb_version)
    pkgversion_var = DebVersionVariable.from_changelog(branch_name, cl)
    base_branch.deb_version = pkgversion_var.replace(base_branch.deb_version)


def substitute_branch_vars(base_branch, child_branch):
    """Substitute the branch variables for the given branch name in deb_version.

    :param base_branch: The base `RecipeBranch` to operate on.
    :param child_branch: The child `RecipeBranch` to operate on.
    """
    if base_branch.deb_version is None:
        return
    revno_var = RevnoVariable(child_branch)
    base_branch.deb_version = revno_var.replace(base_branch.deb_version)
    # XXX assert absence of {svn-revno} for now
    if base_branch.format_version < 0.4:
        # The other variables were introduced in recipe format 0.4
        return
    git_commit_var = GitCommitVariable(child_branch)
    base_branch.deb_version = git_commit_var.replace(base_branch.deb_version)
    latest_tag_var = LatestTagVariable(child_branch)
    base_branch.deb_version = latest_tag_var.replace(base_branch.deb_version)
    revdate_var = RevdateVariable(child_branch)
    base_branch.deb_version = revdate_var.replace(base_branch.deb_version)
    revtime_var = RevtimeVariable(child_branch)
    base_branch.deb_version = revtime_var.replace(base_branch.deb_version)
    changelog_exists = False
    show_process = subprocess.Popen(
        ["git", "-C", child_branch._get_git_path(),
         "show", "%s:debian/changelog" % child_branch.get_revspec()],
        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
    try:
        cl = Changelog(file=show_process.stdout)
    finally:
        show_process.stdout.close()
        changelog_exists = (show_process.wait() == 0)
    if changelog_exists:
        substitute_changelog_vars(base_branch, child_branch.name, cl)


def substitute_time(base_branch, time):
    """Substitute the time into deb_version if needed.

    :param time: A `datetime.datetime` with the desired time.
    """
    if base_branch.deb_version is None:
        return
    base_branch.deb_version = TimeVariable(time).replace(
        base_branch.deb_version)
    base_branch.deb_version = DateVariable(time).replace(
        base_branch.deb_version)