Skip to content

Commit 688edf0

Browse files
authored
Get latest libmagic from MSYS2 for Windows (#10)
1 parent 33c827e commit 688edf0

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: ci
22
on: [push, pull_request]
3+
concurrency: # https://stackoverflow.com/questions/66335225#comment133398800_72408109
4+
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
5+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
36
jobs:
47
ci:
58
strategy:

.github/workflows/wheels.yml

+41-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ on:
88
types: [released, prereleased]
99
workflow_dispatch: # allows running workflow manually from the Actions tab
1010

11-
jobs:
11+
concurrency: # https://stackoverflow.com/questions/66335225#comment133398800_72408109
12+
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
13+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
1214

15+
jobs:
1316
build-sdist:
1417
runs-on: ubuntu-latest
1518

@@ -86,14 +89,50 @@ jobs:
8689
if: runner.os == 'Linux'
8790
uses: docker/setup-qemu-action@v3
8891

92+
# For Windows, grabbing latest file from MSYS2 is easier than building from source
93+
# It's generally up to date ref https://packages.msys2.org/base/mingw-w64-file
94+
- name: Setup MSYS2 and install file
95+
if: runner.os == 'Windows'
96+
uses: msys2/[email protected]
97+
with:
98+
msystem: ${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}
99+
location: D:\
100+
install: >-
101+
${{ endsWith(matrix.only, '32') && 'mingw-w64-i686-file' || 'mingw-w64-x86_64-file' }}
102+
103+
# The DLL dependency tree flattened out ref "Dependencies" https://packages.msys2.org/packages/mingw-w64-x86_64-file
104+
- name: Copy Windows ddl and mgc
105+
if: runner.os == 'Windows'
106+
run: |
107+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/share/misc/magic.mgc" "magic"
108+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libmagic-1.dll" "magic"
109+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libsystre-0.dll" "magic"
110+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libtre-5.dll" "magic"
111+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libasprintf-0.dll" "magic"
112+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libintl-8.dll" "magic"
113+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libatomic-1.dll" "magic"
114+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libgomp-1.dll" "magic"
115+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libquadmath-0.dll" "magic"
116+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libstdc++-6.dll" "magic"
117+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libcharset-1.dll" "magic"
118+
cp "/msys64/${{ endsWith(matrix.only, '32') && 'mingw32' || 'mingw64' }}/bin/libiconv-2.dll" "magic"
119+
120+
# These are needed additionally in the win32 wheel ref https://packages.msys2.org/packages/mingw-w64-i686-file
121+
- name: Copy additional 32-bit runtime DLLs
122+
if: runner.os == 'Windows' && endsWith(matrix.only, '32')
123+
run: |
124+
cp "/msys64/mingw32/bin/libgcc_s_dw2-1.dll" "magic"
125+
cp "/msys64/mingw32/bin/libwinpthread-1.dll" "magic"
126+
89127
- uses: pypa/[email protected] # sync version with pip install cibuildwheel above
90128
timeout-minutes: 10
91129
with:
92130
only: ${{ matrix.only }}
93131
env:
94132
CIBW_BUILD_VERBOSITY: 1
95133
# add compiled libmagic to the build directory (to include in the wheel)
96-
CIBW_BEFORE_BUILD: ${{ ( startsWith( matrix.os, 'macos' ) && 'sudo -E bash add_libmagic.sh' ) || 'bash add_libmagic.sh' }}
134+
CIBW_BEFORE_BUILD_MACOS: sudo -E bash add_libmagic.sh
135+
CIBW_BEFORE_BUILD_LINUX: bash add_libmagic.sh
97136
# build macos wheels with maximum backwards compatibility (gcc -mmacosx-version-min flag)
98137
MACOSX_DEPLOYMENT_TARGET: ${{ ( endsWith( matrix.only, 'arm64' ) && '11.0' ) || '10.9' }}
99138
# simple smoke test run on each wheel: this is an HLS MP4 video, only recognised in recent versions of libmagic

add_libmagic.sh

+3-16
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ install_precompiled() {
2727
# Debian https://packages.ubuntu.com/libmagic1
2828
# Alpine https://pkgs.alpinelinux.org/package/libmagic
2929
# RHEL https://git.almalinux.org/rpms/file
30-
# Windows https://github.com/julian-r/file-windows
3130
if [ -n "$(which brew)" ]; then
3231
brew install libmagic
3332
elif [ -n "$(which apt-get)" ]; then
@@ -37,20 +36,6 @@ install_precompiled() {
3736
apk add --update libmagic
3837
elif [ -n "$(which dnf)" ]; then
3938
dnf --setopt install_weak_deps=false -y install file-libs
40-
else
41-
# windows (no install, just download into current working directory)
42-
# could also consider install using `pacman`: https://packages.msys2.org/base/mingw-w64-file
43-
# which would require an update of copy_libmagic below to account for new magic.mgc paths
44-
python <<EOF
45-
import platform, sysconfig, io, zipfile, urllib.request
46-
assert platform.system() == "Windows"
47-
machine = "x86" if sysconfig.get_platform() == "win32" else "x64"
48-
url = f"https://github.com/julian-r/file-windows/releases/download/v5.44/file_5.44-build104-vs2022-{machine}.zip"
49-
print("Downloading", url)
50-
zipfile.ZipFile(io.BytesIO(urllib.request.urlopen(url).read())).extractall(".")
51-
EOF
52-
# check what was copied
53-
ls -ltra
5439
fi
5540
}
5641

@@ -60,12 +45,14 @@ copy_libmagic() {
6045
# this python command relies on current working directory containing `./magic/loader.py`
6146
libmagic_path="$(python -c 'from magic.loader import load_lib; print(load_lib()._name)')" &&
6247
cp "${libmagic_path}" "magic" &&
63-
# only on linux/macos: additionally copy compiled db into magic dir (prefer the one installed by install_source)
48+
# additionally copy compiled db into magic dir (prefer the one installed by install_source)
6449
( ( cp "/usr/local/share/misc/magic.mgc" "magic" || cp "/usr/share/misc/magic.mgc" "magic" ) || true ) &&
6550
# check what was copied
6651
ls -ltra magic
6752
}
6853

54+
# skip windows (taken care of separately in wheels.yml)
55+
python -c 'import platform; assert platform.system() != "Windows"' || ( echo "skipping on windows" && exit 0 )
6956
# prefer a recent build from source
7057
install_source || install_precompiled
7158
# files to be copied into the wheel

setup.py

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
import os
77
import sys
88

9-
# python packages should not install succesfully if libraries are missing
10-
from magic.loader import load_lib
11-
12-
load_lib()._name
13-
149

1510
def read(file_name):
1611
"""Read a text file and return the content as a string."""

0 commit comments

Comments
 (0)