Skip to content

Commit

Permalink
feat(pypac): add tldextract cache directory in pyinstaller (#564)
Browse files Browse the repository at this point in the history
closes #563 

Add new script `builder\tldextract_update.py` to create `tldextract`
cache directory and use it in pyinstaller.
  • Loading branch information
jmkerloch authored Oct 8, 2024
2 parents 75c7bf7 + 246a958 commit f22f37f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
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()

0 comments on commit f22f37f

Please sign in to comment.