2009-08-04 13:43:31 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# common.py - common methods used by requestsync
|
|
|
|
#
|
|
|
|
# Copyright © 2009 Michael Bienia <geser@ubuntu.com>
|
|
|
|
#
|
2009-08-05 23:10:05 +02:00
|
|
|
# This module may contain code written by other authors/contributors to
|
|
|
|
# the main requestsync script. See there for their names.
|
|
|
|
#
|
2009-08-04 13:43:31 +02:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
2009-08-05 23:10:05 +02:00
|
|
|
# as published by the Free Software Foundation; version 2
|
2010-12-03 00:06:43 +01:00
|
|
|
#
|
2009-08-04 13:43:31 +02:00
|
|
|
# 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.
|
|
|
|
#
|
2009-08-05 23:10:05 +02:00
|
|
|
# Please see the /usr/share/common-licenses/GPL-2 file for the full text
|
2009-08-04 13:43:31 +02:00
|
|
|
# of the GNU General Public License license.
|
|
|
|
|
2009-08-07 13:27:45 +02:00
|
|
|
import os
|
2009-08-04 13:43:31 +02:00
|
|
|
import sys
|
|
|
|
import urllib2
|
2009-08-07 13:27:45 +02:00
|
|
|
import re
|
|
|
|
import tempfile
|
2010-06-08 19:09:40 +02:00
|
|
|
from debian.changelog import Changelog
|
2009-08-04 13:43:31 +02:00
|
|
|
|
2011-05-24 20:22:37 +02:00
|
|
|
from ubuntutools import subprocess
|
|
|
|
|
2009-08-06 00:23:58 +02:00
|
|
|
def raw_input_exit_on_ctrlc(*args, **kwargs):
|
2010-12-22 23:04:29 +02:00
|
|
|
'''
|
|
|
|
A wrapper around raw_input() to exit with a normalized message on Control-C
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
return raw_input(*args, **kwargs)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print '\nAbort requested. No sync request filed.'
|
|
|
|
sys.exit(1)
|
2009-08-06 00:23:58 +02:00
|
|
|
|
2011-09-10 15:42:40 +02:00
|
|
|
def get_changelog(srcpkg, distro):
|
2010-12-22 23:04:29 +02:00
|
|
|
'''
|
2011-09-10 10:28:41 +02:00
|
|
|
Download and return a parsed changelog for srcpackage, from
|
|
|
|
packages.debian.org or changelogs.ubuntu.com
|
2010-12-22 23:04:29 +02:00
|
|
|
'''
|
|
|
|
pkgname = srcpkg.getPackageName()
|
|
|
|
pkgversion = srcpkg.getVersion()
|
|
|
|
component = srcpkg.getComponent()
|
|
|
|
if pkgname.startswith('lib'):
|
|
|
|
subdir = 'lib%s' % pkgname[3]
|
|
|
|
else:
|
|
|
|
subdir = pkgname[0]
|
|
|
|
# Strip epoch from version
|
|
|
|
if ':' in pkgversion:
|
|
|
|
pkgversion = pkgversion[pkgversion.find(':')+1:]
|
2011-09-10 10:28:41 +02:00
|
|
|
extension = ''
|
|
|
|
if distro == 'debian':
|
|
|
|
base = 'http://packages.debian.org/'
|
|
|
|
extension = '.txt'
|
|
|
|
elif distro == 'ubuntu':
|
|
|
|
base = 'http://changelogs.ubuntu.com/'
|
2010-12-22 23:04:29 +02:00
|
|
|
|
2011-09-10 10:28:41 +02:00
|
|
|
url = os.path.join(base, 'changelogs', 'pool', component, subdir, pkgname,
|
|
|
|
pkgname + '_' + pkgversion, 'changelog' + extension)
|
2010-12-22 23:04:29 +02:00
|
|
|
try:
|
2011-09-10 10:28:41 +02:00
|
|
|
return Changelog(urllib2.urlopen(url))
|
2010-12-22 23:04:29 +02:00
|
|
|
except urllib2.HTTPError, error:
|
2011-09-10 10:28:41 +02:00
|
|
|
print >> sys.stderr, ('Unable to connect to %s: %s' % (base, error))
|
2010-12-22 23:04:29 +02:00
|
|
|
return None
|
|
|
|
|
2011-09-10 10:28:41 +02:00
|
|
|
# TODO: Move this into requestsync.mail, and implement an LP version
|
|
|
|
# when LP: #833384 is fixed
|
2011-09-10 15:42:40 +02:00
|
|
|
def get_debian_changelog(srcpkg, version):
|
2011-09-10 10:28:41 +02:00
|
|
|
'''
|
|
|
|
Return the new changelog entries since 'version'.
|
|
|
|
'''
|
2011-09-10 15:42:40 +02:00
|
|
|
changelog = get_changelog(srcpkg, 'debian')
|
2011-09-10 10:28:41 +02:00
|
|
|
if changelog is None:
|
|
|
|
return None
|
|
|
|
new_entries = []
|
2010-12-22 23:04:29 +02:00
|
|
|
for block in changelog:
|
2011-09-10 10:28:41 +02:00
|
|
|
if block.version <= version:
|
|
|
|
break
|
|
|
|
new_entries.append(unicode(block))
|
|
|
|
return u''.join(new_entries)
|
2009-08-07 13:27:45 +02:00
|
|
|
|
|
|
|
def edit_report(subject, body, changes_required = False):
|
2010-12-22 23:04:29 +02:00
|
|
|
'''
|
|
|
|
Ask if the user wants to edit a report (consisting of subject and body)
|
|
|
|
in sensible-editor.
|
|
|
|
|
|
|
|
If changes_required is True then the file has to be edited before we
|
|
|
|
can proceed.
|
|
|
|
|
|
|
|
Returns (new_subject, new_body).
|
|
|
|
'''
|
|
|
|
|
|
|
|
editing_finished = False
|
|
|
|
while not editing_finished:
|
|
|
|
report = 'Summary (one line):\n%s\n\nDescription:\n%s' % (subject, body)
|
|
|
|
|
|
|
|
if not changes_required:
|
|
|
|
print 'Currently the report looks as follows:\n%s' % report
|
|
|
|
while True:
|
2010-12-23 00:01:39 +02:00
|
|
|
val = raw_input_exit_on_ctrlc('Do you want to edit the report '
|
|
|
|
'[y/N]? ')
|
2010-12-22 23:04:29 +02:00
|
|
|
if val.lower() in ('y', 'yes'):
|
|
|
|
break
|
|
|
|
elif val.lower() in ('n', 'no', ''):
|
|
|
|
editing_finished = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print 'Invalid answer.'
|
|
|
|
|
|
|
|
if not editing_finished:
|
|
|
|
# Create tempfile and remember mtime
|
|
|
|
report_file = tempfile.NamedTemporaryFile(prefix='requestsync_')
|
|
|
|
report_file.write(report.encode('utf-8'))
|
|
|
|
report_file.flush()
|
|
|
|
mtime_before = os.stat(report_file.name).st_mtime
|
|
|
|
|
|
|
|
# Launch editor
|
|
|
|
try:
|
|
|
|
subprocess.check_call(['sensible-editor', report_file.name])
|
|
|
|
except subprocess.CalledProcessError, e:
|
2010-12-23 00:01:39 +02:00
|
|
|
print >> sys.stderr, ('Error calling sensible-editor: %s\n'
|
|
|
|
'Aborting.' % e)
|
2010-12-22 23:04:29 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Check if the tempfile has been changed
|
|
|
|
if changes_required:
|
|
|
|
if mtime_before == os.stat(report_file.name).st_mtime:
|
2010-12-23 00:01:39 +02:00
|
|
|
print ('The report has not been changed, but you have to '
|
|
|
|
'explain why the Ubuntu changes can be dropped.')
|
|
|
|
raw_input_exit_on_ctrlc('Press [Enter] to retry or '
|
|
|
|
'[Control-C] to abort. ')
|
2010-12-22 23:04:29 +02:00
|
|
|
else:
|
|
|
|
changes_required = False
|
|
|
|
|
|
|
|
report_file.seek(0)
|
|
|
|
report = report_file.read().decode('utf-8')
|
|
|
|
report_file.close()
|
|
|
|
|
|
|
|
# Undecorate report again
|
|
|
|
(subject, body) = report.split("\nDescription:\n", 1)
|
|
|
|
# Remove prefix and whitespace from subject
|
2010-12-23 00:01:39 +02:00
|
|
|
subject = re.sub('^Summary \(one line\):\s*', '', subject,
|
|
|
|
1).strip()
|
2010-12-22 23:04:29 +02:00
|
|
|
|
|
|
|
return (subject, body)
|