Exemple #1
0
def run(child_name, params=[], process_finished_callback=None, base_dir='.', protocol_class=ChildProcessProtocol):
    """
    This is another portable solution to execute a process.
    """
    if dhnio.isFrozen() and dhnio.Windows():
        progpath = os.path.abspath(os.path.join(base_dir, child_name + '.exe'))
        executable = progpath
        cmdargs = [progpath]
        cmdargs.extend(params)
    else:
        progpath = os.path.abspath(os.path.join(base_dir, child_name + '.py'))
        executable = sys.executable
        cmdargs = [executable, progpath]
        cmdargs.extend(params)
    if not os.path.isfile(executable):
        dhnio.Dprint(1, 'child_process.run ERROR %s not found' % executable)
        return None
    if not os.path.isfile(progpath):
        dhnio.Dprint(1, 'child_process.run ERROR %s not found' % progpath)
        return None
    dhnio.Dprint(6, 'child_process.run: "%s"' % (' '.join(cmdargs)))

    if dhnio.Windows():
        from twisted.internet import _dumbwin32proc
        real_CreateProcess = _dumbwin32proc.win32process.CreateProcess
        def fake_createprocess(_appName, _commandLine, _processAttributes,
                            _threadAttributes, _bInheritHandles, creationFlags,
                            _newEnvironment, _currentDirectory, startupinfo):
            import win32con
            flags = win32con.CREATE_NO_WINDOW 
            return real_CreateProcess(_appName, _commandLine,
                            _processAttributes, _threadAttributes,
                            _bInheritHandles, flags, _newEnvironment,
                            _currentDirectory, startupinfo)        
        setattr(_dumbwin32proc.win32process, 'CreateProcess', fake_createprocess)
        
    try:
        Process = reactor.spawnProcess(protocol_class(child_name), executable, cmdargs, path=base_dir)
    except:
        dhnio.Dprint(1, 'child_process.run ERROR executing: %s' % str(cmdargs))
        dhnio.DprintException()
        return None
    
    if dhnio.Windows():
        setattr(_dumbwin32proc.win32process, 'CreateProcess', real_CreateProcess)

    dhnio.Dprint(6, 'child_process.run [%s] pid=%d' % (child_name, Process.pid))
    return Process    
def run(params=[], process_finished_callback=None):
    global _BaseDir
    global _CSpaceProcess
    if _CSpaceProcess is not None:
        return None
    if dhnio.isFrozen() and dhnio.Windows():
        progpath = os.path.abspath(os.path.join(_BaseDir, 'dhnet.exe'))
        executable = progpath
        cmdargs = [progpath]
        cmdargs.extend(params)
    else:
        progpath = os.path.abspath(os.path.join(_BaseDir, 'dhnet.py'))
        executable = sys.executable
        cmdargs = [executable, progpath]
        cmdargs.extend(params)
    if not os.path.isfile(executable):
        dhnio.Dprint(1, 'transport_cspace.run ERROR %s not found' % executable)
        return None
    if not os.path.isfile(progpath):
        dhnio.Dprint(1, 'transport_cspace.run ERROR %s not found' % progpath)
        return None
    dhnio.Dprint(6, 'transport_cspace.run execute: "%s"' % (' '.join(cmdargs)))

    if dhnio.Windows():
        from twisted.internet import _dumbwin32proc
        real_CreateProcess = _dumbwin32proc.win32process.CreateProcess
        def fake_createprocess(_appName, _commandLine, _processAttributes,
                            _threadAttributes, _bInheritHandles, creationFlags,
                            _newEnvironment, _currentDirectory, startupinfo):
            import win32con
            flags = win32con.CREATE_NO_WINDOW 
            return real_CreateProcess(_appName, _commandLine,
                            _processAttributes, _threadAttributes,
                            _bInheritHandles, flags, _newEnvironment,
                            _currentDirectory, startupinfo)        
        setattr(_dumbwin32proc.win32process, 'CreateProcess', fake_createprocess)
    try:
        _CSpaceProcess = reactor.spawnProcess(CSpaceServiceProtocol(process_finished_callback), executable, cmdargs, path=_BaseDir)
    except:
        dhnio.Dprint(1, 'transport_cspace.run ERROR executing: %s' % str(cmdargs))
        dhnio.DprintException()
        return None
    
    if dhnio.Windows():
        setattr(_dumbwin32proc.win32process, 'CreateProcess', real_CreateProcess)

    dhnio.Dprint(6, 'transport_cspace.run pid=%d' % _CSpaceProcess.pid)
    return _CSpaceProcess