Skip to content

Commit f0195e5

Browse files
committed
Rejig some things with link checking and error handling, fix Makefile
1 parent 358b628 commit f0195e5

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ init_dist_dir:
1414
basic_build: clean init_dist_dir
1515
python -m app
1616
.PHONY: basic_build_new_internal_links
17-
basic_build_new_internal_links: clean init_dist_dir
18-
python -m app --new-internal-links
17+
basic_build_warn_links: clean init_dist_dir
18+
python -m app --warn-links
1919
.PHONY: pygmentize_css
2020
pygmentize_css:
2121
pygmentize -S solarized-light -f html -a .codehilite > dist/styles.css
@@ -24,8 +24,8 @@ zip:
2424
gzip -k -9 dist/*
2525
.PHONY: build
2626
build: lint basic_build pygmentize_css zip
27-
.PHONY: build_new_internal_links
28-
build_new_internal_links: lint basic_build_new_internal_links pygmentize_css zip
27+
.PHONY: build_warn_links
28+
build_warn_links: lint basic_build_warn_links pygmentize_css zip
2929
.PHONY: rsync
3030
rsync:
3131
rsync -rvz --progress -e 'ssh -p 57018' ./dist/* andrew@let-them.cyou:/srv/www/lt/andrew/tutorials/python

app/__main__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@
1818
)
1919

2020
parser = argparse.ArgumentParser()
21-
parser.add_argument('--new-internal-links')
2221
parser.add_argument(
2322
'--dry-run',
23+
action="store_true",
2424
help="""
2525
Do not write any files or directories.
2626
Still perform network requests for link checks
2727
"""
2828
)
29+
parser.add_argument(
30+
'--warn-links',
31+
action="store_true",
32+
help="Do not error on link checker failures",
33+
)
2934

3035
def run_build_checks(env, ctx, args):
3136
"""QA for build process"""
3237
testers = (
33-
LinkTester(ignore_internal_links=args.new_internal_links),
38+
LinkTester(warn=args.warn_links),
3439
CodeblocksTester(),
3540
UnresolvedTemplateVariablesTester(env, ctx),
3641
)

app/qa.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,42 @@ class LinkTester(BuildTest):
6767
"""Make sure all templated links are alive"""
6868

6969
name = 'Links'
70-
def __init__(self, concurrency=16, ignore_internal_links=False):
70+
def __init__(self, concurrency=16, warn=False):
7171
"""
7272
Semaphore limits number of concurrent requests for performance
7373
Is still nonblocking
7474
"""
7575
self.concurrency = concurrency
76-
self.ignore_internal_links = ignore_internal_links
76+
self.warn_only = warn
7777

78-
@staticmethod
79-
async def fetch(session, url, lock):
78+
async def fetch(self, session, url, lock):
8079
"""Locked async HTTP GET"""
8180
async with lock:
8281
try:
8382
return await session.get(url, allow_redirects=False, timeout=TIMEOUT_SEC)
84-
except (aiohttp.ClientConnectorSSLError, aiohttp.ClientConnectorCertificateError):
85-
print(f"SSL error when fetching {url}")
86-
return await session.get(url, allow_redirects=False, timeout=TIMEOUT_SEC, ssl=False)
87-
except asyncio.exceptions.TimeoutError:
88-
print(f"Took too long trying to fetch {url}")
83+
except (
84+
aiohttp.ClientConnectorSSLError,
85+
aiohttp.ClientConnectorCertificateError
86+
) as err:
87+
if self.warn_only:
88+
print(f"SSL error when fetching {url}")
89+
return await session.get(
90+
url,
91+
allow_redirects=False,
92+
timeout=TIMEOUT_SEC,
93+
ssl=False
94+
)
95+
raise err
96+
except aiohttp.ClientResponseError as err:
97+
if self.warn_only:
98+
print(f"Error fetching {url}, status code: {err.status}")
99+
else:
100+
raise err
101+
except asyncio.exceptions.TimeoutError as err:
102+
if self.warn_only:
103+
print(f"Took too long trying to fetch {url}")
104+
else:
105+
raise err
89106

90107
async def get_failures(self, links):
91108
"""
@@ -106,17 +123,17 @@ def _test(self):
106123
def strip_fragment_identifiers(link):
107124
return link.split('#')[0]
108125

126+
# formatting
109127
print()
110128

111-
if self.ignore_internal_links:
112-
links = set(
113-
strip_fragment_identifiers(link) for template_var, link in links.items()
114-
if not template_var.startswith('int')
115-
)
116-
else:
117-
links = set(strip_fragment_identifiers(link) for link in links.values())
129+
links = set(strip_fragment_identifiers(link) for link in links.values())
118130

119131
fails = asyncio.run(self.get_failures(links))
132+
if self.warn_only:
133+
print('\nAdditional Warnings:')
134+
for fail in fails:
135+
print(f'{fail.url}: {fail.status}')
136+
return BuildTestResult(True)
120137
return BuildTestResult(
121138
not bool(fails),
122139
[f'{fail.url}: {fail.status}' for fail in fails]

0 commit comments

Comments
 (0)