github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/sawtooth_sdk/processor/config.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 import os 16 import sys 17 import toml 18 import yaml 19 20 21 def get_config_dir(): 22 """Returns the sawtooth configuration directory based on the 23 SAWTOOTH_HOME environment variable (if set) or OS defaults. 24 """ 25 if 'SAWTOOTH_HOME' in os.environ: 26 return os.path.join(os.environ['SAWTOOTH_HOME'], 'etc') 27 28 if os.name == 'nt': 29 base_dir = \ 30 os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 31 return os.path.join(base_dir, 'conf') 32 33 return '/etc/sawtooth' 34 35 36 def _get_dir(toml_config_setting, 37 sawtooth_home_dir, 38 windows_dir, 39 default_dir): 40 """Determines the directory path based on configuration. 41 42 Arguments: 43 toml_config_setting (str): The name of the config setting related 44 to the directory which will appear in path.toml. 45 sawtooth_home_dir (str): The directory under the SAWTOOTH_HOME 46 environment variable. For example, for 'data' if the data 47 directory is $SAWTOOTH_HOME/data. 48 windows_dir (str): The windows path relative to the computed base 49 directory. 50 default_dir (str): The default path on Linux. 51 52 Returns: 53 directory (str): The path. 54 """ 55 conf_file = os.path.join(get_config_dir(), 'path.toml') 56 if os.path.exists(conf_file): 57 with open(conf_file) as fd: 58 raw_config = fd.read() 59 toml_config = toml.loads(raw_config) 60 if toml_config_setting in toml_config: 61 return toml_config[toml_config_setting] 62 63 if 'SAWTOOTH_HOME' in os.environ: 64 return os.path.join(os.environ['SAWTOOTH_HOME'], sawtooth_home_dir) 65 66 if os.name == 'nt': 67 base_dir = \ 68 os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 69 return os.path.join(base_dir, windows_dir) 70 71 return default_dir 72 73 74 def _get_log_config(filename=None): 75 """Determines if there is a log config in the config directory 76 and returns it. If it does not exist, return None. 77 78 Arguments: 79 filename (str): The name of the logging config specific to the 80 transaction processor that is being started. 81 82 Returns: 83 log_config (dict): The dictionary to pass to logging.config.dictConfig 84 """ 85 if filename is not None: 86 87 conf_file = os.path.join(get_config_dir(), filename) 88 if os.path.exists(conf_file): 89 with open(conf_file) as fd: 90 raw_config = fd.read() 91 if filename.endswith(".yaml"): 92 log_config = yaml.safe_load(raw_config) 93 else: 94 log_config = toml.loads(raw_config) 95 return log_config 96 return None 97 98 99 def _get_processor_config(filename=None): 100 """Determines if there is a proccesor config in the config directory 101 and returns it. If it does not exist, return None. 102 103 Arguments: 104 filename (str): The name of the processor config specific to the 105 transaction processor that is being started. 106 107 Returns: 108 processor_config (dict): The dictionary to set transaction processor 109 """ 110 111 if filename is not None: 112 113 conf_file = os.path.join(get_config_dir(), filename) 114 if os.path.exists(conf_file): 115 with open(conf_file) as fd: 116 raw_config = fd.read() 117 log_config = toml.loads(raw_config) 118 return log_config 119 return None 120 121 122 def get_log_dir(): 123 """Returns the configured data directory.""" 124 return _get_dir( 125 toml_config_setting='log_dir', 126 sawtooth_home_dir='logs', 127 windows_dir='logs', 128 default_dir='/var/log/sawtooth') 129 130 131 def get_log_config(filename=None): 132 """Returns the log config dictinary if it exists.""" 133 return _get_log_config(filename) 134 135 136 def get_processor_config(filename=None): 137 """Returns the log config dictinary if it exists.""" 138 return _get_processor_config(filename)