def __init__(self, args, cfg_list, select_prefix=None): self.log = pb_logging.logger.getChild("ConfigManager.PrefixInfo") self.prefix_dir = None self.prefix_cfg_dir = None self.prefix_src = None self.alias = None self.src_dir = None self.cfg_file = None self.inv_file = None self.inventory = None self.recipe_dir = None self.target_dir = None self.env = os.environ.copy() self.is_virtualenv = False self._cfg_info = OrderedDict(self.default_config_info) if select_prefix is not None: args.prefix = select_prefix # 1) Load the config info for cfg_file in reversed(cfg_list): self._cfg_info = \ self._merge_config_info_from_file(cfg_file, self._cfg_info) # 2) Find the prefix directory self._find_prefix_dir(args) if self.prefix_dir is None: self.log.debug("Cannot establish a prefix directory. This may cause issues down the line.") self._set_attrs() return assert self.prefix_dir is not None if self.alias is not None and self.alias in self._cfg_info['prefix_config_dir']: self.prefix_cfg_dir = npath(self._cfg_info['prefix_config_dir'][self.alias]) self.log.debug("Choosing prefix config dir from alias: {0}".format(self.prefix_cfg_dir)) elif self.prefix_dir in self._cfg_info['prefix_config_dir']: self.prefix_cfg_dir = npath(self._cfg_info['prefix_config_dir'][self.prefix_dir]) self.log.debug("Choosing prefix config dir from path lookup in prefix_config_dir: {0}".format(self.prefix_cfg_dir)) else: self.prefix_cfg_dir = npath(os.path.join(self.prefix_dir, self.prefix_conf_dir)) self.log.debug("Choosing default prefix config dir: {0}".format(self.prefix_cfg_dir)) if not os.path.isdir(self.prefix_cfg_dir): self.log.debug("Config dir does not yet exist.") self.is_virtualenv = sysutils.is_virtualenv(self.prefix_dir) if self.is_virtualenv: self.log.info("Prefix is a Python virtualenv.") # 3) Find the config file self.cfg_file = npath(os.path.join(self.prefix_cfg_dir, ConfigManager.cfg_file_name)) config_section = {} if not os.path.isfile(self.cfg_file): self.log.debug("Prefix configuration file not found: {0}, assuming empty.".format(self.cfg_file)) else: config_section = PBConfigFile(self.cfg_file).get('config') self._cfg_info = self._merge_config_info_from_file(self.cfg_file, self._cfg_info) # 4) Find the src dir self.src_dir = npath(config_section.get('srcdir', os.path.join(self.prefix_dir, self.src_dir_name))) self.log.debug("Prefix source dir is: {0}".format(self.src_dir)) if not os.path.isdir(self.src_dir): self.log.debug("Source dir does not exist.") # 5) Find the inventory file self.inv_file = npath(os.path.join(self.prefix_cfg_dir, self.inv_file_name)) if not os.path.isfile(self.inv_file): self.log.debug("Prefix inventory file not found: {0}".format(self.inv_file)) self.inventory = inventory.Inventory(inventory_file=self.inv_file) # 6) Prefix-specific recipes. There's two places for these: # - A 'recipes/' subdirectory # - Anything declared in the config.yml file inside the prefix self.recipe_dir = npath(config_section.get('recipes', os.path.join(self.prefix_cfg_dir, 'recipes'))) if os.path.isdir(self.recipe_dir): self.log.debug("Prefix-local recipe dir is: {0}".format(self.recipe_dir)) else: self.recipe_dir = None # 7) Load environment # If there's a setup_env option in the current config file, we use that if self.setup_env_key in config_section: self.log.debug('Loading environment from shell script: {0}'.format(config_section[self.setup_env_key])) self.env = self._load_environ_from_script(config_section[self.setup_env_key]) else: self.env = self._load_default_env(self.env) # Set env vars that we always need self.env[self.env_prefix_var] = self.prefix_dir self.env[self.env_srcdir_var] = self.src_dir # env: sections are always respected: OLD_ENV = os.environ # Bit of an ugly hack to allow use of os.environ = self.env # os.path.expandvars() on self.env for k, v in iteritems(self._cfg_info['env']): self.env[k.upper()] = os.path.expandvars(v.strip()) os.environ = OLD_ENV # 8) Keep relevant config sections as attributes self._set_attrs()
def __init__(self, args, cfg_list, select_prefix=None): self.log = pb_logging.logger.getChild("ConfigManager.PrefixInfo") self.prefix_dir = None self.prefix_cfg_dir = None self.prefix_src = None self.alias = None self.src_dir = None self.cfg_file = None self.inv_file = None self.inventory = None self.recipe_dir = None self.target_dir = None self.env = os.environ.copy() self.is_virtualenv = False self._cfg_info = OrderedDict(self.default_config_info) if select_prefix is not None: args.prefix = select_prefix # 1) Load the config info for cfg_file in reversed(cfg_list): self._cfg_info = \ self._merge_config_info_from_file(cfg_file, self._cfg_info) # 2) Find the prefix directory self._find_prefix_dir(args) if self.prefix_dir is None: self.log.debug("Cannot establish a prefix directory. This may cause issues down the line.") self._set_attrs() return assert self.prefix_dir is not None if self.alias is not None and self.alias in self._cfg_info['prefix_config_dir']: self.prefix_cfg_dir = npath(self._cfg_info['prefix_config_dir'][self.alias]) self.log.debug("Choosing prefix config dir from alias: {0}".format(self.prefix_cfg_dir)) elif self.prefix_dir in self._cfg_info['prefix_config_dir']: self.prefix_cfg_dir = npath(self._cfg_info['prefix_config_dir'][self.prefix_dir]) self.log.debug("Choosing prefix config dir from path lookup in prefix_config_dir: {0}".format(self.prefix_cfg_dir)) else: self.prefix_cfg_dir = npath(os.path.join(self.prefix_dir, self.prefix_conf_dir)) self.log.debug("Choosing default prefix config dir: {0}".format(self.prefix_cfg_dir)) if not os.path.isdir(self.prefix_cfg_dir): self.log.debug("Config dir does not yet exist.") self.is_virtualenv = sysutils.is_virtualenv(self.prefix_dir) if self.is_virtualenv: self.log.info("Prefix is a Python virtualenv.") # 3) Find the config file self.cfg_file = npath(os.path.join(self.prefix_cfg_dir, ConfigManager.cfg_file_name)) config_section = {} if not os.path.isfile(self.cfg_file): self.log.debug("Prefix configuration file not found: {0}, assuming empty.".format(self.cfg_file)) else: config_section = PBConfigFile(self.cfg_file).get('config') self._cfg_info = self._merge_config_info_from_file(self.cfg_file, self._cfg_info) # 4) Find the src dir self.src_dir = npath(config_section.get('srcdir', os.path.join(self.prefix_dir, self.src_dir_name))) self.log.debug("Prefix source dir is: {0}".format(self.src_dir)) if not os.path.isdir(self.src_dir): self.log.debug("Source dir does not exist.") # 5) Find the inventory file self.inv_file = npath(os.path.join(self.prefix_cfg_dir, self.inv_file_name)) if not os.path.isfile(self.inv_file): self.log.debug("Prefix inventory file not found: {0}".format(self.inv_file)) self.inventory = inventory.Inventory(inventory_file=self.inv_file) # 6) Prefix-specific recipes. There's two places for these: # - A 'recipes/' subdirectory # - Anything declared in the config.yml file inside the prefix self.recipe_dir = npath(config_section.get('recipes', os.path.join(self.prefix_cfg_dir, 'recipes'))) if os.path.isdir(self.recipe_dir): self.log.debug("Prefix-local recipe dir is: {0}".format(self.recipe_dir)) else: self.recipe_dir = None # 7) Load environment # If there's a setup_env option in the current config file, we use that if self.setup_env_key in config_section: self.log.debug('Loading environment from shell script: {0}'.format(config_section[self.setup_env_key])) self.env = self._load_environ_from_script(config_section[self.setup_env_key]) else: self.env = self._load_default_env(self.env) # Set env vars that we always need self.env[self.env_prefix_var] = self.prefix_dir self.env[self.env_srcdir_var] = self.src_dir # env: sections are always respected: OLD_ENV = os.environ # Bit of an ugly hack to allow use of os.environ = self.env # os.path.expandvars() on self.env for k, v in iteritems(self._cfg_info['env']): self.env[k.upper()] = os.path.expandvars(v.strip()) os.environ = OLD_ENV # 8) Keep relevant config sections as attributes self._set_attrs()