def run(self):
interpreter = sys.executable
if verify_requirements() is None:
print("You don't have the requirements to start Red. "
"Install them from the launcher.")
if not INTERACTIVE_MODE:
exit(1)
if self.autostart is True:
cmd = (interpreter, "launcher.py", "--start", "--auto-restart")
else:
cmd = (interpreter, "launcher.py", "--start")
try:
code = subprocess.call(cmd, creationflags=subprocess.CREATE_NEW_CONSOLE, shell=False)
except KeyboardInterrupt:
code = 0
print("Red has been terminated. Exit code: %d" % code)
python类CREATE_NEW_CONSOLE的实例源码
def run(self):
interpreter = sys.executable
if interpreter is None:
print("Python interpreter not found.")
return
args = [
interpreter, "-m",
"pip", "install",
"--upgrade", "pip"
]
code = subprocess.call(args, creationflags=subprocess.CREATE_NEW_CONSOLE, shell=False)
if code == 0:
print("\nPip has been updated.")
else:
print("\nAn error occurred and pip might not have been updated.")
def run(self):
subprocess.Popen(("git", "pull", "--ff-only"),
creationflags=subprocess.CREATE_NEW_CONSOLE, shell=False)
def run(self):
remove_reqs_readonly()
interpreter = sys.executable
if interpreter is None:
print("Python interpreter not found.")
return
txt = REQS_TXT if self.audio else REQS_NO_AUDIO_TXT
args = [
interpreter, "-m",
"pip", "install",
"--upgrade",
"--target", REQS_DIR,
"-r", txt
]
if IS_MAC: # --target is a problem on Homebrew. See PR #552
args.remove("--target")
args.remove(REQS_DIR)
code = subprocess.call(args, creationflags=subprocess.CREATE_NEW_CONSOLE, shell=False)
if code == 0:
print("Requirements setup completed.")
else:
print("An error occurred and the requirements "
"setup might not be completed. Consult the docs.")
def run(self):
if self.reqs:
try:
shutil.rmtree(REQS_DIR, onerror=remove_readonly)
print("Installed local packages have been wiped.")
except FileNotFoundError:
pass
except Exception as e:
print("An error occurred when trying to remove installed "
"requirements: {}".format(e))
if self.data:
try:
shutil.rmtree("data", onerror=remove_readonly)
print("'data' folder has been wiped.")
except FileNotFoundError:
pass
except Exception as e:
print("An error occurred when trying to remove the 'data' folder: "
"{}".format(e))
if self.cogs:
try:
shutil.rmtree("cogs", onerror=remove_readonly)
print("'cogs' folder has been wiped.")
except FileNotFoundError:
pass
except Exception as e:
print("An error occurred when trying to remove the 'cogs' folder: "
"{}".format(e))
if self.git_reset:
code = subprocess.call(("git", "reset", "--hard"),
creationflags=subprocess.CREATE_NEW_CONSOLE, shell=False)
if code == 0:
print("Red has been restored to the last local commit.")
else:
print("The repair has failed.")
def write_windows_console(self, *args):
retcode = subprocess.call(args,
# use a new console to not flood the test output
creationflags=subprocess.CREATE_NEW_CONSOLE,
# use a shell to hide the console window (SW_HIDE)
shell=True)
self.assertEqual(retcode, 0)
def start_server(self):
if self.android is None:
if self.enabled and self.lang.voice is not None:
# voices = ["-s 190 -a 100 -p 75 -ven+m1 ", "-s 170 -a 100 -p 80 -ven+m2 ","-s 175 -a 100 -p 80 -ven+m3 ","-s 190 -a 100 -p 60 -ven+f1 ","-s 170 -a 100 -p 75 -ven+f2 ","-s 170 -a 100 -p 80 -ven+m2 "]
cmd = ['espeak']
cmd.extend(self.lang.voice)
try:
# IS_WIN32 = 'win32' in str(sys.platform).lower() #maybe sys.platform is more secure
is_win = platform.system() == "Windows"
if is_win:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
kwargs = {}
kwargs['startupinfo'] = startupinfo
# self.process = subprocess.Popen(cmd, shell=True, bufsize=0, close_fds=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
self.process = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, startupinfo=startupinfo)
else:
self.process = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.started = True
except:
self.enabled = False
self.started = False
print(
"eduActiv8: You may like to install espeak to get some extra functionality, however this is not required to successfully use the game.")
# stdout and stderr only used to hide the messages from terminal
else:
self.process = None
def subprocess(program, cwd, environment, newlines, joined, shell=True, show=False):
"""Create a subprocess using subprocess.Popen."""
stderr = subprocess.STDOUT if joined else subprocess.PIPE
if os.name == 'nt':
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = 0 if show else subprocess.SW_HIDE
cf = subprocess.CREATE_NEW_CONSOLE if show else 0
return subprocess.Popen(program, universal_newlines=newlines, shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, close_fds=False, startupinfo=si, creationflags=cf, cwd=cwd, env=environment)
return subprocess.Popen(program, universal_newlines=newlines, shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, close_fds=True, cwd=cwd, env=environment)
def __subprocess_call(self, *args, **kwargs):
if self.IS_WIN32:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
kwargs['startupinfo'] = startupinfo
retcode = subprocess.call(*args, **kwargs)
return retcode
def write_windows_console(self, *args):
retcode = subprocess.call(args,
# use a new console to not flood the test output
creationflags=subprocess.CREATE_NEW_CONSOLE,
# use a shell to hide the console window (SW_HIDE)
shell=True)
self.assertEqual(retcode, 0)
def call_command(param, cwd=None):
if sys.platform == "win32":
param.insert(0, 'cmd.exe')
param.insert(1, '/K')
subprocess.Popen(param, cwd=cwd, creationflags=subprocess.CREATE_NEW_CONSOLE)
else:
original_command = ' '.join(param)
# param.insert(0, 'gnome-terminal')
# param.insert(1, '-e')
# print(param)
cmd = ['gnome-terminal', '-x', 'bash', '-c', '"' + original_command + '; bash"']
print(' '.join(cmd))
print(cwd)
subprocess.Popen(cmd, cwd=cwd, shell=True)
def _pokeSniper(name, lat, lng):
if _nt and _colors:
wincolors.paint(wincolors.colors.WARNING)
try:
if _terminal:
subp = subprocess.Popen([ps_path, name, lat, lng], cwd=ps_dir, creationflags = subprocess.CREATE_NEW_CONSOLE)
subp.wait()
else:
subp = subprocess.Popen([ps_path, name, lat, lng], cwd=ps_dir)
subp.wait()
except OSError, e:
error = "[WARNING] - PokeSniper2 Not found on Sniper folder"
if _nt and _colors:
wincolors.paint(wincolors.colors.ERROR)
if _verbose == 0:
print error
elif _verbose == 1:
print '[ERROR] OSError'
print error
elif _verbose == 2:
print '[ERROR] OSError = ' + str(e)
print error
_logError(str(e))
_logError(error)
#Loop
def _fork_worker(namespace_path, child_env, pid_file, invocation, chdir,
stdin_goto, stdout_goto, stderr_goto, _exit_caller, args):
''' Opens a fork worker, shielding the parent from cancellation via
signal sending. Basically, thanks Windows for being a dick about
signals.
'''
# Find out our PID so the daughter can tell us to exit
my_pid = os.getpid()
# Pack up all of the args that the child will need to use.
# Prepend it to *args
payload = (my_pid, pid_file, chdir, stdin_goto, stdout_goto,
stderr_goto, _exit_caller) + args
# Pack it up. We're shielded from pickling errors already because pickle is
# needed to start the worker.
# Write the payload to the namespace passer using the highest available
# protocol
with open(namespace_path, 'wb') as f:
pickle.dump(payload, f, protocol=-1)
# Invoke the invocation!
daemon = subprocess.Popen(
invocation,
# This is important, because the parent _forkish is telling the child
# to run as a daemon via env. Also note that we need to calculate this
# in the root _daemonize1, or else we'll have a polluted environment
# due to the '__CREATE_DAEMON__' key.
env = child_env,
# This is vital; without it, our process will be reaped at parent
# exit.
creationflags = subprocess.CREATE_NEW_CONSOLE,
)
# Busy wait until either the daemon exits, or it sends a signal to kill us.
daemon.wait()
def write_windows_console(self, *args):
retcode = subprocess.call(args,
# use a new console to not flood the test output
creationflags=subprocess.CREATE_NEW_CONSOLE,
# use a shell to hide the console window (SW_HIDE)
shell=True)
self.assertEqual(retcode, 0)
def RunDFC(self):
try:
from subprocess import Popen
Popen(dfcLoc, creationflags=subprocess.CREATE_NEW_CONSOLE)
#file.communicate()
except:
logging.info('Unable to load Dummy File Creator. Ensure it can be found in '+ dfcLoc)
def RunCommandEx(self, cwd, cmd, cmdargs):
try:
cmdline = "{0} {1}".format(cmd, cmdargs)
p = psutil.Popen(cmdline, close_fds=True, cwd=cwd, creationflags=subprocess.CREATE_NEW_CONSOLE)
return p
except Exception:
return None
def RunCommandAsEx(self, username, password, cwd, cmd, cmdargs):
try:
cmdline = "PsExec -u {0} -p {1} {2} {3}".format(username, password, cwd, cmd, cmdargs)
p = psutil.Popen(cmdline, close_fds=True, cwd=cwd, creationflags=subprocess.CREATE_NEW_CONSOLE)
return p
except Exception:
return None
def write_windows_console(self, *args):
retcode = subprocess.call(args,
# use a new console to not flood the test output
creationflags=subprocess.CREATE_NEW_CONSOLE,
# use a shell to hide the console window (SW_HIDE)
shell=True)
self.assertEqual(retcode, 0)
def convert(self):
if self.args:
p = Popen([sys.executable, os.path.abspath(HPtoHPX.__file__), *self.args], creationflags=CREATE_NEW_CONSOLE)
Thread(target=self._parent.watch_process, args=(None, p)).start()
self.close()
def start_server(self):
p = Popen([sys.executable, os.path.abspath(main.__file__)], creationflags=CREATE_NEW_CONSOLE)
Thread(target=self.watch_process, args=(self.toggle_server, p)).start()
return p
def interactive_open(program=None, encoding=None):
try:
if program is None:
if sys.platform=="win32":
program="cmd.exe"
else:
if "SHELL" in os.environ:
program=os.environ["SHELL"]
else:
program="/bin/sh"
encoding=None
fullargs=[program]
if sys.platform=="win32":
try:
#couldn't find a better way, none of the following methods worked for me : kernel32.SetConsoleOutputCP(), locale.getpreferredencoding(), sys.stdout.encoding
encoding="cp"+str(re.findall(r".*:\s*([0-9]+)",subprocess.check_output("chcp", shell=True))[0])
except:
pass
if program.endswith("powershell") or program.endswith("powershell.exe"):
fullargs=["powershell.exe", "-C", "-"] # trick to make powershell work without blocking
if encoding is None:
encoding=locale.getpreferredencoding()
print "Opening interactive %s (with encoding %s)..."%(program,encoding)
if sys.platform=="win32":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
p = Popen(fullargs, stdout=PIPE, stderr=PIPE, stdin=PIPE, bufsize=0, close_fds=ON_POSIX, universal_newlines=True, startupinfo=startupinfo)
else:
p = Popen(fullargs, stdout=PIPE, stderr=PIPE, stdin=PIPE, bufsize=0, close_fds=ON_POSIX, universal_newlines=True)
q = Queue()
q2 = Queue()
t = Thread(target=write_output, args=(p.stdout, q))
t.daemon = True
t.start()
t = Thread(target=write_output, args=(p.stderr, q2))
t.daemon = True
t.start()
t = Thread(target=flush_loop, args=(q, encoding))
t.daemon = True
t.start()
t = Thread(target=flush_loop, args=(q2, encoding))
t.daemon = True
t.start()
while True:
line = raw_input()
p.stdin.write(line+"\n")
p.stdin.flush()
if line.strip()=="exit":
break
except Exception as e:
print(traceback.format_exc())
def interactive_open(program=None, encoding=None):
try:
if program is None:
if sys.platform=="win32":
program="cmd.exe"
else:
if "SHELL" in os.environ:
program=os.environ["SHELL"]
else:
program="/bin/sh"
encoding=None
fullargs=[program]
if sys.platform=="win32":
try:
#couldn't find a better way, none of the following methods worked for me : kernel32.SetConsoleOutputCP(), locale.getpreferredencoding(), sys.stdout.encoding
encoding="cp"+str(re.findall(r".*:\s*([0-9]+)",subprocess.check_output("chcp", shell=True))[0])
except:
pass
if program.endswith("powershell") or program.endswith("powershell.exe"):
fullargs=["powershell.exe", "-C", "-"] # trick to make powershell work without blocking
if encoding is None:
encoding=locale.getpreferredencoding()
print "Opening interactive %s (with encoding %s)..."%(program,encoding)
if sys.platform=="win32":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
p = Popen(fullargs, stdout=PIPE, stderr=PIPE, stdin=PIPE, bufsize=0, close_fds=ON_POSIX, universal_newlines=True, startupinfo=startupinfo)
else:
p = Popen(fullargs, stdout=PIPE, stderr=PIPE, stdin=PIPE, bufsize=0, close_fds=ON_POSIX, universal_newlines=True)
q = Queue()
q2 = Queue()
t = Thread(target=write_output, args=(p.stdout, q))
t.daemon = True
t.start()
t = Thread(target=write_output, args=(p.stderr, q2))
t.daemon = True
t.start()
t = Thread(target=flush_loop, args=(q, encoding))
t.daemon = True
t.start()
t = Thread(target=flush_loop, args=(q2, encoding))
t.daemon = True
t.start()
while True:
line = raw_input()
p.stdin.write(line+"\n")
p.stdin.flush()
if line.strip()=="exit":
break
except Exception as e:
print(traceback.format_exc())