Hi all,

Here's a patch for service-discovery-applet that checks if gobby is in the 
system path, and asks for permission to
install it if it's not.  For the moment, it only works on Debian or Ubuntu, but 
eventually I'll add Red Hat, SUSE, and
Gentoo support, (after I get a couple of test VMs set up).  While it works as 
is, it's a little ugly in places; I want
to try the same thing with another plugin and add support for other distro's 
before I refactor it.

I've not decided if the installation logic should be a plugin superclass, or 
part of pluginutils.  Any thoughts?

I just started using svk to generate my patch; let me know if there's anything 
wrong with it.

Thanks,

~Jason
=== plugins/obby.py.in
==================================================================
--- plugins/obby.py.in	(/zeroconf/sda/trunk)	(revision 137)
+++ plugins/obby.py.in	(/local/zeroconf/sda/trunk)	(revision 137)
@@ -14,17 +14,105 @@
 
 from sdapplet.pluginutils import *
 import subprocess
+import os.path
 
+# Installation routines shamelessly stolen from a jokosher installation script
+
+# START: refactor to pluginutils after api has jelled
+import pygtk
+import gtk
+
+pygtk.require('2.0')
+
+def confirmation_dialog(msg, type="info"):
+    msg_type = eval("gtk.MESSAGE_%s" % type.upper())
+    d = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
+                          type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_YES_NO)
+    d.set_markup(msg)
+    d.show_all()
+    rsp = (d.run() == -8)
+    if rsp:
+        print "%s: allowed" % msg
+    else:
+        print "%s: denied" % msg
+    d.destroy()
+    return rsp
+# END: refactor to pluginutils after api has jelled
+
+
 class plugin_obby:
     def __init__(self):
         self.service_type = ["_lobby._tcp"]
         self.author = "Sebastien Estienne"
         self.description = "Start an obby sessions with Gobby"
+        self.executable = "gobby"
+        self.platform_package_info = { 'debian': ('gobby', self.install_on_debian)}
 
     def connect(self, use_host_names, name, stype, hostname, address, port, txts):
-	cmdline = ["@toolsdir@/exec_wrapper", "gobby", "--join=%s:%s" % (hostname,port) ]
-	print cmdline
-	pid = subprocess.Popen(cmdline, close_fds=True).pid
+        if self.executable_not_in_system_path(): 
+            if self.executable_is_installable():
+                if self.attempt_installation():
+                    #oops, installation failed
+                    return
+            else:
+                msg = "Unable to install %s, please install it manually" % self.executable
+                print msg
+                error_msg(msg)
+                return
+        cmdline = ["@toolsdir@/exec_wrapper", self.executable, "--join=%s:%s" % (hostname,port) ]
+        print cmdline
+        pid = subprocess.Popen(cmdline, close_fds=True).pid
 
+    def executable_not_in_system_path(self):
+        possible_paths = [os.sep.join([x, self.executable]) for x in os.getenv('PATH').split(':')]
+        ex_not_in_path = not bool( filter( lambda x: os.path.exists(x), possible_paths))
+        if ex_not_in_path:
+            print "%s not in path" % self.executable
+        return ex_not_in_path
+
+    def detect_distro(self):
+        import platform
+        return platform.dist()[0]
+
+    def executable_is_installable(self):
+        ex_is_installable = self.detect_distro() in self.platform_package_info
+        if ex_is_installable:
+            print "%s is installable" % self.executable
+        else:
+            print "%s is not installable" % self.executable
+        return ex_is_installable
+
+    def attempt_installation(self):
+        return self.platform_package_info[self.detect_distro()][1]()
+
+    def install_on_debian(self):
+        missing_msg = "%s is not installed, would you like to install it now?" % self.executable
+        if not confirmation_dialog(missing_msg):
+            error_msg("Installation request denied.  Unable to open service.")
+            print "Installation request denied"
+            return 1
+        else:
+        # they want to install the packages
+        # thanks to deskbar-apt for this bit! 
+            from tempfile import mktemp
+            fn = mktemp()
+            fp = open(fn,"w")
+            fp.write( '\n'.join( ['%s\tinstall' % self.executable] ) )
+            fp.close()
+            cmd = [
+              '/usr/bin/gksudo', 
+              '-m', 
+              'Please enter your password to install the %s package' % self.executable,
+              '--',
+              '/usr/sbin/synaptic',
+              '--non-interactive',
+              '--hide-main-window',
+              '--progress-str','Please wait, this can take some time',
+              '--finish-str','Installation complete!',
+              '--set-selections-file',fn
+            ]
+            ret = os.spawnv(os.P_WAIT,cmd[0],cmd)
+            os.remove(fn)
+
 def load():
     return plugin_obby()
_______________________________________________
avahi mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/avahi

Reply via email to