scan_tutorials.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. from pathlib import Path
  3. import re
  4. # Tasks
  5. # 1. Find all tutorials
  6. # 2. Generate tree (@subpage)
  7. # 3. Check prev/next nodes
  8. class Tutorial(object):
  9. def __init__(self, path):
  10. self.path = path
  11. self.title = None # doxygen title
  12. self.children = [] # ordered titles
  13. self.prev = None
  14. self.next = None
  15. with open(path, "rt") as f:
  16. self.parse(f)
  17. def parse(self, f):
  18. rx_title = re.compile(r"\{#(\w+)\}")
  19. rx_subpage = re.compile(r"@subpage\s+(\w+)")
  20. rx_prev = re.compile(r"@prev_tutorial\{(\w+)\}")
  21. rx_next = re.compile(r"@next_tutorial\{(\w+)\}")
  22. for line in f:
  23. if self.title is None:
  24. m = rx_title.search(line)
  25. if m:
  26. self.title = m.group(1)
  27. continue
  28. if self.prev is None:
  29. m = rx_prev.search(line)
  30. if m:
  31. self.prev = m.group(1)
  32. continue
  33. if self.next is None:
  34. m = rx_next.search(line)
  35. if m:
  36. self.next = m.group(1)
  37. continue
  38. m = rx_subpage.search(line)
  39. if m:
  40. self.children.append(m.group(1))
  41. continue
  42. def verify_prev_next(self, storage):
  43. res = True
  44. if self.title is None:
  45. print("[W] No title")
  46. res = False
  47. prev = None
  48. for one in self.children:
  49. c = storage[one]
  50. if c.prev is not None and c.prev != prev:
  51. print("[W] Wrong prev_tutorial: expected {} / actual {}".format(c.prev, prev))
  52. res = False
  53. prev = c.title
  54. next = None
  55. for one in reversed(self.children):
  56. c = storage[one]
  57. if c.next is not None and c.next != next:
  58. print("[W] Wrong next_tutorial: expected {} / actual {}".format(c.next, next))
  59. res = False
  60. next = c.title
  61. if len(self.children) == 0 and self.prev is None and self.next is None:
  62. print("[W] No prev and next tutorials")
  63. res = False
  64. return res
  65. if __name__ == "__main__":
  66. p = Path('tutorials')
  67. print("Looking for tutorials in: '{}'".format(p))
  68. all_tutorials = dict()
  69. for f in p.glob('**/*'):
  70. if f.suffix.lower() in ('.markdown', '.md'):
  71. t = Tutorial(f)
  72. all_tutorials[t.title] = t
  73. res = 0
  74. print("Found: {}".format(len(all_tutorials)))
  75. print("------")
  76. for title, t in all_tutorials.items():
  77. if not t.verify_prev_next(all_tutorials):
  78. print("[E] Verification failed: {}".format(t.path))
  79. print("------")
  80. res = 1
  81. exit(res)