async def on_message(self, target, source, message): """Parse incoming messages for commands and whatnot.""" log.log(self.network, target, source, message) message = message.split() if source == self.nickname: return if message[0].startswith('.'): # flood protection check = log.check_rate(self.network, target, source) if check[0] <= 3: pass if check[0] == 4: await self.notice( source, "You can only send 3 commands every 30 seconds. " + "Your commands will be ignored for " + f"{30 - (int(time.time()) - check[1])} more seconds, " + "provided you don't continue to spam lines that start " + "with a dot.") return if check[0] > 4: return if message[0].startswith('.bots'): # comply with IRC Bot Identification Protocol await ibip.reply(self, target, source) elif message[0].startswith('.eval'): # evaluate Python expressions, useful for debugging if await self.is_admin(source): await self.eval(target, source, message) elif message[0].startswith('.join'): # join a channel or channels if await self.is_owner(source): await self.join(message[1]) elif message[0].startswith('.quit'): # quit IRC and exit the program if await self.is_owner(source): await self.quit() elif message[0].startswith('.reload'): # attempt to reload a Python module if await self.is_admin(source): await self.module_reload(target, source, message) elif message[0].startswith('.uptime'): # display uptime await self.message( target, f"{source}: My current uptime is: {self.uptime()}") return
def build_project_generic(options, platform, target, pp, env=None): package = options.project.split('.')[-1] if package == 'ignifuga': error('Name your project something else than ignifuga please') exit() if package +'.py' == basename(options.main).lower(): error('Your main file can not have the same name as the project. If your project is com.mdqinc.test your main file can not be named test.py') exit() platform_build = join(target.project, platform) main_file = join(platform_build, basename(options.main)) cython_src = join(target.project, platform, 'cython_src') info('Building %s for %s (package: %s)' % (options.project, platform, package)) if not isdir(target.project): os.makedirs(target.project) # Prepare and cythonize project sources prepare_project(target.project_root, platform_build) # Remove main file as it'll be cythonized differently if isfile(main_file): os.unlink(main_file) cfiles, glue_h, glue_c = cythonize(platform_build, package, options, [options.main,]) # Cythonize main file main_file_ct = getctime(abspath(options.main)) main_file_c = join(cython_src, splitext(basename(options.main))[0] + '.cpp') cfiles.append(main_file_c) if not isfile(main_file_c) or getctime(main_file_c) < main_file_ct: log('Cythonizing main file %s' % main_file) mfc = join(cython_src, splitext(basename(main_file))[0] + '.cpp') cmd = 'cython --embed --cplus --include-dir "%s/.." -o "%s" "%s" ' % (ROOT_DIR, mfc, abspath(options.main)) Popen(shlex.split(cmd), cwd = cython_src).communicate() if not isfile(mfc): error ('Could not cythonize main file') exit() # Insert SDL.h into the cythonized file if not options.bare: with file(mfc, 'r') as original: mfc_data = original.read() mfc_data = mfc_data.replace('PyErr_Print();', 'PyErr_Print();fflush(stdout);fflush(stderr);') with file(mfc, 'w') as modified: modified.write("#include \"SDL.h\"\n"+mfc_data) shutil.move(mfc, main_file_c) # Build the executable sources = '' for cf in cfiles: sources += cf + ' ' mod = __import__('modules.project.'+platform, fromlist=['make']) mod.make(options, env, target, sources, cython_src, cfiles) info('Project built successfully')
def preprocess_sources(pp, folder, extensions=['pyx', 'pxd', 'py']): # Preprocess files sources = [] for extension in extensions: for root, dirnames, filenames in os.walk(folder): for filename in fnmatch.filter(filenames, '*.' + extension): sources.append(os.path.join(root, filename)) pp.removeMeta = True for source in sources: log('Preprocessing %s' % basename(source)) pp.input = source pp.output = source pp.parse()
def preprocess_sources(pp, folder, extensions=['pyx', 'pxd', 'py']): # Preprocess files sources = [] for extension in extensions: for root, dirnames, filenames in os.walk(folder): for filename in fnmatch.filter(filenames, '*.'+extension): sources.append(os.path.join(root, filename)) pp.removeMeta = True for source in sources: log('Preprocessing %s' % basename(source)) pp.input = source pp.output = source pp.parse()
def clean_modules(platforms, modules, everything=False): log('Cleaning Build Directories') if isinstance(platforms, str): platforms = [platforms,] for platform in platforms: target = get_target(platform) if not everything: if isdir(target.tmp): if 'ignifuga' in modules: if isdir(target.builds.IGNIFUGA): shutil.rmtree(target.builds.IGNIFUGA) if isdir(target.builds.PYTHON): shutil.rmtree(target.builds.PYTHON) if 'sdl' in modules and isdir(target.builds.SDL): shutil.rmtree(target.builds.SDL) else: if isdir(target.tmp): shutil.rmtree(target.tmp) if isdir(target.dist): shutil.rmtree(target.dist)
def prepare_ignifuga(platform, pp, options, srcdir=SOURCES['IGNIFUGA']): # Copy .py, .pyx .pxd .h .c .cpp files log("Preparing Ignifuga source code") target = get_target(platform) excludes = '' if not options.rocket: excludes += '--exclude Rocket*' cmd = 'rsync -auqPm --exclude .svn %s --include "*/" --include "*.py" --include "*.pyx" --include "*.pxd" --include "*.h" --include "*.hpp" --include "*.inl" --include "*.c" --include "*.cpp" --exclude "*" %s/ %s' % ( excludes, srcdir, target.builds.IGNIFUGA) Popen(shlex.split(cmd), cwd=srcdir).communicate() # Copy Rocket from external if options.rocket and not options.bare: log("Preparing libRocket source code") cmd = 'rsync -auqPm %s/Source/ %s' % ( SOURCES['ROCKET'], join(target.builds.IGNIFUGA, 'Rocket')) Popen(shlex.split(cmd), cwd=srcdir).communicate() cmd = 'rsync -auqPm %s/Include/Rocket/ %s' % ( SOURCES['ROCKET'], join(target.builds.IGNIFUGA, 'Rocket')) Popen(shlex.split(cmd), cwd=srcdir).communicate() log("Preparing boost::python source code") cmd = 'rsync -auqPm %s/python/src/ %s' % ( SOURCES['BOOST'], join(target.builds.IGNIFUGA, 'boost')) Popen(shlex.split(cmd), cwd=srcdir).communicate() cmd = 'rsync -auqPm %s/python/include/ %s' % ( SOURCES['BOOST'], join(target.builds.IGNIFUGA, 'boost')) Popen(shlex.split(cmd), cwd=srcdir).communicate() preprocess_sources(pp, target.builds.IGNIFUGA)
def prepare_ignifuga(platform, pp, options, srcdir=SOURCES['IGNIFUGA']): # Copy .py, .pyx .pxd .h .c .cpp files log("Preparing Ignifuga source code") target = get_target(platform) excludes = '' if not options.rocket: excludes += '--exclude Rocket*' cmd = 'rsync -auqPm --exclude .svn %s --include "*/" --include "*.py" --include "*.pyx" --include "*.pxd" --include "*.h" --include "*.hpp" --include "*.inl" --include "*.c" --include "*.cpp" --exclude "*" %s/ %s' % (excludes, srcdir, target.builds.IGNIFUGA) Popen(shlex.split(cmd), cwd = srcdir).communicate() # Copy Rocket from external if options.rocket and not options.bare: log("Preparing libRocket source code") cmd = 'rsync -auqPm %s/Source/ %s' % (SOURCES['ROCKET'], join(target.builds.IGNIFUGA, 'Rocket')) Popen(shlex.split(cmd), cwd = srcdir).communicate() cmd = 'rsync -auqPm %s/Include/Rocket/ %s' % (SOURCES['ROCKET'], join(target.builds.IGNIFUGA, 'Rocket')) Popen(shlex.split(cmd), cwd = srcdir).communicate() log("Preparing boost::python source code") cmd = 'rsync -auqPm %s/python/src/ %s' % (SOURCES['BOOST'], join(target.builds.IGNIFUGA, 'boost')) Popen(shlex.split(cmd), cwd = srcdir).communicate() cmd = 'rsync -auqPm %s/python/include/ %s' % (SOURCES['BOOST'], join(target.builds.IGNIFUGA, 'boost')) Popen(shlex.split(cmd), cwd = srcdir).communicate() preprocess_sources(pp, target.builds.IGNIFUGA)
def clean_modules(platforms, modules, everything=False): log('Cleaning Build Directories') if isinstance(platforms, str): platforms = [ platforms, ] for platform in platforms: target = get_target(platform) if not everything: if isdir(target.tmp): if 'ignifuga' in modules: if isdir(target.builds.IGNIFUGA): shutil.rmtree(target.builds.IGNIFUGA) if isdir(target.builds.PYTHON): shutil.rmtree(target.builds.PYTHON) if 'sdl' in modules and isdir(target.builds.SDL): shutil.rmtree(target.builds.SDL) else: if isdir(target.tmp): shutil.rmtree(target.tmp) if isdir(target.dist): shutil.rmtree(target.dist)
def build_project_generic(options, platform, target, pp, env=None): package = options.project.split('.')[-1] if package == 'ignifuga': error('Name your project something else than ignifuga please') exit() if package + '.py' == basename(options.main).lower(): error( 'Your main file can not have the same name as the project. If your project is com.mdqinc.test your main file can not be named test.py' ) exit() platform_build = join(target.project, platform) main_file = join(platform_build, basename(options.main)) cython_src = join(target.project, platform, 'cython_src') info('Building %s for %s (package: %s)' % (options.project, platform, package)) if not isdir(target.project): os.makedirs(target.project) # Prepare and cythonize project sources prepare_project(target.project_root, platform_build) # Remove main file as it'll be cythonized differently if isfile(main_file): os.unlink(main_file) cfiles, glue_h, glue_c = cythonize(platform_build, package, options, [ options.main, ]) # Cythonize main file main_file_ct = getctime(abspath(options.main)) main_file_c = join(cython_src, splitext(basename(options.main))[0] + '.cpp') cfiles.append(main_file_c) if not isfile(main_file_c) or getctime(main_file_c) < main_file_ct: log('Cythonizing main file %s' % main_file) mfc = join(cython_src, splitext(basename(main_file))[0] + '.cpp') cmd = 'cython --embed --cplus --include-dir "%s/.." -o "%s" "%s" ' % ( ROOT_DIR, mfc, abspath(options.main)) Popen(shlex.split(cmd), cwd=cython_src).communicate() if not isfile(mfc): error('Could not cythonize main file') exit() # Insert SDL.h into the cythonized file if not options.bare: with file(mfc, 'r') as original: mfc_data = original.read() mfc_data = mfc_data.replace( 'PyErr_Print();', 'PyErr_Print();fflush(stdout);fflush(stderr);') with file(mfc, 'w') as modified: modified.write("#include \"SDL.h\"\n" + mfc_data) shutil.move(mfc, main_file_c) # Build the executable sources = '' for cf in cfiles: sources += cf + ' ' mod = __import__('modules.project.' + platform, fromlist=['make']) mod.make(options, env, target, sources, cython_src, cfiles) info('Project built successfully')
def cythonize(build_dir, package_name, options, skip=[]): files = [] cfiles = [] updatedfiles = [] for f in locate('*.py', build_dir): files.append(f) for f in locate('*.pyx', build_dir): files.append(f) # Cythonize the source files for f in files: if f[len(build_dir):] not in skip and f[len(build_dir) + 1:] not in skip: mf = getctime(f) cf = splitext(f)[0] + '.cpp' if not isfile(cf) or getctime(cf) < mf: ccf = join(build_dir, 'cython_src', cf.replace(os.sep, '+')[len(build_dir) + 1:]) if not isfile(ccf) or getctime(ccf) < mf: log('Cythonizing %s' % basename(f)) cmd = 'cython --cplus --include-dir "%s/.." "%s"' % ( ROOT_DIR, f) p = Popen(shlex.split(cmd), cwd=build_dir) p.communicate() if p.returncode != 0: error("Problem cythonizing file") exit() updatedfiles.append(cf) else: log('Skipping Cython for %s' % basename(f)) cfiles.append(cf) # Flatten the directory structure, replace / by + signs files = cfiles[:] cfiles = [] cython_src = join(build_dir, 'cython_src') if not isdir(cython_src): os.makedirs(cython_src) for f in files: d = f[len(build_dir) + 1:].replace(os.sep, '+') cfile = join(cython_src, d) if isfile(f): shutil.move(f, cfile) cfiles.append(cfile) if f in updatedfiles: updatedfiles.remove(f) updatedfiles.append(cfile) # Walk the files, arrange the package in the proper hierachy glue_h = "" glue_c = "" packages = [ package_name, ] for f in cfiles: filename = basename(f) package = filename.replace('+', '.').replace('.cpp', '') # The last part of the package is the current module module = package.split('.')[-1] # Remove the module from the package package = '.'.join(package.split('.')[:-1]) subpackage = package.split('.') if len(subpackage) > 0: subpackage = subpackage[-1] else: subpackage = '' if package != '': package = package_name + '.' + package else: package = package_name if package not in packages: packages.append(package) # Patch the correct paths and package name in the updated cython generated files if f in updatedfiles: log('Patching %s' % (basename(f), )) if module != '__init__': cmd = SED_CMD + """'s|Py_InitModule4(__Pyx_NAMESTR("\(.*\)")|Py_InitModule4(__Pyx_NAMESTR("%s.\\1")|g' %s""" % ( package, f) else: cmd = SED_CMD + """'s|Py_InitModule4(__Pyx_NAMESTR("\(.*\)")|Py_InitModule4(__Pyx_NAMESTR("%s")|g' %s""" % ( package, f) Popen(shlex.split(cmd), cwd=cython_src).communicate() if module != '__init__': cmd = SED_CMD + """'s|init%s|init%s_%s|g' %s""" % ( module, package.replace('.', '_'), module, f) else: cmd = SED_CMD + """'s|init%s|init%s|g' %s""" % ( subpackage, package.replace('.', '_'), f) Popen(shlex.split(cmd), cwd=cython_src).communicate() cmd = SED_CMD + """'s|__pyx_import_star_type_names|__pyx_import_star_type_names_%s%s|g' %s""" % ( package.replace('.', '_'), module, f) Popen(shlex.split(cmd), cwd=cython_src).communicate() if module != '__init__': glue_h += "extern void init%s_%s(void);\n" % (package.replace( '.', '_'), module) glue_c += ' PyImport_AppendInittab("%s.%s", init%s_%s);\n' % ( package, module, package.replace('.', '_'), module) else: glue_h += "extern void init%s(void);\n" % (package.replace( '.', '_')) glue_c += ' PyImport_AppendInittab("%s", init%s);\n' % ( package, package.replace('.', '_')) if options.rocket and not options.bare: # Special case: add libRocket to the glue glue_h += "extern void init_rocketcore(void);\n" glue_c += ' PyImport_AppendInittab("_rocketcore", init_rocketcore);\n' glue_h += "extern void init_rocketcontrols(void);\n" glue_c += ' PyImport_AppendInittab("_rocketcontrols", init_rocketcontrols);\n' # Make package xxx_glue.c with no frozen modules glue = make_glue(package_name, glue_h, glue_c) f = open(join(cython_src, package_name + '_glue.c'), 'w') f.write(glue) f.close() cfiles.append(join(cython_src, package_name + '_glue.c')) # We have to flatten CPP files as well, because this will all be compiled using Python's build system # which is "flat structure" oriented, ie all .o files end up in python/Modules for f in locate( '*.cpp', build_dir, [cython_src, join(build_dir, 'android_project')]): if f not in cfiles: d = f[len(build_dir) + 1:].replace(os.sep, '+') cfile = join(cython_src, d) if not isfile(cfile) or getmtime(f) > getmtime(cfile): cmd = 'cp %s %s' % (f, cfile) Popen(shlex.split(cmd), cwd=build_dir).communicate() cfiles.append(cfile) return cfiles, glue_h, glue_c
def make_python_freeze(platform, modules, frozen_file): """Get a list of python native modules, return them frozen""" target = get_target(platform) frozen_h = '//Ignifuga auto generated file, contains the following modules: %s\n#include "Python.h"\n\n' % ( ','.join(modules)) mod_sizes = {} # Locate the Python library locations = os.listdir(join(HOST_DIST_DIR, 'lib')) python_version = None for l in locations: if l.startswith('python') and isdir(join(HOST_DIST_DIR, 'lib', l)): python_version = l if python_version == None: error('Could not find Python library') exit() # Copy module source to a temp location modtemp = join(target.tmp, 'freezer') if isdir(modtemp): shutil.rmtree(modtemp) os.makedirs(modtemp) for mod in modules: f = join(HOST_DIST_DIR, 'lib', python_version, mod.replace('.', os.sep)) if isdir(f): # It's a package! f = join(HOST_DIST_DIR, 'lib', python_version, mod.replace('.', os.sep), '__init__') + '.py' newf = join(modtemp, mod.replace('.', os.sep), '__init__') + '.py' else: f = f + '.py' newf = join(modtemp, mod.replace('.', os.sep)) + '.py' if isfile(f): if not isdir(dirname(newf)): os.makedirs(dirname(newf)) shutil.copy(f, newf) # Patch some modules if mod == 'site': # Patch USER_BASE, etc cmd = 'patch -p0 -i %s %s' % (join( PATCHES_DIR, 'site.py.diff'), basename(newf)) Popen(shlex.split(cmd), cwd=dirname(newf)).communicate() elif mod == 'platform': # Add Android platform detection cmd = 'patch -p0 -i %s %s' % (join( PATCHES_DIR, 'platform.py.diff'), basename(newf)) Popen(shlex.split(cmd), cwd=dirname(newf)).communicate() f = open(join(modtemp, 'ignifuga_compile.py'), 'w') f.write(""" import compileall compileall.compile_dir("%s") """ % (modtemp, )) f.close() cmd = '%s %s' % (join(HOST_DIST_DIR, 'bin', 'python'), join(modtemp, 'ignifuga_compile.py')) Popen(shlex.split(cmd), cwd=modtemp).communicate() for mod in modules: is_package = False f = join(modtemp, mod.replace('.', os.sep)) if isdir(f): # It's a package! f = join(modtemp, mod.replace('.', os.sep), '__init__') + '.pyc' is_package = True else: f = f + '.pyc' if isfile(f): log("Freezing...%s" % f) fp = open(f, 'rb') if fp.read(4) == imp.get_magic(): fp.read(4) code = marshal.dumps(marshal.load(fp)) mod_sizes[mod] = len(code) if not is_package else -len( code) # A package is signaled by a negative size frozen_h += 'unsigned char M_ignifuga_frozen_%s[] = {' % mod.replace( '.', '_') for i in range(0, len(code), 16): frozen_h += '\n\t' for c in code[i:i + 16]: frozen_h += '%d,' % ord(c) frozen_h += '\n};\n' fp.close() else: error("Could not Freeze...%s" % f) exit() frozen_h += 'static struct _frozen _PyImport_FrozenModules[] = {\n' for mod in modules: if mod in mod_sizes: frozen_h += '\t{"%s",M_ignifuga_frozen_%s, %d},\n' % ( mod, mod.replace('.', '_'), mod_sizes[mod]) frozen_h += '\t{0, 0, 0} /* sentinel */\n};\n' frozen_h += '\nstruct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;\n' f = open(frozen_file, 'w') f.write(frozen_h) f.close()
import os, sys import hashlib import json import time, datetime sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from conf import setting from modules import log INPUT = {} USER_STATUS = {'power': None, 'username': None, 'type': False} # type 用户登陆状态,power:权限;username: 用户 log =log.log(setting.ATM_LOG) def check_lock(user_name): ''' 检查帐号是否加锁 :param user_name:帐号 :return:null ''' file_dict = get_file_dir(setting.PATH_USER) if user_name in file_dict.keys(): user_dict = json.load(open(file_dict[user_name], 'r')) if user_dict['type'] == 'N': return True else: return False
def cythonize(build_dir, package_name, options, skip=[]): files = [] cfiles = [] updatedfiles = [] for f in locate('*.py', build_dir): files.append(f) for f in locate('*.pyx', build_dir): files.append(f) # Cythonize the source files for f in files: if f[len(build_dir):] not in skip and f[len(build_dir)+1:] not in skip: mf = getctime(f) cf = splitext(f)[0] + '.cpp' if not isfile(cf) or getctime(cf) < mf: ccf = join(build_dir, 'cython_src', cf.replace(os.sep, '+')[len(build_dir)+1:]) if not isfile(ccf) or getctime(ccf) < mf: log('Cythonizing %s' % basename(f)) cmd = 'cython --cplus --include-dir "%s/.." "%s"' % (ROOT_DIR, f) p = Popen(shlex.split(cmd), cwd = build_dir) p.communicate() if p.returncode != 0: error("Problem cythonizing file") exit() updatedfiles.append(cf) else: log('Skipping Cython for %s' % basename(f)) cfiles.append(cf) # Flatten the directory structure, replace / by + signs files = cfiles[:] cfiles = [] cython_src = join(build_dir, 'cython_src') if not isdir(cython_src): os.makedirs(cython_src) for f in files: d = f[len(build_dir)+1:].replace(os.sep, '+') cfile = join(cython_src, d) if isfile(f): shutil.move(f, cfile) cfiles.append(cfile) if f in updatedfiles: updatedfiles.remove(f) updatedfiles.append(cfile) # Walk the files, arrange the package in the proper hierachy glue_h = "" glue_c = "" packages = [package_name,] for f in cfiles: filename = basename(f) package = filename.replace('+', '.').replace('.cpp', '') # The last part of the package is the current module module = package.split('.')[-1] # Remove the module from the package package = '.'.join(package.split('.')[:-1]) subpackage = package.split('.') if len(subpackage)>0: subpackage = subpackage[-1] else: subpackage = '' if package != '': package = package_name+'.'+package else: package = package_name if package not in packages: packages.append(package) # Patch the correct paths and package name in the updated cython generated files if f in updatedfiles: log('Patching %s' % (basename(f),)) if module != '__init__': cmd = SED_CMD + """'s|Py_InitModule4(__Pyx_NAMESTR("\(.*\)")|Py_InitModule4(__Pyx_NAMESTR("%s.\\1")|g' %s""" % (package,f) else: cmd = SED_CMD + """'s|Py_InitModule4(__Pyx_NAMESTR("\(.*\)")|Py_InitModule4(__Pyx_NAMESTR("%s")|g' %s""" % (package,f) Popen(shlex.split(cmd), cwd = cython_src).communicate() if module != '__init__': cmd = SED_CMD + """'s|init%s|init%s_%s|g' %s""" % (module,package.replace('.', '_'),module,f) else: cmd = SED_CMD + """'s|init%s|init%s|g' %s""" % (subpackage,package.replace('.', '_'),f) Popen(shlex.split(cmd), cwd = cython_src).communicate() cmd = SED_CMD + """'s|__pyx_import_star_type_names|__pyx_import_star_type_names_%s%s|g' %s""" % (package.replace('.', '_'),module, f) Popen(shlex.split(cmd), cwd = cython_src).communicate() if module != '__init__': glue_h += "extern void init%s_%s(void);\n" % (package.replace('.', '_'),module) glue_c += ' PyImport_AppendInittab("%s.%s", init%s_%s);\n' % (package, module, package.replace('.', '_'),module) else: glue_h += "extern void init%s(void);\n" % (package.replace('.', '_')) glue_c += ' PyImport_AppendInittab("%s", init%s);\n' % (package, package.replace('.', '_')) if options.rocket and not options.bare: # Special case: add libRocket to the glue glue_h += "extern void init_rocketcore(void);\n" glue_c += ' PyImport_AppendInittab("_rocketcore", init_rocketcore);\n' glue_h += "extern void init_rocketcontrols(void);\n" glue_c += ' PyImport_AppendInittab("_rocketcontrols", init_rocketcontrols);\n' # Make package xxx_glue.c with no frozen modules glue = make_glue(package_name, glue_h, glue_c) f = open(join(cython_src, package_name+'_glue.c'), 'w') f.write(glue) f.close() cfiles.append(join(cython_src, package_name+'_glue.c')) # We have to flatten CPP files as well, because this will all be compiled using Python's build system # which is "flat structure" oriented, ie all .o files end up in python/Modules for f in locate('*.cpp', build_dir, [cython_src, join(build_dir, 'android_project')]): if f not in cfiles: d = f[len(build_dir)+1:].replace(os.sep, '+') cfile = join(cython_src, d) if not isfile(cfile) or getmtime(f) > getmtime(cfile): cmd = 'cp %s %s' % (f, cfile) Popen(shlex.split(cmd), cwd = build_dir).communicate() cfiles.append(cfile) return cfiles, glue_h, glue_c
def make_python_freeze(platform, modules, frozen_file): """Get a list of python native modules, return them frozen""" target = get_target(platform) frozen_h = '//Ignifuga auto generated file, contains the following modules: %s\n#include "Python.h"\n\n' % (','.join(modules)) mod_sizes = {} # Locate the Python library locations = os.listdir(join(HOST_DIST_DIR, 'lib')) python_version = None for l in locations: if l.startswith('python') and isdir(join(HOST_DIST_DIR, 'lib', l)): python_version = l if python_version == None: error('Could not find Python library') exit() # Copy module source to a temp location modtemp = join(target.tmp, 'freezer') if isdir(modtemp): shutil.rmtree(modtemp) os.makedirs(modtemp) for mod in modules: f = join(HOST_DIST_DIR, 'lib', python_version, mod.replace('.', os.sep)) if isdir(f): # It's a package! f = join(HOST_DIST_DIR, 'lib', python_version, mod.replace('.', os.sep), '__init__') + '.py' newf = join(modtemp, mod.replace('.', os.sep), '__init__')+'.py' else: f = f + '.py' newf = join(modtemp, mod.replace('.', os.sep)) + '.py' if isfile(f): if not isdir(dirname(newf)): os.makedirs(dirname(newf)) shutil.copy(f, newf) # Patch some modules if mod == 'site': # Patch USER_BASE, etc cmd = 'patch -p0 -i %s %s' % (join(PATCHES_DIR, 'site.py.diff'), basename(newf)) Popen(shlex.split(cmd), cwd=dirname(newf)).communicate() elif mod == 'platform': # Add Android platform detection cmd = 'patch -p0 -i %s %s' % (join(PATCHES_DIR, 'platform.py.diff'), basename(newf)) Popen(shlex.split(cmd), cwd=dirname(newf)).communicate() f = open(join(modtemp, 'ignifuga_compile.py'), 'w') f.write(""" import compileall compileall.compile_dir("%s") """ % (modtemp,)) f.close() cmd = '%s %s' % (join(HOST_DIST_DIR, 'bin', 'python'), join(modtemp, 'ignifuga_compile.py')) Popen(shlex.split(cmd), cwd = modtemp).communicate() for mod in modules: is_package = False f = join(modtemp, mod.replace('.', os.sep)) if isdir(f): # It's a package! f = join(modtemp, mod.replace('.', os.sep), '__init__') + '.pyc' is_package=True else: f = f + '.pyc' if isfile(f): log("Freezing...%s" % f) fp = open(f, 'rb') if fp.read(4) == imp.get_magic(): fp.read(4) code = marshal.dumps(marshal.load(fp)) mod_sizes[mod] = len(code) if not is_package else -len(code) # A package is signaled by a negative size frozen_h+='unsigned char M_ignifuga_frozen_%s[] = {' % mod.replace('.', '_') for i in range(0, len(code), 16): frozen_h+='\n\t' for c in code[i:i+16]: frozen_h+='%d,' % ord(c) frozen_h+='\n};\n' fp.close() else: error("Could not Freeze...%s" % f) exit() frozen_h +='static struct _frozen _PyImport_FrozenModules[] = {\n' for mod in modules: if mod in mod_sizes: frozen_h+='\t{"%s",M_ignifuga_frozen_%s, %d},\n' % (mod, mod.replace('.', '_'), mod_sizes[mod]) frozen_h +='\t{0, 0, 0} /* sentinel */\n};\n' frozen_h +='\nstruct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;\n' f = open(frozen_file, 'w') f.write(frozen_h) f.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author : xiongli import socket, socketserver, pickle, sys, os, subprocess, hashlib, json, platform sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from conf import setting from modules import log log = log.log(setting.PATH_LOGFILE) class MyServer(socketserver.BaseRequestHandler): def handle(self): while True: try: data = self.request.recv(1024).strip().decode() cmd_dic = json.loads(str(data)) action = cmd_dic["action"] print("【{}】收到消息:{}".format(self.client_address[0], action)) log.info("【{}】收到消息:{}".format(self.client_address[0], action)) if hasattr(self, action): function = getattr(self, action) function(cmd_dic) if action == 'quit': break except ConnectionResetError as e: print("error", e) log.info("【{}】error {}".format(self.client_address[0], e)) break