File size: 2,897 Bytes
a2fcab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
import os
import sys
import time
import platform
import subprocess
import shutil

def get_platform_name():
    """Get standardized platform name"""
    if sys.platform.startswith('win'):
        return 'windows'
    elif sys.platform.startswith('darwin'):
        return 'macos'
    elif sys.platform.startswith('linux'):
        return 'linux'
    return sys.platform

def retry_rmtree(directory_name, max_retries=3, delay=1):
    """Retry removing directory tree with multiple attempts"""
    for attempt in range(max_retries):
        try:
            if os.path.exists(directory_name):
                shutil.rmtree(directory_name)
            return True
        except PermissionError as error:
            if attempt == max_retries - 1:
                print(f"Warning: Could not remove {directory_name}: {error}")
                return False
            print(f"Retrying removal of {directory_name} in {delay} seconds...")
            time.sleep(delay)
    return False

def clean_build_directories():
    """Clean up build directories"""
    directories_to_clean = ['build', 'dist']
    for directory_name in directories_to_clean:
        if not retry_rmtree(directory_name):
            print(f"Warning: Proceeding without cleaning {directory_name}")

def get_executable_name():
    """Get platform-specific executable name"""
    platform_name = get_platform_name()
    if platform_name == 'windows':
        return 'Spatial Media Metadata Injector.exe'
    elif platform_name == 'macos':
        return 'Spatial Media Metadata Injector.app'
    else:
        return 'Spatial Media Metadata Injector'

def build_executable():
    """Build the executable for the current platform"""
    # Clean previous builds
    try:
        clean_build_directories()
    except Exception as error:
        print(f"Warning: Error during cleanup: {error}")
        print("Attempting to continue with build...")
    
    # Get the specification file path
    specification_file = os.path.join('spatialmedia', 'spatial_media_metadata_injector.spec')
    
    # Build command
    command = ['pyinstaller', '--clean', specification_file]
    
    try:
        subprocess.check_call(command)
        platform_name = get_platform_name()
        exe_name = get_executable_name()
        print(f"Successfully built executable for {platform_name}")
        
        # Show output location
        print(f"Output: ./dist/{exe_name}")
        
        # Set executable permissions for Unix-like systems
        if platform_name in ('linux', 'macos'):
            output_path = os.path.join('dist', exe_name)
            if os.path.exists(output_path):
                os.chmod(output_path, 0o755)
                
    except subprocess.CalledProcessError as error:
        print(f"Error building executable: {error}")
        sys.exit(1)

if __name__ == "__main__":
    build_executable()