|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | + |
| 7 | + |
| 8 | +destdir = "src/documentation" |
| 9 | + |
| 10 | + |
| 11 | +def get_roles(directory): |
| 12 | + _roles = [] |
| 13 | + |
| 14 | + _rolesdir = "%s/roles/" % directory |
| 15 | + for _role in os.listdir(_rolesdir): |
| 16 | + _roledir = "%s/%s" % (_rolesdir, _role) |
| 17 | + if not os.path.isdir(_roledir) or \ |
| 18 | + not os.path.isdir("%s/meta" % _roledir) or \ |
| 19 | + not os.path.isdir("%s/tasks" % _roledir): |
| 20 | + continue |
| 21 | + if _role.startswith("ipa"): |
| 22 | + _role = _role[3:] |
| 23 | + _roles.append(_role) |
| 24 | + |
| 25 | + return sorted(_roles) |
| 26 | + |
| 27 | + |
| 28 | +def get_module_readmes(directory): |
| 29 | + _readmes = [] |
| 30 | + |
| 31 | + for _item in os.listdir(directory): |
| 32 | + if _item.startswith("README-") and \ |
| 33 | + _item.endswith(".md"): |
| 34 | + _item = _item[7:-3] |
| 35 | + _readmes.append(_item) |
| 36 | + |
| 37 | + return sorted(_readmes) |
| 38 | + |
| 39 | + |
| 40 | +def readlines(file_in): |
| 41 | + try: |
| 42 | + in_f = open(file_in, "r") |
| 43 | + except IOError: |
| 44 | + print("Failed to open '%s' for reading" % file_in) |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + lines = in_f.readlines() |
| 48 | + in_f.close() |
| 49 | + |
| 50 | + return lines |
| 51 | + |
| 52 | + |
| 53 | +def open_outfile(file_out): |
| 54 | + try: |
| 55 | + out_f = open(file_out, "w") |
| 56 | + except IOError: |
| 57 | + print("Failed to open '%s' for writing" % file_out) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + print("Writing %s" % file_out) |
| 61 | + |
| 62 | + return out_f |
| 63 | + |
| 64 | + |
| 65 | +def convert_write(lines, out_f): |
| 66 | + in_raw = False |
| 67 | + for line in lines: |
| 68 | + if in_raw: |
| 69 | + out_f.write(line) |
| 70 | + if line == "```\n": |
| 71 | + in_raw = False |
| 72 | + out_f.write("{% endraw %}\n") |
| 73 | + else: |
| 74 | + if line in ["```\n", "```ini\n", "```yaml\n", "```json\n", |
| 75 | + "```bash\n"]: |
| 76 | + in_raw = True |
| 77 | + out_f.write("{% raw %}\n") |
| 78 | + out_f.write(line) |
| 79 | + |
| 80 | + |
| 81 | +def copy_role_doc(srcdir, role): |
| 82 | + file_in = "%s/roles/ipa%s/README.md" % (srcdir, role) |
| 83 | + file_out = "src/documentation/roles/%s.md" % role |
| 84 | + |
| 85 | + lines = readlines(file_in) |
| 86 | + out_f = open_outfile(file_out) |
| 87 | + |
| 88 | + out_f.write("---\nlayout: page\ntitle: ipa%s\n---\n" % role) |
| 89 | + |
| 90 | + if " role" in lines[0] and lines[1].startswith("==="): |
| 91 | + lines = lines[2:] |
| 92 | + |
| 93 | + convert_write(lines, out_f) |
| 94 | + out_f.close() |
| 95 | + |
| 96 | + |
| 97 | +def copy_module_doc(srcdir, module): |
| 98 | + file_in = "%s/README-%s.md" % (srcdir, module) |
| 99 | + file_out = "src/documentation/plugins/%s.md" % module |
| 100 | + |
| 101 | + lines = readlines(file_in) |
| 102 | + out_f = open_outfile(file_out) |
| 103 | + |
| 104 | + out_f.write("---\nlayout: page\ntitle: ipa%s\n---\n" % module) |
| 105 | + |
| 106 | + if (" module" in lines[0] or " Module" in lines[0]) and \ |
| 107 | + lines[1].startswith("==="): |
| 108 | + lines = lines[2:] |
| 109 | + |
| 110 | + convert_write(lines, out_f) |
| 111 | + out_f.close() |
| 112 | + |
| 113 | + |
| 114 | +parser = argparse.ArgumentParser(usage="update_docs.py <ansible-freeipa repo directory>") |
| 115 | +options, args = parser.parse_known_args() |
| 116 | + |
| 117 | +if len(args) != 1: |
| 118 | + parser.print_help() |
| 119 | + sys.exit(1) |
| 120 | + |
| 121 | +srcdir = args[0] |
| 122 | + |
| 123 | +if not os.path.isdir("%s/roles" % srcdir) or \ |
| 124 | + not os.path.isdir("%s/plugins/modules" % srcdir) or \ |
| 125 | + not os.path.isfile("%s/utils/ansible-freeipa.spec.in" % srcdir): |
| 126 | + print("The given path is not a valid ansible-freeipa repository!") |
| 127 | + sys.exit(-1) |
| 128 | + |
| 129 | +roles = get_roles(srcdir) |
| 130 | +module_readmes = get_module_readmes(srcdir) |
| 131 | + |
| 132 | +for role in roles: |
| 133 | + copy_role_doc(srcdir, role) |
| 134 | + |
| 135 | +for readme in module_readmes: |
| 136 | + copy_module_doc(srcdir, readme) |
0 commit comments