reindex.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python
  2. import re, os
  3. class Error(Exception): pass
  4. STATUSES = """DRAFT NEEDS-REVISION NEEDS-RESEARCH OPEN ACCEPTED META FINISHED
  5. CLOSED SUPERSEDED DEAD""".split()
  6. REQUIRED_FIELDS = [ "Filename", "Status", "Title" ]
  7. CONDITIONAL_FIELDS = { "OPEN" : [ "Target" ],
  8. "ACCEPTED" : [ "Target "],
  9. "CLOSED" : [ "Implemented-In" ],
  10. "FINISHED" : [ "Implemented-In" ] }
  11. FNAME_RE = re.compile(r'^(\d\d\d)-.*[^\~]$')
  12. DIR = "."
  13. OUTFILE = "000-index.txt"
  14. TMPFILE = OUTFILE+".tmp"
  15. def indexed(seq):
  16. n = 0
  17. for i in seq:
  18. yield n, i
  19. n += 1
  20. def readProposal(fn):
  21. fields = { }
  22. f = open(fn, 'r')
  23. lastField = None
  24. try:
  25. for lineno, line in indexed(f):
  26. line = line.rstrip()
  27. if not line:
  28. return fields
  29. if line[0].isspace():
  30. fields[lastField] += " %s"%(line.strip())
  31. else:
  32. parts = line.split(":", 1)
  33. if len(parts) != 2:
  34. raise Error("%s:%s: Neither field nor continuation"%
  35. (fn,lineno))
  36. else:
  37. fields[parts[0]] = parts[1].strip()
  38. lastField = parts[0]
  39. return fields
  40. finally:
  41. f.close()
  42. def checkProposal(fn, fields):
  43. status = fields.get("Status")
  44. need_fields = REQUIRED_FIELDS + CONDITIONAL_FIELDS.get(status, [])
  45. for f in need_fields:
  46. if not fields.has_key(f):
  47. raise Error("%s has no %s field"%(fn, f))
  48. if fn != fields['Filename']:
  49. print `fn`, `fields['Filename']`
  50. raise Error("Mismatched Filename field in %s"%fn)
  51. if fields['Title'][-1] == '.':
  52. fields['Title'] = fields['Title'][:-1]
  53. status = fields['Status'] = status.upper()
  54. if status not in STATUSES:
  55. raise Error("I've never heard of status %s in %s"%(status,fn))
  56. if status in [ "SUPERSEDED", "DEAD" ]:
  57. for f in [ 'Implemented-In', 'Target' ]:
  58. if fields.has_key(f): del fields[f]
  59. def readProposals():
  60. res = []
  61. for fn in os.listdir(DIR):
  62. m = FNAME_RE.match(fn)
  63. if not m: continue
  64. if not fn.endswith(".txt"):
  65. raise Error("%s doesn't end with .txt"%fn)
  66. num = m.group(1)
  67. fields = readProposal(fn)
  68. checkProposal(fn, fields)
  69. fields['num'] = num
  70. res.append(fields)
  71. return res
  72. def writeIndexFile(proposals):
  73. proposals.sort(key=lambda f:f['num'])
  74. seenStatuses = set()
  75. for p in proposals:
  76. seenStatuses.add(p['Status'])
  77. out = open(TMPFILE, 'w')
  78. inf = open(OUTFILE, 'r')
  79. for line in inf:
  80. out.write(line)
  81. if line.startswith("====="): break
  82. inf.close()
  83. out.write("Proposals by number:\n\n")
  84. for prop in proposals:
  85. out.write("%(num)s %(Title)s [%(Status)s]\n"%prop)
  86. out.write("\n\nProposals by status:\n\n")
  87. for s in STATUSES:
  88. if s not in seenStatuses: continue
  89. out.write(" %s:\n"%s)
  90. for prop in proposals:
  91. if s == prop['Status']:
  92. out.write(" %(num)s %(Title)s"%prop)
  93. if prop.has_key('Target'):
  94. out.write(" [for %(Target)s]"%prop)
  95. if prop.has_key('Implemented-In'):
  96. out.write(" [in %(Implemented-In)s]"%prop)
  97. out.write("\n")
  98. out.close()
  99. os.rename(TMPFILE, OUTFILE)
  100. try:
  101. os.unlink(TMPFILE)
  102. except OSError:
  103. pass
  104. writeIndexFile(readProposals())