author | Boris Feld <boris.feld@octobus.net> |
Thu, 10 Aug 2017 17:31:23 +0200 | |
changeset 2833 | 08a64770ed24 |
parent 2825 | 7608f1e04205 |
child 2950 | 1b4c92621e23 |
permissions | -rw-r--r-- |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
2 |
|
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
3 |
import os |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
4 |
import os.path as op |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
5 |
import sys |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
6 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
7 |
INDEX = ''' |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
8 |
Mercurial tests |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
9 |
=============== |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
10 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
11 |
.. toctree:: |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
12 |
:maxdepth: 1 |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
13 |
''' |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
14 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
15 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
16 |
def rstify(orig, name): |
2825 | 17 |
newlines = [] |
18 |
||
19 |
code_block_mode = False |
|
20 |
||
21 |
for line in orig.splitlines(): |
|
22 |
||
23 |
# Emtpy lines doesn't change output |
|
24 |
if not line: |
|
25 |
newlines.append(line) |
|
26 |
continue |
|
27 |
||
28 |
codeline = line.startswith(' ') |
|
29 |
if codeline: |
|
30 |
if code_block_mode is False: |
|
31 |
newlines.extend(['::', '']) |
|
32 |
||
33 |
code_block_mode = True |
|
34 |
else: |
|
35 |
code_block_mode = False |
|
36 |
||
37 |
newlines.append(line) |
|
38 |
||
39 |
return "\n".join(newlines) |
|
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
40 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
41 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
42 |
def main(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
43 |
if os.path.isdir(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
44 |
one_dir(base) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
45 |
else: |
525
a0327c78a5d3
doc: remove spurious print in test2rest.py
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
235
diff
changeset
|
46 |
one_file(base) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
47 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
48 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
49 |
def one_dir(base): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
50 |
index = INDEX |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
51 |
# doc = lambda x: op.join(op.dirname(__file__), 'docs', x) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
52 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
53 |
for fn in sorted(os.listdir(base)): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
54 |
if not fn.endswith('.t'): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
55 |
continue |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
56 |
name = os.path.splitext(fn)[0] |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
57 |
content = one_file(op.join(base, fn)) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
58 |
target = op.join(base, name + '.rst') |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
59 |
# with file(doc(name + '.rst'), 'w') as f: |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
60 |
with open(target, 'w') as f: |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
61 |
f.write(content) |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
62 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
63 |
index += '\n ' + name |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
64 |
|
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
65 |
# with file(doc('index.rst'), 'w') as f: |
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
66 |
# f.write(index) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
67 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
68 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
69 |
def one_file(path): |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
70 |
name = os.path.basename(path)[:-2] |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
71 |
return rstify(open(path).read(), name) |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
72 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
73 |
|
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
74 |
if __name__ == '__main__': |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
75 |
if len(sys.argv) != 2: |
2035
94fe2cc9cd41
flake8: fix error in 'test2rst.py'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
525
diff
changeset
|
76 |
print('Please supply a path to tests dir as parameter') |
235
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
77 |
sys.exit() |
8469ccb9550f
[doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff
changeset
|
78 |
main(sys.argv[1]) |