Skip to content

Commit f0ab54a

Browse files
committed
Improve app structure with __main__ file; Add argparse; Improve dead link detection
1 parent c3ee193 commit f0ab54a

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: default
2-
default:
3-
@echo "Read the Makefile..."
2+
default: build
3+
44
.PHONY: clean
55
clean:
66
rm -rf dist
@@ -12,10 +12,10 @@ init_dist_dir:
1212
mkdir -p dist
1313
.PHONY: basic_build
1414
basic_build: clean init_dist_dir
15-
python -m app.build
15+
python -m app
1616
.PHONY: basic_build_new_internal_links
1717
basic_build_new_internal_links: clean init_dist_dir
18-
python -m app.build --new-internal-links
18+
python -m app --new-internal-links
1919
.PHONY: pygmentize_css
2020
pygmentize_css:
2121
pygmentize -S solarized-light -f html -a .codehilite > dist/styles.css

app/build.py renamed to app/__main__.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for building and build-time QA of the python tutorial website"""
2+
import argparse
23
from os import makedirs
34
from pathlib import Path
45
import sys
@@ -12,15 +13,24 @@
1213
from app.templates import TEMPLATES, BASE_HTML
1314
from app.templates.codeblocks import codeblocks
1415
from app.templates.links import links
15-
from app.qa import LinkTester, CodeblocksTester, UnresolvedTemplateVariablesTester, BuildTestFailure
16+
from app.qa import (
17+
LinkTester, CodeblocksTester, UnresolvedTemplateVariablesTester, BuildTestFailure
18+
)
1619

20+
parser = argparse.ArgumentParser()
21+
parser.add_argument('--new-internal-links')
22+
parser.add_argument(
23+
'--dry-run',
24+
help="""
25+
Do not write any files or directories.
26+
Still perform network requests for link checks
27+
"""
28+
)
1729

18-
def run_build_checks(env, ctx):
30+
def run_build_checks(env, ctx, args):
1931
"""QA for build process"""
20-
args = sys.argv[1:]
21-
ignore_internal = '--new-internal-links' in args
2232
testers = (
23-
LinkTester(ignore_internal_links=ignore_internal),
33+
LinkTester(ignore_internal_links=args.new_internal_links),
2434
CodeblocksTester(),
2535
UnresolvedTemplateVariablesTester(env, ctx),
2636
)
@@ -60,13 +70,11 @@ def render_templates(env, ctx):
6070

6171
def write_render(render, path):
6272
"""Write final render to appropriate path"""
63-
args = sys.argv[1:]
64-
if '--dry-run' not in args:
65-
with open(path, 'w', encoding='utf-8') as f:
66-
f.write(render)
73+
with open(path, 'w', encoding='utf-8') as f:
74+
f.write(render)
6775

6876

69-
def build():
77+
def main(args):
7078
"""
7179
Perform all of the actions necessary to output the python tutorial webpages
7280
"""
@@ -76,11 +84,13 @@ def build():
7684
undefined=DebugUndefined,
7785
)
7886
ctx = {**codeblocks, **links}
79-
run_build_checks(env, ctx)
80-
makedirs('dist', exist_ok=True)
81-
for render, path in render_templates(env, ctx):
82-
write_render(render, path)
87+
run_build_checks(env, ctx, args)
88+
if not args.dry_run:
89+
makedirs('dist', exist_ok=True)
90+
for render, path in render_templates(env, ctx):
91+
write_render(render, path)
8392

8493

8594
if __name__ == '__main__':
86-
build()
95+
args_ = parser.parse_args()
96+
main(args_)

app/qa.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async def fetch(session, url, lock):
8080
return await session.get(url, allow_redirects=False, timeout=5)
8181
except asyncio.exceptions.TimeoutError:
8282
print(f"Took too long trying to fetch {url}")
83+
return None
8384

8485
async def get_failures(self, links):
8586
"""
@@ -91,7 +92,7 @@ async def get_failures(self, links):
9192
responses = await asyncio.gather(
9293
*[self.fetch(session, url, lock) for url in links]
9394
)
94-
failures = [r for r in responses if r.status != 200]
95+
failures = [r for r in responses if r is not None and r.status != 200]
9596
return failures
9697

9798
def _test(self):

0 commit comments

Comments
 (0)