Package Gnumed :: Package pycommon :: Module gmShellAPI
[frames] | no frames]

Source Code for Module Gnumed.pycommon.gmShellAPI

  1  __doc__ = """GNUmed general tools.""" 
  2   
  3  #=========================================================================== 
  4  __version__ = "$Revision: 1.13 $" 
  5  __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>" 
  6  __license__ = "GPL v2 or later (details at http://www.gnu.org)" 
  7   
  8   
  9  # stdlib 
 10  import os 
 11  import sys 
 12  import logging 
 13  import subprocess 
 14  import shlex 
 15   
 16   
 17  _log = logging.getLogger('gm.shell') 
 18  _log.info(__version__) 
 19   
 20  #=========================================================================== 
21 -def is_cmd_in_path(cmd=None):
22 23 _log.debug('cmd: [%s]', cmd) 24 dirname = os.path.dirname(cmd) 25 _log.debug('dir: [%s]', dirname) 26 if dirname != u'': 27 _log.info('command with full or relative path, not searching in PATH for binary') 28 return (None, None) 29 30 env_paths = os.environ['PATH'] 31 _log.debug('${PATH}: %s', env_paths) 32 for path in env_paths.split(os.pathsep): 33 candidate = os.path.join(path, cmd).encode(sys.getfilesystemencoding()) 34 if os.access(candidate, os.X_OK): 35 _log.debug('found [%s]', candidate) 36 return (True, candidate.decode(sys.getfilesystemencoding())) 37 else: 38 _log.debug('not found: %s', candidate) 39 40 _log.debug('command not found in PATH') 41 42 return (False, None)
43 #===========================================================================
44 -def is_executable_by_wine(cmd=None):
45 46 if not cmd.startswith('wine'): 47 _log.debug('not a WINE call: %s', cmd) 48 return (False, None) 49 50 exe_path = cmd.encode(sys.getfilesystemencoding()) 51 52 exe_path = exe_path[4:].strip().strip('"').strip() 53 # [wine "/standard/unix/path/to/binary.exe"] ? 54 if os.access(exe_path, os.R_OK): 55 _log.debug('WINE call with UNIX path: %s', exe_path) 56 return (True, cmd) 57 58 # detect [winepath] 59 found, full_winepath_path = is_cmd_in_path(cmd = r'winepath') 60 if not found: 61 _log.error('[winepath] not found, cannot check WINE call for Windows path conformance: %s', exe_path) 62 return (False, None) 63 64 # [wine "drive:\a\windows\path\to\binary.exe"] ? 65 cmd_line = r'%s -u "%s"' % ( 66 full_winepath_path.encode(sys.getfilesystemencoding()), 67 exe_path 68 ) 69 _log.debug('converting Windows path to UNIX path: %s' % cmd_line) 70 cmd_line = shlex.split(cmd_line) 71 try: 72 winepath = subprocess.Popen ( 73 cmd_line, 74 stdout = subprocess.PIPE, 75 stderr = subprocess.PIPE, 76 universal_newlines = True 77 ) 78 except OSError: 79 _log.exception('cannot run <winepath>') 80 return (False, None) 81 82 stdout, stderr = winepath.communicate() 83 full_path = stdout.strip('\r\n') 84 _log.debug('UNIX path: %s', full_path) 85 86 if winepath.returncode != 0: 87 _log.error('<winepath -u> returned [%s], failed to convert path', winepath.returncode) 88 return (False, None) 89 90 if os.access(full_path, os.R_OK): 91 _log.debug('WINE call with Windows path') 92 return (True, cmd) 93 94 _log.warning('Windows path [%s] not verifiable under UNIX: %s', exe_path, full_path) 95 return (False, None)
96 #===========================================================================
97 -def detect_external_binary(binary=None):
98 _log.debug('searching for [%s]', binary) 99 100 binary = binary.lstrip() 101 102 # is it a sufficiently qualified, directly usable, explicit path ? 103 if os.access(binary, os.X_OK): 104 _log.debug('found: executable explicit path') 105 return (True, binary) 106 107 # can it be found in PATH ? 108 found, full_path = is_cmd_in_path(cmd = binary) 109 if found: 110 if os.access(full_path, os.X_OK): 111 _log.debug('found: executable in ${PATH}') 112 return (True, full_path) 113 114 # does it seem to be a call via WINE ? 115 is_wine_call, full_path = is_executable_by_wine(cmd = binary) 116 if is_wine_call: 117 _log.debug('found: is valid WINE call') 118 return (True, full_path) 119 120 # maybe we can be a bit smart about Windows ? 121 # try .exe (but not if already .bat or .exe) 122 if not (binary.endswith('.exe') or binary.endswith('.bat')): 123 _log.debug('re-testing with [.exe] appended') 124 exe_binary = binary + r'.exe' 125 found_dot_exe_binary, full_path = detect_external_binary(binary = exe_binary) 126 if found_dot_exe_binary: 127 return (True, full_path) 128 # not found with .exe, so try .bat: 129 _log.debug('re-testing with [.bat] appended') 130 bat_binary = binary + r'.bat' 131 found_bat_binary, full_path = detect_external_binary(binary = bat_binary) 132 if found_bat_binary: 133 return (True, full_path) 134 135 return (False, None)
136 #===========================================================================
137 -def find_first_binary(binaries=None):
138 found = False 139 binary = None 140 141 for cmd in binaries: 142 _log.debug('looking for [%s]', cmd) 143 if cmd is None: 144 continue 145 found, binary = detect_external_binary(binary = cmd) 146 if found: 147 break 148 149 return (found, binary)
150 #===========================================================================
151 -def run_command_in_shell(command=None, blocking=False, acceptable_return_codes=None):
152 """Runs a command in a subshell via standard-C system(). 153 154 <command> 155 The shell command to run including command line options. 156 <blocking> 157 This will make the code *block* until the shell command exits. 158 It will likely only work on UNIX shells where "cmd &" makes sense. 159 """ 160 if acceptable_return_codes is None: 161 acceptable_return_codes = [0] 162 163 _log.debug('shell command >>>%s<<<', command) 164 _log.debug('blocking: %s', blocking) 165 _log.debug('acceptable return codes: %s', str(acceptable_return_codes)) 166 167 # FIXME: command should be checked for shell exploits 168 command = command.strip() 169 170 # what the following hack does is this: the user indicated 171 # whether she wants non-blocking external display of files 172 # - the real way to go about this is to have a non-blocking command 173 # in the line in the mailcap file for the relevant mime types 174 # - as non-blocking may not be desirable when *not* displaying 175 # files from within GNUmed the really right way would be to 176 # add a "test" clause to the non-blocking mailcap entry which 177 # yields true if and only if GNUmed is running 178 # - however, this is cumbersome at best and not supported in 179 # some mailcap implementations 180 # - so we allow the user to attempt some control over the process 181 # from within GNUmed by setting a configuration option 182 # - leaving it None means to use the mailcap default or whatever 183 # was specified in the command itself 184 # - True means: tack " &" onto the shell command if necessary 185 # - False means: remove " &" from the shell command if its there 186 # - all this, of course, only works in shells which support 187 # detaching jobs with " &" (so, most POSIX shells) 188 if blocking is True: 189 if command[-2:] == ' &': 190 command = command[:-2] 191 elif blocking is False: 192 if command[-2:] != ' &': 193 command += ' &' 194 195 _log.info('running shell command >>>%s<<<', command) 196 # FIXME: use subprocess.Popen() 197 ret_val = os.system(command.encode(sys.getfilesystemencoding())) 198 _log.debug('os.system() returned: [%s]', ret_val) 199 200 exited_normally = False 201 202 if not hasattr(os, 'WIFEXITED'): 203 _log.error('platform does not support exit status differentiation') 204 if ret_val in acceptable_return_codes: 205 _log.info('os.system() return value contained in acceptable return codes') 206 _log.info('continuing and hoping for the best') 207 return True 208 return exited_normally 209 210 _log.debug('exited via exit(): %s', os.WIFEXITED(ret_val)) 211 if os.WIFEXITED(ret_val): 212 _log.debug('exit code: [%s]', os.WEXITSTATUS(ret_val)) 213 exited_normally = (os.WEXITSTATUS(ret_val) in acceptable_return_codes) 214 _log.debug('normal exit: %s', exited_normally) 215 _log.debug('dumped core: %s', os.WCOREDUMP(ret_val)) 216 _log.debug('stopped by signal: %s', os.WIFSIGNALED(ret_val)) 217 if os.WIFSIGNALED(ret_val): 218 _log.debug('STOP signal was: [%s]', os.STOPSIG(ret_val)) 219 _log.debug('TERM signal was: [%s]', os.TERMSIG(ret_val)) 220 221 return exited_normally
222 #===========================================================================
223 -def run_first_available_in_shell(binaries=None, args=None, blocking=False, run_last_one_anyway=False, acceptable_return_codes=None):
224 225 found, binary = find_first_binary(binaries = binaries) 226 227 if not found: 228 _log.warning('cannot find any of: %s', binaries) 229 if run_last_one_anyway: 230 binary = binaries[-1] 231 _log.debug('falling back to trying to run [%s] anyway', binary) 232 else: 233 return False 234 235 return run_command_in_shell(command = '%s %s' % (binary, args), blocking = blocking, acceptable_return_codes = acceptable_return_codes)
236 #=========================================================================== 237 # main 238 #--------------------------------------------------------------------------- 239 if __name__ == '__main__': 240 241 if len(sys.argv) < 2: 242 sys.exit() 243 244 if sys.argv[1] != u'test': 245 sys.exit() 246 247 logging.basicConfig(level = logging.DEBUG) 248 #---------------------------------------------------------
249 - def test_detect_external_binary():
250 found, path = detect_external_binary(binary = sys.argv[2]) 251 if found: 252 print "found as:", path 253 else: 254 print sys.argv[2], "not found"
255 #---------------------------------------------------------
256 - def test_run_command_in_shell():
257 print "-------------------------------------" 258 print "running:", sys.argv[2] 259 if run_command_in_shell(command=sys.argv[2], blocking=True): 260 print "-------------------------------------" 261 print "success" 262 else: 263 print "-------------------------------------" 264 print "failure, consult log"
265 #---------------------------------------------------------
266 - def test_is_cmd_in_path():
267 print is_cmd_in_path(cmd = sys.argv[2])
268 #---------------------------------------------------------
269 - def test_is_executable_by_wine():
270 print is_executable_by_wine(cmd = sys.argv[2])
271 #--------------------------------------------------------- 272 test_run_command_in_shell() 273 #test_detect_external_binary() 274 #test_is_cmd_in_path() 275 #test_is_executable_by_wine() 276 277 #=========================================================================== 278