PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/include_anchors.py

https://github.com/kindkaktus/cxxtest
Python | 90 lines | 75 code | 5 blank | 10 comment | 1 complexity | ec249976240a49182bd8f54f29055c45 MD5 | raw file
Possible License(s): LGPL-3.0
  1. #-------------------------------------------------------------------------
  2. # CxxTest: A lightweight C++ unit testing library.
  3. # Copyright (c) 2008 Sandia Corporation.
  4. # This software is distributed under the LGPL License v3
  5. # For more information, see the COPYING file in the top CxxTest directory.
  6. # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  7. # the U.S. Government retains certain rights in this software.
  8. #-------------------------------------------------------------------------
  9. import re
  10. import sys
  11. import os.path
  12. import os
  13. pat1a = re.compile('include::([a-zA-Z0-9_\.\-/\/]+\/)\.([^\_]+)\_[a-zA-Z0-9]*\.py\[\]')
  14. pat1b = re.compile('include::([a-zA-Z0-9_\.\-/\/]+\/)\.([^\_]+)\_[a-zA-Z0-9]*\.sh\[\]')
  15. pat1c = re.compile('include::([a-zA-Z0-9_\.\-/\/]+\/)\.([^\_]+)\_[a-zA-Z0-9]*\.h\[\]')
  16. pat1d = re.compile('include::([a-zA-Z0-9_\.\-/\/]+\/)\.([^\_]+)\_[a-zA-Z0-9]*\.cpp\[\]')
  17. pat2 = re.compile('([^@]+)@([a-zA-Z0-9]+):')
  18. pat3 = re.compile('([^@]+)@:([a-zA-Z0-9]+)')
  19. processed = set()
  20. def process(dir, root, suffix):
  21. #print "PROCESS ",root, suffix
  22. bname = "%s%s" % (dir, root)
  23. global processed
  24. if bname in processed:
  25. return
  26. #
  27. anchors = {}
  28. anchors[''] = open('%s.%s_.%s' % (dir, root, suffix), 'w')
  29. INPUT = open('%s%s.%s' % (dir, root, suffix), 'r')
  30. for line in INPUT:
  31. m2 = pat2.match(line)
  32. m3 = pat3.match(line)
  33. if m2:
  34. anchor = m2.group(2)
  35. anchors[anchor] = open('%s.%s_%s.%s' % (dir, root, anchor, suffix), 'w')
  36. elif m3:
  37. anchor = m3.group(2)
  38. anchors[anchor].close()
  39. del anchors[anchor]
  40. else:
  41. for anchor in anchors:
  42. os.write(anchors[anchor].fileno(), line)
  43. INPUT.close()
  44. for anchor in anchors:
  45. if anchor != '':
  46. print "ERROR: anchor '%s' did not terminate" % anchor
  47. anchors[anchor].close()
  48. #
  49. processed.add(bname)
  50. for file in sys.argv[1:]:
  51. print "Processing file '%s' ..." % file
  52. INPUT = open(file, 'r')
  53. for line in INPUT:
  54. suffix = None
  55. m = pat1a.match(line)
  56. if m:
  57. suffix = 'py'
  58. #
  59. if suffix is None:
  60. m = pat1b.match(line)
  61. if m:
  62. suffix = 'sh'
  63. #
  64. if suffix is None:
  65. m = pat1c.match(line)
  66. if m:
  67. suffix = 'h'
  68. #
  69. if suffix is None:
  70. m = pat1d.match(line)
  71. if m:
  72. suffix = 'cpp'
  73. #
  74. if not suffix is None:
  75. #print "HERE", line, suffix
  76. fname = m.group(1)+m.group(2)+'.'+suffix
  77. if not os.path.exists(fname):
  78. print line
  79. print "ERROR: file '%s' does not exist!" % fname
  80. sys.exit(1)
  81. process(m.group(1), m.group(2), suffix)
  82. INPUT.close()