Skip to content

Commit 5d95d59

Browse files
authored
Merge pull request #133 from openzim/fix_alpine_tests
Fix alpine tests
2 parents e167913 + 68d3e30 commit 5d95d59

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

Diff for: CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111

1212
- Using openZIM Python bootstrap conventions (including hatch-openzim plugin) #120
13-
- Suuport for Python 3.12, drop Python 3.7 #118
14-
- Replace "iso-369" iso639-lang by "iso639-lang" library
13+
- Add support for Python 3.12, drop Python 3.7 support #118
14+
- Replace "iso-369" by "iso639-lang" library
15+
- Replace "file-magic" by "python-magic" library for Alpine Linux support and better maintenance
1516
- Rework the VideoWebmLow preset for faster encoding and smaller file size (preset has been bumped to version 2)
1617
- When reencoding a video, ffmpeg now uses only 1 CPU thread by default (new arg to `reencode` allows to override this default value)
1718

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ sudo apt install libmagic1 wget ffmpeg \
4545
libharfbuzz-dev libfribidi-dev libxcb1-dev gifsicle
4646
```
4747

48+
## Alpine
49+
```
50+
apk add ffmpeg gifsicle libmagic wget libjpeg
51+
```
52+
53+
**Nota:** i18n features do not work on Alpine, see https://github.com/openzim/python-scraperlib/issues/134 ; there is one corresponding test which is failing.
54+
4855
# Contribution
4956

5057
This project adheres to openZIM's [Contribution Guidelines](https://github.com/openzim/overview/wiki/Contributing)

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"colorthief==0.2.1",
1414
"python-resize-image>=1.1.19,<1.2",
1515
"Babel>=2.9,<3.0",
16-
"file-magic>=0.4.0,<0.5",
16+
"python-magic>=0.4.3,<0.5",
1717
"libzim>=3.4.0,<4.0",
1818
"beautifulsoup4>=4.9.3,<4.10", # upgrade to 4.10 and later to be done
1919
"lxml>=4.6.3,<4.10", # upgrade to 4.10 and later to be done

Diff for: src/zimscraperlib/filesystem.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def get_content_mimetype(content: bytes) -> str:
3131
"""MIME Type of content retrieved from magic headers"""
3232

3333
try:
34-
detected_mime = magic.detect_from_content(content).mime_type
34+
detected_mime = magic.from_buffer(content, mime=True)
35+
if isinstance(
36+
detected_mime, bytes
37+
): # pragma: no cover (old python-magic versions where returning bytes)
38+
detected_mime = detected_mime.decode()
3539
except UnicodeDecodeError:
3640
return "application/octet-stream"
3741
return MIME_OVERRIDES.get(detected_mime, detected_mime)

Diff for: tests/filesystem/test_filesystem.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def test_content_mimetype_fallback(monkeypatch, undecodable_byte_stream):
2828
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
2929

3030
# mock then so we keep coverage on systems where magic works
31-
def raising_magic(*args): # noqa: ARG001
31+
def raising_magic(*args, **kwargs): # noqa: ARG001
3232
raise UnicodeDecodeError("nocodec", b"", 0, 1, "noreason")
3333

34-
monkeypatch.setattr(magic, "detect_from_content", raising_magic)
34+
monkeypatch.setattr(magic, "from_buffer", raising_magic)
3535
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
3636

3737

Diff for: tests/video/test_video.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,17 @@ def test_get_media_info(media_format, media, expected, test_files):
130130
with tempfile.TemporaryDirectory() as t:
131131
src = pathlib.Path(t).joinpath(media)
132132
shutil.copy2(test_files[media_format], src)
133-
assert get_media_info(src) == expected
133+
result = get_media_info(src)
134+
assert result.keys() == expected.keys()
135+
assert result["codecs"] == expected["codecs"]
136+
assert result["duration"] == expected["duration"]
137+
# for bitrate, we need to allow some variability, not all ffmpeg version are
138+
# reporting the same values (e.g. Alpine Linux is reporting 3837275 instead of
139+
# 3818365 for video.mp4) ; we allow 1% variability with following assertion
140+
assert (
141+
abs(100.0 * (result["bitrate"] - expected["bitrate"]) / expected["bitrate"])
142+
< 1
143+
)
134144

135145

136146
def test_preset_has_mime_and_ext():

0 commit comments

Comments
 (0)