|
1 #!/usr/bin/env python |
|
2 |
|
3 import os, os.path as op, re, sys |
|
4 |
|
5 # line starts with two chars one of which is not a space (and both are not |
|
6 # newlines obviously) and ends with one or more newlines followed by two spaces |
|
7 # on a next line (indented text) |
|
8 CODEBLOCK = re.compile(r'()\n(([^ \n][^\n]|[^\n][^ \n])[^\n]*)\n+ ') |
|
9 |
|
10 INDEX = ''' |
|
11 Mercurial tests |
|
12 =============== |
|
13 |
|
14 .. toctree:: |
|
15 :maxdepth: 1 |
|
16 ''' |
|
17 |
|
18 |
|
19 def rstify(orig, name): |
|
20 header = '%s\n%s\n\n' % (name, '=' * len(name)) |
|
21 content = header + orig |
|
22 content = CODEBLOCK.sub(r'\n\1\n\n::\n\n ', content) |
|
23 return content |
|
24 |
|
25 |
|
26 def main(base): |
|
27 if os.path.isdir(base): |
|
28 one_dir(base) |
|
29 else: |
|
30 one_file(base) |
|
31 |
|
32 |
|
33 def one_dir(base): |
|
34 index = INDEX |
|
35 #doc = lambda x: op.join(op.dirname(__file__), 'docs', x) |
|
36 |
|
37 for fn in sorted(os.listdir(base)): |
|
38 if not fn.endswith('.t'): |
|
39 continue |
|
40 name = os.path.splitext(fn)[0] |
|
41 content = one_file(op.join(base, fn)) |
|
42 target = op.join(base, name + '.rst') |
|
43 #with file(doc(name + '.rst'), 'w') as f: |
|
44 with file(target, 'w') as f: |
|
45 f.write(content) |
|
46 |
|
47 index += '\n ' + name |
|
48 |
|
49 #with file(doc('index.rst'), 'w') as f: |
|
50 # f.write(index) |
|
51 |
|
52 |
|
53 def one_file(path): |
|
54 name = os.path.basename(path)[:-2] |
|
55 return rstify(file(path).read(), name) |
|
56 |
|
57 |
|
58 if __name__ == '__main__': |
|
59 if len(sys.argv) != 2: |
|
60 print 'Please supply a path to tests dir as parameter' |
|
61 sys.exit() |
|
62 main(sys.argv[1]) |