gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 1 | import os |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 2 | import yaml |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 3 | |
| 4 | from petname import Generate as pet_generate |
gstepanov | 725cc30 | 2015-02-09 15:08:06 +0200 | [diff] [blame] | 5 | |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 6 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 7 | cfg_dict = {} |
gstepanov | c82391e | 2015-03-17 15:46:36 +0200 | [diff] [blame] | 8 | |
| 9 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 10 | def load_config(file_name, explicit_folder=None): |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 11 | global cfg_dict |
| 12 | first_load = len(cfg_dict) == 0 |
| 13 | cfg_dict.update(yaml.load(open(file_name).read())) |
gstepanov | 5f936df | 2015-03-18 19:15:59 +0200 | [diff] [blame] | 14 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 15 | if first_load: |
| 16 | var_dir = cfg_dict.get('internal', {}).get('var_dir_root', '/tmp') |
gstepanov | 5f936df | 2015-03-18 19:15:59 +0200 | [diff] [blame] | 17 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 18 | if explicit_folder is None: |
| 19 | while True: |
| 20 | dr = os.path.join(var_dir, pet_generate(2, "_")) |
| 21 | if not os.path.exists(dr): |
| 22 | break |
| 23 | else: |
| 24 | dr = explicit_folder |
gstepanov | 5f936df | 2015-03-18 19:15:59 +0200 | [diff] [blame] | 25 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 26 | cfg_dict['var_dir'] = dr |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 27 | if not os.path.exists(cfg_dict['var_dir']): |
| 28 | os.makedirs(cfg_dict['var_dir']) |
gstepanov | 5f936df | 2015-03-18 19:15:59 +0200 | [diff] [blame] | 29 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 30 | def in_var_dir(fname): |
| 31 | return os.path.join(cfg_dict['var_dir'], fname) |
| 32 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 33 | charts_img_path = in_var_dir('charts') |
| 34 | cfg_dict['charts_img_path'] = charts_img_path |
| 35 | if not os.path.exists(charts_img_path): |
| 36 | os.makedirs(charts_img_path) |
gstepanov | b3c7896 | 2015-03-20 15:47:58 +0200 | [diff] [blame] | 37 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 38 | cfg_dict['vm_ids_fname'] = in_var_dir('os_vm_ids') |
| 39 | cfg_dict['html_report_file'] = in_var_dir('report.html') |
| 40 | cfg_dict['text_report_file'] = in_var_dir('report.txt') |
| 41 | cfg_dict['log_file'] = in_var_dir('log.txt') |
koder aka kdanilov | 12ae063 | 2015-04-15 01:13:43 +0300 | [diff] [blame^] | 42 | cfg_dict['sensor_storage'] = in_var_dir('sensor_storage.txt') |