def get_module_as_zip_from_module_directory(module_directory, b64enc=True, src=True): """Compress a module directory @param module_directory: The module directory @param base64enc: if True the function will encode the zip file with base64 @param src: Integrate the source files @return: a stream to store in a file-like object """ RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I) def _zippy(archive, path, src=True): path = os.path.abspath(path) base = os.path.basename(path) for f in tools.osutil.listdir(path, True): bf = os.path.basename(f) if not RE_exclude.search(bf) and (src or bf == '__terp__.py' or not bf.endswith('.py')): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) archive.writepy(module_directory) _zippy(archive, module_directory, src=src) archive.close() val = archname.getvalue() archname.close() if b64enc: val = base64.encodestring(val) return val
def compile_module(mod_creator_name=None, root=None, mod_scripts_folder=None, mod_name=None, ignore_folders=None, include_folders=None): if not mod_creator_name: mod_creator_name = creator_name if not mod_name: mod_name = os.path.join('..', '..', os.path.basename(os.path.normpath(os.path.dirname(os.path.realpath('__file__'))))) print('No mod name found, setting the path name to \'{}\'.'.format(mod_name)) print('The current working directory {}.'.format(os.getcwd())) if mod_creator_name: print('Mod creator name found, appending mod creator name to file name.') mod_name = '{}_{}'.format(mod_creator_name, mod_name) script_zip_name = '{}.ts4script'.format(mod_name) if not root: ts4script = script_zip_name else: ts4script = os.path.join(root, script_zip_name) try: if os.path.exists(ts4script): print('Script archive found, removing found archive.') os.remove(ts4script) print('Script archive removed.') zf = PyZipFile(ts4script, mode='w', allowZip64=True, optimize=2) child_directories = get_child_directories(mod_scripts_folder) previous_working_directory = os.getcwd() print('Changing the working directory to \'{}\''.format(mod_scripts_folder)) os.chdir(mod_scripts_folder) print('Changed the current working directory \'{}\'.'.format(os.getcwd())) # print('Found child directories {}'.format(pformat(tuple(child_directories)))) for folder in child_directories: # print('Attempting to compile {}'.format(folder)) if ignore_folders is not None and os.path.basename(folder) in ignore_folders: # print('Folder is set to be ignored. Continuing to the next folder.') continue if include_folders is not None and os.path.basename(folder) not in include_folders: # print('Folder is not set to be included. Continuing to the next folder.') continue try: print('Compiling folder \'{}\''.format(folder)) zf.writepy(folder) print('\'{}\' compiled successfully.'.format(folder)) except Exception as ex: print('Failed to write {}. {}'.format(folder, ex.args[1])) continue print('Done compiling files.') zf.close() print('Changing working directory to previous working directory.') os.chdir(previous_working_directory) print('Changed the current working directory to \'{}\''.format(os.getcwd())) except Exception as ex: print('Failed to create {}. {}'.format(ts4script, ex.args[1])) return
def build_zip(module_dir): # This can fail at writepy if there is something wrong with the files # in xframes. Go ahead anyway, but things will probably fail if this job is # distributed try: tf = NamedTemporaryFile(suffix=".zip", delete=False) z = PyZipFile(tf, "w") z.writepy(module_dir) z.close() return tf.name except: logging.warn("Zip file distribution failed -- workers will not get xframes code.") logging.warn("Check for unexpected files in xframes directory.") return None
def compile_module(creator_name, root, mods_folder,mod_name=None): src = os.path.join(root, 'Scripts') if not mod_name: mod_name=os.path.basename(os.path.normpath(os.path.dirname(os.path.realpath('__file__')))) mod_name = creator_name + '_' + mod_name ts4script = os.path.join(root, mod_name + '.ts4script') ts4script_mods = os.path.join(os.path.join(mods_folder), mod_name + '.ts4script') zf = PyZipFile(ts4script, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) for folder, subs, files in os.walk(src): zf.writepy(folder) zf.close() shutil.copyfile(ts4script, ts4script_mods)
def build_zip(): if 'XPATTERNS_HOME' not in os.environ: return None # This can fail at writepy if there is something wrong with the files # in xpatterns. Go ahead anyway, but things will probably fail of this job is # distributed try: tf = NamedTemporaryFile(suffix='.zip', delete=False) z = PyZipFile(tf, 'w') z.writepy(os.path.join(os.environ['XPATTERNS_HOME'], 'xpatterns')) z.close() return tf.name except: print 'Zip file distribution failed -- workers will not get xpatterns code.' print 'Check for unexpected files in XPATTERNS_HOME/xpatterns.' return None
def build_zip(module_dir): # This can fail at writepy if there is something wrong with the files # in xframes. Go ahead anyway, but things will probably fail if this job is # distributed try: tf = NamedTemporaryFile(suffix='.zip', delete=False) z = PyZipFile(tf, 'w') z.writepy(module_dir) z.close() return tf.name except: logging.warn( 'Zip file distribution failed -- workers will not get xframes code.' ) logging.warn('Check for unexpected files in xframes directory.') return None
def collect_python_modules(): """Collect Eskapade Python modules.""" import pathlib from pkg_resources import resource_filename from zipfile import PyZipFile import escore package_dir = resource_filename(escore.__name__, '') lib_path = pathlib.Path(package_dir).joinpath('lib') lib_path.mkdir(exist_ok=True) archive_path = str(lib_path.joinpath(ARCHIVE_FILE)) archive_file = PyZipFile(archive_path, 'w') logger.info('Adding Python modules to egg archive {path}.'.format(path=archive_path)) archive_file.writepy(package_dir) archive_file.close() return archive_path
def get_zip_from_directory(self, directory, b64enc=True): def _zippy(archive, path): path = os.path.abspath(path) base = os.path.basename(path) for f in tools.osutil.listdir(path, True): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) archive.writepy(directory) _zippy(archive, directory) archive.close() val = archname.getvalue() archname.close() if b64enc: val = base64.encodestring(val) return val
def zip_directory(directory, b64enc=True, src=True): """Compress a directory @param directory: The directory to compress @param base64enc: if True the function will encode the zip file with base64 @param src: Integrate the source files @return: a string containing the zip file """ RE_exclude = re.compile( '(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I) def _zippy(archive, path, src=True): path = os.path.abspath(path) base = os.path.basename(path) for f in osutil.listdir(path, True): bf = os.path.basename(f) if not RE_exclude.search(bf) and (src or bf in ('__openerp__.py', '__terp__.py') or not bf.endswith('.py')): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) # for Python 2.5, ZipFile.write() still expects 8-bit strings (2.6 converts to utf-8) directory = tools.ustr(directory).encode('utf-8') archive.writepy(directory) _zippy(archive, directory, src=src) archive.close() archive_data = archname.getvalue() archname.close() if b64enc: return base64.encodestring(archive_data) return archive_data
def get_zip_from_directory(directory, b64enc=True): RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I) def _zippy(archive, path): path = os.path.abspath(path) base = os.path.basename(path) for f in tools.osutil.listdir(path, True): bf = os.path.basename(f) if not RE_exclude.search(bf): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) archive.writepy(directory) _zippy(archive, directory) archive.close() val = archname.getvalue() archname.close() if b64enc: val = base64.encodestring(val) return val
def build_zip(dest): print "Writing", dest from zipfile import PyZipFile f = PyZipFile(dest, "w") f.writepy("src/singleshot") f.writepy("lib") f.writepy("lib/simpletal") f.close()
def get_module_as_zip_from_module_directory(module_directory, b64enc=True, src=True): """Compress a module directory @param module_directory: The module directory @param base64enc: if True the function will encode the zip file with base64 @param src: Integrate the source files @return: a stream to store in a file-like object """ RE_exclude = re.compile( '(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I) def _zippy(archive, path, src=True): path = os.path.abspath(path) base = os.path.basename(path) for f in tools.osutil.listdir(path, True): bf = os.path.basename(f) if not RE_exclude.search(bf) and (src or bf in ('__openerp__.py', '__terp__.py') or not bf.endswith('.py')): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) archive.writepy(module_directory) _zippy(archive, module_directory, src=src) archive.close() val = archname.getvalue() archname.close() if b64enc: val = base64.encodestring(val) return val
def zip_directory(directory, b64enc=True, src=True): """Compress a directory @param directory: The directory to compress @param base64enc: if True the function will encode the zip file with base64 @param src: Integrate the source files @return: a string containing the zip file """ RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I) def _zippy(archive, path, src=True): path = os.path.abspath(path) base = os.path.basename(path) for f in tools.osutil.listdir(path, True): bf = os.path.basename(f) if not RE_exclude.search(bf) and (src or bf in ('__openerp__.py', '__terp__.py') or not bf.endswith('.py')): archive.write(os.path.join(path, f), os.path.join(base, f)) archname = StringIO() archive = PyZipFile(archname, "w", ZIP_DEFLATED) # for Python 2.5, ZipFile.write() still expects 8-bit strings (2.6 converts to utf-8) directory = tools.ustr(directory).encode('utf-8') archive.writepy(directory) _zippy(archive, directory, src=src) archive.close() archive_data = archname.getvalue() archname.close() if b64enc: return base64.encodestring(archive_data) return archive_data
from zipfile import PyZipFile, ZIP_STORED from settings import * root = os.path.dirname(os.path.realpath('__file__')) mod_name = None if __name__ == "__main__": mod_name = input( "Type the name of your mod and hit enter or just hit enter to skip naming: " ) src = os.path.join(root, 'Scripts') if not mod_name: mod_name = os.path.basename( os.path.normpath(os.path.dirname(os.path.realpath('__file__')))) mod_name = creator_name + '_' + mod_name ts4script = os.path.join(root, mod_name + '.ts4script') ts4script_mods = os.path.join(os.path.join(mods_folder), mod_name + '.ts4script') zf = PyZipFile(ts4script, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) for folder, subs, files in os.walk(src): zf.writepy(folder) zf.close() shutil.copyfile(ts4script, ts4script_mods)
#!/usr/bin/env python # # Packs Python standard library into zip file python$(ver).zip # import os, os.path, shutil, sys from zipfile import PyZipFile name = "python%i%i.zip" % (sys.version_info[0], sys.version_info[1]) print "creating %s..." % name # delete tests, we don't need them: for root, dirs, files in os.walk("Lib", topdown=False): if "test" in dirs: shutil.rmtree(os.path.join(root, "test")) # pack Lib to a zipfile: zip = PyZipFile(name, mode="w") for f in os.listdir("Lib"): fn = os.path.join("Lib", f) if os.path.isdir(fn) or fn.endswith(".py"): zip.writepy(fn) else: print "warning: ignoring file %s" % f zip.close()
def compile_mod(cls, names_of_modules_include: Iterator[str], folder_path_to_output_ts4script_to: str, output_ts4script_name: str, names_of_modules_to_exclude: str = None, mod_creator_name: str = None, folder_path_to_gather_script_modules_from: str = '..'): """compile_mod(\ names_of_modules_include,\ folder_path_to_output_ts4script_to,\ output_ts4script_name,\ names_of_modules_to_exclude=None,\ mod_creator_name=None,\ folder_path_to_gather_script_packages_from=None\ ) Compile a mod using unpyc3. """ os.makedirs(folder_path_to_output_ts4script_to, exist_ok=True) from compile_utils import _remove_files_conflicting_with_decompile, _replace_renamed_files _remove_files_conflicting_with_decompile(decompile_ea_scripts=False) names_of_modules_include = tuple(names_of_modules_include) if not mod_creator_name: mod_creator_name = creator_name if not output_ts4script_name: output_ts4script_name = os.path.join( '..', '..', os.path.basename( os.path.normpath( os.path.dirname(os.path.realpath('__file__'))))) print('No mod name found, setting the path name to \'{}\'.'.format( output_ts4script_name)) print(f'The current working directory {os.getcwd()}.') if mod_creator_name: print( 'Mod creator name found, appending mod creator name to file name.' ) output_ts4script_name = '{}_{}'.format(mod_creator_name, output_ts4script_name) output_script_zip_name = '{}.ts4script'.format(output_ts4script_name) if not folder_path_to_output_ts4script_to: ts4script = output_script_zip_name else: ts4script = os.path.join(folder_path_to_output_ts4script_to, output_script_zip_name) # noinspection PyBroadException try: if os.path.exists(ts4script): print('Script archive found, removing found archive.') os.remove(ts4script) print('Script archive removed.') output_zip = PyZipFile(ts4script, mode='w', allowZip64=True, optimize=2) previous_working_directory = os.getcwd() if folder_path_to_gather_script_modules_from is not None: print( f'Changing the working directory to \'{folder_path_to_gather_script_modules_from}\'' ) os.chdir(folder_path_to_gather_script_modules_from) else: folder_path_to_gather_script_modules_from = '..' os.chdir(folder_path_to_gather_script_modules_from) print(f'Changed the current working directory \'{os.getcwd()}\'.') # print('Found child directories {}'.format(pformat(tuple(child_directories)))) for folder_path in cls._child_directories_gen(os.getcwd()): # print(f'Attempting to compile {folder_path}') if names_of_modules_to_exclude is not None and os.path.basename( folder_path) in names_of_modules_to_exclude: # print(f'Folder is set to be ignored {folder_path}. Continuing to the next folder.') continue if names_of_modules_include is not None and os.path.basename( folder_path) not in names_of_modules_include: # print(f'Folder is not set to be included {folder_path}. Continuing to the next folder.') continue # noinspection PyBroadException try: print(f'Compiling folder \'{folder_path}\'') output_zip.writepy(folder_path) print(f'\'{folder_path}\' compiled successfully.') except Exception as ex: print(f'Failed to write {folder_path}. {ex}') traceback.print_exc() continue print('Done compiling modules.') output_zip.close() print('Changing working directory to previous working directory.') os.chdir(previous_working_directory) print( f'Changed the current working directory to \'{os.getcwd()}\'') except Exception as ex: print(f'Failed to create {ts4script}. {ex}') return finally: _replace_renamed_files(decompile_ea_scripts=False)