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 <quozl@laptop.org>
This commit is contained in:
James Cameron 2019-03-20 15:07:23 +11:00
parent 359dbf2c45
commit 2453d13b03

View File

@ -40,12 +40,21 @@ from sugar3 import logger
from sugar3.bundle.bundle import MalformedBundleException from sugar3.bundle.bundle import MalformedBundleException
from distutils.dir_util import mkpath from errno import EEXIST
import time import time
import hashlib import hashlib
import random 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): def create_activity_instance(constructor, handle):
activity = constructor(handle) activity = constructor(handle)
activity.show() activity.show()
@ -113,9 +122,7 @@ def main():
else: else:
activity_class = args[0] activity_class = args[0]
os.environ['SUGAR_BUNDLE_PATH'] = os.path.abspath(os.curdir) bundle_path = os.path.abspath(os.curdir)
bundle_path = os.environ['SUGAR_BUNDLE_PATH']
sys.path.insert(0, bundle_path) sys.path.insert(0, bundle_path)
try: try:
@ -131,25 +138,28 @@ def main():
logging.warning("Activity written for Python 2, consider porting to Python 3.") logging.warning("Activity written for Python 2, consider porting to Python 3.")
activity_class = command.split(" ")[1] 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') profile_id = os.environ.get('SUGAR_PROFILE', 'default')
home_dir = os.environ.get('SUGAR_HOME', os.path.expanduser('~/.sugar')) home_dir = os.environ.get('SUGAR_HOME', os.path.expanduser('~/.sugar'))
base = os.path.join(home_dir, profile_id) base = os.path.join(home_dir, profile_id)
activity_root = os.path.join(base, bundle.get_bundle_id()) activity_root = os.path.join(base, bundle.get_bundle_id())
instance_dir = os.path.join(activity_root, 'instance') instance_dir = os.path.join(activity_root, 'instance')
mkpath(instance_dir) _makedirs(instance_dir)
data_dir = os.path.join(activity_root, 'data') data_dir = os.path.join(activity_root, 'data')
mkpath(data_dir) _makedirs(data_dir)
tmp_dir = os.path.join(activity_root, 'tmp') tmp_dir = os.path.join(activity_root, 'tmp')
mkpath(tmp_dir) _makedirs(tmp_dir)
os.environ['SUGAR_ACTIVITY_ROOT'] = activity_root
os.environ['SUGAR_BUNDLE_PATH'] = bundle.get_path()
os.environ['SUGAR_BUNDLE_PATH'] = bundle_path
os.environ['SUGAR_BUNDLE_ID'] = bundle.get_bundle_id() os.environ['SUGAR_BUNDLE_ID'] = bundle.get_bundle_id()
os.environ['SUGAR_ACTIVITY_ROOT'] = activity_root
os.environ['SUGAR_BUNDLE_NAME'] = bundle.get_name() os.environ['SUGAR_BUNDLE_NAME'] = bundle.get_name()
os.environ['SUGAR_BUNDLE_VERSION'] = str(bundle.get_activity_version()) os.environ['SUGAR_BUNDLE_VERSION'] = str(bundle.get_activity_version())