1 __doc__ = """GNUmed general tools."""
2
3
4
5
6 __version__ = "$Revision: 1.13 $"
7 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
8 __license__ = "GPL (details at http://www.gnu.org)"
9
10
11
12 import os, sys, logging
13
14
15 _log = logging.getLogger('gm.shell')
16 _log.info(__version__)
17
18
20 _log.debug('detecting [%s]', binary)
21
22
23 if os.access(binary, os.X_OK):
24 return (True, binary)
25
26
27 cmd = 'which %s' % binary
28 pipe = os.popen(cmd.encode(sys.getfilesystemencoding()), "r")
29 result = pipe.readline()
30 ret_code = pipe.close()
31 if ret_code is not None:
32 _log.debug('[%s] failed, exit code: %s', cmd, ret_code)
33 else:
34 result = result.strip('\r\n')
35 _log.debug('[%s] returned: %s', cmd, result)
36
37 if os.access(result, os.X_OK):
38 return (True, result)
39 else:
40 _log.debug('[%s] not detected with "which"', binary)
41
42 tmp = binary.lstrip()
43
44 if tmp.startswith('wine'):
45
46 tmp = tmp[4:].strip().strip('"')
47
48
49 if os.access(tmp, os.R_OK):
50 _log.debug('wine call with UNIX path')
51 return (True, binary)
52
53
54 cmd = 'winepath -u "%s"' % tmp
55 pipe = os.popen(cmd.encode(sys.getfilesystemencoding()), "r")
56 result = pipe.readline()
57 ret_code = pipe.close()
58 if ret_code is not None:
59 _log.debug('winepath failed')
60 else:
61 result = result.strip('\r\n')
62 if os.access(result, os.R_OK):
63 _log.debug('wine call with Windows path')
64 return (True, binary)
65 else:
66 _log.warning('"winepath -u %s" returned [%s] but the UNIX path is not verifiable', tmp, result)
67
68 return (False, None)
69
71
72 found = False
73 binary = None
74
75 for cmd in binaries:
76 found, binary = detect_external_binary(binary = cmd)
77 if found:
78 break
79
80 return (found, binary)
81
83 """Runs a command in a subshell via standard-C system().
84
85 <command>
86 The shell command to run including command line options.
87 <blocking>
88 This will make the code *block* until the shell command exits.
89 It will likely only work on UNIX shells where "cmd &" makes sense.
90 """
91 _log.debug('shell command >>>%s<<<', command)
92 _log.debug('blocking: %s', blocking)
93
94
95 command = command.strip()
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 if blocking is True:
116 if command[-2:] == ' &':
117 command = command[:-2]
118 elif blocking is False:
119 if command[-2:] != ' &':
120 command += ' &'
121
122 _log.info('running shell command >>>%s<<<', command)
123
124 ret_val = os.system(command.encode(sys.getfilesystemencoding()))
125 _log.debug('os.system() returned: [%s]', ret_val)
126
127 exited_normally = False
128 _log.debug('exited via exit(): %s', os.WIFEXITED(ret_val))
129 if os.WIFEXITED(ret_val):
130 _log.debug('exit code: [%s]', os.WEXITSTATUS(ret_val))
131 exited_normally = (os.WEXITSTATUS(ret_val) == 0)
132 _log.debug('normal exit: %s', exited_normally)
133 _log.debug('dumped core: %s', os.WCOREDUMP(ret_val))
134 _log.debug('stopped by signal: %s', os.WIFSIGNALED(ret_val))
135 if os.WIFSIGNALED(ret_val):
136 _log.debug('STOP signal was: [%s]', os.STOPSIG(ret_val))
137 _log.debug('TERM signal was: [%s]', os.TERMSIG(ret_val))
138
139 return exited_normally
140
142
143 found, binary = find_first_binary(binaries = binaries)
144
145 if not found:
146 if run_last_one_anyway:
147 binary = binaries[-1]
148 else:
149 _log.warning('cannot find any of: %s', binaries)
150 return False
151
152 return run_command_in_shell(command = '%s %s' % (binary, args), blocking=False)
153
154
155
156 if __name__ == '__main__':
157
158 if len(sys.argv) > 1 and sys.argv[1] == u'test':
159
160 logging.basicConfig(level = logging.DEBUG)
161
162
164 found, path = detect_external_binary(binary = sys.argv[2])
165 if found:
166 print "found as:", path
167 else:
168 print sys.argv[2], "not found"
169
171 print "-------------------------------------"
172 print "running:", sys.argv[2]
173 if run_command_in_shell(command=sys.argv[2], blocking=True):
174 print "-------------------------------------"
175 print "success"
176 else:
177 print "-------------------------------------"
178 print "failure, consult log"
179
180
181
182 test_detect_external_binary()
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227