summaryrefslogtreecommitdiffstats
path: root/mountconfig/SimpleCommandRunner.py
blob: a6767b5c9099fa7f52b8e1f2ca562e653c353196 (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
68
69
###########################################################################
# SimpleCommandRunner.py - description                                    #
# ------------------------------                                          #
# begin     : Tue May 17 2005                                             #
# copyright : (C) 2005 by Simon Edwards                                   #
# email     : simon@simonzone.com                                         #
#                                                                         #
###########################################################################
#                                                                         #
#   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.                                   #
#                                                                         #
###########################################################################
from PyTQt.qt import *
from tdecore import *
import locale

debug = False
#debug = True


class SimpleCommandRunner(TQObject):
    ########################################################################
    def __init__(self):
        TQObject.__init__(self)

    ########################################################################
    def run(self,cmdlist,STDOUT_only=False):
        """Run the given command and return the result.

        Keyword arguments:
        cmdlist - Command and arguments. Given as a list of strings. The first item is
                  the executable.
        STDOUT_only - Do not return STDERR in the output stream.

        Returns a tuple (rc,output). rc is the numeric return code from
        the command, or None if the command couldn't be started. output
        is the output from stdout and stderr.
        """
        global debug
        if debug: print(cmdlist)
        self.STDOUT_only = STDOUT_only
        self.output = ""
        proc = TDEProcess()
        proc.setEnvironment("LANG","US")
        proc.setEnvironment("LC_ALL","US")
        self.connect(proc,SIGNAL("receivedStdout(TDEProcess *,char *,int)"),self.slotStdout)        
        self.connect(proc,SIGNAL("receivedStderr(TDEProcess *,char *,int)"),self.slotStderr)
        proc.setArguments(cmdlist)
        rc = None
        if proc.start(proc.Block,proc.AllOutput)==True:
            if proc.normalExit():
                rc = proc.exitStatus()
        return (rc,self.output)

    ########################################################################
    def slotStdout(self,proc,buffer,buflen):
        global debug 
        if debug: print("slotStdout() |"+buffer+"|")
        self.output += buffer.decode(locale.getpreferredencoding())

    ########################################################################
    def slotStderr(self,proc,buffer,buflen):
        global debug
        if debug: print("slotStderr() |"+buffer+"|")
        if not self.STDOUT_only:
            self.output += buffer.decode(locale.getpreferredencoding())