#!/usr/bin/env python # Copyright (C) 2007, Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from __future__ import with_statement import os import sys import dbus # Initialize logging. import logging from sugar import logger #Get the standard logging directory. std_log_dir = logger.get_logs_dir() _logger = logging.getLogger('PyDebug') """ #First log handler: outputs to a file called 'VideoEdit.activity.log' file_handler = logging.FileHandler(os.path.join(std_log_dir,'PyDebug.activity.log') file_formatter = logging.Formatter('%(name)s -- %(asctime)s %(funcName)s: %(lineno)d\n %(message)s\n') file_handler.setFormatter(file_formatter) _logger.addHandler(file_handler) """ _logger.setLevel(logging.DEBUG) #Second log handler: outputs to a the console, using a slightly different output structure console_handler = logging.StreamHandler() console_formatter = logging.Formatter('%(name)s %(levelname)s %(funcName)s: %(lineno)d||| %(message)s') console_handler.setFormatter(console_formatter) _logger.addHandler(console_handler) from sugar.activity import activityfactory from sugar.bundle.activitybundle import ActivityBundle path = sys.argv[1] pydebug_home = os.environ.get('PYDEBUG_HOME','/home/olpc/.sugar/default/org.laptop.PyDebug/data') os.environ['PYDEBUG_HOME'] = pydebug_home os.chdir(path) os.environ['SUGAR_BUNDLE_PATH'] = path _logger.debug('sugar_bundle_path set to %s'%path) #set up module search path sys.path.insert(0,path) activity = ActivityBundle(path) cmd_args = activityfactory.get_command(activity) _logger.debug('command args:%r'%cmd_args) bundle_name = activity.get_name() bundle_id = activity.get_bundle_id() #need to get activity root, but activity bases off of HOME which some applications need to change #following will not work if storage system changes with new OS #required because debugger needs to be able to change home so that foreign apps will work activity_root = os.path.join('/home/olpc/.sugar/default/',bundle_id) os.environ['SUGAR_ACTIVITY_ROOT'] = activity_root _logger.debug('sugar_activity_root set to %s'%activity_root) #following is useful for its side-effects info = activityfactory.get_environment(activity) _logger.debug("Command to execute:%s."%cmd_args[0]) if not cmd_args[0].startswith('sugar-activity'): target = os.path.join(pydebug_home,os.path.basename(cmd_args[0])) with open(target,'w') as write_script_fd: with open(cmd_args[0],'r') as read_script_fd: for line in read_script_fd.readlines(): if line.startswith('exec') or line.startswith('sugar-activity'): pass else: write_script_fd.write(line) line = 'export -p > %s_env\n'%target write_script_fd.write(line) #write the environment variables to another file write_script_fd.close() os.chmod(target,0755) os.system(target) _logger.debug('writing env script:%s'%target) #read the environment back into the current process with open('%s_env'%target,'r') as env_file: env_dict = {} for line in env_file.readlines(): if not line.startswith('export'): pass payload = line.split()[1] pair = payload.split('=') if len(pair)> 1: key = pair[0] value = pair[1] env_dict[key] = value _logger.debug('reading environment. %s => %s'%(key,value,)) os.environ = env_dict more_args = ['-a',bundle_name,'-b',bundle_id] sys.argv = cmd_args[:2] + more_args _logger.debug('about to call main.main() with args %r'%sys.argv) from sugar.activity import main main.main()