docs/test2rst.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 21 Sep 2017 10:39:49 +0200
changeset 2951 2ddc63d13af8
parent 2950 1b4c92621e23
child 2958 1cb715257130
permissions -rw-r--r--
Backed out changeset 1b4c92621e23 This got published by mistake.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    17
    newlines = []
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    18
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    19
    code_block_mode = False
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    20
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    21
    for line in orig.splitlines():
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    22
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    23
        # Emtpy lines doesn't change output
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    24
        if not line:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    25
            newlines.append(line)
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    26
            continue
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    27
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    28
        codeline = line.startswith('  ')
2951
2ddc63d13af8 Backed out changeset 1b4c92621e23
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2950
diff changeset
    29
        if codeline:
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    30
            if code_block_mode is False:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    31
                newlines.extend(['::', ''])
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    32
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    33
            code_block_mode = True
2951
2ddc63d13af8 Backed out changeset 1b4c92621e23
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2950
diff changeset
    34
        else:
2ddc63d13af8 Backed out changeset 1b4c92621e23
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2950
diff changeset
    35
            code_block_mode = False
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    36
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    37
        newlines.append(line)
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    38
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
    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])