|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | + |
| 7 | + |
| 8 | +class ParsingException(Exception): |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +def convert_variable(variable_name: str, default_value: str, comment: str, indentation: int = None): |
| 13 | + if indentation is None: |
| 14 | + indentation = 0 |
| 15 | + |
| 16 | + variable_name_upper = variable_name.upper() |
| 17 | + env_var_name = ".Env.%s" % variable_name_upper |
| 18 | + |
| 19 | + lines = [] |
| 20 | + lines.append("{{ if (contains .Env \"%s\") -}}" % variable_name_upper) |
| 21 | + #lines.append(comment) |
| 22 | + #lines.append("// default/example value: %s" % default_value) |
| 23 | + lines.append("%s \"{{ %s }}\"" % (variable_name, env_var_name)) |
| 24 | + lines.append("{{ else -}}") |
| 25 | + lines.append("// %s %s %s" % (variable_name, default_value, comment)) |
| 26 | + lines.append("{{ end -}}") |
| 27 | + lines.append("") |
| 28 | + |
| 29 | + return textwrap.indent("\n".join(lines), " "*indentation) |
| 30 | + |
| 31 | + |
| 32 | +def parse_and_convert_variable(pattern: str, line: str, indentation: int = None): |
| 33 | + match = re.match(pattern, line) |
| 34 | + |
| 35 | + if not match: |
| 36 | + return |
| 37 | + |
| 38 | + return convert_variable(*match.groups(), indentation) |
| 39 | + |
| 40 | + |
| 41 | +def parse_regular_variable(line: str): |
| 42 | + return parse_and_convert_variable(r"^//\s?((?:sv_|admin|server)\w+)\s+(.+)\s+(\/\/.*)$", line) |
| 43 | + |
| 44 | + |
| 45 | +def parse_rehashing_variable(line: str): |
| 46 | + return parse_and_convert_variable(r"^ //\s*((?!irc)\w+)\s+(.+)\s+(\/\/.*)$", line, 4) |
| 47 | + |
| 48 | + |
| 49 | +def parse_add_variable(line: str): |
| 50 | + match = re.match(r"^//\s? (add\w+)\s+(.+)\s*(|\/\/.*)$", line) |
| 51 | + |
| 52 | + if not match: |
| 53 | + return |
| 54 | + |
| 55 | + variable_name, default_value, comment = match.groups() |
| 56 | + |
| 57 | + variable_name_upper = variable_name.upper() |
| 58 | + env_var_name = ".Env.%s" % variable_name_upper |
| 59 | + |
| 60 | + lines = [] |
| 61 | + lines.append("{{ if (contains .Env \"%s\") -}}" % variable_name_upper) |
| 62 | + #lines.append(comment) |
| 63 | + #lines.append("// default/example value: %s" % default_value) |
| 64 | + lines.append("{{ range $e := ( split %s \";\" ) -}}" % env_var_name) |
| 65 | + lines.append("%s {{ $e }}" % variable_name) |
| 66 | + lines.append("{{ end -}}") |
| 67 | + lines.append("{{ else -}}") |
| 68 | + lines.append(line) |
| 69 | + lines.append("{{ end -}}") |
| 70 | + lines.append("") |
| 71 | + |
| 72 | + return "\n".join(lines) |
| 73 | + |
| 74 | + |
| 75 | +def parse_and_convert_line(line: str) -> str: |
| 76 | + for f in [parse_regular_variable, parse_rehashing_variable, parse_add_variable]: |
| 77 | + parsed = f(line) |
| 78 | + |
| 79 | + if parsed is not None: |
| 80 | + return parsed |
| 81 | + |
| 82 | + else: |
| 83 | + return line |
| 84 | + |
| 85 | + |
| 86 | +def make_irc_section(): |
| 87 | + return textwrap.dedent( |
| 88 | + """ |
| 89 | + {{ if (contains .Env "ENABLE_IRC") -}} |
| 90 | + // special single-server IRC configuration, suitable for our Docker deployment |
| 91 | + // setting ENABLE_IRC to some value will be sufficient in most cases |
| 92 | + if (= $rehashing 0) [ |
| 93 | + ircfilter {{ default .Env.IRC_FILTER "1" }} // defines the way the colour-to-irc filter works; 0 = off, "1" = convert, 2 = strip |
| 94 | +
|
| 95 | + ircaddrelay ircrelay {{ default .Env.IRC_RELAY_HOSTNAME "localhost" }} {{ default .Env.IRC_RELAY_PORT "6667" }} {{ default .Env.IRC_RELAY_NICK "re-server" }} |
| 96 | +
|
| 97 | + {{ if (contains .Env "IRC_BIND_ADDRESS") -}} |
| 98 | + ircbind ircrelay {{ .Env.IRC_BIND_ADDRESS }} // use this only if you need to bind to a specific address, eg. multihomed machines |
| 99 | + {{ end -}} |
| 100 | +
|
| 101 | + {{ if (contains .Env "IRC_SERVER_PASS" ) -}} |
| 102 | + ircpass ircrelay {{ .Env.IRC_SERVER_PASS }} // some networks can use the PASS field to identify to nickserv |
| 103 | + {{ end -}} |
| 104 | +
|
| 105 | + {{ if (contains .Env "IRC_CHANNELS") -}} |
| 106 | + {{ range $e := ( split .Env.IRC_CHANNELS "," ) -}} |
| 107 | + ircaddchan ircrelay "{{ $e }}" |
| 108 | + ircrelaychan ircrelay "{{ $e }}" 3 |
| 109 | + {{ end -}} |
| 110 | + {{ end -}} |
| 111 | +
|
| 112 | + ircconnect ircrelay // and tell it to connect! |
| 113 | + ] |
| 114 | + {{ end -}} |
| 115 | + """ |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def make_additional_vars_section(): |
| 120 | + text = textwrap.dedent( |
| 121 | + """ |
| 122 | + {{ if (contains .Env "ADDITIONAL_VARS") -}} |
| 123 | + // additional variables |
| 124 | + {{ range $e := (split .Env.ADDITIONAL_VARS ";") -}} |
| 125 | + {{ $a := (split $e "=") -}} |
| 126 | + {{ index $a 0 }} {{ index $a 1 }} |
| 127 | + {{ end -}} |
| 128 | + {{ end -}} |
| 129 | +
|
| 130 | + {{ if (contains .Env "SV_DUELMAXQUEUED") -}} |
| 131 | + sv_duelmaxqueued "{{ .Env.SV_DUELMAXQUEUED }}" |
| 132 | + {{ end -}} |
| 133 | + """ |
| 134 | + ) |
| 135 | + |
| 136 | + for i in ["duelmaxqueued", "teamneutralcolour"]: |
| 137 | + pass |
| 138 | + text += textwrap.dedent( |
| 139 | + """ |
| 140 | + {{{{ if (contains .Env "SV_{upper}") -}}}} |
| 141 | + sv_{lower} "{{{{ .Env.SV_{upper} }}}}" |
| 142 | + {{{{ end -}}}} |
| 143 | + """.format(lower=i, upper=i.upper()) |
| 144 | + ) |
| 145 | + |
| 146 | + return text |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + with open(sys.argv[1]) as f: |
| 151 | + lines = f.read().splitlines() |
| 152 | + |
| 153 | + for line in lines: |
| 154 | + print(parse_and_convert_line(line)) |
| 155 | + |
| 156 | + print(make_irc_section()) |
| 157 | + print(make_additional_vars_section()) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + try: |
| 162 | + sys.exit(main()) |
| 163 | + except BrokenPipeError: |
| 164 | + pass |
0 commit comments