~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
#!/usr/bin/python
# pull-debian-debdiff - find and download a specific version of a Debian
#                       package and its immediate parent to generate a debdiff.
#
# Copyright (C) 2010-2011, Stefano Rivera <stefanor@ubuntu.com>
# Inspired by a tool of the same name by Kees Cook.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

import optparse
import sys

import debian.debian_support
import debian.changelog

from devscripts.logger import Logger

from ubuntutools.archive import DebianSourcePackage, DownloadError
from ubuntutools.config import UDTConfig

def previous_version(package, version, distance):
    "Given an (extracted) package, determine the version distance versions ago"
    upver = debian.debian_support.Version(version).upstream_version
    filename = '%s-%s/debian/changelog' % (package, upver)
    changelog_file = open(filename, 'r')
    changelog = debian.changelog.Changelog(changelog_file.read())
    changelog_file.close()
    seen = 0
    for entry in changelog:
        if entry.distributions == 'UNRELEASED':
            continue
        if seen == distance:
            return entry.version.full_version
        seen += 1
    return False

def main():
    parser = optparse.OptionParser('%prog [options] <package> <version> '
                                   '[distance]')
    parser.add_option('-f', '--fetch',
                      dest='fetch_only', default=False, action='store_true',
                      help="Only fetch the source packages, don't diff.")
    parser.add_option('-d', '--debian-mirror', metavar='DEBIAN_MIRROR',
                      dest='debian_mirror',
                      help='Preferred Debian mirror '
                           '(default: http://ftp.debian.org/debian)')
    parser.add_option('-s', '--debsec-mirror', metavar='DEBSEC_MIRROR',
                      dest='debsec_mirror',
                      help='Preferred Debian Security mirror '
                           '(default: http://security.debian.org)')
    parser.add_option('--no-conf',
                      dest='no_conf', default=False, action='store_true',
                      help="Don't read config files or environment variables")

    opts, args = parser.parse_args()
    if len(args) < 2:
        parser.error('Must specify package and version')
    elif len(args) > 3:
        parser.error('Too many arguments')
    package = args[0]
    version = args[1]
    distance = int(args[2]) if len(args) > 2 else 1

    config = UDTConfig(opts.no_conf)
    if opts.debian_mirror is None:
        opts.debian_mirror = config.get_value('DEBIAN_MIRROR')
    if opts.debsec_mirror is None:
        opts.debsec_mirror = config.get_value('DEBSEC_MIRROR')
    mirrors = [opts.debsec_mirror, opts.debian_mirror]

    Logger.normal('Downloading %s %s', package, version)

    newpkg = DebianSourcePackage(package, version, mirrors=mirrors)
    try:
        newpkg.pull()
    except DownloadError, e:
        Logger.error('Failed to download: %s', str(e))
        sys.exit(1)
    newpkg.unpack()

    if opts.fetch_only:
        sys.exit(0)

    oldversion = previous_version(package, version, distance)
    if not oldversion:
        Logger.error('No previous version could be found')
        sys.exit(1)
    Logger.normal('Downloading %s %s', package, oldversion)

    oldpkg = DebianSourcePackage(package, oldversion, mirrors=mirrors)
    try:
        oldpkg.pull()
    except DownloadError, e:
        Logger.error('Failed to download: %s', str(e))
        sys.exit(1)
    oldpkg.unpack()
    print 'file://' + oldpkg.debdiff(newpkg, diffstat=True)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        Logger.normal('User abort.')