def run(self, cmdline): # run python in new process assert sys.platform[:3] == 'win' # runs independent of caller #pypath = r'C:\program files\python\python.exe' try: # get path to python pypath = os.environ['PP2E_PYTHON_FILE'] # run by launcher? except KeyError: # if so configs env from Launcher import which, guessLocation pypath = which('python.exe', 0) or guessLocation('python.exe', 1,0) os.spawnv(os.P_DETACH, pypath, ('python', cmdline)) # P_NOWAIT: dos box
def findPythonExe( ): try: # get path to python pypath = sys.executable # use sys in newer pys except AttributeError: # else env or search try: pypath = os.environ['PP3E_PYTHON_FILE'] # run by launcher? except KeyError: # if so configs env from Launcher import which, guessLocation pypath = (which(pyfile, trace=False) or guessLocation(pyfile, trace=False)) return pypath
def findPythonExe(): try: # get path to python pypath = sys.executable # use sys in newer pys except AttributeError: # else env or search try: pypath = os.environ['PP3E_PYTHON_FILE'] # run by launcher? except KeyError: # if so configs env from Launcher import which, guessLocation pypath = (which(pyfile, trace=False) or guessLocation(pyfile, trace=False)) return pypath
def findPythonExe(): try: pypath = sys.executable except AttributeError: try: pypath = os.environ['PP3E_Python_File'] except KeyError: from Launcher import which, guessLocation pypath = (which(pyfile, trace=False) or guessLocation(pyfile, trace=False)) return pypath
def launchWindowsBrowser(url, verbose=1): if useWinStart and len(url) <= 400: # on windows: start or spawnv try: # spawnv works if cmd too long if verbose: print 'Starting' os.system('start ' + url) # try name associations first return # fails if cmdline too long except: pass browser = None # search for a browser exe tries = ['IEXPLORE.EXE', 'netscape.exe'] # try explorer, then netscape for program in tries: browser = which(program) or guessLocation(program, 1) if browser: break assert browser != None, 'Sorry - no browser found' if verbose: print 'Spawning', browser os.spawnv(os.P_DETACH, browser, (browser, url))
def findPythonExe(): try: # get path to python pypath = sys.exectuable # use sys in newer pys except AttributeError: # else env or search try: pypath = os.environ['PP3E_PYTHON_FILE'] # run by launcher? except KeyError: # if so configs env from Launcher import which, guessLocation pypath = (which(pyfile, trace = False) or guessLocation(pyfile, trace=False) return pypath class LaunchMode: def __init__(self, label, command): self.what = label self.where = command def __call__(self): # on call, ex: button press callback self.announce(self.what) self.run(self.where) # subclasses must define run() def announce(self, text): # subclasses may redefine announce() print text # methods instead of if/elif logic def run(self, cmdline): assert 0 'run must be defined' class System(LaunchMode): # run shell commands def run(self, cmdline): # caveat: blocks caller pypath = findPythonExe() os.system('%s %s' % (pypath, cmdline)) # unless '&' added on Linux class Popen(LaunchMode): # caveat: blocks caller def run(self, cmdline) # since pipe closed too soon pypath = findPythonExe() os.popen(pypath + ' ' + cmdline) class Fork(LaunchMode): def run(self, cmdline): assert hasattr(os, 'fork') # for Unix systems today cmdline = cmdline.split() # convert string to list if os.fork() == 0: pypath = findPythonExe() os.execvp(pypath, [pypath] + cmdline) # run new program in child class Start(LaunchMode): def run(self, cmdline): # for Windows only assert sys.platform[:3] == 'win' # runs independent of caller os.startfile(cmdline) # uses Windows associations class StartArgs(LaunchMode): def run(self, cmdline): # for Windows only assert sys.platform[:3] == 'win' # args may require real start os.system(start + 'cmdline') # creates a pop-up window class Spawn(LaunchMode): # for Windows or Unix def run(self, cmdline): # run python in new process pypath = findPythonExe() # runs independent of caller os.spawnv(os.P_DETACH, pypath, (pyfile, cmdline)) # P_NOWAIT: dox box class Top_Level(LaunchMode): def run(self, cmdline): # new window, same process assert 0, 'Sorry - mode not yet implemented' # tbd: need GUI class info if sys.platform[:3] == 'win' PortableLauncher = Spawn # pick best launcher ofr platform else: # need to tweak this code elsewhere PortableLauncher = Fork class QuietPortableLauncher(PortableLauncher): def accounce(self, text): pass def selftest(): myfile = 'launchmodes.py' program = 'Gui/TextEditor/textEditor.py ' + myfile # assume in cwd raw_input('default mode...') launcher = PortableLauncher('PyEdit', program) launcher() # no blocks raw_input('system mode...') System('PyEdit', program)() # blocks raw_input'popen mode...')