Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks for tzdata and zoneinfo/backports.zoneinfo #339

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/339.new.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add hook for ``tzdata``.
1 change: 1 addition & 0 deletions news/339.new.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add hooks for ``zoneinfo`` and ``backports.zoneinfo``.
5 changes: 5 additions & 0 deletions requirements-test-libraries.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# 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
adbutils==0.11.0; sys_platform == 'darwin' or sys_platform == 'win32'
APScheduler==3.8.0
backports.zoneinfo==0.2.1; python_version < '3.9'
boto==2.49.0
boto3==1.18.63
botocore==1.21.63
Expand Down Expand Up @@ -58,6 +62,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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ------------------------------------------------------------------
# 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.compat import is_win

# On Windows, timezone data is provided by the tzdata package that is
# not directly loaded.
if is_win:
hiddenimports = ['tzdata']
22 changes: 22 additions & 0 deletions src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-tzdata.py
Original file line number Diff line number Diff line change
@@ -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")
18 changes: 18 additions & 0 deletions src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-zoneinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ------------------------------------------------------------------
# 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.compat import is_win

# On Windows, timezone data is provided by the tzdata package that is
# not directly loaded.
if is_win:
hiddenimports = ['tzdata']
51 changes: 50 additions & 1 deletion src/_pyinstaller_hooks_contrib/tests/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -886,3 +886,52 @@ 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)
""")


@importorskip("backports.zoneinfo")
@pytest.mark.skipif(is_win and not can_import_module('tzdata'),
reason='On Windows, backports.zoneinfo requires tzdata.')
def test_backports_zoneinfo(pyi_builder):
pyi_builder.test_source("""
from backports import zoneinfo
tz = zoneinfo.ZoneInfo("Europe/Ljubljana")
print(tz)
""")


@importorskip("zoneinfo")
@pytest.mark.skipif(is_win and not can_import_module('tzdata'),
reason='On Windows, zoneinfo requires tzdata.')
def test_zoneinfo(pyi_builder):
pyi_builder.test_source("""
import zoneinfo
tz = zoneinfo.ZoneInfo("Europe/Ljubljana")
print(tz)
""")