applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / netns / netnsapplication.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.resources.netns.netnsbase import NetNSBase
21 from nepi.execution.resource import clsinit_copy, ResourceState
22
23 import shlex
24
25 @clsinit_copy
26 class NetNSApplication(NetNSBase):
27     _rtype = "netns::Application"
28
29     def __init__(self, ec, guid):
30         super(NetNSApplication, self).__init__(ec, guid)
31         self._traces = dict()
32
33     @classmethod
34     def _register_attributes(cls):
35         command = Attribute("command", "Command to execute", flags=Flags.Design)
36         cls._register_attribute(command)
37
38     @property
39     def emulation(self):
40         return self.node.emulation
41
42     @property
43     def node(self):
44         from nepi.resources.netns.netnsnode import NetNSNode
45         node = self.get_connected(NetNSNode.get_rtype())
46
47         if not node: 
48             msg = "Route not connected to Node!!"
49             self.error(msg)
50             raise RuntimeError(msg)
51
52         return node[0]
53
54     @property
55     def _rms_to_wait(self):
56         return [self.node]
57
58     def do_start(self):
59         if self.emulation.state < ResourceState.STARTED:
60             self.debug("---- RESCHEDULING START ----" )
61             self.ec.schedule(self.reschedule_delay, self.start)
62         else:
63             self._configure_traces()
64
65             command = shlex.split(self.get("command"))
66             stdout = self._traces["stdout"]
67             stderr = self._traces["stderr"]
68             self._uuid = self.emulation.invoke(self.node.uuid, 
69                     "Popen", command, stdout = stdout, 
70                     stderr = stderr)
71
72             super(NetNSApplication, self).do_start()
73             self._start_time = self.emulation.start_time
74
75     def _configure_traces(self):
76         stdout = "%s/%d.stdout" % (self.emulation.run_home, self.guid)
77         stderr = "%s/%d.stderr" % (self.emulation.run_home, self.guid)
78         self._trace_filename["stdout"] = stdout 
79         self._trace_filename["stderr"] = stderr
80         self._traces["stdout"] = self.emulation.create("open", stdout, "w")
81         self._traces["stderr"] = self.emulation.create("open", stderr, "w")
82
83     @property
84     def state(self):
85         if self._state == ResourceState.STARTED:
86             retcode = self.emulation.invoke(self.uuid, "poll")
87    
88             if retcode is not None:
89                 if retcode == 0:
90                     self.set_stopped()
91                 else:
92                     out = ""
93                     msg = " Failed to execute command '%s'" % self.get("command")
94                     err = self.trace("stderr")
95                     self.error(msg, out, err)
96                     self.do_fail()
97
98         return self._state
99