github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/admin_command/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 _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, sawtooth_home_dir, windows_dir, default_dir): 37 """Determines the directory path based on configuration. 38 39 Arguments: 40 toml_config_setting (str): The name of the config setting related 41 to the directory which will appear in path.toml. 42 sawtooth_home_dir (str): The directory under the SAWTOOTH_HOME 43 environment variable. For example, for 'data' if the data 44 directory is $SAWTOOTH_HOME/data. 45 windows_dir (str): The windows path relative to the computed base 46 directory. 47 default_dir (str): The default path on Linux. 48 49 Returns: 50 directory (str): The path. 51 """ 52 conf_file = os.path.join(_get_config_dir(), 'path.toml') 53 if os.path.exists(conf_file): 54 with open(conf_file) as fd: 55 raw_config = fd.read() 56 toml_config = toml.loads(raw_config) 57 if toml_config_setting in toml_config: 58 return toml_config[toml_config_setting] 59 60 if 'SAWTOOTH_HOME' in os.environ: 61 return os.path.join(os.environ['SAWTOOTH_HOME'], sawtooth_home_dir) 62 63 if os.name == 'nt': 64 base_dir = \ 65 os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 66 return os.path.join(base_dir, windows_dir) 67 68 return default_dir 69 70 71 def get_data_dir(): 72 """Returns the configured data directory.""" 73 return _get_dir( 74 toml_config_setting='data_dir', 75 sawtooth_home_dir='data', 76 windows_dir='data', 77 default_dir='/var/lib/sawtooth') 78 79 80 def get_key_dir(): 81 """Returns the configured key directory.""" 82 return _get_dir( 83 toml_config_setting='key_dir', 84 sawtooth_home_dir='keys', 85 windows_dir=os.path.join('conf', 'keys'), 86 default_dir='/etc/sawtooth/keys')