Created
March 15, 2019 05:14
-
-
Save Enerccio/925408647237522671d61266902fe027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import zipfile | |
import tempfile | |
import subprocess | |
import StringIO | |
import threading | |
AUTORUN = """# autogenerated | |
import sys | |
sys.path.append("{0}") | |
import debugger | |
debugger.attach() | |
""" | |
class InMemoryZip(NoRollback): | |
def __init__(self): | |
self.in_memory_zip = StringIO.StringIO() | |
def append(self, filename_in_zip, file_contents): | |
zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False) | |
zf.writestr(filename_in_zip, file_contents) | |
for zfile in zf.filelist: | |
zfile.create_system = 0 | |
return self | |
def read(self): | |
self.in_memory_zip.seek(0) | |
return self.in_memory_zip.read() | |
def writetofile(self, filename): | |
f = file(filename, "w") | |
f.write(self.read()) | |
f.close() | |
def launch_renpy_project(debugger_path, renpy_path, project_path, wait=True): | |
autorun_code = AUTORUN.format(debugger_path) | |
zip = InMemoryZip() | |
zip.append("autorun.py", autorun_code) | |
extension_dir = tempfile.mkdtemp("renpydebugger") | |
zip.writetofile(os.path.join(extension_dir, "debugger.rpe")) | |
executable_path = renpy_path | |
if renpy.renpy.windows: | |
extension = ".exe" | |
else: | |
extension = "" | |
if persistent.windows_console: | |
executables = [ "python" + extension ] | |
else: | |
executables = [ "pythonw" + extension ] | |
executables.append(sys.executable) | |
for i in executables: | |
executable = os.path.join(executable_path, i) | |
if os.path.exists(executable): | |
break | |
else: | |
raise Exception("Python interpreter not found: %r", executables) | |
# Put together the basic command line. | |
cmd = [ executable, "-EO", sys.argv[0] ] | |
# Add project path | |
cmd.append(project_path) | |
environ = dict(os.environ) | |
environ["RENPY_LAUNCHER_LANGUAGE"] = "english" | |
environ["RENPY_SEARCHPATH"] = extension_dir | |
if not wait: | |
environ["RENPY_DEBUGGER_NOWAIT"] = "True" | |
encoded_environ = { } | |
for k, v in environ.items(): | |
if v is None: | |
continue | |
encoded_environ[renpy.fsencode(k)] = renpy.fsencode(v) | |
cmd = [ renpy.fsencode(i) for i in cmd ] | |
def enqueue_output(out): | |
for line in iter(out.readline, b''): | |
print line | |
out.close() | |
def enqueue_error(out): | |
for line in iter(out.readline, b''): | |
print line | |
out.close() | |
ON_POSIX = 'posix' in sys.builtin_module_names | |
print cmd | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, close_fds=ON_POSIX, env=encoded_environ) | |
t = threading.Thread(target=enqueue_output, args=(p.stdout,)) | |
t.daemon = True | |
t.start() | |
t = threading.Thread(target=enqueue_error, args=(p.stderr,)) | |
t.daemon = True | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment