From 2453d13b03a1affa1f151f84005f80cd55829086 Mon Sep 17 00:00:00 2001 From: James Cameron Date: Wed, 20 Mar 2019 15:07:23 +1100 Subject: [PATCH] Clean up activity environment variables Ensure environment variables needed by Python activities are available in each of the three supported scenarios; - started from Sugar, - started from Terminal inside Sugar, - started by other desktop environments. Variables always available are; SUGAR_ACTIVITY_ROOT SUGAR_BUNDLE_ID SUGAR_BUNDLE_NAME SUGAR_BUNDLE_PATH SUGAR_BUNDLE_VERSION Variables also available when started from Sugar are; SUGAR_ACTIVITIES_HIDDEN SUGAR_APISOCKET_KEY SUGAR_APISOCKET_PORT SUGAR_GROUP_LABELS SUGAR_HOME SUGAR_MIME_DEFAULTS SUGAR_PROFILE SUGAR_SCALING SUGAR_VERSION Variables also available when started from Terminal are; SUGAR_TERMINAL_VERSION Other changes; - use os.makedirs in place of distutils.dir_util.mkpath, - avoid redundant setting of SUGAR_BUNDLE_PATH, - do not set SUGAR_BUNDLE_ID unnecessarily, - add explanatory comment, Tested on Ubuntu 18.04. Signed-off-by: James Cameron --- src/sugar3/activity/activityinstance.py | 30 ++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/sugar3/activity/activityinstance.py b/src/sugar3/activity/activityinstance.py index b756f54f..b60ac104 100644 --- a/src/sugar3/activity/activityinstance.py +++ b/src/sugar3/activity/activityinstance.py @@ -40,12 +40,21 @@ from sugar3 import logger from sugar3.bundle.bundle import MalformedBundleException -from distutils.dir_util import mkpath +from errno import EEXIST + import time import hashlib import random +def _makedirs(path): + try: + os.makedirs(path) + except OSError as e: + if e.errno != EEXIST: + raise e + + def create_activity_instance(constructor, handle): activity = constructor(handle) activity.show() @@ -113,9 +122,7 @@ def main(): else: activity_class = args[0] - os.environ['SUGAR_BUNDLE_PATH'] = os.path.abspath(os.curdir) - - bundle_path = os.environ['SUGAR_BUNDLE_PATH'] + bundle_path = os.path.abspath(os.curdir) sys.path.insert(0, bundle_path) try: @@ -131,25 +138,28 @@ def main(): logging.warning("Activity written for Python 2, consider porting to Python 3.") activity_class = command.split(" ")[1] - if 'SUGAR_VERSION' not in os.environ: + # when an activity is started outside sugar, + # activityfactory.get_environment has not executed in parent + # process, so parts of get_environment must happen here. + if 'SUGAR_BUNDLE_PATH' not in os.environ: profile_id = os.environ.get('SUGAR_PROFILE', 'default') home_dir = os.environ.get('SUGAR_HOME', os.path.expanduser('~/.sugar')) base = os.path.join(home_dir, profile_id) activity_root = os.path.join(base, bundle.get_bundle_id()) instance_dir = os.path.join(activity_root, 'instance') - mkpath(instance_dir) + _makedirs(instance_dir) data_dir = os.path.join(activity_root, 'data') - mkpath(data_dir) + _makedirs(data_dir) tmp_dir = os.path.join(activity_root, 'tmp') - mkpath(tmp_dir) + _makedirs(tmp_dir) + os.environ['SUGAR_BUNDLE_PATH'] = bundle_path + os.environ['SUGAR_BUNDLE_ID'] = bundle.get_bundle_id() os.environ['SUGAR_ACTIVITY_ROOT'] = activity_root - os.environ['SUGAR_BUNDLE_PATH'] = bundle.get_path() - os.environ['SUGAR_BUNDLE_ID'] = bundle.get_bundle_id() os.environ['SUGAR_BUNDLE_NAME'] = bundle.get_name() os.environ['SUGAR_BUNDLE_VERSION'] = str(bundle.get_activity_version())