Advertisement
Diapolo10

Pyinstaller Qt config fix

Jun 16th, 2023 (edited)
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import zipfile
  3. from pathlib import Path
  4. from tkinter import filedialog
  5.  
  6. CONFIG_FILE_NAME = 'qt-config.ini'
  7.  
  8.  
  9. def resolve_path(path: str | Path) -> Path:
  10.     if getattr(sys, "frozen", False):
  11.         # If the script is running as a bundled app (.exe)
  12.         script_dir = Path(sys.executable).resolve().parent
  13.     else:
  14.         # If the script is running in development mode
  15.         script_dir = Path(__file__).resolve().parent
  16.  
  17.     return script_dir / path
  18.  
  19. def open_qt_config() -> None:
  20.     # Locate "qt-config.ini" file in the "config" folder
  21.     config_dir = resolve_path('config')
  22.  
  23.     for file in config_dir.rglob(CONFIG_FILE_NAME):
  24.         if file.is_file():
  25.             qt_config_file = file
  26.             break
  27.     else:
  28.         print(f"No '{CONFIG_FILE_NAME}' file found in the 'config' folder.")
  29.         return
  30.  
  31.     with open(qt_config_file) as file:
  32.         for line in file:
  33.             if line.startswith('nand_directory='):
  34.                 input_folder = Path(line.replace('nand_directory=', '').strip(), 'user', 'save')
  35.                 break
  36.         else:
  37.             print(f"No 'nand_directory' entry found in '{CONFIG_FILE_NAME}'.")
  38.             return
  39.  
  40.     # Select output file
  41.     output_file = filedialog.asksaveasfilename(
  42.         title="Select output file",
  43.         defaultextension=".zip",
  44.         filetypes=(("Zip Files", "*.zip"),)
  45.     )
  46.  
  47.     if not output_file:
  48.         print("No output file location or name selected.")
  49.         return
  50.  
  51.     # Create a .zip file in write mode
  52.     with zipfile.ZipFile(output_file, "w", compression=zipfile.ZIP_DEFLATED) as zipf:
  53.         # Traverse files and folders in the input folder
  54.         for file in input_folder.rglob('*'):
  55.             if file.is_dir() or 'cache' in str(file.parent):
  56.                 continue
  57.                
  58.             relative_path = file.relative_to(input_folder)
  59.            
  60.             zipf.write(file, arcname=relative_path)
  61.  
  62.     print(f"Compressed file copy created at: {output_file}")
  63.  
  64. if __name__ == "__main__":
  65.     open_qt_config()
  66.     input("Press Enter to exit.")
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement