6
6
import platform
7
7
import subprocess
8
8
import sys
9
+ from typing import Any , Dict , List , Mapping , MutableMapping , NoReturn , Optional
9
10
10
11
import jinja2
11
12
12
13
13
14
# Utility functions
14
- def log (txt ) :
15
+ def log (txt : str ) -> None :
15
16
print (txt , file = sys .stderr )
16
17
17
18
18
- def error (txt ) :
19
+ def error (txt : str ) -> NoReturn :
19
20
log (txt )
20
21
sys .exit (2 )
21
22
22
23
23
- def convert (src , dst , environ ) :
24
+ def convert (src : str , dst : str , environ : Mapping [ str , object ]) -> None :
24
25
"""Generate a file from a template
25
26
26
27
Args:
27
- src (str) : path to input file
28
- dst (str) : path to file to write
29
- environ (dict) : environment dictionary, for replacement mappings.
28
+ src: path to input file
29
+ dst: path to file to write
30
+ environ: environment dictionary, for replacement mappings.
30
31
"""
31
32
with open (src ) as infile :
32
33
template = infile .read ()
@@ -35,25 +36,30 @@ def convert(src, dst, environ):
35
36
outfile .write (rendered )
36
37
37
38
38
- def generate_config_from_template (config_dir , config_path , environ , ownership ):
39
+ def generate_config_from_template (
40
+ config_dir : str ,
41
+ config_path : str ,
42
+ os_environ : Mapping [str , str ],
43
+ ownership : Optional [str ],
44
+ ) -> None :
39
45
"""Generate a homeserver.yaml from environment variables
40
46
41
47
Args:
42
- config_dir (str) : where to put generated config files
43
- config_path (str) : where to put the main config file
44
- environ (dict) : environment dictionary
45
- ownership (str|None) : "<user>:<group>" string which will be used to set
48
+ config_dir: where to put generated config files
49
+ config_path: where to put the main config file
50
+ os_environ : environment mapping
51
+ ownership: "<user>:<group>" string which will be used to set
46
52
ownership of the generated configs. If None, ownership will not change.
47
53
"""
48
54
for v in ("SYNAPSE_SERVER_NAME" , "SYNAPSE_REPORT_STATS" ):
49
- if v not in environ :
55
+ if v not in os_environ :
50
56
error (
51
57
"Environment variable '%s' is mandatory when generating a config file."
52
58
% (v ,)
53
59
)
54
60
55
61
# populate some params from data files (if they exist, else create new ones)
56
- environ = environ . copy ( )
62
+ environ : Dict [ str , Any ] = dict ( os_environ )
57
63
secrets = {
58
64
"registration" : "SYNAPSE_REGISTRATION_SHARED_SECRET" ,
59
65
"macaroon" : "SYNAPSE_MACAROON_SECRET_KEY" ,
@@ -127,12 +133,12 @@ def generate_config_from_template(config_dir, config_path, environ, ownership):
127
133
subprocess .check_output (args )
128
134
129
135
130
- def run_generate_config (environ , ownership ) :
136
+ def run_generate_config (environ : Mapping [ str , str ], ownership : Optional [ str ]) -> None :
131
137
"""Run synapse with a --generate-config param to generate a template config file
132
138
133
139
Args:
134
- environ (dict) : env var dict
135
- ownership (str|None) : "userid:groupid" arg for chmod. If None, ownership will not change.
140
+ environ: env vars from `os.enrivon`.
141
+ ownership: "userid:groupid" arg for chmod. If None, ownership will not change.
136
142
137
143
Never returns.
138
144
"""
@@ -178,7 +184,7 @@ def run_generate_config(environ, ownership):
178
184
os .execv (sys .executable , args )
179
185
180
186
181
- def main (args , environ ) :
187
+ def main (args : List [ str ] , environ : MutableMapping [ str , str ]) -> None :
182
188
mode = args [1 ] if len (args ) > 1 else "run"
183
189
184
190
# if we were given an explicit user to switch to, do so
0 commit comments