teleport/build/builder/core/configs.py

98 lines
2.7 KiB
Python
Raw Normal View History

2017-01-11 12:04:11 +00:00
# -*- coding: utf8 -*-
import os
import sys
import platform
2017-01-16 13:28:13 +00:00
import configparser
2017-01-11 12:04:11 +00:00
from . import colorconsole as cc
__all__ = ['cfg']
2017-01-16 13:28:13 +00:00
class AttrDict(dict):
2017-01-11 12:04:11 +00:00
"""
可以像属性一样访问字典的 Keyvar.key 等同于 var['key']
"""
def __getattr__(self, name):
try:
return self[name]
except KeyError:
# print(self.__class__.__name__)
raise
def __setattr__(self, name, val):
self[name] = val
2017-01-16 13:28:13 +00:00
class ConfigFile(AttrDict):
2017-01-11 12:04:11 +00:00
def __init__(self, **kwargs):
super().__init__(**kwargs)
def init(self, cfg_file):
self['ROOT_PATH'] = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
self['py_exec'] = sys.executable
_py_ver = platform.python_version_tuple()
self['py_ver'] = _py_ver
self['py_ver_str'] = '%s%s' % (_py_ver[0], _py_ver[1])
self['is_py2'] = sys.version_info[0] == 2
self['is_py3'] = sys.version_info[0] == 3
_bits = platform.architecture()[0]
if _bits == '64bit':
self['is_x64'] = True
self['is_x86'] = False
else:
self['is_x64'] = False
self['is_x86'] = True
_os = platform.system().lower()
2017-01-16 13:28:13 +00:00
self['is_win'] = False
self['is_linux'] = False
self['is_macos'] = False
2017-01-11 12:04:11 +00:00
self['dist'] = ''
if _os == 'windows':
2017-01-16 13:28:13 +00:00
self['is_win'] = True
2017-01-11 12:04:11 +00:00
self['dist'] = 'windows'
elif _os == 'linux':
2017-01-16 13:28:13 +00:00
self['is_linux'] = True
2017-01-11 12:04:11 +00:00
self['dist'] = 'linux'
elif _os == 'darwin':
2017-01-16 13:28:13 +00:00
self['is_macos'] = True
2017-01-11 12:04:11 +00:00
self['dist'] = 'macos'
else:
cc.e('not support this OS: {}'.format(platform.system()))
return False
2017-01-16 13:28:13 +00:00
_cfg = configparser.ConfigParser()
_cfg.read(cfg_file)
if 'external_ver' not in _cfg.sections() or 'toolchain' not in _cfg.sections():
cc.e('invalid configuration file: need `external_ver` and `toolchain` section.')
2017-01-11 12:04:11 +00:00
return False
2017-01-16 13:28:13 +00:00
_tmp = _cfg['external_ver']
if 'libuv' not in _tmp or 'mbedtls' not in _tmp or 'sqlite' not in _tmp:
cc.e('invalid configuration file: external version not set.')
2017-01-11 12:04:11 +00:00
return False
2017-01-16 13:28:13 +00:00
self['ver'] = AttrDict()
for k in _tmp:
self['ver'][k] = _tmp[k]
2017-01-11 12:04:11 +00:00
2017-01-16 13:28:13 +00:00
_tmp = _cfg['toolchain']
self['toolchain'] = AttrDict()
2017-01-16 13:28:13 +00:00
if self.is_win:
self['toolchain']['nsis'] = _tmp.get('nsis', None)
self['toolchain']['msbuild'] = None # msbuild always read from register.
2017-01-16 13:28:13 +00:00
else:
self['toolchain']['cmake'] = _tmp.get('cmake', '/usr/bin/cmake')
2017-01-11 12:04:11 +00:00
2017-01-16 13:28:13 +00:00
return True
2017-01-11 12:04:11 +00:00
cfg = ConfigFile()
del ConfigFile