Skip to content

Commit 47e9b17

Browse files
authored
Merge pull request #237 from cisagov/bug/switch_psl_helper_package
Switch helper package for the Public Suffix List
2 parents e3f6a43 + fb108ad commit 47e9b17

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_version(version_file):
9494
py_modules=[splitext(basename(path))[0] for path in glob("src/*.py")],
9595
install_requires=[
9696
"docopt>=0.6.2",
97-
"publicsuffix>=1.1.0",
97+
"publicsuffixlist[update]>=0.9.2 ",
9898
"pyopenssl>=17.5.0",
9999
"pytablereader>=0.15.0",
100100
"pytablewriter>=0.27.2",

src/pshtt/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""This file defines the version of this module."""
2-
__version__ = "0.6.9"
2+
__version__ = "0.6.10"

src/pshtt/pshtt.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import re
1010
import sys
1111
from urllib import parse as urlparse
12-
from urllib.error import URLError
1312

1413
# Third-Party Libraries
1514
import OpenSSL
16-
17-
# Unable to find type stubs for the publicsuffix package.
18-
from publicsuffix import PublicSuffixList, fetch # type: ignore
15+
from publicsuffixlist.compat import PublicSuffixList # type: ignore
16+
from publicsuffixlist.update import updatePSL # type: ignore
1917
import requests
2018

2119
# Unable to find type stubs for the sslyze package.
@@ -1594,21 +1592,33 @@ def load_preload_list():
15941592
return fully_preloaded
15951593

15961594

1597-
# Returns an instantiated PublicSuffixList object, and the
1598-
# list of lines read from the file.
1599-
def load_suffix_list():
1595+
# Returns an instantiated PublicSuffixList object.
1596+
def load_suffix_list(cache_suffix_list=None, update_list=False):
16001597
"""Download and load the public suffix list."""
1601-
# File does not exist, download current list and cache it at given location.
1602-
utils.debug("Downloading the Public Suffix List...", divider=True)
1603-
try:
1604-
cache_file = fetch()
1605-
except URLError as err:
1606-
logging.exception("Unable to download the Public Suffix List...")
1607-
utils.debug(err)
1608-
return []
1609-
content = cache_file.readlines()
1610-
suffixes = PublicSuffixList(content)
1611-
return suffixes, content
1598+
if update_list:
1599+
utils.debug("Downloading the Public Suffix List...", divider=True)
1600+
try:
1601+
# Update the local copy
1602+
if cache_suffix_list:
1603+
updatePSL(cache_suffix_list)
1604+
# Update the built-in copy
1605+
else:
1606+
updatePSL()
1607+
except Exception as err:
1608+
logging.exception("Unable to download the Public Suffix List...")
1609+
utils.debug(err)
1610+
return None
1611+
1612+
# Use the local copy
1613+
if cache_suffix_list:
1614+
utils.debug("Using cached Public Suffix List.", divider=True)
1615+
with codecs.open(cache_suffix_list, encoding="utf-8") as cache_file:
1616+
suffixes = PublicSuffixList(cache_file)
1617+
# Use the built-in copy
1618+
else:
1619+
suffixes = PublicSuffixList()
1620+
1621+
return suffixes
16121622

16131623

16141624
def initialize_external_data(
@@ -1696,18 +1706,14 @@ def initialize_external_data(
16961706

16971707
# Load Mozilla's current Public Suffix list.
16981708
if SUFFIX_LIST is None:
1699-
if cache_suffix_list and os.path.exists(cache_suffix_list):
1700-
utils.debug("Using cached suffix list.", divider=True)
1701-
with codecs.open(cache_suffix_list, encoding="utf-8") as cache_file:
1702-
SUFFIX_LIST = PublicSuffixList(cache_file)
1709+
if cache_suffix_list:
1710+
# Retrieve the list if the path does not exist otherwise use the cached copy
1711+
SUFFIX_LIST = load_suffix_list(
1712+
cache_suffix_list, not os.path.exists(cache_suffix_list)
1713+
)
17031714
else:
1704-
SUFFIX_LIST, raw_content = load_suffix_list()
1705-
1706-
if cache_suffix_list:
1707-
utils.debug(
1708-
"Caching suffix list at %s", cache_suffix_list, divider=True
1709-
)
1710-
utils.write("".join(raw_content), cache_suffix_list)
1715+
# Load the built-in PSL
1716+
SUFFIX_LIST = load_suffix_list()
17111717

17121718

17131719
def inspect_domains(domains, options):

0 commit comments

Comments
 (0)