Scripts unification for linux and windows build

This commit is contained in:
Oleksii Shevchuk 2016-10-22 18:40:11 +03:00
parent dd00e0f682
commit 19059fc101
17 changed files with 282 additions and 566 deletions

View File

@ -34,7 +34,6 @@ import inspect
import shlex
import json
import ctypes
import ctypes.wintypes
import threading
import urllib
import urllib2
@ -42,5 +41,9 @@ import getpass
import __future__
import bz2
#needed for scapy :
import new
import new
import fractions
if 'win' in sys.platform:
import ctypes.wintypes
else:
import pty

View File

@ -1,113 +1,77 @@
import sys
from distutils.core import setup
import os
from glob import glob
import zipfile
import shutil
# put necessary library patches/includes/whatever in this directory
sys.path.insert(0, os.path.join("sources","resources","library_patches"))
sys.path.insert(0, os.path.join("..","pupy"))
import pp
import additional_imports
import Crypto
if len(sys.argv)!=2:
exit("usage: python %s (x86|x64)"%sys.argv[0])
if sys.argv[1]=="x86":
arch="x86"
if not os.path.exists("sources\\resources\\python27_x86.dll"):
print "copying installed python version to sources\\resources\\python27_x86.dll"
shutil.copy("C:\\Windows\SysWOW64\python27.dll", "sources\\resources\\python27_x86.dll")
if not os.path.exists("sources\\resources\\msvcr90_x86.dll"):
print "copying x86 msvcr90.dll to sources"
shutil.copy("C:\\Windows\\winsxs\\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_bcb86ed6ac711f91\\msvcr90.dll", "sources\\resources\\msvcr90_x86.dll")
elif sys.argv[1]=="x64":
arch="x64"
if not os.path.exists("sources\\resources\\python27_x64.dll"):
print "copying installed python version to sources\\resources\\python27_x64.dll"
shutil.copy("C:\\Windows\system32\python27.dll", "sources\\resources\\python27_x64.dll")
if not os.path.exists("sources\\resources\\msvcr90_x64.dll"):
print "copying x64 msvcr90.dll to sources"
shutil.copy("C:\\Windows\\winsxs\\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\\msvcr90.dll", "sources\\resources\\msvcr90_x64.dll")
else:
exit("usage: python %s (x86|x64)"%sys.argv[0])
all_dependencies=list(set([x.split(".")[0] for x,m in sys.modules.iteritems() if not "(built-in)" in str(m) and x != "__main__"]))
all_dependencies.extend(["win32file", "win32pipe", "Crypto", "yaml", "rpyc", "pyasn1", "rsa"])
all_dependencies=list(set(all_dependencies))
all_dependencies.remove("pupy") # blacklist the built-in pupy import
zf = zipfile.ZipFile(os.path.join("sources","resources","library%s.zip"%arch), mode='w', compression=zipfile.ZIP_DEFLATED)
try:
if arch=="x86":
print("adding pywintypes.dll")
zf.write("C:\\Windows\\SysWOW64\\pywintypes27.dll", "pywintypes27.dll")
else:
print("adding pywintypes.dll")
zf.write("C:\\Windows\\system32\\pywintypes27.dll", "pywintypes27.dll")
for dep in all_dependencies:
found=False
for path in sys.path:
if os.path.isdir(os.path.join(path, dep)):
print("adding package %s"%dep)
for root, dirs, files in os.walk(os.path.join(path, dep)):
for f in list(set([x.rsplit(".",1)[0] for x in files])):
for ext in (".pyc", ".py", ".pyd", ".pyo"):
if os.path.exists(os.path.join(root,f+ext)):
print("adding file : %s"%os.path.join(root[len(path)+1:], f+ext))
zf.write(os.path.join(path, root, f+ext), os.path.join(root[len(path)+1:], f+ext))
break
if f.endswith(".dll"):
print("adding file : %s"%os.path.join(root[len(path)+1:], f))
zf.write(os.path.join(path, root, f), os.path.join(root[len(path)+1:], f))
found=True
else:
if os.path.exists(os.path.join(path, dep+".pyc")):
print("adding %s.pyc"%(dep))
zf.write(os.path.join(path, dep+".pyc"), dep+".pyc")
found=True
break
elif os.path.exists(os.path.join(path, dep+".py")):
print("compiling/adding %s.py"%(dep))
zf.write(os.path.join(path, dep+".py"), dep+".py")
found=True
break
elif os.path.exists(os.path.join(path, dep+".pyd")):
print("adding %s.pyd"%(dep))
zf.write(os.path.join(path, dep+".pyd"), dep+".pyd")
found=True
break
elif os.path.exists(os.path.join(path, dep+".pyo")):
print("adding %s.pyo"%(dep))
zf.write(os.path.join(path, dep+".pyo"), dep+".pyo")
found=True
break
if not found:
exit("package not found: %s (A package you don't have installed or a .egg you need to decompress)"%dep)
finally:
zf.close()
exit()
def zwalk(path, zf):
for root, dirs, files in os.walk(path):
for file in files:
if file.lower() in excluded_files:
pass
elif file.endswith('.pyd') and "." in file.rsplit(".",1)[0]:
arch_path="/".join(file.rsplit(".",1)[0].split('.'))
zf.write(os.path.join(root,file),arcname=arch_path+".pyd")
else:
zf.write(os.path.join(root, file))
with zipfile.ZipFile('sources/resources/library%s.zip' % outname, 'w', zipfile.ZIP_DEFLATED) as zf:
root = os.getcwd()
os.chdir('build/bdist.win%s/winexe/collect-2.7' % platform)
zwalk('.', zf)
os.chdir('%s/dist' % root)
zwalk('.', zf)
print 'cleaning up'
os.chdir(root)
shutil.rmtree('build')
shutil.rmtree('dist')
import sys
import os
sys.path.insert(0, os.path.join('resources','library_patches'))
sys.path.insert(0, os.path.join('..','..','pupy'))
import additional_imports
import Crypto
import pp
all_dependencies=set(
[
x.split('.')[0] for x,m in sys.modules.iteritems() if not '(built-in)' in str(m) and x != '__main__'
] + [
'Crypto', 'yaml', 'rpyc', 'pyasn1', 'rsa'
]
)
all_dependencies = list(set(all_dependencies))
all_dependencies.remove('pupy')
all_dependencies.remove('additional_imports')
print "ALLDEPS: ", all_dependencies
from distutils.core import setup
from glob import glob
import zipfile
import shutil
import compileall
zf = zipfile.ZipFile(os.path.join('resources','library.zip'), mode='w', compression=zipfile.ZIP_DEFLATED)
if 'win' in sys.platform:
zf.write(r'C:\Python27\Lib\site-packages\pywin32_system32\pywintypes27.dll', 'pywintypes27.dll')
try:
for dep in all_dependencies:
mdep = __import__(dep)
print "DEPENDENCY: ", dep, mdep
if hasattr(mdep, '__path__'):
print('adding package %s'%dep)
path, root = os.path.split(mdep.__path__[0])
for root, dirs, files in os.walk(mdep.__path__[0]):
for f in list(set([x.rsplit('.',1)[0] for x in files])):
found=False
for ext in ('.dll', '.so', '.pyo', '.pyd', '.pyc', '.py'):
if ( ext == '.py' or ext == '.pyc' ) and found:
continue
pypath = os.path.join(root,f+ext)
if os.path.exists(pypath):
if ext == '.py':
compileall.compile_file(pypath)
for extc in ( '.pyc', '.pyo' ):
if os.path.exists(os.path.join(root,f+extc)):
ext = extc
zipname = '/'.join([root[len(path)+1:], f.split('.', 1)[0] + ext])
zipname = zipname.replace('\\', '/')
found=True
# Remove various testcases if any
if any([ '/'+x+'/' in zipname for x in ['tests', 'test', 'SelfTest']]):
continue
print('adding file : {}'.format(zipname))
zf.write(os.path.join(root,f+ext), zipname)
else:
if '<memimport>' in mdep.__file__:
continue
_, ext = os.path.splitext(mdep.__file__)
print('adding %s -> %s'%(mdep.__file__, dep+ext))
zf.write(mdep.__file__, dep+ext)
finally:
zf.close()

View File

@ -16,7 +16,7 @@ def get_encoded_library_string():
modules = dict([
(z.filename, zip.open(z.filename,).read()) for z in zip.infolist() \
if os.path.splitext(z.filename)[1] in [
".py",".pyd",".dll",".pyc",".pyo"
'.py', '.pyd', '.dll', '.pyc', '.pyo', '.so'
]
])

View File

@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import marshal
import struct
import base64
import os.path
import argparse
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
'''
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
'''
if __name__=="__main__":
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
with open(os.path.join('..','..','pupy','pp.py')) as f:
pp = f.read()
pupyimporter = marshal.dumps(
compile(pupyimporter, '<string>', 'exec')
)
bootloader = [
remove_stdout if not args.debug else 'print "DEBUG"\n',
'import sys; sys.path=[]; ' + (
'sys.argv = [];' if not args.pass_argv else ''
) + '\n',
pupyload.format('pupyimporter', repr(pupyimporter)),
'import pupyimporter\n'
'pupyimporter.install({})\n'.format(args.debug),
pp+'\n',
]
with open(os.path.join("resources","bootloader.pyc"),'wb') as w:
w.write(marshal.dumps([
compile(block, '<string>', 'exec') for block in bootloader
]))

View File

@ -18,7 +18,7 @@ endif
LINUX_INJECT_CFLAGS := -include debug.h -include fixes.h -Dmain=linux_inject_main
ifneq ($(DEBUG),)
DEBUG_ADD := debug
DEBUG_ADD := -debug
CFLAGS += -DDEBUG -O0 -g
NAME := "$(NAME)d"
else
@ -58,7 +58,7 @@ ifneq ($(ZLIB),built-in)
resources/zlib.so: $(ZLIB)
$(GZIP) -9 -c $< >$@
resources_zlib_so.c: gen_resource_header.py resources/zlib.so
resources_zlib_so.c: ../gen_resource_header.py resources/zlib.so
$(PYTHON) $(PFLAGS) $+
endif
@ -68,16 +68,16 @@ import-tab.c import-tab.h: mktab.py
Python-dynload.o: Python-dynload.c import-tab.c import-tab.h
$(CC) -c -o $@ $< $(CFLAGS)
resources/library_compressed_string.txt: gen_library_compressed_string.py resources/library.zip
$(PYTHON) $(PFLAGS) gen_library_compressed_string.py
resources/library_compressed_string.txt: ../gen_library_compressed_string.py resources/library.zip
$(PYTHON) $(PFLAGS) ../gen_library_compressed_string.py
resources_library_compressed_string_txt.c: gen_resource_header.py resources/library_compressed_string.txt resources/library.zip
$(PYTHON) $(PFLAGS) gen_resource_header.py resources/library_compressed_string.txt
resources_library_compressed_string_txt.c: ../gen_resource_header.py resources/library_compressed_string.txt resources/library.zip
$(PYTHON) $(PFLAGS) ../gen_resource_header.py resources/library_compressed_string.txt
resources/bootloader.pyc: gen_python_bootloader.py ../../pupy/packages/all/pupyimporter.py ../../pupy/pp.py
$(PYTHON) $(PFLAGS) gen_python_bootloader.py $(DEBUG_ADD)
resources/bootloader.pyc: ../gen_python_bootloader.py ../../pupy/packages/all/pupyimporter.py ../../pupy/pp.py
$(PYTHON) $(PFLAGS) ../gen_python_bootloader.py $(DEBUG_ADD)
resources_bootloader_pyc.c: gen_resource_header.py resources/bootloader.pyc
resources_bootloader_pyc.c: ../gen_resource_header.py resources/bootloader.pyc
$(PYTHON) $(PFLAGS) $+
linux-inject/%.o: linux-inject/%.c
@ -89,10 +89,10 @@ resources/python27.so: $(LIBPYTHON)
$(GZIP) -9 -c $@.tmp >$@
rm -f $@.tmp
resources/library.zip: build_library_zip.py additional_imports.py
resources/library.zip: ../build_library_zip.py ../additional_imports.py
$(PYTHON) $(PFLAGS) $<
resources_python27_so.c: gen_resource_header.py resources/python27.so
resources_python27_so.c: ../gen_resource_header.py resources/python27.so
$(PYTHON) $(PFLAGS) $+
$(TEMPLATE_OUTPUT_PATH)/pupyx$(NAME).lin: main_exe.o $(PYOBJS) $(COMMON_OBJS)

View File

@ -1,49 +0,0 @@
import encodings
import socket
import threading
import Queue
import collections
import SocketServer
import struct
import os
import sys
import time
import traceback
import uuid
import subprocess
import StringIO
import imp
import hashlib
import base64
import logging
import re
import ssl
import tempfile
import string
import datetime
import random
import shutil
import platform
import errno, stat
import zlib
import code
import glob
import multiprocessing
import math
import binascii
import inspect
import shlex
import json
import threading
import urllib
import urllib2
import getpass
import __future__
import bz2
#needed for scapy :
import new
import fractions
import Crypto
import pty
import rsa
import pyasn1

View File

@ -1,47 +0,0 @@
import sys
from distutils.core import setup
import os
from glob import glob
import zipfile
import shutil
sys.path.insert(0, os.path.join('resources','library_patches'))
sys.path.insert(0, os.path.join('..','..','pupy'))
import pp
import additional_imports
import Crypto
all_dependencies=set([x.split('.')[0] for x,m in sys.modules.iteritems() if not '(built-in)' in str(m) and x != '__main__'])
print "ALLDEPS: ", all_dependencies
zf = zipfile.ZipFile(os.path.join('resources','library.zip'), mode='w', compression=zipfile.ZIP_DEFLATED)
try:
for dep in all_dependencies:
mdep = __import__(dep)
print "DEPENDENCY: ", dep, mdep
if hasattr(mdep, '__path__'):
print('adding package %s'%dep)
path, root = os.path.split(mdep.__path__[0])
for root, dirs, files in os.walk(mdep.__path__[0]):
for f in list(set([x.rsplit('.',1)[0] for x in files])):
found=False
for ext in ('.pyo', '.so', '.pyc', '.py'):
if ( ext == '.py' or ext == '.pyc' ) and found:
continue
if os.path.exists(os.path.join(root,f+ext)):
zipname = os.path.join(root[len(path)+1:], f.split('.', 1)[0] + ext)
print('adding file : {}'.format(zipname))
zf.write(os.path.join(root, f+ext), zipname)
found=True
else:
if '<memimport>' in mdep.__file__:
continue
_, ext = os.path.splitext(mdep.__file__)
print('adding %s -> %s'%(mdep.__file__, dep+ext))
zf.write(mdep.__file__, dep+ext)
finally:
zf.close()

View File

@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import StringIO, zipfile, os.path, imp, sys
import marshal
import zlib
def get_encoded_library_string():
filepath=os.path.join("resources","library.zip")
f = StringIO.StringIO()
f.write(open(filepath, "rb").read())
zip = zipfile.ZipFile(f)
modules = dict([(z.filename, zip.open(z.filename,).read()) for z in zip. infolist() if os.path.splitext(z.filename)[1] in [".py",".so",".pyc",".pyo"]])
return zlib.compress(marshal.dumps(modules),9)
with open(os.path.join("resources","library_compressed_string.txt"),'wb') as w:
w.write(get_encoded_library_string())
print "Encoded library generated"

View File

@ -1,57 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
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":
remove_stdout="print 'DEBUG activated'\n"
def get_load_module_code(code, modulename):
loader="""
import marshal, imp, sys
fullname={}
mod = imp.new_module(fullname)
mod.__file__ = "<bootloader>\\%s" % fullname
exec marshal.loads({}) in mod.__dict__
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.path = [];", "<string>", "exec"))
with open(os.path.join("..", "..", "pupy", "packages","all", "pupyimporter.py")) as f:
code=f.read()
code=marshal.dumps(compile(code, '<string>', 'exec'))
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:
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)

View File

@ -1,27 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import binascii
MAX_CHAR_PER_LINE=50
if __name__=="__main__":
h_file=""
file_bytes=b""
with open(sys.argv[1], "rb") as f:
file_bytes=f.read()
h_file += "int %s_size = %s;"%(sys.argv[1].replace(".","_").replace("/","_").replace("/","_"), len(file_bytes))
h_file += "\nchar %s_start[] = {\n"%sys.argv[1].replace(".","_").replace("/","_").replace("/","_")
current_size=0
for c in file_bytes:
h_file+="'\\x%s',"%binascii.hexlify(c)
current_size+=1
if current_size>MAX_CHAR_PER_LINE:
current_size=0
h_file+="\n"
h_file += "'\\x00' };\n"
with open(sys.argv[1].replace(".","_").replace("/","_").replace("/","_")+".c",'w') as w:
w.write(h_file)

View File

@ -20,7 +20,7 @@ PPARCH := x86
endif
ifdef DEBUG
DEBUG_ADD :=DEBUG
DEBUG_ADD := -debug
CFLAGS := $(CFLAGS) /DDEBUG /DVERBOSE
LINKER_OPTS :=
PPARCH := $(PPARCH)d
@ -49,22 +49,22 @@ COMMON_OBJS := \
all: $(TEMPLATE_OUTPUT_PATH)/pupy$(PPARCH).exe $(TEMPLATE_OUTPUT_PATH)/pupy$(PPARCH).dll
$(BUILDENV_READY):
./buildenv.sh $(BUILDENV)
./buildenv.sh "$(BUILDENV)"
resources/library_compressed_string.txt: gen_library_compressed_string.py resources/library.zip $(BUILDENV_READY)
$(PYTHON) gen_library_compressed_string.py
resources/library_compressed_string.txt: ../gen_library_compressed_string.py resources/library.zip $(BUILDENV_READY)
$(PYTHON) ../gen_library_compressed_string.py
resources/library.zip: build_library_zip.py $(BUILDENV_READY)
$(PYTHON) build_library_zip.py
resources/library.zip: ../build_library_zip.py $(BUILDENV_READY)
$(PYTHON) ../build_library_zip.py -windows
resources_library_compressed_string_txt.c: gen_resource_header.py resources/library_compressed_string.txt resources/library.zip $(BUILDENV_READY)
$(PYTHON) gen_resource_header.py resources/library_compressed_string.txt
resources_library_compressed_string_txt.c: ../gen_resource_header.py resources/library_compressed_string.txt resources/library.zip $(BUILDENV_READY)
$(PYTHON) ../gen_resource_header.py resources/library_compressed_string.txt
resources/bootloader.pyc: ../../pupy/packages/all/pupyimporter.py ../../pupy/pp.py gen_python_bootloader.py $(BUILDENV_READY)
$(PYTHON) gen_python_bootloader.py $(DEBUG_ADD)
resources/bootloader.pyc: ../../pupy/packages/all/pupyimporter.py ../../pupy/pp.py ../gen_python_bootloader.py $(BUILDENV_READY)
$(PYTHON) ../gen_python_bootloader.py $(DEBUG_ADD)
resources_bootloader_pyc.c: resources/bootloader.pyc gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) gen_resource_header.py $<
resources_bootloader_pyc.c: resources/bootloader.pyc ../gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) ../gen_resource_header.py $<
resources/python27.dll: $(BUILDENV)/$(ARCH)/drive_c/Python27/python27.dll $(BUILDENV_READY)
cp $< $@
@ -72,11 +72,11 @@ resources/python27.dll: $(BUILDENV)/$(ARCH)/drive_c/Python27/python27.dll $(BUIL
resources/msvcr90.dll: $(BUILDENV)/$(ARCH)/drive_c/Python27/msvcr90.dll $(BUILDENV_READY)
cp $< $@
resources_python27_dll.c: resources/python27.dll gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) gen_resource_header.py $<
resources_python27_dll.c: resources/python27.dll ../gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) ../gen_resource_header.py $<
resources_msvcr90_dll.c: resources/msvcr90.dll gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) gen_resource_header.py $<
resources_msvcr90_dll.c: resources/msvcr90.dll ../gen_resource_header.py $(BUILDENV_READY)
$(PYTHON) ../gen_resource_header.py $<
$(PYOBJS): %.obj: %.c
$(CC) /c $(CFLAGS) /I$(PYTHONPATH)\\include $<
@ -98,16 +98,14 @@ $(TEMPLATE_OUTPUT_PATH)/pupy$(PPARCH).dll: main_reflective.obj $(PYOBJS) Reflect
.PHONY: clean
ifneq "$(QUICK)" "1"
.PHONY: resources/python27.dll resources/msvcr90.dll
endif
clean:
rm -f *.obj
rm -f *.pyo
rm -f *.pyc
rm -f *.txt
rm -f *_txt.c
rm -f *_pyc.c
rm -f *_dll.c
rm -f resources/*.pyo
rm -f resources/*.pyc
rm -f resources/*.txt

View File

@ -1,46 +0,0 @@
import socket
import threading
import Queue
import collections
import SocketServer
import struct
import os
import sys
import time
import traceback
import uuid
import subprocess
import StringIO
import imp
import hashlib
import base64
import logging
import re
import ssl
import tempfile
import string
import datetime
import random
import shutil
import platform
import errno, stat
import zlib
import code
import glob
import multiprocessing
import math
import binascii
import inspect
import shlex
import json
import ctypes
import ctypes.wintypes
import threading
import urllib
import urllib2
import getpass
import __future__
import bz2
#needed for scapy :
import new
import fractions

View File

@ -1,58 +0,0 @@
import sys
from distutils.core import setup
import os
from glob import glob
import zipfile
import shutil
sys.path.insert(0, os.path.join('resources','library_patches'))
sys.path.insert(0, os.path.join('..','..','pupy'))
import additional_imports
import Crypto
import pp
all_dependencies=set(
[
x.split('.')[0] for x,m in sys.modules.iteritems() if not '(built-in)' in str(m) and x != '__main__'
] + [
'win32file', 'win32pipe', 'Crypto', 'yaml', 'rpyc', 'pyasn1', 'rsa'
]
)
all_dependencies = list(set(all_dependencies))
all_dependencies.remove('pupy')
all_dependencies.remove('additional_imports')
print "ALLDEPS: ", all_dependencies
zf = zipfile.ZipFile(os.path.join('resources','library.zip'), mode='w', compression=zipfile.ZIP_DEFLATED)
zf.write('C:\\Python27\\Lib\\site-packages\\pywin32_system32\\pywintypes27.dll', 'pywintypes27.dll')
try:
for dep in all_dependencies:
mdep = __import__(dep)
print "DEPENDENCY: ", dep, mdep
if hasattr(mdep, '__path__'):
print('adding package %s'%dep)
path, root = os.path.split(mdep.__path__[0])
for root, dirs, files in os.walk(mdep.__path__[0]):
for f in list(set([x.rsplit('.',1)[0] for x in files])):
found=False
for ext in ('.pyo', '.pyd', '.pyc', '.py', '.dll', '.so'):
if ( ext == '.py' or ext == '.pyc' ) and found:
continue
if os.path.exists(os.path.join(root,f+ext)):
zipname = os.path.join(root[len(path)+1:], f.split('.', 1)[0] + ext)
print('adding file : {}'.format(zipname))
zf.write(os.path.join(root, f+ext), zipname)
found=True
else:
if '<memimport>' in mdep.__file__:
continue
_, ext = os.path.splitext(mdep.__file__)
print('adding %s -> %s'%(mdep.__file__, dep+ext))
zf.write(mdep.__file__, dep+ext)
finally:
zf.close()

View File

@ -2,7 +2,7 @@
unset WINEARCH WINEPREFIX
set -e
set -xe
SELF=`readlink -f $0`
CWD=`dirname $0`

View File

@ -1,57 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
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":
remove_stdout="print 'DEBUG activated'\n"
def get_load_module_code(code, modulename):
loader="""
import marshal, imp, sys
fullname={}
mod = imp.new_module(fullname)
mod.__file__ = "<bootloader>\\%s" % fullname
exec marshal.loads({}) in mod.__dict__
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"))
with open(os.path.join("..", "..", "pupy", "packages","all", "pupyimporter.py")) as f:
code=f.read()
code=marshal.dumps(compile(code, '<string>', 'exec'))
code_bytes.append(compile(get_load_module_code(code,"pupyimporter")+"\n", "<string>", "exec"))
code_bytes.append(compile("import pupyimporter;pupyimporter.install();pupyimporter.load_pywintypes();\n", "<string>", "exec"))
with open(os.path.join("..",'..','pupy',"pp.py")) as f:
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)

View File

@ -18,16 +18,18 @@
#
import sys, imp, zlib, marshal
sep = '/'
if 'win' in sys.platform:
sep = '\\'
__debug = False;
def dprint(msg):
global __debug
if __debug:
print msg
builtin_memimporter=False
try:
import _memimporter
builtin_memimporter=True
builtin_memimporter = True
except ImportError:
pass
builtin_memimporter = False
modules={}
try:
@ -35,18 +37,21 @@ try:
if not (hasattr(pupy, 'pseudo') and pupy.pseudo):
modules = marshal.loads(zlib.decompress(pupy._get_compressed_library_string()))
except ImportError:
#modules = marshal.loads(zlib.decompress(open("resources\\library_compressed_string.txt",'rb').read()))
pass
def get_module_files(fullname):
""" return the file to load """
global modules
f=fullname.replace(".",sep)
files=[]
for x in modules.iterkeys():
if x.rsplit(".",1)[0]==f or f+sep+"__init__.py"==x or f+sep+"__init__.pyc"==x or f+sep+"__init__.pyo"==x:
files.append(x)
return files
path = fullname.replace('.','/')
return [
module for module in modules.iterkeys() \
if module.rsplit(".",1)[0] == path or any([
path+'/__init__'+ext == module for ext in [
'.py', '.pyc', '.pyo'
]
])
]
def pupy_add_package(pkdic):
""" update the modules dictionary to allow remote imports of new packages """
@ -66,7 +71,7 @@ class PupyPackageLoader:
def load_module(self, fullname):
imp.acquire_lock()
try:
#print "loading module %s"%fullname
dprint('loading module {}'.format(fullname))
if fullname in sys.modules:
return sys.modules[fullname]
mod=None
@ -74,10 +79,10 @@ class PupyPackageLoader:
if self.extension=="py":
mod = imp.new_module(fullname)
mod.__name__ = fullname
mod.__file__ = '<memimport>{}{}'.format(sep, self.path)
mod.__file__ = '<memimport>/{}'.format(self.path)
mod.__loader__ = self
if self.is_pkg:
mod.__path__ = [mod.__file__.rsplit(sep,1)[0]]
mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
mod.__package__ = fullname
else:
mod.__package__ = fullname.rsplit('.', 1)[0]
@ -87,10 +92,10 @@ class PupyPackageLoader:
elif self.extension in ["pyc","pyo"]:
mod = imp.new_module(fullname)
mod.__name__ = fullname
mod.__file__ = '<memimport>{}{}'.format(sep, self.path)
mod.__file__ = '<memimport>/{}'.format(self.path)
mod.__loader__ = self
if self.is_pkg:
mod.__path__ = [mod.__file__.rsplit(sep,1)[0]]
mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
mod.__package__ = fullname
else:
mod.__package__ = fullname.rsplit('.', 1)[0]
@ -99,12 +104,12 @@ class PupyPackageLoader:
exec c in mod.__dict__
elif self.extension in ("dll","pyd","so"):
initname = "init" + fullname.rsplit(".",1)[-1]
path=fullname.replace(".",sep)+"."+self.extension
#print "Loading %s from memory"%fullname
#print "init:%s, %s.%s"%(initname,fullname,self.extension)
path=fullname.replace(".",'/')+"."+self.extension
dprint('Loading {} from memory'.format(fullname))
dprint('init:{}, {}.{}'.format(initname,fullname,self.extension))
mod = _memimporter.import_module(self.contents, initname, fullname, path)
mod.__name__=fullname
mod.__file__ = '<memimport>{}{}'.format(sep, self.path)
mod.__file__ = '<memimport>/{}'.format(self.path)
mod.__loader__ = self
mod.__package__ = fullname.rsplit('.',1)[0]
sys.modules[fullname]=mod
@ -114,7 +119,9 @@ class PupyPackageLoader:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_tb(exc_traceback)
print "PupyPackageLoader: Error while loading package %s (%s) : %s"%(fullname, self.extension, str(e))
dprint('PupyPackageLoader: '
'Error while loading package {} ({}) : {}'.format(
(fullname, self.extension, str(e))))
raise e
finally:
imp.release_lock()
@ -124,46 +131,67 @@ class PupyPackageLoader:
class PupyPackageFinder:
def __init__(self, modules):
self.modules = modules
self.modules_list=[x.rsplit(".",1)[0] for x in self.modules.iterkeys()]
self.modules_list=[
x.rsplit(".",1)[0] for x in self.modules.iterkeys()
]
def find_module(self, fullname, path=None):
imp.acquire_lock()
try:
files=[]
if fullname in ("pywintypes", "pythoncom"):
if fullname in ( 'pywintypes', 'pythoncom' ):
fullname = fullname + "%d%d" % sys.version_info[:2]
fullname = fullname.replace(".", sep) + ".dll"
files=[fullname]
fullname = fullname.replace(".", '/') + ".dll"
files = [ fullname ]
else:
files=get_module_files(fullname)
#print "find_module(\"%s\",\"%s\")"%(fullname,path)
if not builtin_memimporter:
files=[f for f in files if not f.lower().endswith((".pyd",".dll"))]
if not files:
#print "%s not found in %s"%(fullname,path)
return None
selected=None
for f in files:
if f.endswith(sep+"__init__.pyc") or f.endswith(sep+"__init__.py") or f.endswith(sep+"__init__.pyo"):
selected=f # we select packages in priority
if not selected:
for f in files:
if f.endswith(".pyd"):
selected=f # then we select pyd
if not selected:
for f in files:
if f.endswith(".py"):
selected=f # we select .py before .pyc
if not selected:
selected=files[0]
files = get_module_files(fullname)
#print "%s found in %s"%(fullname,selected)
content=self.modules[selected]
extension=selected.rsplit(".",1)[1].strip().lower()
is_pkg=False
if selected.endswith(sep+"__init__.py") or selected.endswith(sep+"__init__.pyc") or selected.endswith(sep+"__init__.pyo"):
is_pkg=True
#print "--> Loading %s(%s).%s is_package:%s"%(fullname,selected,extension, is_pkg)
dprint('find_module({},{}) in {})'.format(fullname, path, files))
if not builtin_memimporter:
files = [
f for f in files if not f.lower().endswith((".pyd",".dll",".so"))
]
if not files:
dprint('{} not found in {} - no files'.format(fullname,path))
return None
criterias = [
lambda f: any([
f.endswith('/__init__'+ext) for ext in [
'.pyo', '.pyc', '.py'
]
]),
lambda f: any ([
f.endswith(ext) for ext in [
'.pyo', '.pyc'
]
]),
lambda f: any ([
f.endswith(ext) for ext in [
'.pyd', '.py', '.so', '.dll'
]
]),
]
selected = None
for criteria in criterias:
for pyfile in files:
if criteria(pyfile):
selected = pyfile
break
if not selected:
dprint('{} not found in {}: not in {} files'.format(
fullname, selected, len(files)))
dprint('{} found in {}'.format(fullname, selected))
content = self.modules[selected]
extension = selected.rsplit(".",1)[1].strip().lower()
is_pkg = any([selected.endswith('/__init__'+ext) for ext in [ '.pyo', '.pyc', '.py' ]])
dprint('--> Loading {} ({}) package={}'.format(
fullname, selected, is_pkg))
return PupyPackageLoader(fullname, content, extension, is_pkg, selected)
except Exception as e:
raise e
@ -177,9 +205,17 @@ def load_pywintypes():
import pupy
pupy.load_dll("pywintypes27.dll", modules["pywintypes27.dll"])
except Exception as e:
print e
dprint('Loading pywintypes27.dll.. failed: {}'.format(e))
pass
def install():
def install(debug=False):
global __debug
__debug = debug
sys.meta_path.append(PupyPackageFinder(modules))
sys.path_importer_cache.clear()
if 'win' in sys.platform:
load_pywintypes()
if __debug:
print 'Bundled modules:'
for module in modules.iterkeys():
print '+ {}'.format(module)