--- /dev/null
+#!/usr/bin/python
+
+import re
+import subprocess
+import sys
+
+COMMAND = 'darcs'
+CHANGE_LOG_ARG = 'changes'
+BUILDS_TO_SHOW = 10
+
+search = re.compile('^(Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)', re.MULTILINE)
+
+searchStartInsert = re.compile('^\.\. INSERT CHANGE LOG HERE', re.MULTILINE)
+searchEndInsert = re.compile('^\.\. END INSERT CHANGE LOG', re.MULTILINE)
+
+MUST_HAVE = """
+.. INSERT CHANGE LOG HERE
+.. END INSERT CHANGE LOG
+"""
+
+def getBuildCount(data):
+ """returns number of latest build (a.k.a. total number of builds)
+ """
+ return len(search.findall(data))
+
+
+def process(filePath, buildsToShow=BUILDS_TO_SHOW):
+ p = subprocess.Popen([COMMAND, CHANGE_LOG_ARG], stdout=subprocess.PIPE)
+
+ data = p.stdout.read()
+ buildCount = getBuildCount(data)
+
+ buildNumIter = iter(range(buildCount, 0, -1))
+
+ new_data = []
+ count = 0
+ for line in data.split('\n'):
+ #Find a new build or move on
+ if not search.search(line):
+ new_data.append(line)
+ else:
+ if count < buildsToShow:
+ new_data.append('Build: %s - %s\n' % (buildNumIter.next(), line))
+ else:
+ break
+ count += 1
+
+ #Time to insert changelog
+ f = open(filePath, 'r')
+ dataInsert = f.read()
+ f.close()
+
+ new_data = ".. INSERT CHANGE LOG HERE\n\n::\n\n " \
+ + '\n '.join(new_data) + "\n.. END INSERT CHANGE LOG\n"
+
+ moStart = searchStartInsert.search(dataInsert)
+ moEnd = searchEndInsert.search(dataInsert)
+
+ if moStart is None or moEnd is None:
+ msg = 'Document %s must have the folling within the file: %s' % (filePath, MUST_HAVE)
+ raise ValueError, msg
+
+ data = dataInsert[:moStart.start()] + new_data + dataInsert[moEnd.end():]
+
+ f = open(filePath, 'w')
+ f.write(data)
+ f.close()
+
+
+
+if __name__ == '__main__':
+ if len(sys.argv) != 3:
+ print 'Usage: %s <n_builds_to_show> <insert_into_filename> ' % (sys.argv[0])
+ sys.exit(1)
+ else:
+ filePath = sys.argv[2]
+ buildsToShow = int(sys.argv[1])
+ process(filePath, buildsToShow)
\ No newline at end of file