~larry-e-works/uci-engine/amqp-to-kombu

« back to all changes in this revision

Viewing changes to cupstream2distro/citrain/manual/reverter

  • Committer: Francis Ginther
  • Date: 2014-06-10 20:42:46 UTC
  • mto: This revision was merged to the branch mainline in revision 571.
  • Revision ID: francis.ginther@canonical.com-20140610204246-b1bsrik7nlcolqy7
Import lp:cupstream2distro rev 605.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
# Copyright (C) 2014 Canonical
 
4
#
 
5
# Authors:
 
6
#  Didier Roche
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify it under
 
9
# the terms of the GNU General Public License as published by the Free Software
 
10
# Foundation; version 3.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but WITHOUT
 
13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
15
# details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along with
 
18
# this program; if not, write to the Free Software Foundation, Inc.,
 
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
20
 
 
21
import argparse
 
22
import lazr
 
23
import logging
 
24
import os
 
25
import tempfile
 
26
import shutil
 
27
import sys
 
28
import subprocess
 
29
 
 
30
sys.path.append(os.path.abspath(os.path.join(__file__, '..', '..')))  # add local cupstream2distro
 
31
 
 
32
from cupstream2distro import launchpadmanager, packagemanager
 
33
 
 
34
 
 
35
def get_upstream_version(version, remove_epoch=True):
 
36
    """Return upstream version"""
 
37
 
 
38
    if remove_epoch:
 
39
        version = version.split(':')[-1]  # remove epoch is there is one
 
40
    splitted_version = version.split('-')
 
41
    if len(splitted_version) > 1:
 
42
        splitted_version = splitted_version[:-1]  # we don't want the ubuntu or debian version (it's not in the source package name)
 
43
    return '-'.join(splitted_version)
 
44
 
 
45
 
 
46
if __name__ == "__main__":
 
47
    parser = argparse.ArgumentParser(
 
48
        description="Revert to previous version of the one published in the release pocket of selected packages")
 
49
 
 
50
    parser.add_argument('source', nargs='+',
 
51
                        help='Source package name. To force a specific version to downgrade to, use source=<version>')
 
52
    parser.add_argument('commitmessage', 
 
53
                        help='Commit message to use in all packages')
 
54
    parser.add_argument('--ppa', help="ppa destination if destination isn't ubuntu archives")
 
55
    parser.add_argument('--series', help="force a specific series")
 
56
    parser.add_argument('-d', '--debug', action='store_true', default=False, help="Enable debug infos")
 
57
 
 
58
    args = parser.parse_args()
 
59
 
 
60
    logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO,
 
61
                        format="%(asctime)s %(levelname)s %(message)s")
 
62
    if args.debug:
 
63
        logging.debug("Debug mode enabled")
 
64
 
 
65
    if args.series:
 
66
        try:
 
67
            series = launchpadmanager.get_series(args.series)
 
68
        except lazr.restfulclient.errors.NotFound:
 
69
            logging.error("{} doesn't exist".format(args.series))
 
70
            sys.exit(1)
 
71
    else:
 
72
        series = launchpadmanager.get_ubuntu().current_series
 
73
        logging.info("Taking {} as current destination series".format(series.name))
 
74
 
 
75
    if args.ppa:
 
76
        dest = launchpadmanager.get_ppa(args.ppa)
 
77
    else:
 
78
        dest = launchpadmanager.get_ubuntu_archive()
 
79
 
 
80
    workdir = tempfile.mkdtemp()
 
81
    os.chdir(workdir)
 
82
    upload_dir = os.path.join(workdir, 'upload')
 
83
    latest_dir = os.path.join(workdir, 'latest')
 
84
    os.makedirs(upload_dir)
 
85
    os.makedirs(latest_dir)
 
86
    source_version_revert = {}
 
87
    changes_revert_path = {}
 
88
    for source in args.source:
 
89
        sourceversion = source.split('=')
 
90
        source_package_name = sourceversion[0]
 
91
        try:
 
92
            version_to_downgrade_to = sourceversion[1]
 
93
        except IndexError:
 
94
            # downgrade to previous version than latest published one
 
95
            source_collection = packagemanager.sort_by_date_created(dest.getPublishedSources(exact_match=True, source_name=source_package_name, distro_series=series, pocket='Release'))
 
96
            version_to_downgrade_to = source_collection[1].source_package_version
 
97
        source_version_revert[source_package_name] = version_to_downgrade_to
 
98
        logging.info("Downgrading {} to {}".format(source_package_name, version_to_downgrade_to))
 
99
 
 
100
    confirmation = raw_input("Commit message will be:\n{}\n\nOk with the versions and commit message above? [y/N] ".format(args.commitmessage))
 
101
    if confirmation.lower() not in ('y', 'yes'):
 
102
        sys.exit(1)
 
103
 
 
104
 
 
105
    for source in source_version_revert:
 
106
        orig_tarball = None
 
107
 
 
108
        os.chdir(upload_dir)
 
109
        revert_version = source_version_revert[source]
 
110
        new_version_path = os.path.abspath(packagemanager.get_source_package_from_dest(source, dest, revert_version, series.name))
 
111
 
 
112
        os.chdir(latest_dir)
 
113
        latest_version = packagemanager.get_current_version_for_series(source, series.name, dest=dest)
 
114
        latest_version_path = os.path.abspath(packagemanager.get_source_package_from_dest(source, dest, latest_version, series.name))
 
115
 
 
116
        shutil.copy2(os.path.join(latest_version_path, 'debian', 'changelog'), os.path.join(new_version_path, 'debian', 'changelog'))
 
117
 
 
118
        source_upload_dir = os.path.abspath(os.path.dirname(new_version_path))
 
119
        os.chdir(source_upload_dir)
 
120
        changes_revert_path[source] = source_upload_dir
 
121
 
 
122
        for file_name in os.listdir('.'):
 
123
            if ".orig." in file_name:
 
124
                orig_tarball = file_name
 
125
 
 
126
        if not orig_tarball:
 
127
            logging.error("Couldn't find original tarball")
 
128
            sys.exit(1)
 
129
 
 
130
        new_orig_tarball = "{}_{}.is.{}.orig.{}".format(source, get_upstream_version(latest_version), get_upstream_version(revert_version), orig_tarball.split(".orig.")[-1])
 
131
        os.rename(orig_tarball, new_orig_tarball)
 
132
 
 
133
        new_packaging_version = "{}.is.{}-0ubuntu1".format(get_upstream_version(latest_version, remove_epoch=False), get_upstream_version(revert_version))
 
134
        logging.info("Preparing {} with version {}".format(source, new_packaging_version))
 
135
        os.chdir(new_version_path)
 
136
        if subprocess.call(['dch', '-v', new_packaging_version, args.commitmessage]) != 0:
 
137
            sys.exit(1)
 
138
        if subprocess.call(['dch', '-r', '']) != 0:
 
139
            sys.exit(1)
 
140
        if subprocess.call(['debuild', '-S']) != 0:
 
141
            sys.exit(1)
 
142
 
 
143
    print("="*80)
 
144
    subprocess.call("cat {}/*/*/*.changes".format(upload_dir), shell=True)
 
145
    confirmation = raw_input("Ok to upload those? [y/N] ")
 
146
    if confirmation.lower() not in ('y', 'yes'):
 
147
        sys.exit(1)
 
148
 
 
149
    for source in changes_revert_path:
 
150
        os.chdir(changes_revert_path[source])
 
151
        subprocess.call("dput *.changes", shell=True)
 
152
 
 
153
    shutil.rmtree(workdir)
 
 
b'\\ No newline at end of file'