def GenerateMission(filename): # Create XML parser parser = make_parser() generator = MissionGenerator() parser.setContentHandler(generator) # Parse KMZ file kmz = PyZipFile(filename, 'r') kml = kmz.open('doc.kml', 'r') parser.parse(kml) kmz.close() # Arrange the XML items into useful variables items = {} items['base_location'] = ParseCoordinates(generator.mapping, 'Base') items['landing_site'] = ParseCoordinates(generator.mapping, 'Fake reported location') items['path'] = ParseCoordinates(generator.mapping, 'Sample Nominal Path') items['return_path']= items['path'].reverse() items['base_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Base geofence')) items['landing_site_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Sample remote landing site')) items['mission_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Sample full geofence')) return items
def compile_src(creator_name: str, src_dir: str, build_dir: str, mods_dir: str, mod_name: str = "Untitled") -> None: """ Packages your mod into a proper mod file. It creates 2 mod files, a full mod file which contains all the files in the source folder unchanged along with the compiled python versions next to uncompiled ones and a slim mod-file which contains only the compiled versions. Modified from andrew's code. https://sims4studio.com/thread/15145/started-python-scripting :param creator_name: The creators name :param src_dir: Source dir for the mod files :param build_dir: Place to put the mod files :param mods_dir: Place to an extra copy of the slim mod file for testing :param mod_name: Name to call the mod :return: Nothing """ # Prepend creator name to mod name mod_name = creator_name + '_' + mod_name mods_sub_dir = os.path.join(mods_dir, mod_name) # Create ts4script paths ts4script_full_build_path = os.path.join(build_dir, mod_name + '.ts4script') ts4script_mod_path = os.path.join(mods_sub_dir, mod_name + '.ts4script') print("Clearing out old builds...") # Delete and re-create build and sub-folder in Mods is_devmode = symlink_exists_win("", mods_dir, mod_name) symlink_remove_win("", mods_dir, mod_name) if is_devmode: print("Exiting Dev Mode...") remove_dir(build_dir) ensure_path_created(build_dir) ensure_path_created(mods_sub_dir) print("Re-building mod...") # Compile the mod zf = PyZipFile(ts4script_full_build_path, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) compile_full(src_dir, zf) zf.close() # Copy it over to the mods folder shutil.copyfile(ts4script_full_build_path, ts4script_mod_path) print("----------") print("Complete")
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 debug_install_mod(mod_src: str, mods_dir: str, mod_name: str, mod_folder_name: str) -> None: """ Compiles and installs the mod which adds a cheat code so the user can setup the debugger in-game :param mod_src: Path to the script which does this :param mods_dir: Path to the users mod folder :param mod_name: Name of the mod :param mod_folder_name: Name of mod Subfolder :return: Nothing """ print("Compiling and installing the cheatcode mod...") # Get destination file path mods_sub_dir = os.path.join(mods_dir, mod_folder_name) mod_path = os.path.join(mods_sub_dir, mod_name + '.ts4script') ensure_path_created(mods_sub_dir) # Get compiled path and compile mod mod_src_pyc = replace_extension(mod_src, "pyc") py_compile.compile(mod_src, mod_src_pyc) # Create mod at destination and add compiled file to it zf = PyZipFile(mod_path, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) zf.write(mod_src_pyc, mod_name + ".pyc") zf.close()
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 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(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 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 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 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)
def debug_install_egg(egg_path: str, mods_dir, dest_name: str, mod_folder_name: str) -> None: """ Copies the debug egg provided by Pycharm Pro which adds the capability to make debugging happen inside of PyCharm Pro. A bit of work goes into this so it'll be much slower. :param egg_path: Path to the debug egg :param mods_dir: Path to the mods folder :param dest_name: Name of the mod :param mod_folder_name: Name of mod Subfolder :return: """ print("Re-packaging and installing the debugging capability mod...") # Get egg filename and path filename = Path(egg_path).name mods_sub_dir = os.path.join(mods_dir, mod_folder_name) mod_path = os.path.join(mods_sub_dir, dest_name + ".ts4script") ensure_path_created(mods_sub_dir) # Get python ctypes folder sys_ctypes_folder = os.path.join(get_sys_folder(), "Lib", "ctypes") # Create temp directory tmp_dir = tempfile.TemporaryDirectory() tmp_egg = tmp_dir.name + os.sep + filename # Remove old mod in mods folder there, if it exists remove_file(mod_path) # Copy egg to temp path shutil.copyfile(egg_path, tmp_egg) # Extract egg # This step is a bit redundant but I need to copy over everything but one folder into the zip file and I don't # know how to do that in python so I copy over the zip, extract it, copy in the whole folder, delete the one # sub-folder, then re-zip everything up. It's a pain but it's what I know hwo to do now and Google's not much help zip = PyZipFile(tmp_egg) zip.extractall(tmp_dir.name) zip.close() # Remove archive remove_file(tmp_egg) # Copy ctype folder to extracted archive shutil.copytree(sys_ctypes_folder, tmp_dir.name + os.sep + "ctypes") # Remove that one folder remove_dir(tmp_dir.name + os.sep + "ctypes" + os.sep + "__pycache__") # Grab a handle on the egg zf = PyZipFile(mod_path, mode='w', compression=ZIP_STORED, allowZip64=True, optimize=2) # Add all the files in the tmp directory to the zip file for folder, subs, files in os.walk(tmp_dir.name): for file in files: archive_path = get_rel_path(folder + os.sep + file, tmp_dir.name) zf.write(folder + os.sep + file, archive_path) zf.close() # There's a temporary directory bug that causes auto-cleanup to sometimes fail # We're preventing crash messages from flooding the screen to keep things tidy try: tmp_dir.cleanup() except: pass
#!/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)