build-doc 4.3 KB

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