Improve error messages on formatting errors
authorGuido Günther <agx@sigxcpu.org>
Tue, 19 Aug 2014 18:07:35 +0000 (20:07 +0200)
committerJun Wang <junbill.wang@samsung.com>
Tue, 26 Jan 2016 15:52:56 +0000 (23:52 +0800)
Make it easier for the user to detect misformated replacement strings in
config files and command line options.

Conflicts:
debian/git-buildpackage.install
gbp/scripts/import_orig.py

gbp/deb/git.py
gbp/format.py [new file with mode: 0644]
gbp/scripts/buildpackage.py
gbp/scripts/import_orig.py

index 6105fe7..2a848d4 100644 (file)
@@ -19,6 +19,7 @@
 import re
 from gbp.git import GitRepository, GitRepositoryError
 from gbp.deb.pristinetar import DebianPristineTar
+from gbp.format import format_msg
 
 class DebianGitRepository(GitRepository):
     """A git repository that holds the source of a Debian package"""
@@ -104,7 +105,7 @@ class DebianGitRepository(GitRepository):
         >>> DebianGitRepository.version_to_tag("debian/%(version)s", "0:0~0")
         'debian/0%0_0'
         """
-        return format % dict(version=DebianGitRepository._sanitize_version(version))
+        return format_msg(format, dict(version=DebianGitRepository._sanitize_version(version)))
 
     @staticmethod
     def _sanitize_version(version):
diff --git a/gbp/format.py b/gbp/format.py
new file mode 100644 (file)
index 0000000..2a4af15
--- /dev/null
@@ -0,0 +1,44 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2014 Guido Guenther <agx@sigxcpu.org>
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    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.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+"""Format a message"""
+
+from gbp.errors import GbpError
+
+def format_msg(msg, args):
+    """
+    Format a strin with the given dict. Be a bit more verbose than
+    default python about the error cause.
+
+    >>> format_msg("%(foo)", {})
+    Traceback (most recent call last):
+    ...
+    GbpError: Failed to format %(foo): Missing value 'foo' in {}
+    >>> format_msg("%(foo)", {'foo': 'bar'})
+    Traceback (most recent call last):
+    ...
+    GbpError: Failed to format %(foo) with {'foo': 'bar'}: incomplete format
+    >>> format_msg("A %(foo)s is a %(bar)s", {'foo': 'dog', 'bar': 'mamal'})
+    'A dog is a mamal'
+    """
+    try:
+        return msg % args
+    except ValueError as e:
+        raise GbpError("Failed to format %s with %s: %s" % (msg, args, e))
+    except KeyError as e:
+            raise GbpError("Failed to format %s: Missing value %s in %s" % (msg, e, args))
+
+
index 01754e6..dc85029 100755 (executable)
@@ -29,6 +29,7 @@ from gbp.command_wrappers import (Command,
 from gbp.config import (GbpOptionParserDebian, GbpOptionGroup)
 from gbp.deb.git import (GitRepositoryError, DebianGitRepository)
 from gbp.deb.source import DebianSource, DebianSourceError
+from gbp.format import format_msg
 from gbp.git.vfs import GitVfs
 from gbp.deb.upstreamsource import DebianUpstreamSource
 from gbp.errors import GbpError
@@ -620,9 +621,9 @@ def main(argv):
             gbp.log.info("Tagging %s as %s" % (source.changelog.version, tag))
             if options.retag and repo.has_tag(tag):
                 repo.delete_tag(tag)
-            tag_msg=options.debian_tag_msg % dict(
-                            pkg=source.sourcepkg,
-                            version=source.changelog.version)
+            tag_msg = format_msg(options.debian_tag_msg,
+                                 dict(pkg=source.sourcepkg,
+                                      version=source.changelog.version))
             repo.create_tag(name=tag,
                             msg=tag_msg,
                             sign=options.sign_tags, keyid=options.keyid)
index fb6070e..a42a9eb 100644 (file)
@@ -29,6 +29,7 @@ from gbp.deb.changelog import ChangeLog, NoChangeLogError
 from gbp.deb.git import (GitRepositoryError, DebianGitRepository)
 from gbp.config import GbpOptionParserDebian, GbpOptionGroup, no_upstream_branch_msg
 from gbp.errors import GbpError
+from gbp.format import format_msg
 import gbp.log
 from gbp.pkg import compressor_opts
 from gbp.scripts.common.import_orig import (orig_needs_repack, cleanup_tmp_tree,
@@ -383,7 +384,7 @@ def main(argv):
                             epoch = '%s:' % cp.epoch
                     info = { 'version': "%s%s-1" % (epoch, version) }
                     env = { 'GBP_BRANCH': options.packaging_branch }
-                    gbpc.Command(options.postimport % info, extra_env=env, shell=True)()
+                    gbpc.Command(format_msg(options.postimport, info), extra_env=env, shell=True)()
             # Update working copy and index if we've possibly updated the
             # checked out branch
             current_branch = repo.get_branch()