From b770493016f4483baa1f20f66488bfe3d4d09c01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Guido=20G=C3=BCnther?= Date: Sat, 8 Jan 2011 14:27:33 +0100 Subject: [PATCH] git-buildpackage: Add support for sending notifications via libnotify after the build finished. --- debian/control | 2 +- debian/git-buildpackage.bash-completion | 2 +- docs/manpages/git-buildpackage.sgml | 16 ++++++++ gbp.conf | 4 +- gbp/config.py | 7 +++- gbp/notifications.py | 71 +++++++++++++++++++++++++++++++++ git-buildpackage | 7 ++++ 7 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 gbp/notifications.py diff --git a/debian/control b/debian/control index e7db98e..8fcd5c5 100644 --- a/debian/control +++ b/debian/control @@ -15,7 +15,7 @@ Architecture: all Depends: ${python:Depends}, ${shlibs:Depends}, ${misc:Depends}, devscripts (>= 2.10.66~), git (>= 1:1.7.0.4-2) | git-core (>= 1:1.5.0.1-1), python-dateutil Recommends: pristine-tar (>= 0.5), cowbuilder -Suggests: git-load-dirs +Suggests: git-load-dirs, python-notify Description: Suite to help with Debian packages in Git repositories This package contains the following tools: * git-import-{dsc,dscs}: import existing Debian source packages into a git diff --git a/debian/git-buildpackage.bash-completion b/debian/git-buildpackage.bash-completion index 8c61170..1b85504 100644 --- a/debian/git-buildpackage.bash-completion +++ b/debian/git-buildpackage.bash-completion @@ -75,7 +75,7 @@ _git_buildpackage() local options=$(_gbp_options git-buildpackage) local branch_opts="--git-debian-branch\= --git-upstream-branch\=" local tag_opts="--git-debian-tag\= --git-upstream-tag\=" - local tristate_opts="--git-color\=" + local tristate_opts="--git-color\= --git-notify\=" _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts" } diff --git a/docs/manpages/git-buildpackage.sgml b/docs/manpages/git-buildpackage.sgml index 64d552c..7839d92 100644 --- a/docs/manpages/git-buildpackage.sgml +++ b/docs/manpages/git-buildpackage.sgml @@ -22,6 +22,8 @@ + [auto|on|off] + [auto|on|off] treeish branch_name @@ -155,6 +157,20 @@ + [auto|on|off] + + + Wheter to use colored output. + + + + [auto|on|off] + + + Wheter to send a desktop notification after the build. + + + =branch_name diff --git a/gbp.conf b/gbp.conf index c7ecbb6..9ab4093 100644 --- a/gbp.conf +++ b/gbp.conf @@ -16,7 +16,7 @@ #pristine-tar = True # don't check if debian-branch == current branch: #ignore-branch = True -# Use color when on a terminal, alternatives: on, off +# Use color when on a terminal, alternatives: on/true, off/false or auto #color = auto # Options only affecting git-buildpackage @@ -41,6 +41,8 @@ #compression = bzip2 # use best compression #compression-level = best +# Don't send notifications, alternatives: on/true, off/false or auto +#notify = off # Options only affecting git-import-orig [git-import-orig] diff --git a/gbp/config.py b/gbp/config.py index e623c3b..8448762 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -94,6 +94,7 @@ class GbpOptionParser(OptionParser): 'customizations' : '', 'spawn-editor' : 'release', 'patch-numbers' : 'True', + 'notify' : 'auto', } help = { 'debian-branch': @@ -145,9 +146,11 @@ class GbpOptionParser(OptionParser): 'color': "color output, default is '%(color)s'", 'spawn-editor': - "Wether to spawn an editor after adding the changelog entry, default is '%(spawn-editor)s'", + "Whether to spawn an editor after adding the changelog entry, default is '%(spawn-editor)s'", 'patch-numbers': - "Wether to number patch files, default is %(patch-numbers)s", + "Whether to number patch files, default is %(patch-numbers)s", + 'notify': + "Whether to send a desktop notification after the build, default is '%(notify)s'", } config_files = [ '/etc/git-buildpackage/gbp.conf', os.path.expanduser('~/.gbp.conf'), diff --git a/gbp/notifications.py b/gbp/notifications.py new file mode 100644 index 0000000..b2534e3 --- /dev/null +++ b/gbp/notifications.py @@ -0,0 +1,71 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2011 Guido Guenther +# 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 + +import log +import warnings + +notify_module = None + +def enable_notifications(): + global notify_module + + with warnings.catch_warnings(): + # Avoid GTK+ cannot open display warning: + warnings.simplefilter("ignore") + try: + import pynotify + notify_module = pynotify + except ImportError: + return False + + return notify_module.init("git-buildpackage") + + +def build_msg(cp, success): + summary = "Gbp %s" % ["failed", "successful"][success] + msg = ("Build of %s %s %s" % + (cp['Source'], cp['Version'], ["failed", "succeeded"][success])) + + return summary, msg + + +def send_notification(summary, msg): + n = notify_module.Notification(summary, msg) + try: + if not n.show(): + return False + except: + return False + return True + + +def notify(cp, success, notify_opt): + """ + Send a notifications + @return: False on error + """ + + if notify_opt.is_off(): + return True + + enable = enable_notifications() + if not enable: + return [True, False][notify_opt.is_on()] + + summary, msg = build_msg(cp, success) + return notify_opt.do(send_notification, summary, msg) + diff --git a/git-buildpackage b/git-buildpackage index a7a0cec..78212bf 100755 --- a/git-buildpackage +++ b/git-buildpackage @@ -32,6 +32,7 @@ from gbp.config import (GbpOptionParser, GbpOptionGroup) from gbp.errors import GbpError from glob import glob import gbp.log +import gbp.notifications # when we want to reference the index in a treeish context we call it: index_name = "INDEX" @@ -218,6 +219,7 @@ def main(argv): changelog = 'debian/changelog' retval = 0 prefix = "git-" + cp = None args = [ arg for arg in argv[1:] if arg.find('--%s' % prefix) == 0 ] dpkg_args = [ arg for arg in argv[1:] if arg.find('--%s' % prefix) == -1 ] @@ -248,6 +250,7 @@ def main(argv): parser.add_option("--git-verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") parser.add_config_file_option(option_name="color", dest="color", type='tristate') + parser.add_config_file_option(option_name="notify", dest="notify", type='tristate') tag_group.add_option("--git-tag", action="store_true", dest="tag", default=False, help="create a tag after a successful build") tag_group.add_option("--git-tag-only", action="store_true", dest="tag_only", default=False, @@ -443,6 +446,10 @@ def main(argv): if options.export_dir and options.purge and not retval: RemoveTree(export_dir)() + if cp and not gbp.notifications.notify(cp, not retval, options.notify): + gbp.log.err("Failed to send notification") + retval = 1 + return retval if __name__ == '__main__': -- 2.7.4