]>
git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - pkgs/core/pyfire/src/executil.py
2 # executil.py - generic utility functions for executing programs
4 # Erik Troan <ewt@redhat.com>
6 # Copyright 1999-2002 Red Hat, Inc.
8 # This software may be freely redistributed under the terms of the GNU
9 # library public license.
11 # You should have received a copy of the GNU Library Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 def getfd(filespec
, readOnly
= 0):
22 if type(filespec
) == types
.IntType
:
25 filespec
= "/dev/null"
27 flags
= os
.O_RDWR | os
.O_CREAT
30 return os
.open(filespec
, flags
)
32 def execWithRedirect(command
, argv
, stdin
= 0, stdout
= 1, stderr
= 2,
33 searchPath
= 0, root
= '/', newPgrp
= 0, ignoreTermSigs
= 0):
36 stdout
= getfd(stdout
)
39 stdout
= getfd(stdout
)
40 stderr
= getfd(stderr
)
44 if (root
and root
!= '/'):
49 signal
.signal(signal
.SIGTSTP
, signal
.SIG_IGN
)
50 signal
.signal(signal
.SIGINT
, signal
.SIG_IGN
)
52 if type(stdin
) == type("a"):
53 stdin
= os
.open(stdin
, os
.O_RDONLY
)
54 if type(stdout
) == type("a"):
55 stdout
= os
.open(stdout
, os
.O_RDWR
)
56 if type(stderr
) == type("a"):
57 stderr
= os
.open(stderr
, os
.O_RDWR
)
71 os
.execvp(command
, argv
)
73 os
.execv(command
, argv
)
78 os
.setpgid(childpid
, childpid
)
79 oldPgrp
= os
.tcgetpgrp(0)
80 os
.tcsetpgrp(0, childpid
)
84 (pid
, status
) = os
.waitpid(childpid
, 0)
85 except OSError, (errno
, msg
):
86 print __name__
, "waitpid:", msg
89 os
.tcsetpgrp(0, oldPgrp
)
93 ## Run an external program and capture standard out.
94 # @param command The command to run.
95 # @param argv A list of arguments.
96 # @param stdin The file descriptor to read stdin from.
97 # @param stderr The file descriptor to redirect stderr to.
98 # @param root The directory to chroot to before running command.
99 # @return The output of command from stdout.
100 def execWithCapture(command
, argv
, stdin
= 0, stderr
= 2, root
='/'):
105 if type(stdin
) == type("string"):
106 if os
.access(stdin
, os
.R_OK
):
110 if type(stderr
) == type("string"):
111 stderr
= open(stderr
, "w")
114 pipe
= subprocess
.Popen([command
] + argv
, stdin
=stdin
,
115 stdout
=subprocess
.PIPE
,
116 stderr
=subprocess
.STDOUT
,
117 preexec_fn
=chroot
, cwd
=root
)
118 except OSError, (errno
, msg
):
119 log
.error ("Error running " + command
+ ": " + msg
)
120 raise RuntimeError, "Error running " + command
+ ": " + msg
122 rc
= pipe
.stdout
.read()
126 def execWithCaptureStatus(command
, argv
, searchPath
= 0, root
= '/', stdin
= 0,
127 catchfd
= 1, closefd
= -1):
129 if not os
.access (root
+ command
, os
.X_OK
):
130 raise RuntimeError, command
+ " can not be run"
132 (read
, write
) = os
.pipe()
136 if (root
and root
!= '/'): os
.chroot (root
)
137 if isinstance(catchfd
, tuple):
141 os
.dup2(write
, catchfd
)
153 os
.execvp(command
, argv
)
155 os
.execv(command
, argv
)
164 select
.select([read
], [], [])
165 s
= os
.read(read
, 1000)
173 (pid
, status
) = os
.waitpid(childpid
, 0)
174 except OSError, (errno
, msg
):
175 print __name__
, "waitpid:", msg
177 if os
.WIFEXITED(status
) and (os
.WEXITSTATUS(status
) == 0):
178 status
= os
.WEXITSTATUS(status
)
184 def execWithCaptureErrorStatus(command
, argv
, searchPath
= 0, root
= '/',
185 stdin
= 0, catchfd
= 1, catcherrfd
= 2, closefd
= -1):
187 if not os
.access (root
+ command
, os
.X_OK
):
188 raise RuntimeError, command
+ " can not be run"
190 (read
, write
) = os
.pipe()
191 (read_err
,write_err
) = os
.pipe()
195 if (root
and root
!= '/'): os
.chroot (root
)
196 if isinstance(catchfd
, tuple):
200 os
.dup2(write
, catchfd
)
204 if isinstance(catcherrfd
, tuple):
205 for fd
in catcherrfd
:
206 os
.dup2(write_err
, fd
)
208 os
.dup2(write_err
, catcherrfd
)
220 os
.execvp(command
, argv
)
222 os
.execv(command
, argv
)
234 select
.select([read
], [], [])
235 s
= os
.read(read
, 1000)
236 t
= os
.read(read_err
, 1000)
246 (pid
, status
) = os
.waitpid(childpid
, 0)
247 except OSError, (errno
, msg
):
248 print __name__
, "waitpid:", msg
250 if os
.WIFEXITED(status
) and (os
.WEXITSTATUS(status
) == 0):
251 status
= os
.WEXITSTATUS(status
)
255 return (rc
, rc_err
, status
)