Improve ActivityFactory utility functions and reduce code-path duplication.

* Remove a race in open_log_file()

  * Make open_log_file return a path as well as a file-object so that
    Rainbow-launched activities can log to the same path that sugar-launched
    ones do.

  * Change the call to Rainbow so that it writes to the log-file provided by
    open_log_file()

  * Define SUGAR_BUNDLE_ID env variable and change the location of the check
    for /etc/olpc-security so that Rainbow doesn't have to search for or parse
    the activity bundle.

  * Remove import statements marked as unnecessary by pylint.

  * Fix up some whitespace issues.
This commit is contained in:
Michael Stone 2007-11-05 13:55:56 -05:00
parent 9e4b78766b
commit 0a9676171d

View File

@ -21,12 +21,10 @@ import subprocess
import dbus
import gobject
import gtk
from sugar.presence import presenceservice
from sugar.activity.activityhandle import ActivityHandle
from sugar.activity import registry
from sugar.datastore import datastore
from sugar import util
from sugar import env
@ -93,6 +91,7 @@ def get_environment(activity):
os.mkdir(tmp_dir)
environ['SUGAR_BUNDLE_PATH'] = activity.path
environ['SUGAR_BUNDLE_ID'] = activity.bundle_id
environ['SUGAR_ACTIVITY_ROOT'] = activity_root
environ['PATH'] = bin_path + ':' + environ['PATH']
@ -115,11 +114,17 @@ def get_command(activity, activity_id=None, object_id=None, uri=None):
return command
def open_log_file(activity, activity_id):
for i in range(1, 100):
def open_log_file(activity):
i = 1
while True:
path = env.get_logs_path('%s-%s.log' % (activity.bundle_id, i))
if not os.path.exists(path):
return open(path, 'w')
try:
fd = os.open(path, os.O_EXCL | os.O_CREAT \
| os.O_SYNC | os.O_WRONLY, 0644)
f = os.fdopen(fd, 'w', 0)
return (path, f)
except:
i += 1
class ActivityCreationHandler(gobject.GObject):
"""Sugar-side activity creation interface
@ -191,27 +196,30 @@ class ActivityCreationHandler(gobject.GObject):
reply_handler=self._no_reply_handler,
error_handler=self._notify_launch_error_handler)
if not os.path.exists('/etc/olpc-security'):
activity_registry = registry.get_registry()
activity = activity_registry.get_activity(self._service_name)
if activity:
env = get_environment(activity)
log_file = open_log_file(activity, self._handle.activity_id)
environ = get_environment(activity)
(log_path, log_file) = open_log_file(activity)
command = get_command(activity, self._handle.activity_id,
self._handle.object_id,
self._handle.uri)
process = subprocess.Popen(command, env=env, cwd=activity.path,
if not os.path.exists('/etc/olpc-security'):
process = subprocess.Popen(command, env=environ, cwd=activity.path,
stdout=log_file, stderr=log_file)
else:
log_file.close()
system_bus = dbus.SystemBus()
factory = system_bus.get_object(_RAINBOW_SERVICE_NAME,
_RAINBOW_ACTIVITY_FACTORY_PATH)
stdio_paths = {'stdout': '/logs/stdout', 'stderr': '/logs/stderr'}
factory.CreateActivity(
self._service_name,
self._handle.get_dict(),
stdio_paths,
timeout=120 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND,
log_path,
environ,
command,
environ['SUGAR_BUNDLE_PATH'],
environ['SUGAR_BUNDLE_ID'],
timeout=30 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND,
reply_handler=self._create_reply_handler,
error_handler=self._create_error_handler,
dbus_interface=_RAINBOW_ACTIVITY_FACTORY_INTERFACE)