forked from cirosantilli/linux-kernel-module-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-doc
executable file
·113 lines (104 loc) · 3.82 KB
/
build-doc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import re
import common
from shell_helpers import LF
import os
import subprocess
class Main(common.LkmcCliFunction):
def __init__(self):
super().__init__(
defaults = {
'show_time': False,
},
description='''\
https://cirosantilli.com/linux-kernel-module-cheat#build-the-documentation
''',
)
self.add_argument(
'--github-pages',
default=False,
help='''
Build for GitHub pages instead of a local build. This redirects all links
from the README to example sources to GitHub rather than locally.
'''
)
def timed_main(self):
asciidoctor_dir = os.path.join(self.env['root_dir'], 'asciidoctor')
if self.env['github_pages']:
link_target_script = 'link-target-github.rb'
else:
link_target_script = 'link-target-up.rb'
exit_status = self.sh.run_cmd(
[
'asciidoctor', LF,
'--attribute', 'docinfo=shared', LF,
'--failure-level', 'info', LF,
'--require', os.path.join(asciidoctor_dir, link_target_script), LF,
'--out-file', self.env['readme_out'], LF,
'--trace', LF,
'--verbose', LF,
] +
[
self.env['readme'], LF,
],
out_file=self.env['build_doc_log'],
)
# Check that all local files linked from README exist.
external_link_re = re.compile('^https?://')
for link in self.sh.check_output([
os.path.join(asciidoctor_dir, 'extract-link-targets'),
self.env['readme']
]).decode().splitlines():
if not external_link_re.match(link):
if not os.path.lexists(os.path.join(self.env['root_dir'], link)):
self.log_error('broken link to local file: ' + link)
exit_status = 1
# Check that there are not links to the GitHub README.
# https://github.com/isaacs/github/issues/1610
for grep_line in self.sh.check_output(
[
'git',
'grep',
'--fixed-strings',
self.env['github_repo_url'] + '#',
LF
],
cwd=self.env['root_dir'],
raise_on_failure=False
).decode().splitlines():
self.log_error('link to GitHub readme: {}'.format(
grep_line
))
exit_status = 1
# Check that non-README links to README IDs exit.
header_ids = set()
grep_line_location_re = re.compile('^(.*?:\d+):')
grep_line_hash_re = re.compile('^([a-z0-9_-]+)')
for header_id in self.sh.check_output([
os.path.join(asciidoctor_dir, 'extract-header-ids'),
self.env['readme']
]).decode().splitlines():
header_ids.add(header_id)
for grep_line in self.sh.check_output(
[
'git',
'grep',
'--fixed-strings',
self.env['homepage_url'] + '#',
LF
],
cwd=self.env['root_dir']
).decode().splitlines():
url_index = grep_line.index(self.env['homepage_url'])
hash_start_index = url_index + len(self.env['homepage_url'])
if len(grep_line) > hash_start_index:
hash_str = grep_line_hash_re.search(grep_line[hash_start_index + 1:]).group(1)
if not hash_str in header_ids:
self.log_error('broken link to {} at {}'.format(
hash_str,
grep_line_location_re.search(grep_line).group(1))
)
exit_status = 1
return exit_status
if __name__ == '__main__':
Main().cli()