1#!/usr/bin/env python
2
3"""
4Internal tool used to automatically generate an up-to-date version of the tuir
5man page. Currently this script should be manually ran after each version bump.
6In the future, it would be nice to have this functionality built into setup.py.
7
8Usage:
9    $ python scripts/build_manpage.py
10"""
11
12import os
13import sys
14from datetime import datetime
15
16_filepath = os.path.dirname(os.path.relpath(__file__))
17ROOT = os.path.abspath(os.path.join(_filepath, '..'))
18sys.path.insert(0, ROOT)
19
20import tuir
21from tuir import config
22
23
24def main():
25
26    parser = config.build_parser()
27    help_text = parser.format_help()
28    help_sections = help_text.split('\n\n')
29    del help_sections[1]
30
31    data = {}
32    print('Fetching version')
33    data['version'] = tuir.__version__
34    print('Fetching release date')
35    data['release_date'] = datetime.utcnow().strftime('%B %d, %Y')
36    print('Fetching synopsis')
37    synopsis = help_sections[0].replace('usage: ', '')
38    synopsis = ' '.join(line.strip() for line in synopsis.split('\n'))
39    data['synopsis'] = synopsis
40    print('Fetching description')
41    data['description'] = help_sections[1]
42    # Build the options section for each argument from the help section
43    # Example Before:
44    #         -h, --help        show this help message and exit
45    # Example After
46    #         .TP
47    #         \fB-h\fR, \fB--help\fR
48    #         show this help message and exit
49    options = ''
50    lines = help_sections[2].split('\n')[1:]  # positional arguments
51    lines.extend(help_sections[3].split('\n')[1:])  # optional arguments
52    lines = [line.strip() for line in lines]
53    arguments = []
54    for line in lines:
55        if line.startswith('-'):
56            arguments.append(line)
57        elif line.startswith('URL'):
58            # Special case for URL which is a positional argument
59            arguments.append(line)
60        else:
61            arguments[-1] = arguments[-1] + ' ' + line
62    for argument in arguments:
63        flag, description = (col.strip() for col in argument.split('  ', 1))
64        flag = ', '.join(r'\fB'+f+r'\fR' for f in flag.split(', '))
65        options += '\n'.join(('.TP', flag, description, '\n'))
66    data['options'] = options
67    print('Fetching license')
68    data['license'] = tuir.__license__
69    print('Fetching copyright')
70    data['copyright'] = tuir.__copyright__
71    # Escape dashes is all of the sections
72    data = {k: v.replace('-', r'\-') for k, v in data.items()}
73    print('Reading from %s/scripts/tuir.1.template' % ROOT)
74    with open(os.path.join(ROOT, 'scripts/tuir.1.template')) as fp:
75        template = fp.read()
76    print('Populating template')
77    out = template.format(**data)
78    print('Writing to %s/tuir.1' % ROOT)
79    with open(os.path.join(ROOT, 'tuir.1'), 'w') as fp:
80        fp.write(out)
81
82
83if __name__ == '__main__':
84    main()
85