1
1
# -*- coding: utf-8 -*-
2
2
# Copyright 2014-2016 OpenMarket Ltd
3
+ # Copyright 2017-2018 New Vector Ltd
4
+ # Copyright 2019 The Matrix.org Foundation C.I.C.
3
5
#
4
6
# Licensed under the Apache License, Version 2.0 (the "License");
5
7
# you may not use this file except in compliance with the License.
@@ -217,7 +219,7 @@ def load_config(cls, description, argv):
217
219
"--keys-directory" ,
218
220
metavar = "DIRECTORY" ,
219
221
help = "Where files such as certs and signing keys are stored when"
220
- " their location is given explicitly in the config."
222
+ " their location is not given explicitly in the config."
221
223
" Defaults to the directory containing the last config file" ,
222
224
)
223
225
@@ -229,10 +231,22 @@ def load_config(cls, description, argv):
229
231
230
232
config_files = find_config_files (search_paths = config_args .config_path )
231
233
234
+ if not config_files :
235
+ config_parser .error ("Must supply a config file." )
236
+
237
+ if config_args .keys_directory :
238
+ config_dir_path = config_args .keys_directory
239
+ else :
240
+ config_dir_path = os .path .dirname (config_files [- 1 ])
241
+ config_dir_path = os .path .abspath (config_dir_path )
242
+ data_dir_path = os .getcwd ()
243
+
232
244
config_dict = obj .read_config_files (
233
- config_files , keys_directory = config_args .keys_directory
245
+ config_files , config_dir_path = config_dir_path , data_dir_path = data_dir_path
246
+ )
247
+ obj .parse_config_dict (
248
+ config_dict , config_dir_path = config_dir_path , data_dir_path = data_dir_path
234
249
)
235
- obj .parse_config_dict (config_dict )
236
250
237
251
obj .invoke_all ("read_arguments" , config_args )
238
252
@@ -283,14 +297,28 @@ def load_or_generate_config(cls, description, argv):
283
297
metavar = "DIRECTORY" ,
284
298
help = (
285
299
"Specify where additional config files such as signing keys and log"
286
- " config should be stored. Defaults to the same directory as the main "
300
+ " config should be stored. Defaults to the same directory as the last "
287
301
" config file."
288
302
),
289
303
)
290
304
config_args , remaining_args = config_parser .parse_known_args (argv )
291
305
292
306
config_files = find_config_files (search_paths = config_args .config_path )
293
307
308
+ if not config_files :
309
+ config_parser .error (
310
+ "Must supply a config file.\n A config file can be automatically"
311
+ ' generated using "--generate-config -H SERVER_NAME'
312
+ ' -c CONFIG-FILE"'
313
+ )
314
+
315
+ if config_args .config_directory :
316
+ config_dir_path = config_args .config_directory
317
+ else :
318
+ config_dir_path = os .path .dirname (config_files [- 1 ])
319
+ config_dir_path = os .path .abspath (config_dir_path )
320
+ data_dir_path = os .getcwd ()
321
+
294
322
generate_missing_configs = config_args .generate_missing_configs
295
323
296
324
obj = cls ()
@@ -301,20 +329,10 @@ def load_or_generate_config(cls, description, argv):
301
329
"Please specify either --report-stats=yes or --report-stats=no\n \n "
302
330
+ MISSING_REPORT_STATS_SPIEL
303
331
)
304
- if not config_files :
305
- config_parser .error (
306
- "Must supply a config file.\n A config file can be automatically"
307
- ' generated using "--generate-config -H SERVER_NAME'
308
- ' -c CONFIG-FILE"'
309
- )
332
+
310
333
(config_path ,) = config_files
311
334
if not cls .path_exists (config_path ):
312
335
print ("Generating config file %s" % (config_path ,))
313
- if config_args .config_directory :
314
- config_dir_path = config_args .config_directory
315
- else :
316
- config_dir_path = os .path .dirname (config_path )
317
- config_dir_path = os .path .abspath (config_dir_path )
318
336
319
337
server_name = config_args .server_name
320
338
if not server_name :
@@ -325,7 +343,7 @@ def load_or_generate_config(cls, description, argv):
325
343
326
344
config_str = obj .generate_config (
327
345
config_dir_path = config_dir_path ,
328
- data_dir_path = os . getcwd () ,
346
+ data_dir_path = data_dir_path ,
329
347
server_name = server_name ,
330
348
report_stats = (config_args .report_stats == "yes" ),
331
349
generate_secrets = True ,
@@ -368,35 +386,37 @@ def load_or_generate_config(cls, description, argv):
368
386
obj .invoke_all ("add_arguments" , parser )
369
387
args = parser .parse_args (remaining_args )
370
388
371
- if not config_files :
372
- config_parser .error (
373
- "Must supply a config file.\n A config file can be automatically"
374
- ' generated using "--generate-config -H SERVER_NAME'
375
- ' -c CONFIG-FILE"'
376
- )
377
-
378
389
config_dict = obj .read_config_files (
379
- config_files , keys_directory = config_args . config_directory
390
+ config_files , config_dir_path = config_dir_path , data_dir_path = data_dir_path
380
391
)
381
392
382
393
if generate_missing_configs :
383
394
obj .generate_missing_files (config_dict )
384
395
return None
385
396
386
- obj .parse_config_dict (config_dict )
397
+ obj .parse_config_dict (
398
+ config_dict , config_dir_path = config_dir_path , data_dir_path = data_dir_path
399
+ )
387
400
obj .invoke_all ("read_arguments" , args )
388
401
389
402
return obj
390
403
391
- def read_config_files (self , config_files , keys_directory = None ):
404
+ def read_config_files (self , config_files , config_dir_path , data_dir_path ):
392
405
"""Read the config files into a dict
393
406
407
+ Args:
408
+ config_files (iterable[str]): A list of the config files to read
409
+
410
+ config_dir_path (str): The path where the config files are kept. Used to
411
+ create filenames for things like the log config and the signing key.
412
+
413
+ data_dir_path (str): The path where the data files are kept. Used to create
414
+ filenames for things like the database and media store.
415
+
394
416
Returns: dict
395
417
"""
396
- if not keys_directory :
397
- keys_directory = os .path .dirname (config_files [- 1 ])
398
-
399
- self .config_dir_path = os .path .abspath (keys_directory )
418
+ # FIXME: get rid of this
419
+ self .config_dir_path = config_dir_path
400
420
401
421
# first we read the config files into a dict
402
422
specified_config = {}
@@ -410,8 +430,8 @@ def read_config_files(self, config_files, keys_directory=None):
410
430
raise ConfigError (MISSING_SERVER_NAME )
411
431
server_name = specified_config ["server_name" ]
412
432
config_string = self .generate_config (
413
- config_dir_path = self . config_dir_path ,
414
- data_dir_path = os . getcwd () ,
433
+ config_dir_path = config_dir_path ,
434
+ data_dir_path = data_dir_path ,
415
435
server_name = server_name ,
416
436
generate_secrets = False ,
417
437
)
@@ -431,8 +451,24 @@ def read_config_files(self, config_files, keys_directory=None):
431
451
)
432
452
return config
433
453
434
- def parse_config_dict (self , config_dict ):
435
- self .invoke_all ("read_config" , config_dict )
454
+ def parse_config_dict (self , config_dict , config_dir_path , data_dir_path ):
455
+ """Read the information from the config dict into this Config object.
456
+
457
+ Args:
458
+ config_dict (dict): Configuration data, as read from the yaml
459
+
460
+ config_dir_path (str): The path where the config files are kept. Used to
461
+ create filenames for things like the log config and the signing key.
462
+
463
+ data_dir_path (str): The path where the data files are kept. Used to create
464
+ filenames for things like the database and media store.
465
+ """
466
+ self .invoke_all (
467
+ "read_config" ,
468
+ config_dict ,
469
+ config_dir_path = config_dir_path ,
470
+ data_dir_path = data_dir_path ,
471
+ )
436
472
437
473
def generate_missing_files (self , config_dict ):
438
474
self .invoke_all ("generate_files" , config_dict )
0 commit comments