diff --git a/builder/pyinstaller_build_macos.py b/builder/pyinstaller_build_macos.py index a075efd8..66c7e468 100644 --- a/builder/pyinstaller_build_macos.py +++ b/builder/pyinstaller_build_macos.py @@ -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 # ############################################################################# @@ -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}", diff --git a/builder/pyinstaller_build_ubuntu.py b/builder/pyinstaller_build_ubuntu.py index 2b99122c..a4ea9263 100644 --- a/builder/pyinstaller_build_ubuntu.py +++ b/builder/pyinstaller_build_ubuntu.py @@ -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 # ############################################################################# @@ -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}", diff --git a/builder/pyinstaller_build_windows.py b/builder/pyinstaller_build_windows.py index 71f59728..33ae6074 100644 --- a/builder/pyinstaller_build_windows.py +++ b/builder/pyinstaller_build_windows.py @@ -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 # ############################################################################# @@ -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')}", diff --git a/builder/tldextract_update.py b/builder/tldextract_update.py new file mode 100644 index 00000000..abee2a95 --- /dev/null +++ b/builder/tldextract_update.py @@ -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()