~jelmer/ubuntu-dev-tools/lptools-migration

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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# submittodebian - tool to submit patches to Debian's BTS
# Copyright (C) 2007, 2009 Canonical Ltd.
# Author: Soren Hansen <soren@ubuntu.com>,
#         Steve Langasek <slangasek@canonical.com>
#
# ##################################################################
#
# 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.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################

import os
import re
import sys
from tempfile import mkstemp

from distro_info import UbuntuDistroInfo

from ubuntutools.config import ubu_email
from ubuntutools.question import YesNoQuestion

try:
    from debian.changelog import Changelog
except ImportError:
    print ('This utility requires modules from the «python-debian» package, '
           'which isn\'t currently installed.')
    sys.exit(1)

if not os.path.exists('/usr/bin/reportbug'):
    print ('This utility requires the «reportbug» package, which isn\'t '
           'currently installed.')
    sys.exit(1)

def get_most_recent_debian_version(changelog):
    for version in changelog.get_versions():
        if not re.search('(ubuntu|build)', version.full_version):
            return version.full_version

def get_bug_body(changelog):
    msg = """In Ubuntu, the attached patch was applied to achieve the following:

## ---------------- REPLACE THIS WITH ACTUAL INFORMATION ---------------------
## Please add all necessary information about why the change needed to go in
## Ubuntu, quote policy, spec or any other background material and why it can
## and should be used in Debian too.  If the patch is composed of multiple
## independent pieces, please send them as separate bug reports.
## ---------------- REPLACE THIS WITH ACTUAL INFORMATION ---------------------

%s

Thanks for considering the patch.
""" % ("\n".join([a for a in changelog._blocks[0].changes()]))
    return msg

def gen_debdiff(changelog):
    pkg = changelog.package

    oldver = changelog._blocks[1].version
    newver = changelog._blocks[0].version

    (fd, debdiff) = mkstemp()
    os.close(fd)

    if os.system('bzr diff -r tag:%s > /dev/null 2>&1' % oldver) == 256:
        print "Extracting bzr diff between %s and %s" % (oldver, newver)
        cmd = 'bzr diff -r tag:%s | filterdiff -x "*changelog*" > %s' % \
              (oldver, debdiff)
        run_cmd(cmd)
    else:
        if oldver.epoch is not None:
            oldver = str(oldver)[str(oldver).index(":")+1:]
        if newver.epoch is not None:
            newver = str(newver)[str(newver).index(":")+1:]

        olddsc = '../%s_%s.dsc' % (pkg, oldver)
        newdsc = '../%s_%s.dsc' % (pkg, newver)

        check_file(olddsc)
        check_file(newdsc)

        print "Generating debdiff between %s and %s" % (oldver, newver)
        cmd = 'debdiff %s %s | filterdiff -x "*changelog*" > %s' % \
              (olddsc, newdsc, debdiff)
        run_cmd(cmd)

    return debdiff

def check_file(fname, critical = True):
    if os.path.exists(fname):
        return fname
    else:
        if not critical:
            return False
        print "Couldn't find «%s».\n" % fname
        sys.exit(1)

def edit_debdiff(debdiff):
    cmd = 'sensible-editor %s' % (debdiff)
    run_cmd(cmd)

def submit_bugreport(body, debdiff, deb_version, changelog):
    cmd = ('reportbug -P "User: ubuntu-devel@lists.ubuntu.com" '
           '-P "Usertags: origin-ubuntu %s ubuntu-patch" -T patch -A %s '
           '-B debian -i %s -V %s %s') % \
           (UbuntuDistroInfo().devel(), debdiff, body, deb_version,
            changelog.package)
    run_cmd(cmd)

def run_cmd(cmd):
    if os.getenv('DEBUG'):
        print "%s\n" % cmd
    os.system(cmd)

def check_reportbug_config():
    fn = os.path.expanduser('~/.reportbugrc')
    if os.path.exists(fn):
        return
    email = ubu_email()[1]
    reportbugrc = """# Reportbug configuration generated by submittodebian(1)
# See reportbug.conf(5) for the configuration file format.

# Use Debian's reportbug SMTP Server:
# Note: it's limited to 5 connections per hour, and cannot CC you at submission
# time. See /usr/share/doc/reportbug/README.Users.gz for more details.
smtphost reportbug.debian.org:587
header "X-Debbugs-CC: %s"
no-cc

# Use GMail's SMTP Server:
#smtphost smtp.googlemail.com:587
#smtpuser "<your address>@gmail.com"
#smtptls
""" % email

    with file(fn, 'w') as f:
        f.write(reportbugrc)

    print """\
You have not configured reportbug. Assuming this is the first time you have
used it. Writing a ~/.reportbugrc that will use Debian's mail server, and CC
the bug to you at <%s>

--- Generated ~/.reportbugrc ---
%s
--- End of ~/.reportbugrc ---

If this is not correct, please exit now and edit ~/.reportbugrc or run
reportbug --configure for its configuration wizard.
""" % (email, reportbugrc.strip())

    if YesNoQuestion().ask("Continue submitting this bug", "yes") == "no":
        sys.exit(1)

def main():
    check_reportbug_config()
    changelog_file = (check_file('debian/changelog', critical = False) or
                      check_file('../debian/changelog'))
    changelog = Changelog(file(changelog_file).read())

    deb_version = get_most_recent_debian_version(changelog)
    bug_body = get_bug_body(changelog)

    fd, body = mkstemp()
    fp = os.fdopen(fd, 'w')
    fp.write(bug_body)
    fp.close()

    debdiff = gen_debdiff(changelog)
    edit_debdiff(debdiff)
    submit_bugreport(body, debdiff, deb_version, changelog)
    os.unlink(body)
    os.unlink(debdiff)

if __name__ == '__main__':
    main()