Skip to content

Commit 1adc0a5

Browse files
committed
Add CIBW_TEST_COMMAND and indent Makefile
1 parent 4a715e2 commit 1adc0a5

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

.github/workflows/main.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- uses: actions/setup-python@v4
4949
with:
5050
python-version: '3.x'
51-
- run: pip install cibuildwheel==2.15.0
51+
- run: pip install cibuildwheel==2.16.2 # sync version with pypa/cibuildwheel below
5252
- id: set-matrix
5353
env:
5454
CIBW_PROJECT_REQUIRES_PYTHON: '==3.8.*'
@@ -82,13 +82,15 @@ jobs:
8282
if: runner.os == 'Linux'
8383
uses: docker/setup-qemu-action@v2
8484

85-
- uses: pypa/cibuildwheel@v2.15.0
85+
- uses: pypa/cibuildwheel@v2.16.2 # sync version with pip install cibuildwheel above
8686
timeout-minutes: 10
8787
with:
8888
only: ${{ matrix.only }}
8989
env:
9090
CIBW_BUILD_VERBOSITY: 1
91-
CIBW_BEFORE_BUILD: 'bash -c "make install_libmagic"'
91+
MACOSX_DEPLOYMENT_TARGET: 11.0 # oldest available github runner ("macos-11" above)
92+
CIBW_BEFORE_BUILD: bash -c "make install_libmagic"
93+
CIBW_TEST_COMMAND: python -c "import magic; assert magic.Magic(mime=True).from_buffer(b'\x00\x00\x00\x1cftypisom\x00\x00\x02\x00isomiso2mp41\x00') == 'video/mp4'"
9294

9395
- uses: actions/upload-artifact@v3
9496
with:

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ SHELL := /bin/bash
33
.PHONY: install_libmagic
44
## Install libmagic
55
install_libmagic:
6+
# Mac https://formulae.brew.sh/formula/libmagic
67
# Debian https://packages.ubuntu.com/libmagic1
8+
# Alpine https://pkgs.alpinelinux.org/package/libmagic
79
# RHEL https://git.almalinux.org/rpms/file
8-
# Mac https://formulae.brew.sh/formula/libmagic
910
# Windows https://github.com/julian-r/file-windows
10-
( ( ( brew install libmagic || ( apt-get update && apt-get install -y libmagic1 ) ) || apk add --update libmagic ) || yum install file-libs ) || ( python -c 'import platform, sysconfig, io, zipfile, urllib.request; assert platform.system() == "Windows"; machine = "x86" if sysconfig.get_platform() == "win32" else "x64"; print(machine); zipfile.ZipFile(io.BytesIO(urllib.request.urlopen(f"https://github.com/julian-r/file-windows/releases/download/v5.44/file_5.44-build104-vs2022-{machine}.zip").read())).extractall(".")' && ls -ltra )
11+
( ( ( \
12+
brew install libmagic \
13+
|| ( apt-get update && apt-get install -y libmagic1 ) ) \
14+
|| apk add --update libmagic ) \
15+
|| yum install file-libs ) \
16+
|| ( python -c 'import platform, sysconfig, io, zipfile, urllib.request; assert platform.system() == "Windows"; machine = "x86" if sysconfig.get_platform() == "win32" else "x64"; print(machine); zipfile.ZipFile(io.BytesIO(urllib.request.urlopen(f"https://github.com/julian-r/file-windows/releases/download/v5.44/file_5.44-build104-vs2022-{machine}.zip").read())).extractall(".")' && ls -ltra )
1117
# on cibuildwheel, the lib needs to exist in the project before running setup.py
18+
# copy lib into the magic dir, regardless of platform
1219
python -c "import subprocess; from magic.loader import load_lib; lib = load_lib()._name; print(f'linking {lib}'); subprocess.check_call(['cp', lib, 'magic'])"
13-
cp /usr/share/misc/magic.mgc magic || true # only on linux
20+
# only on linux: additionally copy compiled db into magic dir
21+
cp /usr/share/misc/magic.mgc magic || true
22+
# check what was copied
1423
ls -ltra magic
1524

1625
.DEFAULT_GOAL := help

magic/loader.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _lib_candidates():
1212

1313
paths = [
1414
here,
15-
os.path.abspath("."),
15+
os.path.abspath('.'),
1616
'/opt/local/lib',
1717
'/usr/local/lib',
1818
'/opt/homebrew/lib',
@@ -29,18 +29,23 @@ def _lib_candidates():
2929
# find_library searches in %PATH% but not the current directory,
3030
# so look for both
3131
yield os.path.join(here, '%s.dll' % i)
32-
yield os.path.join(os.path.abspath("."), '%s.dll' % i)
32+
yield os.path.join(os.path.abspath('.'), '%s.dll' % i)
3333
yield find_library(i)
3434

3535
elif sys.platform == 'linux':
36-
# on some linux systems (musl/alpine), find_library('magic') returns None
37-
yield subprocess.check_output(
38-
"( ldconfig -p | grep 'libmagic.so.1' | grep -o '/.*' ) || echo '/usr/lib/libmagic.so.1'",
39-
shell=True,
40-
universal_newlines=True,
41-
).strip()
42-
yield os.path.join(here, 'libmagic.so.1')
43-
yield os.path.join(os.path.abspath("."), 'libmagic.so.1')
36+
37+
prefixes = ['libmagic.so.1', 'libmagic.so']
38+
39+
for i in prefixes:
40+
yield os.path.join(here, i)
41+
yield os.path.join(os.path.abspath('.'), i)
42+
# on some linux systems (musl/alpine), find_library('magic') returns None
43+
# first try ldconfig with backup string in case of error
44+
yield subprocess.check_output(
45+
"( ldconfig -p | grep '{0}' | grep -o '/.*' ) || echo '/usr/lib/{0}'".format(i),
46+
shell=True,
47+
universal_newlines=True,
48+
).strip()
4449

4550
yield find_library('magic')
4651

@@ -53,9 +58,7 @@ def load_lib():
5358
continue
5459
try:
5560
return ctypes.CDLL(lib)
56-
except OSError as exc:
61+
except OSError: # file not found
5762
pass
58-
else:
59-
# It is better to raise an ImportError since we are importing magic module
60-
raise ImportError('failed to find libmagic. Check your installation')
63+
raise ImportError('failed to find libmagic. Check your installation')
6164

0 commit comments

Comments
 (0)