2
# -*- coding: utf-8 -*-
3
# Copyright (C) 2014 Canonical
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.
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
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
30
sys.path.append(os.path.abspath(os.path.join(__file__, '..', '..'))) # add local cupstream2distro
32
from cupstream2distro import launchpadmanager, packagemanager
35
def get_upstream_version(version, remove_epoch=True):
36
"""Return upstream version"""
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)
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")
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")
58
args = parser.parse_args()
60
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO,
61
format="%(asctime)s %(levelname)s %(message)s")
63
logging.debug("Debug mode enabled")
67
series = launchpadmanager.get_series(args.series)
68
except lazr.restfulclient.errors.NotFound:
69
logging.error("{} doesn't exist".format(args.series))
72
series = launchpadmanager.get_ubuntu().current_series
73
logging.info("Taking {} as current destination series".format(series.name))
76
dest = launchpadmanager.get_ppa(args.ppa)
78
dest = launchpadmanager.get_ubuntu_archive()
80
workdir = tempfile.mkdtemp()
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]
92
version_to_downgrade_to = sourceversion[1]
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))
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'):
105
for source in source_version_revert:
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))
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))
116
shutil.copy2(os.path.join(latest_version_path, 'debian', 'changelog'), os.path.join(new_version_path, 'debian', 'changelog'))
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
122
for file_name in os.listdir('.'):
123
if ".orig." in file_name:
124
orig_tarball = file_name
127
logging.error("Couldn't find original tarball")
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)
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:
138
if subprocess.call(['dch', '-r', '']) != 0:
140
if subprocess.call(['debuild', '-S']) != 0:
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'):
149
for source in changes_revert_path:
150
os.chdir(changes_revert_path[source])
151
subprocess.call("dput *.changes", shell=True)
153
shutil.rmtree(workdir)
b'\\ No newline at end of file'