2016-10-22 15:40:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sys
|
|
|
|
import os.path
|
2017-07-03 19:55:03 +00:00
|
|
|
import os
|
2016-10-22 15:40:11 +00:00
|
|
|
import argparse
|
|
|
|
|
2018-01-13 17:54:06 +00:00
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
sys.path.append(os.path.join(ROOT, 'pupy', 'pupylib'))
|
|
|
|
|
|
|
|
from PupyCompile import pupycompile
|
|
|
|
|
2019-05-07 07:29:15 +00:00
|
|
|
remove_stdout = '''
|
2016-10-22 15:40:11 +00:00
|
|
|
import sys
|
2017-05-07 15:20:24 +00:00
|
|
|
sys.tracebacklimit = 0
|
2016-10-22 15:40:11 +00:00
|
|
|
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
|
|
|
|
'''
|
|
|
|
|
|
|
|
pupyload = '''
|
|
|
|
import marshal, imp, sys
|
|
|
|
fullname = "{}"
|
|
|
|
mod = imp.new_module(fullname)
|
|
|
|
mod.__file__ = "<bootloader>/{{}}".format(fullname)
|
|
|
|
exec marshal.loads({}) in mod.__dict__
|
|
|
|
sys.modules[fullname]=mod
|
|
|
|
'''
|
|
|
|
|
2019-05-07 07:29:15 +00:00
|
|
|
if __name__ == "__main__":
|
2016-10-22 15:40:11 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'-debug',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Show debug messages from bootloader'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-pass-argv',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Pass argv to the pp.py'
|
|
|
|
)
|
|
|
|
args = parser.parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
pupyimporter = None
|
|
|
|
with open(os.path.join('..', '..', 'pupy', 'packages', 'all', 'pupyimporter.py')) as f:
|
|
|
|
pupyimporter = f.read()
|
|
|
|
|
|
|
|
pp = None
|
2019-05-07 07:29:15 +00:00
|
|
|
with open(os.path.join('..', '..', 'pupy', 'pp.py')) as f:
|
2016-10-22 15:40:11 +00:00
|
|
|
pp = f.read()
|
|
|
|
|
2018-01-13 09:10:42 +00:00
|
|
|
# We are interested to consume embedded modules
|
|
|
|
# This will help to preload some
|
2018-03-12 15:11:22 +00:00
|
|
|
preload = ''
|
|
|
|
if args.debug:
|
|
|
|
with open(os.path.join('..','additional_imports.py')) as f:
|
|
|
|
preload = f.read()
|
2018-01-13 09:10:42 +00:00
|
|
|
|
2018-02-02 12:07:06 +00:00
|
|
|
pupyimporter = pupycompile(pupyimporter, raw=True, debug=args.debug)
|
2016-10-22 15:40:11 +00:00
|
|
|
|
2017-04-25 20:08:36 +00:00
|
|
|
if not args.debug:
|
|
|
|
print 'Generate bootloader with blackholed stderr/stdout'
|
|
|
|
|
2016-10-22 15:40:11 +00:00
|
|
|
bootloader = [
|
|
|
|
remove_stdout if not args.debug else 'print "DEBUG"\n',
|
2019-05-07 07:29:15 +00:00
|
|
|
'import sys; sys.path=[]; sys.path_hooks=[]; sys.meta_path=[]; '
|
2019-05-07 20:20:02 +00:00
|
|
|
'sys.real_argv=getattr(sys, "argv", []);' + (
|
2019-05-07 07:29:15 +00:00
|
|
|
'sys.argv = [];' if not args.pass_argv else ''
|
|
|
|
) + 'sys.prefix = "";\n',
|
2016-10-22 15:40:11 +00:00
|
|
|
pupyload.format('pupyimporter', repr(pupyimporter)),
|
|
|
|
'import pupyimporter\n'
|
|
|
|
'pupyimporter.install({})\n'.format(args.debug),
|
2018-01-13 09:10:42 +00:00
|
|
|
preload+'\n',
|
2016-10-22 15:40:11 +00:00
|
|
|
pp+'\n',
|
|
|
|
]
|
|
|
|
|
2017-07-03 19:55:03 +00:00
|
|
|
if not os.path.exists('resources'):
|
|
|
|
os.makedirs('resources')
|
|
|
|
|
2019-05-07 07:29:15 +00:00
|
|
|
with open(os.path.join('resources', 'bootloader.pyc'), 'wb') as w:
|
|
|
|
w.write(pupycompile(
|
|
|
|
'\n'.join(bootloader), raw=True, debug=args.debug, main=True))
|