Add PyInstaller compatibility for diskpart script location handling

- Detect if running as PyInstaller executable using sys.frozen
- Use different directory priority order for PyInstaller vs development:
  - PyInstaller: prioritize user home directory and executable location
  - Development: prioritize current working directory
- Include executable directory (sys.executable) as fallback for PyInstaller
- Ensures diskpart script creation works reliably in both scenarios
- Addresses file access issues when running as compiled executable
parent 57273887
......@@ -275,25 +275,70 @@ assign letter={usb_drive[0]}
exit
"""
# Write diskpart script to a safe temporary location
# Write diskpart script to an accessible location - try multiple directories
import tempfile
import os
# Use Windows temp directory and ensure file is accessible
temp_dir = os.environ.get('TEMP', os.environ.get('TMP', 'C:\\Windows\\Temp'))
script_path = os.path.join(temp_dir, f'diskpart_script_{os.getpid()}.txt')
script_filename = f'diskpart_script_{os.getpid()}.txt'
# Check if we're running as a PyInstaller executable
is_frozen = getattr(sys, 'frozen', False)
# Try multiple directories in order of preference
# For PyInstaller executables, prefer more reliable directories first
if is_frozen:
candidate_dirs = [
os.path.expanduser('~'), # User home directory (most reliable for executables)
os.environ.get('USERPROFILE', ''), # Windows user profile
tempfile.gettempdir(), # Python's temp directory
os.environ.get('TEMP', ''), # Windows TEMP
os.environ.get('TMP', ''), # Windows TMP
os.path.dirname(sys.executable), # Directory where executable is located
'C:\\Temp', # Common temp directory
os.getcwd(), # Current working directory (may be temp for PyInstaller)
'C:\\Windows\\Temp' # System temp directory
]
else:
candidate_dirs = [
os.getcwd(), # Current working directory (good for development)
os.path.expanduser('~'), # User home directory
os.environ.get('USERPROFILE', ''), # Windows user profile
tempfile.gettempdir(), # Python's temp directory
os.environ.get('TEMP', ''), # Windows TEMP
os.environ.get('TMP', ''), # Windows TMP
'C:\\Temp', # Common temp directory
'C:\\Windows\\Temp' # System temp directory
]
script_path = None
last_error = None
# Try each directory until we find one that works
for temp_dir in candidate_dirs:
if not temp_dir or not os.path.exists(temp_dir):
continue
try:
candidate_path = os.path.join(temp_dir, script_filename)
# Try to write script file
with open(candidate_path, 'w') as script_file:
script_file.write(diskpart_script)
# Verify file exists and is readable
if os.path.exists(candidate_path):
script_path = candidate_path
self.status.emit(f"Created diskpart script: {script_path}")
break
except Exception as e:
last_error = e
continue
if not script_path:
raise Exception(f"Failed to create diskpart script in any accessible directory. Last error: {last_error}")
try:
# Write script file
with open(script_path, 'w') as script_file:
script_file.write(diskpart_script)
# Ensure file exists and is readable
if not os.path.exists(script_path):
raise Exception(f"Failed to create diskpart script at {script_path}")
self.status.emit(f"Created diskpart script: {script_path}")
# Run diskpart to format the USB drive
self.status.emit("Formatting USB drive...")
result = subprocess.run([
......@@ -361,11 +406,17 @@ exit
raise Exception(f"Failed to format USB drive (Error {e.returncode}): {e}")
except PermissionError:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
try:
os.unlink(script_path)
except:
pass
raise Exception("Administrator privileges required. Please run the application as administrator.")
except Exception as e:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
try:
os.unlink(script_path)
except:
pass
# Check if it's a Windows elevation error
error_str = str(e).lower()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment