github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/consensus/poet/cli/sawtooth_poet_cli/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 16 import os 17 import sys 18 import toml 19 20 21 def _select_dir(sawtooth_home_dir, windows_dir, default_dir): 22 """Returns the directory value, selected using the SAWTOOTH_HOME 23 environment variable (if set) or OS defaults. 24 """ 25 if 'SAWTOOTH_HOME' in os.environ: 26 return os.path.join(os.environ['SAWTOOTH_HOME'], sawtooth_home_dir) 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, windows_dir) 32 33 return default_dir 34 35 36 def get_config_dir(): 37 """Returns the sawtooth configuration directory based on the 38 SAWTOOTH_HOME environment variable (if set) or OS defaults. 39 """ 40 return _select_dir('etc', 'conf', '/etc/sawtooth') 41 42 43 def _get_dir(toml_config_setting, 44 sawtooth_home_dir, 45 windows_dir, 46 default_dir): 47 """Determines the directory path based on configuration. 48 49 Arguments: 50 toml_config_setting (str): The name of the config setting related 51 to the directory which will appear in path.toml. 52 sawtooth_home_dir (str): The directory under the SAWTOOTH_HOME 53 environment variable. For example, for 'data' if the data 54 directory is $SAWTOOTH_HOME/data. 55 windows_dir (str): The windows path relative to the computed base 56 directory. 57 default_dir (str): The default path on Linux. 58 59 Returns: 60 directory (str): The path. 61 """ 62 conf_file = os.path.join(get_config_dir(), 'path.toml') 63 if os.path.exists(conf_file): 64 with open(conf_file) as fd: 65 raw_config = fd.read() 66 toml_config = toml.loads(raw_config) 67 if toml_config_setting in toml_config: 68 return toml_config[toml_config_setting] 69 70 return _select_dir(sawtooth_home_dir, windows_dir, default_dir) 71 72 73 def get_data_dir(): 74 """Returns the configured data directory.""" 75 return _get_dir( 76 toml_config_setting='data_dir', 77 sawtooth_home_dir='data', 78 windows_dir='data', 79 default_dir='/var/lib/sawtooth') 80 81 82 def get_key_dir(): 83 """Returns the configured key directory.""" 84 return _get_dir( 85 toml_config_setting='key_dir', 86 sawtooth_home_dir='keys', 87 windows_dir=os.path.join('conf', 'keys'), 88 default_dir='/etc/sawtooth/keys')