def create_install_bat(install_bat_file, remove_previous=False, **kwargs): # Gets the logs level values from the kwargs level = 0 if "level" in kwargs: level = kwargs["level"] # if a clean is needed, the old one is deleted. if os.path.exists(install_bat_file) and remove_previous: logger.info(("Removing Current install.bat -> %s" % install_bat_file), level=level) os.remove(install_bat_file) # if the install_bat_file doesn't exist, is created. if not os.path.exists(install_bat_file): logger.info(("Creating install.bat -> %s" % install_bat_file), level=level) install_bat_source_file = os.path.join(get_current_folder(), "install", "install.bat") shutil.copyfile(install_bat_source_file, install_bat_file) # Searches pip dependent modules to prepare the install.bat for a good resources install. if os.path.exists(install_bat_file): pip_dependencies = inspector.get_file_pip_dependencies(source_file, recursive=True) if pip_dependencies: for pip_dependency in pip_dependencies: logger.info(("Setting up pip module install -> %s" % pip_dependency), level=level) pip_pattern = "REM START /WAIT python -m pip install <module_name>" pip_string = "START /WAIT python -m pip install <module_name>" pip_string = pip_pattern + "\n" + pip_string.replace("<module_name>", pip_dependency) io.replace_line_in_file(install_bat_file, pip_pattern, pip_string)
def create_execute_bat(execute_bat_file, execution_file_name, remove_previous=False, compiled=True, standalone=True, **kwargs): # Gets the logs level values from the kwargs level = 0 if "level" in kwargs: level = kwargs["level"] # if a clean is needed, the old one is deleted. if os.path.exists(execute_bat_file) and remove_previous: logger.info(("Removing Current execute.bat -> %s" % execute_bat_file), level=level) os.remove(execute_bat_file) # if the execute_bat_file doesn't exist, is created. if not os.path.exists(execute_bat_file): logger.info(("Creating execute.bat -> %s" % execute_bat_file), level=level) execute_bat_source_file = os.path.join(get_current_folder(), "source", "execute.bat") shutil.copyfile(execute_bat_source_file, execute_bat_file) # Searches pip dependent modules to prepare the install.bat for a good resources install. if os.path.exists(execute_bat_file): logger.info(("Setting up execution string -> %s" % execute_bat_file), level=level) execute_pattern = "START <python_command> <file_name>" execute_string = "START <python_command> <file_name>" if standalone: execute_string = execute_string.replace("<python_command>", os.path.join("python_dist", "python.exe")) else: execute_string = execute_string.replace("<python_command>", "python") if compiled: execution_file_name = execution_file_name.replace(".py", ".pyc") execute_string = execute_string.replace("<file_name>", execution_file_name) io.replace_line_in_file(execute_bat_file, execute_pattern, execute_string)
def pack_file(source_file, pack_folder=None, recursive=True, **kwargs): """Summary Args: source_file (string): Path of the file to pack. pack_folder (string, optional): Destination Pack Folder. recursive (bool, optional): Indicates if pack all dependencies. Default True. **kwargs: Extra arguments to have some more options. level (int): Indicates the level of depth in the logs. packaging_mode (string): Indicates the packaging mode for the file. To do special operations. packaging_type (string): Indicates the type of packaging for the file. remove_previous (bool): Indicates if removes previous packaging. Returns: string: The path to the created file. None if not created. """ # Some configurations remove_previous_packaging_modes = ["root"] explorable_packaging_types = ["lib"] explorable_file_types = [".py"] packable_import_types = ["custom_module"] # Gets the logs level values from the kwargs level = 0 if "level" in kwargs: level = kwargs["level"] packaging_mode = "root" if "packaging_mode" in kwargs: packaging_mode = kwargs["packaging_mode"] packaging_type = "lib" if "packaging_type" in kwargs: packaging_type = kwargs["packaging_type"] remove_previous = False if "remove_previous" in kwargs: remove_previous = kwargs["remove_previous"] compiled = True if "compiled" in kwargs: compiled = kwargs["compiled"] standalone = True if "standalone" in kwargs: standalone = kwargs["standalone"] # If source file does not exist, cannot start the packaging. if not os.path.exists(source_file): logger.error(("File to Pack does not exist -> %s" % source_file), level=level) return None # Shows info about the file to package. logger.info(("Packaging File -> %s" % source_file), level=level) # This should be the destination path folder # If is the root one, needs to add the subfolder with tool name. # If not, the packaging type should be used as subfolder name. If already is in that level, does not add it again. package_sub_folder = "" if packaging_mode == "root": # package_sub_folder = os.path.basename(os.path.splitext(source_file)[0]) package_sub_folder = "" elif os.path.basename(os.path.normpath(pack_folder)) != packaging_type: package_sub_folder = packaging_type if not package_sub_folder.startswith("lib") and os.path.basename(os.path.normpath(pack_folder)) != "lib": package_sub_folder = os.path.join("lib", package_sub_folder) dest_folder = os.path.join(pack_folder, package_sub_folder) logger.info(("Destination Folder -> %s" % dest_folder), level=level) # This should be the destination path file dest_file = os.path.join(dest_folder, os.path.basename(source_file)) logger.info(("Destination File -> %s" % dest_file), level=level) # If has to remove the previous and it exists, mark it remove_previous_folder = remove_previous and packaging_mode in remove_previous_packaging_modes # If dest pack folder does not exist, cannot start the packaging. Tries to create it. setup_file_pack_folder(dest_file, remove_previous=remove_previous_folder, level=level+1) # Gets the destination file type. file_type = os.path.splitext(dest_file)[1] logger.info(("File Type -> %s" % dest_file), level=level) # Prints the type of packaging. logger.info(("Packaging Type -> %s" % packaging_type), level=level) shutil.copyfile(source_file, dest_file) if os.path.exists(dest_file): logger.info(("File Packaged -> %s" % dest_file), level=level) # If the packaging_mode is root, need to create the execute.bat if packaging_mode == "root": execute_bat_file = os.path.join(os.path.dirname(dest_file), "execute.bat") create_execute_bat(execute_bat_file, os.path.basename(dest_file), compiled=compiled, standalone=standalone, remove_previous=True, level=level+1) # In this case is explorable. Can contain imports, ui dependencies, etc. if packaging_type in explorable_packaging_types and file_type in explorable_file_types: logger.info(("File can be recursive explored -> %s" % source_file), level=level) logger.info(("Searching source file imports -> %s" % source_file), level=level) imports_info = inspector.get_file_imports_info(source_file) # If it has imports info, packages the imports as libraries if imports_info: import_relative_path = "lib" if packaging_mode == "root" else "" for import_info in imports_info: if import_info["type"] in packable_import_types: # Packages the import source as a library logger.info(("Packaging lib file -> %s" % import_info["path"]), level=level) pack_file(import_info["path"], pack_folder=dest_folder, level=level+1, packaging_type="lib", packaging_mode="regular") # After packaging the lib, must replace in dest_file the import for the new one. import_statement = inspector.build_import_statement(import_info, force_relative=True, relative_path=import_relative_path) logger.info(("Replacing import -> %s <- with -> %s" % (import_info["source"], import_statement)), level=level) io.replace_line_in_file(dest_file, import_info["source"], import_statement, keep_old_commented=True) # Search dependencies like ui files and package them. logger.info(("Searching source UI dependencies -> %s" % source_file), level=level) ui_search_folder = os.path.dirname(source_file) ui_files = inspector.get_file_ui_dependencies(source_file) # If it has ui files, packages the files as uis if ui_files: for ui_file in ui_files: # Prints the type of packaging. logger.info(("Packaging UI dependency -> %s" % ui_file), level=level) pack_file(ui_file, pack_folder=dest_folder, level=level+1, packaging_type="uis", packaging_mode="regular") # Search dependencies like png and jpg files and package them. logger.info(("Searching source Image dependencies -> %s" % source_file), level=level) image_search_folder = os.path.dirname(source_file) image_files = inspector.get_file_image_dependencies(source_file) # If it has image files, packages the files as images if image_files: for image_file in image_files: # Prints the type of packaging. logger.info(("Packaging Image dependency -> %s" % image_file), level=level) pack_file(image_file, pack_folder=dest_folder, level=level+1, packaging_type="images", packaging_mode="regular") # Search dependencies like qss files and package them. logger.info(("Searching source QSS dependencies -> %s" % source_file), level=level) qss_search_folder = os.path.dirname(source_file) qss_files = inspector.get_file_qss_dependencies(source_file) # If it has qss files, packages the files as themes if qss_files: for qss_file in qss_files: # Prints the qss dependency. logger.info(("Packaging QSS dependency -> %s" % qss_file), level=level) # pack_file(qss_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "styles"), packaging_mode="regular") pack_file(qss_file, pack_folder=dest_folder, level=level+1, packaging_type="styles", packaging_mode="regular") # Search dependencies like font .ttf files and package them. logger.info(("Searching source font TTF dependencies -> %s" % source_file), level=level) font_search_folder = os.path.dirname(source_file) font_files = inspector.get_file_font_dependencies(source_file) # If it has ttf files, packages the files as fonts if font_files: for font_file in font_files: # Prints the font dependency. logger.info(("Packaging TTF dependency -> %s" % font_file), level=level) # pack_file(font_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "fonts"), packaging_mode="regular") pack_file(font_file, pack_folder=dest_folder, level=level+1, packaging_type="fonts", packaging_mode="regular") # Search dependencies like qss files and package them. logger.info(("Searching source EXE dependencies -> %s" % source_file), level=level) exe_search_folder = os.path.dirname(source_file) exe_files = inspector.get_file_exe_dependencies(source_file) # If it has exe files, packages the files as exes if exe_files: for exe_file in exe_files: # Prints the exe dependency. logger.info(("Packaging EXE dependency -> %s" % exe_file), level=level) # pack_file(exe_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "exes"), packaging_mode="regular") pack_file(exe_file, pack_folder=dest_folder, level=level+1, packaging_type="exes", packaging_mode="regular") else: logger.info(("Packaging/File Type non explorable. Direct Copy to -> %s" % dest_file), level=level) # Finally returns the packaged file path. return dest_file else: logger.error(("File could not be packaged -> %s" % dest_file), level=level) return None