-
Notifications
You must be signed in to change notification settings - Fork 152
/
readme_generate
executable file
·49 lines (34 loc) · 1.43 KB
/
readme_generate
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
#!/usr/bin/env python3
import logging
import os.path
import subprocess
import sys
from pathlib import Path
here = Path(os.path.abspath(os.path.dirname(__file__)))
readme_index = ''
readme_content = '\n'
with (here / 'README_header.rst').open() as rheader:
readme_header = rheader.read()
for cmd_file in sorted((here / 'habu' / 'cli').glob('cmd_*.py')):
cmd_name = 'habu.' + cmd_file.stem.replace('cmd_', '').replace('_', '.')
cmd_idx = cmd_name.replace('habu.', '')
cmd_link = cmd_name.replace('.', '')
readme_index += '* `{} <#{}>`_\n'.format(cmd_idx, cmd_link)
readme_content += cmd_name + '\n'
readme_content += '-' * len(cmd_name) + '\n\n'
readme_content += '.. code-block::\n\n'
p = subprocess.Popen([ 'python3', cmd_file, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
if p.returncode not in [0, None]:
logging.error('Error on {}'.format(cmd_name))
logging.error('Return code: {}'.format(p.returncode))
logging.error(p.stderr.read().decode())
logging.error('README.rst generation cancelled')
sys.exit(1)
for line in p.stdout.read().decode().split('\n'):
readme_content += ' ' + line.replace(cmd_file.name, cmd_name) + '\n' #p.stdout.read().decode()
readme_content += '\n'
with (here / 'README.rst').open('w') as output:
output.write(readme_header)
output.write(readme_index)
output.write(readme_content)