-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
228 lines (185 loc) · 5.98 KB
/
Copy pathbuild.py
File metadata and controls
228 lines (185 loc) · 5.98 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Build script for creating Windows executable using PyInstaller
This script creates a standalone .exe file for the Arbeitszeit Tracker.
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
def run_command(cmd, description):
"""Run a command and handle errors"""
print(f"\n {description}...")
try:
result = subprocess.run(cmd, shell=True, check=True)
print(f" {description} completed successfully")
return True
except subprocess.CalledProcessError as e:
print(f" {description} failed: {e}")
return False
def check_pyinstaller():
"""Check if PyInstaller is installed"""
try:
import PyInstaller
print(f"PyInstaller is available (version {PyInstaller.__version__})")
print(f"Using Python: {sys.executable}")
return True
except ImportError:
print(" PyInstaller not found")
print("Installing PyInstaller...")
python_exe = sys.executable
return run_command(f'"{python_exe}" -m pip install pyinstaller', "Installing PyInstaller")
def clean_build():
"""Clean previous build artifacts"""
print("\n Cleaning previous builds...")
dirs_to_clean = ["build", "dist", "__pycache__"]
files_to_clean = ["*.spec"]
for dir_name in dirs_to_clean:
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
print(f" Removed {dir_name}/")
for pattern in files_to_clean:
import glob
for file in glob.glob(pattern):
os.remove(file)
print(f" Removed {file}")
print(" Build artifacts cleaned")
def create_spec_file():
"""Create PyInstaller spec file"""
spec_content = '''# -*- mode: python ; coding: utf-8 -*-
import sys
from pathlib import Path
# Get the src directory
src_dir = Path.cwd() / "src"
a = Analysis(
['src/main.py'],
pathex=[str(src_dir)],
binaries=[],
datas=[
('README.md', '.'),
('LICENSE', '.'),
('requirements.txt', '.'),
],
hiddenimports=[
'pandas',
'openpyxl',
'tkinter',
'tkinter.messagebox',
'tkinter.simpledialog',
'tkinter.filedialog',
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=None)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='taskonaut',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False, # Disabled for faster startup - reduces compression but improves speed
upx_exclude=[],
runtime_tmpdir=None,
console=False, # No console window
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=None, # Add icon path here if you have one
)
'''
with open("taskonaut.spec", "w", encoding="utf-8") as f:
f.write(spec_content)
print(" Spec file created")
def build_executable():
"""Build the executable"""
# Use explicit Python path to ensure we use the correct Python version
python_exe = sys.executable
cmd = f'"{python_exe}" -m PyInstaller taskonaut.spec --clean'
return run_command(cmd, "Building executable")
def test_executable():
"""Test the created executable"""
dist_dir = Path("dist")
if not dist_dir.exists() or not dist_dir.is_dir():
print(" 'dist' directory not found")
return False
# List files in dist
files = [p for p in dist_dir.iterdir() if p.is_file()]
if not files:
print(" No files found in dist/")
return False
print(" Found build artifacts in dist/:")
for p in files:
try:
size_mb = p.stat().st_size / 1024 / 1024
except Exception:
size_mb = 0
print(f" {p} {size_mb:.1f} MB")
# Prefer explicit Windows exe if present
exe_candidates = [p for p in files if p.suffix.lower() == ".exe"]
if exe_candidates:
chosen = exe_candidates[0]
print(f" Windows executable created: {chosen}")
return True
# On POSIX check for an executable bit
if os.name != "nt":
exec_files = [p for p in files if os.access(p, os.X_OK)]
if exec_files:
print(f" Executable created: {exec_files[0]}")
return True
# If we have any file at all, consider the build successful (e.g., macOS/Linux app or pkg)
print(f" Build artifacts present (no .exe found) - first file: {files[0]}")
return True
def main():
"""Main build function"""
print("Arbeitszeit Tracker - Build Script")
print("=" * 50)
# Change to script directory
os.chdir(Path(__file__).parent)
success = True
# Check PyInstaller
success &= check_pyinstaller()
# Clean previous builds
clean_build()
# Create spec file
create_spec_file()
# Build executable
success &= build_executable()
# Test result
success &= test_executable()
print("\n" + "=" * 50)
if success:
print(" Build completed successfully!")
# Report actual artifacts in dist/
dist_dir = Path("dist")
if dist_dir.exists():
print("\nArtifacts in 'dist/':")
for p in dist_dir.iterdir():
if p.is_file():
try:
size_mb = p.stat().st_size / 1024 / 1024
except Exception:
size_mb = 0
print(f" - {p} ({size_mb:.1f} MB)")
else:
print("Note: 'dist/' not found after build.")
print("\nYou can now distribute these files to target systems.")
print("Note: First run may create config.json in the same directory.")
else:
print(" Build encountered errors. Please check the output above.")
sys.exit(1)
if __name__ == "__main__":
main()