Skip to content

Commit 6e42110

Browse files
committed
feat(pypac): add tldextract cache directory in pyinstaller
1 parent 3c081dc commit 6e42110

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

.github/workflows/build_release.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ jobs:
9292
- name: Install project as a package
9393
run: python -m pip install -e .
9494

95+
- name: Generates tldextract cache
96+
run: python -O ./builder/tldextract_update.py
97+
9598
- name: Generates Executable
9699
run: python -O ./builder/pyinstaller_build_macos.py
97100

@@ -126,6 +129,8 @@ jobs:
126129
127130
- name: Install project as a package
128131
run: python -m pip install -e .
132+
- name: Generates tldextract cache
133+
run: python -O ./builder/tldextract_update.py
129134

130135
- name: Generates Executable
131136
run: python -O ./builder/pyinstaller_build_ubuntu.py
@@ -165,6 +170,9 @@ jobs:
165170
- name: Generates MS Version Info
166171
run: python .\builder\version_info_templater.py
167172

173+
- name: Generates tldextract cache
174+
run: python -O .\builder\tldextract_update.py
175+
168176
- name: Generates MS Executable
169177
run: python -O .\builder\pyinstaller_build_windows.py
170178

builder/pyinstaller_build_macos.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
[
5454
"--add-data=LICENSE:.",
5555
"--add-data=README.md:.",
56+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
57+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
5658
f"--add-data={package_folder.joinpath('shortcuts/shortcut_freedesktop.template').resolve()}:profiles/",
5759
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",
5860
f"--name={output_filename}",

builder/pyinstaller_build_ubuntu.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
[
5353
"--add-data=LICENSE:.",
5454
"--add-data=README.md:.",
55+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
56+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
5557
f"--add-data={package_folder.joinpath('shortcuts/shortcut_freedesktop.template').resolve()}:shortcuts/",
5658
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",
5759
f"--name={output_filename}",

builder/pyinstaller_build_windows.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
[
5151
"--add-data=LICENSE:.",
5252
"--add-data=README.md:.",
53+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/ '.suffix_cache'}:tldextract/.suffix_cache",
54+
f"--add-data={Path(__file__).parent / 'build' / 'tldextract_cache'/'.tld_set_snapshot'}:tldextract/",
5355
# "--clean",
5456
f"--icon={package_folder.parent.resolve()}/docs/static/logo_qdt.ico",
5557
f"--log-level={getenv('PYINSTALLER_LOG_LEVEL', 'WARN')}",

builder/tldextract_update.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#! python3 # noqa: E265
2+
3+
"""
4+
Create tldextract update cache folder
5+
6+
"""
7+
8+
# #############################################################################
9+
# ########## Libraries #############
10+
# ##################################
11+
12+
# Standard library
13+
import argparse
14+
import sys
15+
import urllib.request
16+
from os import W_OK, access, path
17+
from pathlib import Path
18+
19+
# 3rd party
20+
from tldextract import TLDExtract
21+
22+
sys.path.insert(0, path.abspath(r"."))
23+
24+
25+
# #############################################################################
26+
# ########### MAIN #################
27+
# ##################################
28+
29+
30+
def run():
31+
"""Minimal CLI to generate a tldextract cache.
32+
33+
:raises PermissionError: if output directory already exists but it's not writable
34+
:raises SystemExit: in case of user abort
35+
36+
:example:
37+
38+
.. code-block:: bash
39+
40+
python tldextract_update.py
41+
"""
42+
# variables
43+
script_path = Path(__file__).parent
44+
45+
# cli parser arguments
46+
parser = argparse.ArgumentParser(
47+
epilog=("tdlextract cache are created in output folder")
48+
)
49+
parser.add_argument(
50+
"-o",
51+
"--output",
52+
default=script_path / "build" / "tldextract_cache",
53+
help="tld extract cache output folder",
54+
type=Path,
55+
)
56+
57+
args = parser.parse_args()
58+
59+
try:
60+
# check output directory
61+
output_dir = Path(args.output)
62+
if output_dir.exists() and not access(output_dir, W_OK):
63+
raise PermissionError(output_dir.resolve())
64+
65+
output_dir.mkdir(exist_ok=True, parents=True)
66+
67+
tld_extract = TLDExtract(str(output_dir / ".suffix_cache"))
68+
tld_extract.update(True)
69+
70+
urllib.request.urlretrieve(
71+
"https://publicsuffix.org/list/public_suffix_list.dat",
72+
output_dir / ".tld_set_snapshot",
73+
)
74+
75+
# log user
76+
print(f"tldextract cache written to: {output_dir.resolve()}")
77+
except KeyboardInterrupt:
78+
raise SystemExit("Aborted by user request.")
79+
80+
81+
# Stand alone execution
82+
if __name__ == "__main__":
83+
run()

docs/development/packaging.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ The output binary and all embedded dependencies is located into a subfolder name
1414
# Generates MS Version Info
1515
python .\builder\version_info_templater.py
1616
17+
# Generates tldextract cache
18+
python .\builder\tldextract_update.py
19+
1720
# Generates MS Executable
1821
python -O .\builder\pyinstaller_build_windows.py
1922
```
@@ -29,6 +32,8 @@ To run it, double-click on the executable file (*.exe) loated into `dist` folder
2932
```sh
3033
# Generates binary executable
3134
python -O ./builder/pyinstaller_build_ubuntu.py
35+
# Generates tldextract cache
36+
python ./builder/tldextract_update.py
3237
# make it runnable
3338
chmod +x dist/QGISDeploymentToolbelt_*
3439
```

0 commit comments

Comments
 (0)