diff --git a/news/339.new.0.rst b/news/339.new.0.rst new file mode 100644 index 00000000..b8ff24e8 --- /dev/null +++ b/news/339.new.0.rst @@ -0,0 +1 @@ +Add hook for ``tzdata``. diff --git a/requirements-test-libraries.txt b/requirements-test-libraries.txt index 2800d549..1b785171 100644 --- a/requirements-test-libraries.txt +++ b/requirements-test-libraries.txt @@ -1,3 +1,6 @@ +# Backport of importlib.resources for python 3.8 and earlier. +importlib_resources==5.3.0; python_version < '3.9' + # ------------------ LIBRARIES ------------------ # # TODO: Add most of the libraries we have hooks for, and write tests av==8.0.3 @@ -58,6 +61,7 @@ swagger-spec-validator==2.7.3 thinc==8.0.10 timezonefinder==5.2.0 tableauhyperapi==0.0.13617 +tzdata==2021.5 Unidecode==1.3.2 web3==5.24.0 websockets==10.0; python_version >= '3.7' diff --git a/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-tzdata.py b/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-tzdata.py new file mode 100644 index 00000000..9a79021f --- /dev/null +++ b/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-tzdata.py @@ -0,0 +1,22 @@ +# ------------------------------------------------------------------ +# Copyright (c) 2021 PyInstaller Development Team. +# +# This file is distributed under the terms of the GNU General Public +# License (version 2.0 or later). +# +# The full license is available in LICENSE.GPL.txt, distributed with +# this software. +# +# SPDX-License-Identifier: GPL-2.0-or-later +# ------------------------------------------------------------------ + +from PyInstaller.utils.hooks import collect_data_files, collect_submodules + +# Collect timezone data files +datas = collect_data_files("tzdata") + +# Collect submodules; each data subdirectory is in fact a package +# (e.g., zoneinfo.Europe), so we need its __init__.py for data files +# (e.g., zoneinfo/Europe/Ljubljana) to be discoverable via +# importlib.resources +hiddenimports = collect_submodules("tzdata") diff --git a/src/_pyinstaller_hooks_contrib/tests/test_libraries.py b/src/_pyinstaller_hooks_contrib/tests/test_libraries.py index c422b8d7..1ff8a617 100644 --- a/src/_pyinstaller_hooks_contrib/tests/test_libraries.py +++ b/src/_pyinstaller_hooks_contrib/tests/test_libraries.py @@ -12,7 +12,7 @@ import pytest from pathlib import Path from PyInstaller.compat import is_darwin, is_linux, is_py39, is_win -from PyInstaller.utils.hooks import is_module_satisfies +from PyInstaller.utils.hooks import is_module_satisfies, can_import_module from PyInstaller.utils.tests import importorskip, requires, xfail @@ -886,3 +886,30 @@ def test_pypeteer(pyi_builder): import pypeteer print(pypeteer.version) """) + + +@importorskip("tzdata") +@pytest.mark.skipif(not is_py39 and not can_import_module('importlib_resources'), + reason='importlib_resources is required on python < 3.9.') +def test_tzdata(pyi_builder): + pyi_builder.test_source(""" + import tzdata.zoneinfo # hiddenimport + + try: + import importlib.resources as importlib_resources + except ImportError: + import importlib_resources + + # This emulates time-zone data retrieval from tzdata, as peformed by + # zoneinfo / backports.zoneinfo + zone_name = "Europe/Ljubljana" + + components = zone_name.split("/") + package_name = ".".join(["tzdata.zoneinfo"] + components[:-1]) + resource_name = components[-1] + + with importlib_resources.open_binary(package_name, resource_name) as fp: + data = fp.read() + + print(data) + """)