home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2523 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.5 KB  |  115 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import win32api
  5. import win32process
  6. import win32security
  7. import win32event
  8. import win32con
  9. import msvcrt
  10. import win32gui
  11.  
  12. def logonUser(loginString):
  13.     (domain, user, passwd) = loginString.split('\n')
  14.     return win32security.LogonUser(user, domain, passwd, win32con.LOGON32_LOGON_INTERACTIVE, win32con.LOGON32_PROVIDER_DEFAULT)
  15.  
  16.  
  17. class Process:
  18.     
  19.     def __init__(self, cmd, login = None, hStdin = None, hStdout = None, hStderr = None, show = 1, xy = None, xySize = None, desktop = None):
  20.         si = win32process.STARTUPINFO()
  21.         si.dwFlags = win32con.STARTF_USESTDHANDLES ^ win32con.STARTF_USESHOWWINDOW
  22.         if hStdin is None:
  23.             si.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)
  24.         else:
  25.             si.hStdInput = hStdin
  26.         if hStdout is None:
  27.             si.hStdOutput = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE)
  28.         else:
  29.             si.hStdOutput = hStdout
  30.         if hStderr is None:
  31.             si.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
  32.         else:
  33.             si.hStdError = hStderr
  34.         si.wShowWindow = show
  35.         if xy is not None:
  36.             (si.dwX, si.dwY) = xy
  37.             si.dwFlags ^= win32con.STARTF_USEPOSITION
  38.         
  39.         if xySize is not None:
  40.             (si.dwXSize, si.dwYSize) = xySize
  41.             si.dwFlags ^= win32con.STARTF_USESIZE
  42.         
  43.         if desktop is not None:
  44.             si.lpDesktop = desktop
  45.         
  46.         procArgs = (None, cmd, None, None, 1, win32process.CREATE_NEW_CONSOLE, None, None, si)
  47.         if login is not None:
  48.             hUser = logonUser(login)
  49.             win32security.ImpersonateLoggedOnUser(hUser)
  50.             procHandles = win32process.CreateProcessAsUser(hUser, *procArgs)
  51.             win32security.RevertToSelf()
  52.         else:
  53.             procHandles = win32process.CreateProcess(*procArgs)
  54.         (self.hProcess, self.hThread, self.PId, self.TId) = procHandles
  55.  
  56.     
  57.     def wait(self, mSec = None):
  58.         if mSec is None:
  59.             mSec = win32event.INFINITE
  60.         
  61.         return win32event.WaitForSingleObject(self.hProcess, mSec)
  62.  
  63.     
  64.     def kill(self, gracePeriod = 5000):
  65.         win32gui.EnumWindows(self.__close__, 0)
  66.         if self.wait(gracePeriod) != win32event.WAIT_OBJECT_0:
  67.             win32process.TerminateProcess(self.hProcess, 0)
  68.             win32api.Sleep(100)
  69.         
  70.  
  71.     
  72.     def __close__(self, hwnd, dummy):
  73.         (TId, PId) = win32process.GetWindowThreadProcessId(hwnd)
  74.         if PId == self.PId:
  75.             win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
  76.         
  77.  
  78.     
  79.     def exitCode(self):
  80.         return win32process.GetExitCodeProcess(self.hProcess)
  81.  
  82.  
  83.  
  84. def run(cmd, mSec = None, stdin = None, stdout = None, stderr = None, **kw):
  85.     if stdin is not None:
  86.         kw['hStdin'] = msvcrt.get_osfhandle(stdin.fileno())
  87.     
  88.     if stdout is not None:
  89.         kw['hStdout'] = msvcrt.get_osfhandle(stdout.fileno())
  90.     
  91.     if stderr is not None:
  92.         kw['hStderr'] = msvcrt.get_osfhandle(stderr.fileno())
  93.     
  94.     child = Process(cmd, **kw)
  95.     if child.wait(mSec) != win32event.WAIT_OBJECT_0:
  96.         child.kill()
  97.         raise WindowsError, 'process timeout exceeded'
  98.     child.wait(mSec) != win32event.WAIT_OBJECT_0
  99.     return child.exitCode()
  100.  
  101. if __name__ == '__main__':
  102.     print 'Testing winprocess.py...'
  103.     import tempfile
  104.     timeoutSeconds = 15
  105.     cmdString = 'REM      Test of winprocess.py piping commands to a shell.\r\nREM      This window will close in %d seconds.\r\nvol\r\nnet user\r\n_this_is_a_test_of_stderr_\r\n' % timeoutSeconds
  106.     cmd = tempfile.TemporaryFile()
  107.     out = tempfile.TemporaryFile()
  108.     cmd.write(cmdString)
  109.     cmd.seek(0)
  110.     print 'CMD.EXE exit code:', run('cmd.exe', show = 0, stdin = cmd, stdout = out, stderr = out)
  111.     cmd.close()
  112.     print 'NOTEPAD exit code:', run('notepad.exe %s' % out.file.name, show = win32con.SW_MAXIMIZE, mSec = timeoutSeconds * 1000)
  113.     out.close()
  114.  
  115.