Skip to content
This repository
Browse code

Stubbed in a notify plugin for libnotify.

  • Loading branch information...
commit 3d2cde300f456b07320ef825f2978dade320a15d 1 parent 4efc1be
Thomas Gideon authored

Showing 1 changed file with 112 additions and 0 deletions. Show diff stats Hide diff stats

  1. 112  src/flashbake/plugins/notify.py
112  src/flashbake/plugins/notify.py
... ...
@@ -0,0 +1,112 @@
  1
+#    copyright 2011 Thomas Gideon
  2
+#
  3
+#    This file is part of flashbake.
  4
+#
  5
+#    flashbake is free software: you can redistribute it and/or modify
  6
+#    it under the terms of the GNU General Public License as published by
  7
+#    the Free Software Foundation, either version 3 of the License, or
  8
+#    (at your option) any later version.
  9
+#
  10
+#    flashbake is distributed in the hope that it will be useful,
  11
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13
+#    GNU General Public License for more details.
  14
+#
  15
+#    You should have received a copy of the GNU General Public License
  16
+#    along with flashbake.  If not, see <http://www.gnu.org/licenses/>.
  17
+
  18
+# notify.py - libnotify notification flashbake plugin for standard Linux desktop notifications
  19
+
  20
+from . import AbstractNotifyPlugin, AbstractPlugin, PluginError, PLUGIN_ERRORS
  21
+import flashbake
  22
+import logging
  23
+import os
  24
+import re
  25
+import subprocess
  26
+import pynotify
  27
+
  28
+
  29
+
  30
+class Notify(AbstractNotifyPlugin):
  31
+    def __init__(self, plugin_spec):
  32
+        AbstractPlugin.__init__(self, plugin_spec)
  33
+        if not pynotify.init("icon-summary-body"):
  34
+            raise PluginError(PLUGIN_ERRORS.ignorable_error, #@UndefinedVariable
  35
+                              self.plugin_spec,
  36
+                              'Cannot initialize libnotify; are the library and the Python bindings installed?') 
  37
+        
  38
+    def __notify(self, title, message):
  39
+        n = pynotify.Notification(
  40
+            title,
  41
+            message,
  42
+            "notification-message-im")
  43
+        n.show()
  44
+
  45
+    def warn(self, hot_files, config):
  46
+        ''' Emits one message per file, with less explanation than the email plugin.
  47
+            The most common case is that one or two files will be off, a large number
  48
+            of them can be considered pathological, e.g. someone who didn't read the
  49
+            documentation about lack of support for symlinks, for instance. '''
  50
+        # if calling notify locally, then the current user must be logged into the console
  51
+        if not self.__active_console():
  52
+            logging.debug('Current user does not have console access.')
  53
+            return
  54
+
  55
+        logging.debug('Trying to warn via notify.')
  56
+        project_name = os.path.basename(hot_files.project_dir)
  57
+
  58
+        [self.__notify('Missing in project, %s' % project_name,
  59
+                          'The file, "%s", is missing.' % file)
  60
+         for file in hot_files.not_exists]
  61
+
  62
+        [self.__notify('Deleted in project, %s' % project_name,
  63
+                          'The file, "%s", has been deleted from version control.' % file)
  64
+         for file in hot_files.deleted]
  65
+
  66
+        [self.__notify('Link in project, %s' % project_name,
  67
+                          'The file, "%s", is a link.' % file)
  68
+         for (file, link) in hot_files.linked_files.iteritems()
  69
+         if file == link]
  70
+        
  71
+        [self.__notify('Link in project, %s' % project_name,
  72
+                          'The file, "%s", is a link to %s.' % (link, file))
  73
+         for (file, link) in hot_files.linked_files.iteritems()
  74
+         if file != link]
  75
+
  76
+
  77
+        [self.__notify('External file in project, %s' % project_name,
  78
+                           'The file, "%s", exists outside of the project directory.' % file)
  79
+        for file in hot_files.outside_files]
  80
+
  81
+    def notify_commit(self, to_commit, hot_files, config):
  82
+        logging.debug('Trying to notify.')
  83
+        self.__notify(os.path.basename(hot_files.project_dir),
  84
+                          'Tracking changes to:\n' + '\n'.join(to_commit))
  85
+
  86
+    def __whoami(self):
  87
+        cmd = flashbake.find_executable('whoami')
  88
+        if cmd:
  89
+            proc = subprocess.Popen([cmd], stdout=subprocess.PIPE,
  90
+                                    stderr=subprocess.STDOUT)
  91
+            return proc.communicate()[0].strip()
  92
+        else:
  93
+            return None
  94
+    
  95
+    def __active_console(self):
  96
+        user = self.__whoami()
  97
+        if not user:
  98
+            return False
  99
+        cmd = flashbake.find_executable('who')
  100
+        if not cmd:
  101
+            return False
  102
+        proc = subprocess.Popen([cmd], stdout=subprocess.PIPE,
  103
+                                stderr=subprocess.STDOUT)
  104
+        active = False
  105
+        for line in proc.communicate()[0].splitlines():
  106
+            logging.debug(line)
  107
+            m = re.match('^%s\s+pts.*$' % user, line)
  108
+            if m:
  109
+                active = True
  110
+                break
  111
+        return active
  112
+

0 notes on commit 3d2cde3

Please sign in to comment.
Something went wrong with that request. Please try again.