build-doc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. exit_status = self.sh.run_cmd(
  32. [
  33. 'asciidoctor', LF,
  34. '--failure-level', 'info', LF,
  35. '--require', os.path.join(asciidoctor_dir, link_target_script), LF,
  36. '--out-file', self.env['readme_out'], LF,
  37. '--trace', LF,
  38. '--verbose', LF,
  39. self.env['readme'], LF,
  40. ],
  41. out_file=self.env['build_doc_log'],
  42. )
  43. # Check that all local files linked from README exist.
  44. external_link_re = re.compile('^https?://')
  45. for link in self.sh.check_output([
  46. os.path.join(asciidoctor_dir, 'extract-link-targets'),
  47. self.env['readme']
  48. ]).decode().splitlines():
  49. if not external_link_re.match(link):
  50. if not os.path.lexists(os.path.join(self.env['root_dir'], link)):
  51. self.log_error('broken link to local file: ' + link)
  52. exit_status = 1
  53. # Check that there are not links to the GitHub README.
  54. # https://github.com/isaacs/github/issues/1610
  55. for grep_line in self.sh.check_output(
  56. [
  57. 'git',
  58. 'grep',
  59. '--fixed-strings',
  60. self.env['github_repo_url'] + '#',
  61. LF
  62. ],
  63. cwd=self.env['root_dir'],
  64. raise_on_failure=False
  65. ).decode().splitlines():
  66. self.log_error('link to GitHub readme: {}'.format(
  67. grep_line
  68. ))
  69. exit_status = 1
  70. # Check that non-README links to README IDs exit.
  71. header_ids = set()
  72. grep_line_location_re = re.compile('^(.*?:\d+):')
  73. grep_line_hash_re = re.compile('^([a-z0-9_-]+)')
  74. for header_id in self.sh.check_output([
  75. os.path.join(asciidoctor_dir, 'extract-header-ids'),
  76. self.env['readme']
  77. ]).decode().splitlines():
  78. header_ids.add(header_id)
  79. for grep_line in self.sh.check_output(
  80. [
  81. 'git',
  82. 'grep',
  83. '--fixed-strings',
  84. self.env['homepage_url'] + '#',
  85. LF
  86. ],
  87. cwd=self.env['root_dir']
  88. ).decode().splitlines():
  89. url_index = grep_line.index(self.env['homepage_url'])
  90. hash_start_index = url_index + len(self.env['homepage_url'])
  91. if len(grep_line) > hash_start_index:
  92. hash_str = grep_line_hash_re.search(grep_line[hash_start_index + 1:]).group(1)
  93. if not hash_str in header_ids:
  94. self.log_error('broken link to {} at {}'.format(
  95. hash_str,
  96. grep_line_location_re.search(grep_line).group(1))
  97. )
  98. exit_status = 1
  99. return exit_status
  100. if __name__ == '__main__':
  101. Main().cli()