Debian Codegraph @

windows/python-gtk3-install.ps1

raw
1 <#
2 Install Python and GTK development and build environment for BleachBit on Windows.
3 This is a special version built for BleachBit.
4 It installs in a portable style.
5 This script may be run in an empty directory like `c:\projects`.
6
7 Afterwards, launch the application like this:
8 c:\projects\x86-windows\tools\python3\python.exe c:\projects\bleachbit\bleachbit.py
9
10 This assumes that the BleachBit source code is in `c:\projects\bleachbit` and PyGTK
11 is installed in `c:\projects\pygtk`, but either directory can be relocated.
12
13 Copyright (C) 2008-2025 Andrew Ziem
14
15 This program is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #>
26
27 $root_dir = Join-Path (Get-Location).Path "vcpkg_installed\x86-windows"
28 $python_home = Join-Path $root_dir "tools\python3"
29 $themes_dir = Join-Path $python_home "share\themes"
30 # When the tree is restored from cache python.exe already exists, so the pip
31 # bootstrap and installs below can be skipped. A dependency change busts the
32 # cache key, forcing a fresh unpack where this is false.
33 $python_exists = Test-Path "$python_home\python.exe"
34 # location of this .ps1 script
35 $script_dir = Split-Path -Parent $MyInvocation.MyCommand.Path
36 $base_download_url = "https://github.com/bleachbit/pygtkwin/releases/download/v2026-09-21/"
37
38 function Assert-FileHash($Path, $Expected) {
39 $actual = (Get-FileHash -Path $Path -Algorithm SHA256).Hash
40 if ($actual -ine $Expected) {
41 throw "SHA256 mismatch for ${Path}: expected $Expected, got $actual"
42 }
43 }
44
45 # Visual C++ Redistributable 2015 x86
46 $VC_REDIST_FN = "VC_redist.x86.exe"
47 $VC_REDIST_URL = "https://aka.ms/vs/17/release/vc_redist.x86.exe"
48 if (-not $env:GITHUB_ACTIONS) {
49 if (-not (Test-Path $VC_REDIST_FN)) {
50 Write-Host "Downloading Visual C++ Redistributable..."
51 Invoke-WebRequest -Uri $VC_REDIST_URL -OutFile $VC_REDIST_FN
52 Get-FileHash -Path $VC_REDIST_FN -Algorithm SHA256 | Format-List
53 $fileSizeMB = (Get-Item $VC_REDIST_FN).Length / 1MB
54 if ($fileSizeMB -lt 5) {
55 Write-Warning "The downloaded Visual C++ Redistributable file is smaller than expected (< 5MB). Please verify its integrity."
56 }
57 } else {
58 Write-Host "Visual C++ Redistributable is already downloaded."
59 }
60 if (-not (Test-Path "C:\Windows\SysWOW64\vcruntime140.dll")) {
61 Write-Host "Installing Visual C++ Redistributable..."
62 Write-Host "Tip: If this step seems to freeze, press ALT+TAB to check for the UAC dialog."
63 Start-Process -FilePath $VC_REDIST_FN -ArgumentList "/install", "/quiet", "/norestart" -Wait
64 } else {
65 Write-Host "Visual C++ Redistributable is already installed."
66 }
67 } else {
68 Write-Host "Skipping Visual C++ Redistributable installation in CI."
69 }
70
71 # Python and GTK+
72 $GTK_ZIP_FN = "gtk3.24-x86-windows.zip"
73 if (-not (Test-Path $GTK_ZIP_FN)) {
74 Write-Host "Downloading Python and GTK+..."
75 Invoke-WebRequest -Uri "$base_download_url/$GTK_ZIP_FN" -OutFile $GTK_ZIP_FN
76 } else {
77 Write-Host "Python and GTK+ are already downloaded."
78 }
79 Assert-FileHash $GTK_ZIP_FN "de88f4e3a3404105473477bbf2c88f1b122fe246949ac67c8d8d7c1fee5de9cd"
80
81 if (-not (Test-Path $python_home\python.exe)) {
82 Write-Host "Unpacking Python and GTK+..."
83 $vcpkg_installed = ".\vcpkg_installed"
84 if (-not (Test-Path $vcpkg_installed)) {
85 New-Item -Path $vcpkg_installed -ItemType Directory
86 }
87 Expand-Archive -Path $GTK_ZIP_FN -DestinationPath .
88 } else {
89 Write-Host "Python and GTK+ are already unpacked."
90 }
91
92 if (-not (Test-Path "$root_dir\tools\gtk3\gtk-launch.exe")) {
93 Write-Error "GTK+ is not installed correctly."
94 exit 1
95 }
96
97 $schema_compiler = Join-Path $root_dir "tools\glib\glib-compile-schemas.exe"
98 $schema_dir = Join-Path $root_dir "share\glib-2.0\schemas"
99 if (-not (Test-Path "$schema_dir\gschemas.compiled")) {
100 Write-Host "Compiling GLib schemas..."
101 & $schema_compiler $schema_dir
102 if ($LASTEXITCODE -ne 0) {
103 Write-Error "Failed to compile GLib schemas: exit code $LASTEXITCODE"
104 exit $LASTEXITCODE
105 }
106 if (-not (Test-Path "$schema_dir\gschemas.compiled")) {
107 Write-Error "Failed to compile GLib schemas: $schema_dir\gschemas.compiled does not exist"
108 exit 1
109 }
110 }
111
112 # This fixes the chaff dialog when running on Windows from source.
113 New-Item -ItemType Directory -Path "$python_home\share\glib-2.0\schemas" -Force
114 Copy-Item -Path "$schema_dir\gschemas.compiled" -Destination "$python_home\share\glib-2.0\schemas" -Force
115
116 Write-Host "Checking Python version"
117 & "$python_home\python.exe" -V # show Python version
118 if ($LASTEXITCODE -ne 0) {
119 Write-Error "python.exe -V failed"
120 exit $LASTEXITCODE
121 }
122
123 # Add Python home and scripts to PATH.
124 if ($env:PATH -notlike "*$python_home*") {
125 Write-Host "Adding Python home and scripts to PATH..."
126 $env:PATH += ";$python_home;$python_home\Scripts"
127 Write-Host "Updated PATH: $env:PATH"
128 }
129
130 if (-not $python_exists) {
131 Write-Host "ensurepip"
132 & "$python_home\python.exe" -m ensurepip
133
134 Write-Host "Checking pip version"
135 & "$python_home\Scripts\pip3.exe" --version # show pip version
136
137 Write-Host "Updating pip..."
138 & "$python_home\python.exe" -m pip install --disable-pip-version-check --upgrade pip
139 if ($LASTEXITCODE -ne 0) {
140 Write-Error "Failed to update pip: exit code $LASTEXITCODE"
141 exit $LASTEXITCODE
142 }
143 } else {
144 Write-Host "Python is already unpacked; skipping pip bootstrap."
145 }
146
147 if (-not (Test-Path gtk-themes.zip)) {
148 Write-Host "Downloading GTK themes..."
149 #Invoke-WebRequest -Uri "$base_download_url/gtk-themes.zip" -OutFile "gtk-themes.zip"
150 #FIXME: use new themes
151 Invoke-WebRequest -Uri "https://github.com/mkhon/vcpkg/releases/download/gtk3-introspection-v1/gtk-themes.zip" -OutFile "gtk-themes.zip"
152 } else {
153 Write-Host "GTK themes are already downloaded."
154 }
155 Assert-FileHash gtk-themes.zip "6BD572256773175C0139FCA9AD0D28A0EF23B4E087901D04198FF907FC096624"
156
157 if (-not (Test-Path "$themes_dir")) {
158 Write-Host "Unpacking GTK themes..."
159 Expand-Archive -Path gtk-themes.zip -DestinationPath .
160 Copy-Item -Path "gtk-themes\*" -Destination $python_home -Recurse -ErrorAction SilentlyContinue
161 } else {
162 Write-Host "GTK themes are already unpacked."
163 }
164
165 if (-not (Test-Path "$themes_dir\Adwaita\index.theme")) {
166 Write-Error "GTK themes are not installed correctly."
167 exit 1
168 }
169
170 if (Test-Path "gtk-themes") {
171 Write-Host "Removing temporary directory gtk-themes..."
172 Remove-Item -Path "gtk-themes" -Recurse
173 }
174
175 # psutil 7.1.2 stopped providing 32-bit wheels.
176 # It needs MSVC++ with the python3.lib.
177 Write-Host "Copying python312.lib..."
178 New-Item -ItemType Directory -Path "$python_home\libs" -Force
179 $pylibdest = "$python_home\libs\python3.lib"
180 Copy-Item -Path "$root_dir\lib\python312.lib" -Destination $pylibdest
181 Get-ChildItem -Path $pylibdest | Format-List -Property Name, Length
182
183 # Install pip packages
184 if (-not $python_exists) {
185 Write-Host "pip install -r requirements.txt..."
186 & "$python_home\Scripts\pip3.exe" install --disable-pip-version-check -r "$script_dir\requirements.txt"
187 if ($LASTEXITCODE -ne 0) {
188 Write-Error "Failed to update pip: exit code $LASTEXITCODE"
189 exit $LASTEXITCODE
190 }
191 }
192
193 $PYGOBJECT_FN = "pygobject-3.56.3-cp312-cp312-win32.whl"
194 if (-not (Test-Path $PYGOBJECT_FN)) {
195 Write-Host "Downloading PyGObject..."
196 Invoke-WebRequest -Uri "$base_download_url/$PYGOBJECT_FN" -OutFile "$PYGOBJECT_FN"
197 } else {
198 Write-Host "PyGObject is already downloaded."
199 }
200 Assert-FileHash $PYGOBJECT_FN "1ce56a4a1fb4a42bed2c0f06ba6df6210f005a6b61df3a5731c71d103a28b3a1"
201
202 if (-not $python_exists) {
203 Write-Host "pip install $PYGOBJECT_FN..."
204 & "$python_home\Scripts\pip3.exe" install --disable-pip-version-check $PYGOBJECT_FN
205 if ($LASTEXITCODE -ne 0) {
206 Write-Error "Failed to install PyGObject: exit code $LASTEXITCODE"
207 exit $LASTEXITCODE
208 }
209 }
210
211 # By default, pygobject installs to `x86-windows\lib\girepository-1.0`.
212 # Copy it to `x86-windows\tools\python3\lib\girepository-1.0`.
213 $girepo_dir = "$python_home\lib\girepository-1.0"
214 if (-not (Test-Path $girepo_dir)) {
215 New-Item -Path "$girepo_dir" -ItemType Directory -Force
216 Copy-Item -Path "$root_dir\lib\girepository-1.0\*" -Destination "$girepo_dir" -Recurse -Force -ErrorAction Stop
217 }
218
219 # Copy GTK dependencies to Python home.
220 Get-Content "$script_dir\python-gtk3-deps.lst" | ForEach-Object {
221 Write-Host "Copying $_..."
222 if (-not (Test-Path "$python_home\$_")) {
223 Copy-Item -Path "$root_dir\$_" -Destination "$python_home" -Recurse -Force
224 }
225 }
226
227 Write-Host "Copying gdk-pixbuf-2.0..."
228 $GDK_PIXBUF_DIR = "$python_home\lib\gdk-pixbuf-2.0\2.10.0"
229 if (-not (Test-Path $GDK_PIXBUF_DIR)) {
230 Write-Host "Creating $GDK_PIXBUF_DIR..."
231 New-Item -Path $GDK_PIXBUF_DIR -ItemType Directory -Force
232 }
233
234 $GDK_PIXBUF_LOADER_DIR = Join-Path $GDK_PIXBUF_DIR "loaders"
235 if (-not (Test-Path $GDK_PIXBUF_LOADER_DIR)) {
236 New-Item -Path $GDK_PIXBUF_LOADER_DIR -ItemType Directory -Force | Out-Null
237 }
238 $svg_loader_src = Join-Path $root_dir "lib\gdk-pixbuf-2.0\2.10.0\loaders\pixbufloader-svg.dll"
239 if (Test-Path $svg_loader_src) {
240 Copy-Item -Path $svg_loader_src -Destination $GDK_PIXBUF_LOADER_DIR -Force
241 }
242 Get-ChildItem -Path $GDK_PIXBUF_LOADER_DIR | Format-List -Property Name, Length
243
244 # Update cache file for GDK pixbuf.
245 $env:GDK_PIXBUF_MODULE_FILE = "$GDK_PIXBUF_DIR\loaders.cache"
246 if (-not (Test-Path $env:GDK_PIXBUF_MODULE_FILE)) {
247 Write-Host "Creating $env:GDK_PIXBUF_MODULE_FILE..."
248 $prevPath = $env:PATH
249 try {
250 $env:GDK_PIXBUF_MODULEDIR = $GDK_PIXBUF_LOADER_DIR
251 $env:PATH = "$python_home\bin;$python_home;$python_home\Scripts;$root_dir\tools\gtk3;$prevPath"
252 & "$root_dir\tools\gdk-pixbuf\gdk-pixbuf-query-loaders.exe" --update-cache
253 if ($LASTEXITCODE -ne 0) {
254 Write-Error "gdk-pixbuf-query-loaders.exe failed with exit code $LASTEXITCODE"
255 exit $LASTEXITCODE
256 }
257 } finally {
258 $env:PATH = $prevPath
259 }
260 }