-
Notifications
You must be signed in to change notification settings - Fork 73
/
make_puppet_param_markdown.py
executable file
·68 lines (59 loc) · 1.96 KB
/
make_puppet_param_markdown.py
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
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
#
# Python script to generate MarkDown docblock fragment
# for all parameters of a Puppet parameterized class
# or define.
#
# Simple, naive regex matching. Assumes you style your manifests properly.
#
##################
# Copyright 2014 Jason Antman <[email protected]> <http://www.jasonantman.com>
# Free for any use provided that patches are submitted back to me.
#
# The latest version of this script can be found at:
# <https://github.com/jantman/misc-scripts/blob/master/make_puppet_param_markdown.py>
#
# CHANGELOG:
# 2014-02-06 Jason Antman <[email protected]>:
# - initial script
##########################################################################################
import os.path
import re
import sys
if len(sys.argv) < 2 or len(sys.argv) > 3:
sys.stderr.write("USAGE: make_puppet_param_markdown.py /path/to/manifest.pp\n")
sys.exit(1)
fname = sys.argv[1]
if not os.path.exists(fname):
sys.stderr.write("ERROR: %s does not appear to exist\n" % fname)
sys.exit(1)
start_re = re.compile(r'^\s*(define|class).*\($')
end_re = re.compile(r'.*{$')
comment_re = re.compile(r'^\s*#')
lines = []
in_params = False
with open(fname, 'r') as fh:
for line in fh:
line = line.strip()
if comment_re.match(line):
continue
if not in_params and start_re.match(line):
in_params = True
elif in_params and end_re.match(line):
break
elif in_params:
lines.append(line)
if len(lines) < 1:
sys.stderr.write("ERROR: did not find any params in %s\n" % fname)
sys.exit(1)
line_re = re.compile(r'\s*\$(?P<varname>\S+)(\s+=\s*(?P<val>\S+.*))?,?$')
for line in lines:
foo = line_re.match(line)
d = foo.groupdict()
print("# [*%s*]" % d['varname'].strip(', '))
print("# ()")
if 'val' in d and d['val'] is not None:
print("# (optional; default: %s)" % d['val'].strip(', '))
else:
print("# (required)")
print("#")