~flimm/epidermis/icon-theme-bugfix

« back to all changes in this revision

Viewing changes to epidermis/pshell_service.py

  • Committer: David D Lowe
  • Date: 2011-01-04 22:50:19 UTC
  • Revision ID: daviddlowe.flimm@gmail.com-20110104225019-uo31kb54cbxjt5vt
Tidy up code with better comments.
Deleted unused functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
                         in_signature='', out_signature='b',
35
35
                         sender_keyword='sender', connection_keyword='conn')
36
36
    def prepare(self, sender=None, conn=None):
37
 
        """Prepares this service to accept do() commands from the process
 
37
        """Prepare this service to accept do() commands from the process
38
38
        that is calling this method, identified by its PID and start-time.
39
 
        This should ask the user for a password thanks to PolicyKit."""
 
39
        This should ask the user for a password thanks to PolicyKit.
 
40
        
 
41
        Returns whether the preperation was successful or not.
 
42
        
 
43
        Keyword arguments:
 
44
        sender -- name of the sender's connection
 
45
        conn -- the dbus.connection.Connection
 
46
        """
40
47
        self.__setup_dbus_info(conn)
41
48
        pid = self.dbus_info.GetConnectionUnixProcessID(sender)
42
49
        start_time = self.__get_start_time(pid)
77
84
                        sender_keyword='sender', connection_keyword='conn')
78
85
    def do(self, command, intrShell=False, cwd=None, sender=None, conn=None):
79
86
        """Perform a command as root.
80
 
        command: a list of strings of the command to be run as root
81
 
        intrShell: if True, allows special characters such as * to be 
82
 
        interpreted by the shell
83
 
        cwd: current working directory (string)
84
 
        Returns (exitcode, output, error_output)
85
 
        Before using do, a process must call prepare() once. do() does not
86
 
        ask for PolicyKit authentication once prepare() has been called."""
 
87
        
 
88
        Returns (exitcode, output, error_output).
 
89
        
 
90
        Before using do(), a process must call prepare() once. do() does not
 
91
        ask for PolicyKit authentication once prepare() has been called.
 
92
        
 
93
        Keyword arguments:
 
94
        command -- a list of strings of the command to be run as root
 
95
        intrShell -- if True, allows special characters such as * to be 
 
96
                     interpreted by the shell
 
97
        cwd -- current working directory (string)
 
98
        sender -- name of the sender's connection
 
99
        conn -- the dbus.connection.Connection
 
100
        
 
101
        """
87
102
        self.__setup_dbus_info(conn)
88
103
        self.__verify_pids()
89
104
        pid = self.dbus_info.GetConnectionUnixProcessID(sender)
103
118
                        in_signature='', out_signature='',
104
119
                        sender_keyword='sender', connection_keyword='conn')
105
120
    def leave(self, sender=None, conn=None):
106
 
        """Processes call this method to indicate that will no longer need
107
 
        to call do(). If they do, they can call prepare() again. This method
108
 
        may end the service if no other processes need it."""
 
121
        """Delete the process from list of accepted of processes.
 
122
        
 
123
        Processes call this method to indicate that they will no longer 
 
124
        need to call do(). If they do, they can call prepare() again. 
 
125
        This method may end the service if no other processes need it.
 
126
        
 
127
        Keyword arguments:
 
128
        sender -- name of the sender's connection
 
129
        conn -- the dbus.connection.Connection
 
130
        """
109
131
        self.__setup_dbus_info(conn)
110
132
        pid = self.dbus_info.GetConnectionUnixProcessID(sender)
111
133
        
115
137
        self.__verify_pids()
116
138
    
117
139
    def __setup_dbus_info(self, conn):
 
140
        """Initialise self.dbus_info if necessary."""
118
141
        if self.dbus_info is None:
119
142
            self.dbus_info = dbus.Interface(conn.get_object('org.freedesktop.DBus',
120
143
                '/org/freedesktop/DBus/Bus', False), 'org.freedesktop.DBus')
121
144
    
122
145
    @staticmethod
123
146
    def __get_start_time(pid):
124
 
        """Returns the start-time of linux process (an integer relative
125
 
        to the kernel's start-time"""
 
147
        """Return the start-time of linux process (an integer relative
 
148
        to the kernel's start-time)
 
149
        
 
150
        Keyword arguments:
 
151
        pid -- The process ID integer of the process in question.
 
152
        
 
153
        """
126
154
        stat = open(os.path.join("/proc", str(pid), "stat"), "rb").read()
127
155
        time = stat.split(' ')[21]
128
156
        return int(time)
129
157
    
130
158
    @staticmethod
131
159
    def __verify_pid_is_epidermis(pid):
132
 
        """Checks whether the pid passed is Epidermis or not, if not,
133
 
        raises an exception"""
134
 
        cmdline = open(os.path.join("/proc",str(pid),"cmdline"),"rb").read().split(chr(0))
 
160
        """Check whether the pid passed is Epidermis or not, if not,
 
161
        raise an exception.
 
162
        
 
163
        Returns boolean.
 
164
        
 
165
        Keyword arguments:
 
166
        pid -- The process ID integer of the process in question.
 
167
        
 
168
        """
 
169
        cmdline = open(os.path.join("/proc",str(pid),"cmdline"),
 
170
            "rb").read().split(chr(0))
135
171
        if cmdline[-1] == '':
136
172
            cmdline = cmdline[:-1]
137
173
        if len(cmdline) < 2:
154
190
                   break
155
191
            logger.debug("Set python_exec to \"%s\" through whereis" % python_exec)
156
192
        if python_exec is None:
157
 
            class CantFindPythonExec(Exception):
158
 
                def __str__(self): return "Can't find python executable"
159
 
            raise CantFindPythonExec()
 
193
            raise Exception("Can't find python executable")
160
194
        # find environment variables of pid's process
161
195
        environ = open(os.path.join("/proc",str(pid),"environ"),
162
196
            "rb").read().split(chr(0))
200
234
    
201
235
    def __verify_pids(self):
202
236
        """Verify that the PIDs in self.authorisedPids still exist, and if
203
 
        no PIDs are left, end the service by calling self.mainloop.quit()"""
 
237
        no PIDs are left, end the service by calling self.mainloop.quit()
 
238
        
 
239
        """
204
240
        for pid, start_time in self.authorisedPids.items():
205
241
            if not os.path.exists(os.path.join("/proc", str(pid), "stat")):
206
242
                self.authorisedPids.pop(pid)