Package elisa :: Package plugins :: Package good :: Package xmlmenu :: Package actions :: Module spawn_process_action
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.good.xmlmenu.actions.spawn_process_action

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17   
 18  __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>' 
 19   
 20  from elisa.base_components.action import Action 
 21   
 22  from twisted.internet import defer, protocol, reactor 
 23   
 24  import os 
 25   
26 -class ProcessObserver(protocol.ProcessProtocol):
27
28 - def __init__(self):
29 self.reset()
30
31 - def reset(self):
32 self.deferred = defer.Deferred()
33
34 - def processEnded(self, status):
35 self.deferred.callback(status)
36 37
38 -class SpawnProcessAction(Action):
39 """ 40 This Action starts and spawns a process 41 42 @ivar executable: the executable 43 @type executable: unicode 44 @ivar running: is the program running? 45 @type running: bool 46 @ivar args: the arguments to start the process with 47 @type args: tuple 48 @ivar deferred: the deferred that will be called after the process 49 endet 50 """ 51
52 - def __init__(self):
53 self._running = False 54 self._exec = None 55 self._args = None 56 self._path = None 57 self._protocol = ProcessObserver() 58 self._protocol.deferred.addCallback(self._protocol_callback)
59
60 - def running__get(self):
61 return self._runnig
62
63 - def executable__set(self, exec_string):
64 if not self._running: 65 self._exec = exec_string
66 # raise Exception here 67
68 - def executable__get(self):
69 return self._exec
70
71 - def path__set(self, path):
72 if not self._running: 73 self._path = path
74 # raise Exception? 75
76 - def path__get(self):
77 return self._path
78
79 - def args__get(self, args):
80 if not self._running: 81 self._args = args
82 # raise Exception here 83
84 - def deferred__get(self):
85 return self._protocol.deferred
86
87 - def _protocol_callback(self):
88 self._running = False
89
90 - def __call__(self, sth):
91 92 exc = self._exec 93 94 args = [] 95 96 if ' ' in exc: 97 args = exc.split(' ') 98 exc = args[0] 99 else: 100 args.append(exc) 101 102 if exc[0] != '/': 103 pathes = os.environ.get('PATH') 104 for path in pathes.split(':'): 105 test_path = "%s/%s" % (path, exc) 106 if os.path.exists(test_path): 107 exc = test_path 108 break 109 110 path = self._path 111 112 self._protocol.reset() 113 process = reactor.spawnProcess(self._protocol, 114 exc, 115 path=path, 116 args=args, 117 env=None) 118 self._pid = process.pid 119 self.debug('called %s. Pid:%s' % (exc, self._pid))
120