aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/gbp-posttag-push
blob: 8cb1671569f13bc096f79011398e9753d7abe96a (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
#
# (C) 2009,2012,2015,2016 Guido Guenther <agx@sigxcpu.org>
#
"""gbp-posttag-push: post tag hook to be called by 'gbp buildpackage' to push out
the newly created tag and to forward the remote branch to that position.

It checks for explicit push destinations, if none are found it pushes
to where the branch got merged from. Before pushing it checks if the
tag is signed.

It will only push changes up to the tag and it will figure out if it
needs to push an upstream tag and the upstream branch as well (given
the -u option). Before performing a package upload it checks if it can
push the changes to the repo to ensure the Debian archive and the VCS
stay in sync (as good as possible given the async nature of the upload
into the archive).

Configuration
-------------
In your gbp.conf use

  [buildpackage]
  posttag = gbp-posttag-push -u

to enable the hook. To also enable archive uploads configure an
apropriate upload command:

  [gbp-posttag-push]
  upload-cmd = dput

Options
-------
-d: dry-run
-u: push upstream branch too, if not on remote already
--verbose: verbose command output
"""
from __future__ import print_function

import glob
import os
import subprocess
import sys

import gbp.log
from gbp.command_wrappers import Command, CommandExecFailed
from gbp.config import GbpOptionParserDebian
from gbp.deb.git import DebianGitRepository, GitRepositoryError
from gbp.deb.source import DebianSource
from gbp.errors import GbpError
from gbp.git.vfs import GitVfs
from gbp.scripts.common import ExitCodes


class Env(object):
    pass


def get_push_targets(env):
    """get a list of push targets"""
    dests = {}
    cmd = "git config --get-regexp 'remote\..*\.push' '^%s(:.*)?$'" % env.branch
    for remote in subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].split("\n"):
        if not len(remote):
            continue
        repo, refspec = remote.split()
        repo = ".".join(repo.split('.')[1:-1])  # remote.<repo>.push
        try:
            remote = refspec.split(':')[1]  # src:dest
        except IndexError:
            remote = refspec
        dests[repo] = remote
    return dests


def get_pull(env):
    """where did we pull from?"""
    cmd = 'git config --get branch."%s".remote' % env.branch
    remote = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0].strip()
    if not remote:
        remote = 'origin'
    return {remote: env.branch}


def git_push_sim(*args):
    print("git push %s" % " ".join(args))


def get_upstream_tag(repo, tag, tag_format):
    # FIXME: This assumes the debian version is the last part after the slash:
    version = tag.split('/')[-1]
    upstream = version.rsplit('-')[0]
    tag = tag_format % dict(version=upstream)
    if repo.has_tag(tag):
        return tag
    return None


def build_parser(name):

    # Until we moved this out of examples
    os.environ['GBP_DISABLE_SECTION_DEPRECTATION'] = '1'
    GbpOptionParserDebian.defaults['upload-cmd'] = ""
    GbpOptionParserDebian.help['upload-cmd'] = "Upload command, default is '%(upload-cmd)s'"

    try:
        parser = GbpOptionParserDebian(command=os.path.basename(name),
                                       usage='%prog [options]')
    except GbpError as err:
        gbp.log.err(err)
        return None

    parser.add_option("-d", "--dry-run", dest="dryrun", default=False,
                      action="store_true", help="dry run, don't push.")
    parser.add_option("-u", "--push-upstream", dest="push_upstream",
                      default=False,
                      action="store_true",
                      help="also push upstream branch changes")
    parser.add_config_file_option(option_name="upstream-branch",
                                  dest="upstream_branch")
    parser.add_config_file_option(option_name="upstream-tag",
                                  dest="upstream_tag")
    parser.add_config_file_option(option_name="debian-tag",
                                  dest="debian_tag")
    parser.add_config_file_option(option_name="upload-cmd",
                                  dest="upload_cmd")
    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
    parser.add_config_file_option(option_name="color-scheme",
                                  dest="color_scheme")
    parser.add_option("--verbose", action="store_true", dest="verbose",
                      default=False, help="verbose command execution")
    return parser


def parse_args(argv):
    parser = build_parser(argv[0])
    if not parser:
        return None, None
    return parser.parse_args(argv)


def find_changes(sourcename, version):
    glob_ex = "../%s_%s_*.changes" % (sourcename, version)
    gbp.log.info("Looking for changes at %s" % glob_ex)
    # FIXME: verify version in .changes file
    return glob.glob(glob_ex)


def upload_changes(changes, cmd, dryrun):
    ret = 0
    for name in changes:
        gbp.log.info("Running %s" % " ".join([cmd, name]))
        try:
            if not dryrun:
                Command(cmd, [name], shell=False)()
        except CommandExecFailed:
            gbp.log.err("Upload of '%s' failed." % name)
            ret = 1
    return ret


def do_push(repo, dests, debian_tag, debian_sha1, upstream_tag, upstream_sha1, upstream_branch, dry_run):
    verb = "Dry-run: Pushing" if dry_run else "Pushing"
    for dest in dests:
        gbp.log.info("%s %s to %s" % (verb, debian_tag, dest))
        repo.push_tag(dest, debian_tag, dry_run)
        gbp.log.info("%s %s to %s:%s" % (verb, debian_sha1, dest, dests[dest]))
        repo.push(dest, debian_sha1, dests[dest])
        if upstream_tag:
            gbp.log.info("%s %s to %s" % (verb, upstream_tag, dest))
            repo.push_tag(dest, upstream_tag, dry_run)
            if not repo.branch_contains("%s/%s" % (dest, upstream_branch),
                                        upstream_sha1, remote=True):
                gbp.log.info("%s %s to %s:%s" % (verb, upstream_sha1, dest, upstream_branch))
                repo.push(dest, upstream_sha1, upstream_branch, dry_run)


def main(argv):
    retval = 0
    env = Env()
    upstream_sha1 = None

    (options, args) = parse_args(argv)
    if not options:
        return ExitCodes.parse_error

    gbp.log.setup(options.color, options.verbose, options.color_scheme)

    repo = DebianGitRepository('.')
    if options.dryrun:
        gbp.log.warn("Dry run mode. Not pushing, not uploading.")
        repo.push = git_push_sim
        repo.push_tag = git_push_sim

    for envvar in ["GBP_TAG", "GBP_BRANCH", "GBP_SHA1"]:
        var = os.getenv(envvar)
        if var:
            env.__dict__.setdefault("%s" % envvar.split("_")[1].lower(), var)
        else:
            gbp.log.err("%s not set." % envvar)
            return 1

    dests = get_push_targets(env)
    if not dests:
        dests = get_pull(env)

    upstream_tag = get_upstream_tag(repo, env.tag, options.upstream_tag)
    if upstream_tag:
        upstream_sha1 = repo.rev_parse("%s^{}" % upstream_tag)

    if not repo.verify_tag(env.tag):
        gbp.log.info("Not pushing non-existent or unsigned tag '%s'." % env.tag)
        return 0

    source = DebianSource(GitVfs(repo, env.tag))

    try:
        if options.upload_cmd:
            do_push(repo,
                    dests,
                    env.tag,
                    env.sha1,
                    upstream_tag if options.push_upstream and upstream_tag else None,
                    upstream_sha1,
                    options.upstream_branch,
                    dry_run=True)
            version = repo.tag_to_version(env.tag, options.debian_tag).split(':')[-1]
            changes = find_changes(source.sourcepkg, version)
            if len(changes):
                retval = upload_changes(changes, options.upload_cmd, options.dryrun)
            else:
                gbp.log.info("No changes file found, nothing to upload.")
        else:
            gbp.log.info("No upload command defined, not uploading to the archive")

        do_push(repo,
                dests,
                env.tag,
                env.sha1,
                upstream_tag if options.push_upstream and upstream_tag else None,
                upstream_sha1,
                options.upstream_branch,
                dry_run=False)
    except (GbpError, GitRepositoryError) as err:
        if str(err):
            gbp.log.err(err)
        retval = 1

    return retval


if __name__ == '__main__':
    sys.exit(main(sys.argv))

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