From 5748f9d64fd40caacf66b10b1bfa1062d9d097bd Mon Sep 17 00:00:00 2001 From: Michael Bienia Date: Fri, 7 Aug 2009 13:27:45 +0200 Subject: [PATCH] requestsync: more refactoring --- requestsync | 50 ------------------------------- ubuntutools/requestsync/common.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/requestsync b/requestsync index b500514..0c34bf7 100755 --- a/requestsync +++ b/requestsync @@ -38,7 +38,6 @@ import ubuntutools.lp.udtexceptions as udtexceptions from ubuntutools.lp.lpapicache import Launchpad, LpApiWrapper, Distribution, PersonTeam # https_proxy fix import ubuntutools.common -import ubuntutools.packages from ubuntutools.requestsync.mail import * from ubuntutools.requestsync.common import * @@ -294,55 +293,6 @@ def post_bug(source_package, subscribe, status, bugtitle, bugtext): lp_libsupport.translate_api_web(bug.self_link)) return True -def edit_report(subject, body, changes_required=False): - """Edit a report (consisting of subject and body) in sensible-editor. - - subject and body get decorated, before they are written to the temporary - file and undecorated after editing again. - If changes_required is True and the file has not been edited - (according to its mtime), an error is written to STDERR and the - program exits. - Returns (new_subject, new_body). - """ - import re - import string - - report = "Summary (one line):\n%s\n\nDescription:\n%s" % (subject, body) - - # Create tempfile and remember mtime - import tempfile - report_file = tempfile.NamedTemporaryFile( prefix='requestsync_' ) - report_file.file.write(report) - report_file.file.flush() - mtime_before = os.stat( report_file.name ).st_mtime - - # Launch editor - try: - editor = subprocess.check_call( ['sensible-editor', report_file.name] ) - except subprocess.CalledProcessError, e: - print >> sys.stderr, 'Error calling sensible-editor: %s\nAborting.' % (e,) - sys.exit(1) - - # Check if the tempfile has been changed - if changes_required: - report_file_info = os.stat( report_file.name ) - if mtime_before == os.stat( report_file.name ).st_mtime: - print >> sys.stderr, 'The temporary file %s has not been changed, but you have\nto explain why the Ubuntu changes can be dropped. Aborting. [Press ENTER]' % (report_file.name,) - raw_input() - sys.exit(1) - - report_file.file.seek(0) - report = report_file.file.read() - report_file.file.close() - - # Undecorate report again: - (new_subject, new_body) = report.split("\nDescription:\n", 1) - # Remove prefix and whitespace for subject: - new_subject = string.rstrip( re.sub("\n", " ", re.sub("^Summary \(one line\):\s*", "", new_subject, 1)) ) - - return (new_subject, new_body) - - # # entry point # diff --git a/ubuntutools/requestsync/common.py b/ubuntutools/requestsync/common.py index 2eb305b..149513f 100644 --- a/ubuntutools/requestsync/common.py +++ b/ubuntutools/requestsync/common.py @@ -19,8 +19,12 @@ # Please see the /usr/share/common-licenses/GPL-2 file for the full text # of the GNU General Public License license. +import os import sys import urllib2 +import re +import tempfile +import subprocess from debian_bundle.changelog import Changelog def raw_input_exit_on_ctrlc(*args, **kwargs): @@ -60,3 +64,48 @@ def getDebianChangelog(srcpkg, version): new_entries += str(block) return new_entries + +def edit_report(subject, body, changes_required = False): + '''Edit a report (consisting of subject and body) in sensible-editor. + + subject and body get decorated, before they are written to the + temporary file and undecorated after editing again. + If changes_required is True and the file has not been edited (according + to its mtime), an error is written to STDERR and the program exits. + + Returns (new_subject, new_body). + ''' + + report = 'Summary (one line):\n%s\n\nDescription:\n%s' % (subject, body) + + # Create tempfile and remember mtime + report_file = tempfile.NamedTemporaryFile(prefix='requestsync_') + report_file.file.write(report) + report_file.file.flush() + mtime_before = os.stat(report_file.name).st_mtime + + # Launch editor + try: + editor = subprocess.check_call(['sensible-editor', report_file.name]) + except subprocess.CalledProcessError, e: + print >> sys.stderr, 'Error calling sensible-editor: %s\nAborting.' % (e,) + sys.exit(1) + + # Check if the tempfile has been changed + if changes_required: + report_file_info = os.stat(report_file.name) + if mtime_before == os.stat(report_file.name).st_mtime: + print >> sys.stderr, 'The temporary file %s has not been changed, but you have\nto explain why the Ubuntu changes can be dropped. Aborting. [Press ENTER]' % (report_file.name,) + raw_input() + sys.exit(1) + + report_file.file.seek(0) + report = report_file.file.read() + report_file.file.close() + + # Undecorate report again + (new_subject, new_body) = report.split("\nDescription:\n", 1) + # Remove prefix and whitespace from subject + new_subject = re.sub('^Summary \(one line\):\s*', '', new_subject, 1).strip() + + return (new_subject, new_body)