From 34f68552184f73931b5a1c8a20a2324e606a6917 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 9 May 2012 19:18:36 +0000 Subject: [PATCH] Improve error message in patch application failure. Make exception PatchApplicationFailed() self printable. R=cmp@chromium.org BUG= TEST= Review URL: https://chromiumcodereview.appspot.com/10391033 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@136091 0039d316-1c4b-4281-b951-d872f2087c98 --- apply_issue.py | 8 ++++++-- checkout.py | 39 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/apply_issue.py b/apply_issue.py index 5bf72197b..52f31882d 100755 --- a/apply_issue.py +++ b/apply_issue.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -69,7 +69,11 @@ def main(): parser.error('Couldn\'t determine the scm') # Apply the patch. - scm_obj.apply_patch(patchset) + try: + scm_obj.apply_patch(patchset) + except checkout.PatchApplicationFailed, e: + print >> sys.stderr, str(e) + return 1 return 0 diff --git a/checkout.py b/checkout.py index fa732e8c7..2260ab348 100644 --- a/checkout.py +++ b/checkout.py @@ -49,11 +49,24 @@ def get_code_review_setting(path, key, class PatchApplicationFailed(Exception): """Patch failed to be applied.""" - def __init__(self, filename, status): - super(PatchApplicationFailed, self).__init__(filename, status) - self.filename = filename + def __init__(self, p, status): + super(PatchApplicationFailed, self).__init__(p, status) + self.patch = p self.status = status + @property + def filename(self): + if self.patch: + return self.patch.filename + + def __str__(self): + out = [] + if self.filename: + out.append('Failed to apply patch for %s:' % self.filename) + if self.status: + out.append(self.status) + return '\n'.join(out) + class CheckoutBase(object): # Set to None to have verbose output. @@ -140,12 +153,12 @@ class RawCheckout(CheckoutBase): if p.source_filename: if not p.is_new: raise PatchApplicationFailed( - p.filename, + p, 'File has a source filename specified but is not new') # Copy the file first. if os.path.isfile(filepath): raise PatchApplicationFailed( - p.filename, 'File exist but was about to be overwriten') + p, 'File exist but was about to be overwriten') shutil.copy2( os.path.join(self.project_path, p.source_filename), filepath) if p.diff_hunks: @@ -160,10 +173,10 @@ class RawCheckout(CheckoutBase): for post in post_processors: post(self, p) except OSError, e: - raise PatchApplicationFailed(p.filename, '%s%s' % (stdout, e)) + raise PatchApplicationFailed(p, '%s%s' % (stdout, e)) except subprocess.CalledProcessError, e: raise PatchApplicationFailed( - p.filename, '%s%s' % (stdout, getattr(e, 'stdout', None))) + p, '%s%s' % (stdout, getattr(e, 'stdout', None))) def commit(self, commit_message, user): """Stubbed out.""" @@ -307,12 +320,12 @@ class SvnCheckout(CheckoutBase, SvnMixIn): if p.source_filename: if not p.is_new: raise PatchApplicationFailed( - p.filename, + p, 'File has a source filename specified but is not new') # Copy the file first. if os.path.isfile(filepath): raise PatchApplicationFailed( - p.filename, 'File exist but was about to be overwriten') + p, 'File exist but was about to be overwriten') self._check_output_svn( [ 'copy', @@ -347,10 +360,10 @@ class SvnCheckout(CheckoutBase, SvnMixIn): for post in post_processors: post(self, p) except OSError, e: - raise PatchApplicationFailed(p.filename, '%s%s' % (stdout, e)) + raise PatchApplicationFailed(p, '%s%s' % (stdout, e)) except subprocess.CalledProcessError, e: raise PatchApplicationFailed( - p.filename, + p, 'While running %s;\n%s%s' % ( ' '.join(e.cmd), stdout, getattr(e, 'stdout', ''))) @@ -503,10 +516,10 @@ class GitCheckoutBase(CheckoutBase): for post in post_processors: post(self, p) except OSError, e: - raise PatchApplicationFailed(p.filename, '%s%s' % (stdout, e)) + raise PatchApplicationFailed(p, '%s%s' % (stdout, e)) except subprocess.CalledProcessError, e: raise PatchApplicationFailed( - p.filename, '%s%s' % (stdout, getattr(e, 'stdout', None))) + p, '%s%s' % (stdout, getattr(e, 'stdout', None))) # Once all the patches are processed and added to the index, commit the # index. self._check_call_git(['commit', '-m', 'Committed patch'])