Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/session/session.py
blob: 2f6eab212e9905651efe57c8bd32e9e22c540a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import signal
from ConfigParser import ConfigParser

import pygtk
pygtk.require('2.0')
import gtk

from shell import Shell
from sugar import env

class Session:
	def __init__(self):
		self._activity_processes = {}

	def start(self):
		shell = Shell()
		shell.connect('close', self._shell_close_cb)
		shell.start()
		
		self._run_activities()

	def _run_activities(self):
		base_dirs = []
		
		base_dirs.append(env.get_activities_dir())
		base_dirs.append(os.path.join(env.get_user_dir(), 'activities'))

		for base_dir in base_dirs:
			if os.path.isdir(base_dir):
				for filename in os.listdir(base_dir):
					activity_dir = os.path.join(base_dir, filename)
					if os.path.isdir(activity_dir):
						self._run_activity(os.path.abspath(activity_dir))

	def _run_activity(self, activity_dir):
		env.add_to_python_path(activity_dir)

		activities = []
		for filename in os.listdir(activity_dir):
			if filename.endswith(".activity"):
				path = os.path.join(activity_dir, filename)
				cp = ConfigParser()
				cp.read([path])
				python_class = cp.get('Activity', "python_class")
				activities.append(python_class)

		for activity in activities:
			args = [ 'python', '-m', activity ]
			pid = os.spawnvp(os.P_NOWAIT, 'python', args)
			self._activity_processes[activity] = pid

		try:
			gtk.main()
		except KeyboardInterrupt:
			print 'Ctrl+C pressed, exiting...'
			self.shutdown()
			
	def _shell_close_cb(self, shell):
		self.shutdown()
	
	def shutdown(self):
		# FIXME Obviously we want to notify the activities to
		# shutt down rather then killing them down forcefully.
		for name in self._activity_processes.keys():
			print 'Shutting down %s' % (name) 
			os.kill(self._activity_processes[name], signal.SIGTERM)