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

feat(pypac): add tldextract cache directory in pyinstaller #564

Merged
merged 3 commits into from
Oct 8, 2024
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
5 changes: 5 additions & 0 deletions builder/pyinstaller_build_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# package
sys.path.insert(0, str(Path(".").resolve()))
from builder import tldextract_update
from qgis_deployment_toolbelt import __about__ # noqa: E402

# #############################################################################
Expand Down Expand Up @@ -49,10 +50,14 @@
mac_os_version, _, _ = platform.mac_ver()
mac_os_version = "-".join(mac_os_version.split(".")[:2])

tldextract_update.run()

PyInstaller.__main__.run(
[
"--add-data=LICENSE:.",
"--add-data=README.md:.",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
f"--add-data={package_folder.joinpath('shortcuts/shortcut_freedesktop.template').resolve()}:profiles/",
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",
f"--name={output_filename}",
Expand Down
5 changes: 5 additions & 0 deletions builder/pyinstaller_build_ubuntu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# package
sys.path.insert(0, str(Path(".").resolve()))
from builder import tldextract_update
from qgis_deployment_toolbelt import __about__ # noqa: E402

# #############################################################################
Expand Down Expand Up @@ -48,10 +49,14 @@
)
package_folder = Path("qgis_deployment_toolbelt")

tldextract_update.run()

PyInstaller.__main__.run(
[
"--add-data=LICENSE:.",
"--add-data=README.md:.",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
f"--add-data={package_folder.joinpath('shortcuts/shortcut_freedesktop.template').resolve()}:shortcuts/",
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",
f"--name={output_filename}",
Expand Down
5 changes: 5 additions & 0 deletions builder/pyinstaller_build_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# package
sys.path.insert(0, str(Path(".").resolve()))
from builder import tldextract_update
from qgis_deployment_toolbelt import __about__ # noqa: E402

# #############################################################################
Expand All @@ -46,10 +47,14 @@
)
package_folder = Path("qgis_deployment_toolbelt")

tldextract_update.run()

PyInstaller.__main__.run(
[
"--add-data=LICENSE:.",
"--add-data=README.md:.",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
# "--clean",
f"--icon={package_folder.parent.resolve()}/docs/static/logo_qdt.ico",
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",
Expand Down
79 changes: 79 additions & 0 deletions builder/tldextract_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#! python3 # noqa: E265

"""
Create tldextract update cache folder

"""

# #############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import argparse
import urllib.request
from os import W_OK, access
from pathlib import Path

# 3rd party
from tldextract import TLDExtract

# #############################################################################
# ########### MAIN #################
# ##################################


def run():
"""Minimal CLI to generate a tldextract cache.

:raises PermissionError: if output directory already exists but it's not writable
:raises SystemExit: in case of user abort

:example:

.. code-block:: bash

python tldextract_update.py
"""
# variables
script_path = Path(__file__).parent

# cli parser arguments
parser = argparse.ArgumentParser(
epilog=("tdlextract cache are created in output folder")
)
parser.add_argument(
"-o",
"--output",
default=script_path / "build" / "tldextract_cache",
help="tld extract cache output folder",
type=Path,
)

args = parser.parse_args()

try:
# check output directory
output_dir = Path(args.output)
if output_dir.exists() and not access(output_dir, W_OK):
raise PermissionError(output_dir.resolve())

output_dir.mkdir(exist_ok=True, parents=True)

tld_extract = TLDExtract(str(output_dir / ".suffix_cache"))
tld_extract.update(True)

urllib.request.urlretrieve(
"https://publicsuffix.org/list/public_suffix_list.dat",
output_dir / ".tld_set_snapshot",
)

# log user
print(f"tldextract cache written to: {output_dir.resolve()}")
except KeyboardInterrupt:
raise SystemExit("Aborted by user request.")


# Stand alone execution
if __name__ == "__main__":
run()