|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Generate Overview page of available GUCs in TimescaleDB with descriptions |
| 5 | +# |
| 6 | +# Args: |
| 7 | +# tag: tag to pull the guc.c from |
| 8 | +# |
| 9 | + |
| 10 | +import argparse |
| 11 | +import requests |
| 12 | +import re |
| 13 | +import logging |
| 14 | + |
| 15 | +logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO) |
| 16 | + |
| 17 | +parser = argparse.ArgumentParser() |
| 18 | +parser.add_argument('tag', type=str, help='tag name to pull guc.c') |
| 19 | +parser.add_argument('destination', type=str, help='file name to add output') |
| 20 | +args = parser.parse_args() |
| 21 | + |
| 22 | +TYPES = { |
| 23 | + "DefineCustomBoolVariable": "BOOLEAN", |
| 24 | + "DefineCustomIntVariable": "INTEGER", |
| 25 | + "DefineCustomEnumVariable": "ENUM", |
| 26 | + "DefineCustomStringVariable": "STRING", |
| 27 | +} |
| 28 | + |
| 29 | +# List of GUCs to exclude from the docs |
| 30 | +EXCLUDE = [] |
| 31 | + |
| 32 | +""" |
| 33 | +Fetch the guc.c content from GitHub |
| 34 | +@param url: str |
| 35 | +@return str |
| 36 | +""" |
| 37 | +def get_content(url: str) -> str: |
| 38 | + resp = requests.get(url=url) |
| 39 | + if resp.status_code != 200: |
| 40 | + logging.error("can not fetch: %s" % url) |
| 41 | + exit(10) |
| 42 | + return resp.text |
| 43 | + |
| 44 | +""" |
| 45 | +Unwrap parsed GUCs into a map with GUC name as key and the value with the |
| 46 | +extracted values from the GUC: |
| 47 | + /* name= */, |
| 48 | + /* short_desc= */, |
| 49 | + /* long_desc= */, |
| 50 | + /* valueAddr= */, |
| 51 | + /* Value= */, |
| 52 | + /* context= */, |
| 53 | + /* flags= */, |
| 54 | + /* check_hook= */, |
| 55 | + /* assign_hook= */, |
| 56 | + /* show_hook= */ |
| 57 | +@param gucs: list |
| 58 | +@param guc_type: str |
| 59 | +@return dict |
| 60 | +""" |
| 61 | +def unwrap(gucs: list, guc_type: str) -> dict: |
| 62 | + map = {} |
| 63 | + |
| 64 | + for guc in gucs: |
| 65 | + # sanitize data |
| 66 | + it = [re.sub(r"[\n\t]*", "", v).strip() for v in guc.split(",")] |
| 67 | + |
| 68 | + # sanitize elements |
| 69 | + name = re.sub(r"[\"\(\)]*", "", it[0]) |
| 70 | + short_desc = sanitize_description(it[1]) |
| 71 | + long_desc = short_desc if it[2].lower() == "null" else sanitize_description(it[2]) |
| 72 | + |
| 73 | + # Exclude GUCs (if specified) |
| 74 | + if name not in EXCLUDE: |
| 75 | + map[name] = { |
| 76 | + "name": name, |
| 77 | + "short_desc": short_desc, |
| 78 | + "long_desc": long_desc, |
| 79 | + "value": get_value(guc_type, it), |
| 80 | + "type": guc_type, |
| 81 | + "scopes": [], # assigned later during scope discovery |
| 82 | + } |
| 83 | + |
| 84 | + logging.info("registered %d GUCs of type: %s" % (len(map), guc_type)) |
| 85 | + return map |
| 86 | + |
| 87 | +def sanitize_description(text) -> str: |
| 88 | + # Remove all quotes and normalize whitespace to single line |
| 89 | + return ' '.join(text.replace('"', '').split()).strip() |
| 90 | + |
| 91 | +def strip_comment_pattern(text) -> str: |
| 92 | + pattern = r'/\*\s*[a-zA-Z0-9_]*=\s*\*/' |
| 93 | + return re.sub(pattern, '', extract_gettext_noop_string(text)) |
| 94 | + |
| 95 | +def extract_gettext_noop_string(text): |
| 96 | + pattern = r'gettext_noop\s*\(\s*"([^"]*(?:\\.[^"]*)*)"\s*\)' |
| 97 | + match = re.search(pattern, text, re.DOTALL) |
| 98 | + return match.group(1) if match else text |
| 99 | + |
| 100 | +def get_value(type: str, parts: list) -> str: |
| 101 | + """ |
| 102 | + Get the value of the GUC based on the type |
| 103 | + """ |
| 104 | + if type == "BOOLEAN": |
| 105 | + if parts[5].upper()[0:4] == "PGC_": |
| 106 | + return strip_comment_pattern(parts[4]).strip() |
| 107 | + else: |
| 108 | + return strip_comment_pattern(parts[5]).strip() |
| 109 | + return strip_comment_pattern(parts[5]).strip() |
| 110 | + |
| 111 | +""" |
| 112 | +Parse GUCs and prepare them for rendering |
| 113 | +@param content: str |
| 114 | +@return dict |
| 115 | +""" |
| 116 | +def prepare(content: str) -> dict: |
| 117 | + map = {} |
| 118 | + |
| 119 | + # Find all GUCs based on patterns and prepare them in a dict |
| 120 | + for pattern, val in TYPES.items(): |
| 121 | + map.update(unwrap(re.findall(r"%s\(MAKE_EXTOPTION(.*?)\);" % pattern, content, re.DOTALL), val)) |
| 122 | + |
| 123 | + # TODO: find scopes |
| 124 | + # https://github.com/timescale/timescaledb/blob/2.19.x/src/guc.c#L797 |
| 125 | + |
| 126 | + |
| 127 | + # Return dict with alphabetically sorted keys |
| 128 | + return {i: map[i] for i in sorted(map.keys())} |
| 129 | + |
| 130 | +""" |
| 131 | +Render the GUCs to file |
| 132 | +""" |
| 133 | +def render(gucs: dict, filename: str): |
| 134 | + with open(filename, "w") as f: |
| 135 | + f.write("| Name | Type | Default | Long Description |\n") |
| 136 | + f.write("| -- | -- | -- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n") |
| 137 | + for guc in gucs.values(): |
| 138 | + f.write("| `%s` | `%s` | `%s` | %s |\n" % ( |
| 139 | + guc["name"], guc["type"], guc["value"], guc["long_desc"] |
| 140 | + )) |
| 141 | + logging.info("rendering completed to %s" % filename) |
| 142 | + |
| 143 | +""" |
| 144 | +Main |
| 145 | +""" |
| 146 | +if __name__ == "__main__": |
| 147 | + content = get_content("https://raw.githubusercontent.com/timescale/timescaledb/refs/tags/%s/src/guc.c" % args.tag) |
| 148 | + logging.info("fetched guc.c file for version: %s" % args.tag) |
| 149 | + gucs = prepare(content) |
| 150 | + render(gucs, args.destination) |
| 151 | + |
| 152 | +# print(gucs) |
0 commit comments