write_scripts.py 1.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import os
import shutil
import textwrap

"""
Helper file to get the auto-generated scripts file up to-date!
The top of the scripts file should just be:

Scripts
========

With a section for each individual script like this:

chip-gen
^^^^^^^^

.. argparse::
    :filename: source/scripts/chip-gen
    :func: parser
    :prog: chip-gen

Additionally, we need copies of the scripts from the root level scripts/
folder.  But we only want their parsing information!  If the script
actually does anything it won't work... So... It's gross but I promise
it's worth it.
"""

# Find the scripts folder from relative to where we are
target_path = os.path.abspath(__file__) + "/"
for _ in range(4):
    target_path = os.path.dirname(target_path)
target_path = os.path.join(target_path, "scripts")

with open("scripts.rst", "w") as wh:
    wh.write("Scripts\n========\n\n")
    for root, dirs, files in os.walk(target_path):
        for f in sorted(files):
            # We only want to write files that have an argument parser in them
            # We only want to write up to the point just before parse_args()
            with open(os.path.join(root, f), "r") as rh:
                lines = rh.readlines()
                if any(["ArgumentParser" in line for line in lines]):
                    with open(os.path.join("scripts", f), "w") as new_script:
                        for line in lines:
                            if "parse_args()" in line:
                                break
                            new_script.write(line)
                    wh.write(textwrap.dedent("""
                        %s
                        %s

                        .. argparse::
                            :filename: source/scripts/%s
                            :func: parser
                            :prog: %s

                        """ % (f, "^" * len(f), f, f)))
        break