SHOW:
|
|
- or go back to the newest paste.
1 | import sys | |
2 | import zipfile | |
3 | - | import os |
3 | + | |
4 | - | import shutil |
4 | + | |
5 | ||
6 | - | import tkinter as tk |
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 | - | config_folder = script_dir.parent / 'config' |
17 | + | return script_dir / path |
18 | - | resolved_path = config_folder / path |
18 | + | |
19 | - | return resolved_path |
19 | + | def open_qt_config() -> None: |
20 | # Locate "qt-config.ini" file in the "config" folder | |
21 | - | def open_qt_config(): |
21 | + | config_dir = resolve_path('config') |
22 | ||
23 | - | qt_config_file = resolve_path("qt-config.ini") |
23 | + | for file in config_dir.rglob(CONFIG_FILE_NAME): |
24 | if file.is_file(): | |
25 | - | # Check if "qt-config.ini" file exists |
25 | + | qt_config_file = file |
26 | - | if not os.path.isfile(qt_config_file): |
26 | + | break |
27 | - | print("No 'qt-config.ini' file found in the 'config' folder.") |
27 | + | |
28 | - | input("Press Enter to exit.") |
28 | + | print(f"No '{CONFIG_FILE_NAME}' file found in the 'config' folder.") |
29 | return | |
30 | ||
31 | - | # Read nand_directory from qt-config.ini |
31 | + | with open(qt_config_file) as file: |
32 | - | with open(qt_config_file, 'r') as file: |
32 | + | |
33 | if line.startswith('nand_directory='): | |
34 | input_folder = Path(line.replace('nand_directory=', '').strip(), 'user', 'save') | |
35 | - | input_folder = line.replace('nand_directory=', '').strip() |
35 | + | |
36 | - | # Append additional information to the input_folder path |
36 | + | |
37 | - | input_folder = os.path.join(input_folder, 'user', 'save') |
37 | + | print(f"No 'nand_directory' entry found in '{CONFIG_FILE_NAME}'.") |
38 | return | |
39 | ||
40 | - | print("No 'nand_directory' entry found in 'qt-config.ini'.") |
40 | + | |
41 | - | input("Press Enter to exit.") |
41 | + | output_file = filedialog.asksaveasfilename( |
42 | title="Select output file", | |
43 | defaultextension=".zip", | |
44 | filetypes=(("Zip Files", "*.zip"),) | |
45 | - | output_file = filedialog.asksaveasfilename(title="Select output file", defaultextension=".zip", 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 root_folder, subfolders, filenames in os.walk(input_folder): |
54 | + | for file in input_folder.rglob('*'): |
55 | - | # Get the relative path of the current folder |
55 | + | if file.is_dir() or 'cache' in str(file.parent): |
56 | - | relative_folder = os.path.relpath(root_folder, input_folder) |
56 | + | continue |
57 | ||
58 | - | # Exclude the "cache" folder from being included in the .zip file |
58 | + | relative_path = file.relative_to(input_folder) |
59 | - | if 'cache' in subfolders: |
59 | + | |
60 | - | subfolders.remove('cache') |
60 | + | zipf.write(file, arcname=relative_path) |
61 | ||
62 | - | # Traverse files in the current folder |
62 | + | print(f"Compressed file copy created at: {output_file}") |
63 | - | for filename in filenames: |
63 | + | |
64 | - | # Build the source and destination paths for each file |
64 | + | |
65 | - | input_file = os.path.join(root_folder, filename) |
65 | + | |
66 | - | relative_file = os.path.join(relative_folder, filename) |
66 | + | |
67 |