From d3493aea9ef4ccc79df397580c37283f2bfe9fcf Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 16:35:53 +0100 Subject: [PATCH 1/5] Smarter sugar-activity which can make itself the factory and open one instance. --- shell/sugar-activity | 59 ++++++++++++++++-------- shell/sugar-activity-factory | 4 +- sugar/activity/activityfactory.py | 11 +++-- sugar/activity/activityfactoryservice.py | 15 +++--- sugar/activity/bundle.py | 6 +-- 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/shell/sugar-activity b/shell/sugar-activity index 6f5c9741..75aa967f 100755 --- a/shell/sugar-activity +++ b/shell/sugar-activity @@ -18,30 +18,53 @@ import sys import os -import gobject +import gtk +import dbus +import dbus.glib + +from sugar.activity import bundleregistry from sugar.activity import activityfactory +from sugar.activity import activityfactoryservice from sugar import env -from sugar import util -def _success_cb(handler, activity, loop): - activity.start(util.unique_id()) - loop.quit() +def _setup_bus_address(): + '''Use the bus address of the running Sugar''' + bus_file = os.path.join(env.get_profile_path(), "session_bus_address") + f = open(bus_file, "r") + bus_name = f.read() + f.close() + os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name -def _error_cb(handler, err, loop): - loop.quit() +def _success_cb(handler, exit): + if exit: + gtk.main_quit() -ppath = env.get_profile_path() -bus_file = os.path.join(ppath, "session_bus_address") -f = open(bus_file, "r") -bus_name = f.read() -f.close() -os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name +def _error_cb(handler, err): + print err + gtk.main_quit() -loop = gobject.MainLoop() +_setup_bus_address() -handler = activityfactory.create(sys.argv[1]) -handler.connect('success', _success_cb, loop) -handler.connect('error', _error_cb, loop) +service_name = sys.argv[1] +registry = bundleregistry.get_registry() +bundle = registry.get_bundle(service_name) -loop.run() +bus = dbus.SessionBus() +bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') +try: + name = bus_object.GetNameOwner( + service_name, dbus_interface='org.freedesktop.DBus') +except dbus.DBusException: + name = None + +if name: + print '%s is already running, creating a new instance.' % service_name +else: + activityfactoryservice.run(bundle.get_path()) + +handler = activityfactory.create(service_name) +handler.connect('success', _success_cb, name != None) +handler.connect('error', _error_cb) + +gtk.main() diff --git a/shell/sugar-activity-factory b/shell/sugar-activity-factory index 009b4609..bad6aa8a 100755 --- a/shell/sugar-activity-factory +++ b/shell/sugar-activity-factory @@ -20,4 +20,6 @@ import sys from sugar.activity import activityfactoryservice -activityfactoryservice.run(sys.argv) +activityfactoryservice.run_with_args(sys.argv) + +gtk.main() diff --git a/sugar/activity/activityfactory.py b/sugar/activity/activityfactory.py index 695d149c..cd632ca1 100644 --- a/sugar/activity/activityfactory.py +++ b/sugar/activity/activityfactory.py @@ -53,16 +53,16 @@ def _find_activity_id(): return act_id class ActivityCreationHandler(gobject.GObject): - __gsignals__ = { - 'error': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ([gobject.TYPE_PYOBJECT])), + 'success': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, ([])), + 'error': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT])) } def __init__(self, service_name, activity_handle): gobject.GObject.__init__(self) - self._service_name = service_name self._activity_handle = activity_handle @@ -83,6 +83,7 @@ class ActivityCreationHandler(gobject.GObject): def _reply_handler(self, xid): logging.debug("Activity created %s (%s)." % (self._activity_handle.activity_id, self._service_name)) + self.emit('success') def _error_handler(self, err): logging.debug("Couldn't create activity %s (%s): %s" % diff --git a/sugar/activity/activityfactoryservice.py b/sugar/activity/activityfactoryservice.py index 548ff33f..723c31e1 100644 --- a/sugar/activity/activityfactoryservice.py +++ b/sugar/activity/activityfactoryservice.py @@ -77,22 +77,25 @@ class ActivityFactoryService(dbus.service.Object): if len(self._activities) == 0: gtk.main_quit() -def run(args): +def run_with_args(args): """Start the activity factory.""" parser = OptionParser() parser.add_option("-p", "--bundle-path", dest="bundle_path", help="path to the activity bundle") (options, args) = parser.parse_args() - sys.path.insert(0, options.bundle_path) + run(options.bundle_path) - bundle = Bundle(options.bundle_path) +def run(bundle_path): + sys.path.insert(0, bundle_path) + + bundle = Bundle(bundle_path) logger.start(bundle.get_name()) - os.environ['SUGAR_BUNDLE_PATH'] = options.bundle_path + os.environ['SUGAR_BUNDLE_PATH'] = bundle_path os.environ['SUGAR_BUNDLE_SERVICE_NAME'] = bundle.get_service_name() os.environ['SUGAR_BUNDLE_DEFAULT_TYPE'] = bundle.get_default_type() - factory = ActivityFactoryService(bundle.get_service_name(), args[0]) - gtk.main() + factory = ActivityFactoryService(bundle.get_service_name(), + bundle.get_class()) diff --git a/sugar/activity/bundle.py b/sugar/activity/bundle.py index 8980a115..c4f85030 100644 --- a/sugar/activity/bundle.py +++ b/sugar/activity/bundle.py @@ -43,8 +43,8 @@ class Bundle: if cp.has_option(section, 'class'): self._class = cp.get(section, 'class') - self._exec = '%s %s --bundle-path="%s"' % ( - env.get_bin_path(_PYTHON_FACTORY), self._class, self.get_path()) + self._exec = '%s --bundle-path="%s"' % ( + env.get_bin_path(_PYTHON_FACTORY), self.get_path()) elif cp.has_option(section, 'exec'): self._class = None self._exec = cp.get(section, 'exec') @@ -105,7 +105,7 @@ class Bundle: def get_class(self): """Get the main Activity class""" - return self._exec + return self._class def get_show_launcher(self): """Get whether there should be a visible launcher for the activity""" From a5d303f9ff96cb17a4fb10e611314e7c1c440c7e Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 16:55:18 +0100 Subject: [PATCH 2/5] Smarter bundle name matching --- shell/sugar-activity | 21 ++++++++++++++++----- sugar/activity/bundleregistry.py | 12 ++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/shell/sugar-activity b/shell/sugar-activity index 75aa967f..04fed667 100755 --- a/shell/sugar-activity +++ b/shell/sugar-activity @@ -44,17 +44,28 @@ def _error_cb(handler, err): print err gtk.main_quit() +def print_help(self): + sys.exit(0) + _setup_bus_address() -service_name = sys.argv[1] -registry = bundleregistry.get_registry() -bundle = registry.get_bundle(service_name) +bundle = None + +if len(sys.argv) > 1: + registry = bundleregistry.get_registry() + bundle = registry.find_bundle(sys.argv[1]) + +if bundle == None: + print 'Usage:\n\n' \ + 'sugar-activity [bundle]\n\n' \ + 'Bundle can be a part of the service name or of bundle name.' + sys.exit(0) bus = dbus.SessionBus() bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') try: name = bus_object.GetNameOwner( - service_name, dbus_interface='org.freedesktop.DBus') + bundle.get_service_name(), dbus_interface='org.freedesktop.DBus') except dbus.DBusException: name = None @@ -63,7 +74,7 @@ if name: else: activityfactoryservice.run(bundle.get_path()) -handler = activityfactory.create(service_name) +handler = activityfactory.create(bundle.get_service_name()) handler.connect('success', _success_cb, name != None) handler.connect('error', _error_cb) diff --git a/sugar/activity/bundleregistry.py b/sugar/activity/bundleregistry.py index 2ad68ed5..7b124921 100644 --- a/sugar/activity/bundleregistry.py +++ b/sugar/activity/bundleregistry.py @@ -41,6 +41,18 @@ class BundleRegistry(gobject.GObject): self._search_path = [] self._service_manager = _ServiceManager() + def find_bundle(self, key): + """Find a bundle in the registry""" + key = key.lower() + + for bundle in self._bundles.values(): + name = bundle.get_name().lower() + service_name = bundle.get_service_name().lower() + if name.find(key) != -1 or service_name.find(key) != -1: + return bundle + + return None + def get_bundle(self, service_name): """Returns an bundle given his service name""" if self._bundles.has_key(service_name): From 6c5d28ecf516cca2ad317983fade3dbf0061d6e6 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 17:29:03 +0100 Subject: [PATCH 3/5] Save also the sugar session display and use it in sugar-activity. --- shell/sugar-activity | 21 ++++++++++----------- shell/sugar-shell | 11 +++++++++-- sugar/env.py | 13 ++++++++----- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/shell/sugar-activity b/shell/sugar-activity index 04fed667..7757a76b 100755 --- a/shell/sugar-activity +++ b/shell/sugar-activity @@ -18,6 +18,16 @@ import sys import os +from ConfigParser import ConfigParser + +from sugar import env + +# Setup the environment so that we run inside the Sugar shell +cp = ConfigParser() +cp.read([env.get_profile_path("session.info")]) +os.environ['DBUS_SESSION_BUS_ADDRESS'] = cp.get('Session', 'dbus_address') +os.environ['DISPLAY'] = cp.get('Session', 'display') +del cp import gtk import dbus @@ -26,15 +36,6 @@ import dbus.glib from sugar.activity import bundleregistry from sugar.activity import activityfactory from sugar.activity import activityfactoryservice -from sugar import env - -def _setup_bus_address(): - '''Use the bus address of the running Sugar''' - bus_file = os.path.join(env.get_profile_path(), "session_bus_address") - f = open(bus_file, "r") - bus_name = f.read() - f.close() - os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name def _success_cb(handler, exit): if exit: @@ -47,8 +48,6 @@ def _error_cb(handler, err): def print_help(self): sys.exit(0) -_setup_bus_address() - bundle = None if len(sys.argv) > 1: diff --git a/shell/sugar-shell b/shell/sugar-shell index 4b67e963..277030de 100755 --- a/shell/sugar-shell +++ b/shell/sugar-shell @@ -18,6 +18,7 @@ import sys import os +from ConfigParser import ConfigParser if len(sys.argv) == 2: sys.path.insert(0, sys.argv[1]) @@ -55,9 +56,15 @@ if not key or not len(key): # # WARNING!!! this is going away at some near future point, do not rely on it # -dsba_file = os.path.join(env.get_profile_path(), "session_bus_address") +dsba_file = os.path.join(env.get_profile_path(), "session.info") f = open(dsba_file, "w") -f.write(os.environ["DBUS_SESSION_BUS_ADDRESS"]) + +cp = ConfigParser() +cp.add_section('Session') +cp.set('Session', 'dbus_address', os.environ['DBUS_SESSION_BUS_ADDRESS']) +cp.set('Session', 'display', gtk.gdk.display_get_default().get_name()) +cp.write(f) + f.close() model = ShellModel() diff --git a/sugar/env.py b/sugar/env.py index 65fd1fe5..4cdaf2cc 100644 --- a/sugar/env.py +++ b/sugar/env.py @@ -34,20 +34,23 @@ def is_emulator(): return True return False -def get_profile_path(): +def get_profile_path(path=None): if os.environ.has_key('SUGAR_PROFILE'): profile_id = os.environ['SUGAR_PROFILE'] else: profile_id = 'default' - path = os.path.join(os.path.expanduser('~/.sugar'), profile_id) - if not os.path.isdir(path): + base = os.path.join(os.path.expanduser('~/.sugar'), profile_id) + if not os.path.isdir(base): try: - os.makedirs(path) + os.makedirs(base) except OSError, exc: print "Could not create user directory." - return path + if path != None: + return os.path.join(base, path) + else: + return base def get_user_activities_path(): path = os.path.expanduser('~/Activities') From b8cc8ec68f6fa008edc9c90f860143bd0a61eb5b Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 17:38:05 +0100 Subject: [PATCH 4/5] Drop support for running from source dir. As it is now it's just confusing. Will have to figure out the exact needs and rewrite in a better way. --- shell/sugar-shell | 6 +----- sugar-emulator | 26 +++----------------------- 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/shell/sugar-shell b/shell/sugar-shell index 277030de..24eb7516 100755 --- a/shell/sugar-shell +++ b/shell/sugar-shell @@ -20,9 +20,6 @@ import sys import os from ConfigParser import ConfigParser -if len(sys.argv) == 2: - sys.path.insert(0, sys.argv[1]) - import pygtk pygtk.require('2.0') import gtk @@ -36,8 +33,7 @@ from sugar import TracebackUtils logger.cleanup() logger.start('shell') -if len(sys.argv) == 1: - sys.path.insert(0, env.get_shell_path()) +sys.path.insert(0, env.get_shell_path()) from view.Shell import Shell from model.ShellModel import ShellModel diff --git a/sugar-emulator b/sugar-emulator index d31010d0..09ebaa8a 100755 --- a/sugar-emulator +++ b/sugar-emulator @@ -25,24 +25,12 @@ import gtk os.environ['SUGAR_EMULATOR'] = 'yes' -sourcedir = os.path.abspath(os.path.dirname(__file__)) -if os.path.isfile(os.path.join(sourcedir, 'sugar/__uninstalled__.py')): - print 'Running sugar from ' + sourcedir + ' ...' - sys.path.insert(0, sourcedir) -else: - print 'Running the installed sugar...' - sourcedir = None - from sugar import env -from sugar import util from sugar.emulator import Emulator import _sugar if len(sys.argv) == 1: - if sourcedir: - program = os.path.join(sourcedir, 'shell/sugar-shell') - else: - program = 'sugar-shell' + program = 'sugar-shell' else: program = sys.argv[1] @@ -54,18 +42,10 @@ else: height = 900 dpi = min(_sugar.get_screen_dpi(), 96) - -if sourcedir: - kbd_config = os.path.join(sourcedir, 'emulator/kbdconfig') -else: - kbd_config = os.path.join(env.get_emulator_path('kbdconfig')) +kbd_config = os.path.join(env.get_emulator_path('kbdconfig')) emulator = Emulator(width, height, dpi) emulator.set_keyboard_config(kbd_config) emulator.start() -if sourcedir: - program = os.path.join(sourcedir, program) - os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program, sourcedir) -else: - os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program) +os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program) From cc7ad2d42be9ca426f7d9a903bc2513634b5c873 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 17:43:55 +0100 Subject: [PATCH 5/5] Missing imports --- shell/sugar-activity-factory | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shell/sugar-activity-factory b/shell/sugar-activity-factory index bad6aa8a..a144f343 100755 --- a/shell/sugar-activity-factory +++ b/shell/sugar-activity-factory @@ -18,6 +18,10 @@ import sys +import pygtk +pygtk.require('2.0') +import gtk + from sugar.activity import activityfactoryservice activityfactoryservice.run_with_args(sys.argv)