NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

config.py (3304B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\util\config.py
      2 import os
      3 import string
      4 import sys
      5 from datadog.util.compat import configparser, StringIO, is_p3k, pkg
      6 DATADOG_CONF = 'datadog.conf'
      7 
      8 class CfgNotFound(Exception):
      9     pass
     10 
     11 
     12 class PathNotFound(Exception):
     13     pass
     14 
     15 
     16 def get_os():
     17     if sys.platform == 'darwin':
     18         return 'mac'
     19     elif sys.platform.find('freebsd') != -1:
     20         return 'freebsd'
     21     elif sys.platform.find('linux') != -1:
     22         return 'linux'
     23     elif sys.platform.find('win32') != -1:
     24         return 'windows'
     25     elif sys.platform.find('sunos') != -1:
     26         return 'solaris'
     27     else:
     28         return sys.platform
     29 
     30 
     31 def skip_leading_wsp(f):
     32     if is_p3k():
     33         return StringIO('\n'.join((x.strip(' ') for x in f.readlines())))
     34     else:
     35         return StringIO('\n'.join(map(string.strip, f.readlines())))
     36 
     37 
     38 def _windows_commondata_path():
     39     import ctypes
     40     from ctypes import wintypes, windll
     41     CSIDL_COMMON_APPDATA = 35
     42     _SHGetFolderPath = windll.shell32.SHGetFolderPathW
     43     _SHGetFolderPath.argtypes = [wintypes.HWND,
     44      ctypes.c_int,
     45      wintypes.HANDLE,
     46      wintypes.DWORD,
     47      wintypes.LPCWSTR]
     48     path_buf = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
     49     _SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, path_buf)
     50     return path_buf.value
     51 
     52 
     53 def _windows_config_path():
     54     common_data = _windows_commondata_path()
     55     path = os.path.join(common_data, 'Datadog', DATADOG_CONF)
     56     if os.path.exists(path):
     57         return path
     58     raise PathNotFound(path)
     59 
     60 
     61 def _unix_config_path():
     62     path = os.path.join('/etc/dd-agent', DATADOG_CONF)
     63     if os.path.exists(path):
     64         return path
     65     raise PathNotFound(path)
     66 
     67 
     68 def _mac_config_path():
     69     path = os.path.join('~/.datadog-agent/agent', DATADOG_CONF)
     70     path = os.path.expanduser(path)
     71     if os.path.exists(path):
     72         return path
     73     raise PathNotFound(path)
     74 
     75 
     76 def get_config_path(cfg_path = None, os_name = None):
     77     if cfg_path is not None and os.path.exists(cfg_path):
     78         return cfg_path
     79     if os_name is None:
     80         os_name = get_os()
     81     if os_name == 'windows':
     82         return _windows_config_path()
     83     elif os_name == 'mac':
     84         return _mac_config_path()
     85     else:
     86         return _unix_config_path()
     87 
     88 
     89 def get_config(cfg_path = None, options = None):
     90     agentConfig = {}
     91     try:
     92         path = os.path.realpath(__file__)
     93         path = os.path.dirname(path)
     94         config_path = get_config_path(cfg_path, os_name=get_os())
     95         config = configparser.ConfigParser()
     96         config.readfp(skip_leading_wsp(open(config_path)))
     97         for option in config.options('Main'):
     98             agentConfig[option] = config.get('Main', option)
     99 
    100     except Exception:
    101         raise CfgNotFound
    102 
    103     return agentConfig
    104 
    105 
    106 def get_version():
    107     version = u'unknown'
    108     if not pkg:
    109         return version
    110     try:
    111         dist = pkg.get_distribution('datadog')
    112         dist_loc = os.path.normcase(dist.location)
    113         here = os.path.normcase(__file__)
    114         if not here.startswith(dist_loc):
    115             raise pkg.DistributionNotFound
    116         version = dist.version
    117     except pkg.DistributionNotFound:
    118         version = u'Please install `datadog` with setup.py'
    119     except:
    120         pass
    121 
    122     return version