def _remove_recipes(self): """ Remove a recipe alias and, if applicable, its cache. """ if not self.cfg.get_named_recipe_dirs().has_key(self.args.alias): self.log.error( "Unknown recipe alias: {alias}".format(alias=self.args.alias)) return -1 # Remove from config file cfg_filename = self.cfg.get_named_recipe_cfg_file(self.args.alias) cfg_file = PBConfigFile(cfg_filename) cfg_data = cfg_file.get() cfg_data['recipes'].pop(self.args.alias, None) cfg_file.save(cfg_data) recipe_cache_dir = os.path.join( os.path.split(cfg_filename)[0], self.cfg.recipe_cache_dir, self.args.alias, ) # If the recipe cache is not inside a PyBOMBS dir, don't delete it. if self.cfg.pybombs_dir not in recipe_cache_dir: return if os.path.exists(recipe_cache_dir): self.log.info( "Removing directory: {cdir}".format(cdir=recipe_cache_dir)) shutil.rmtree(recipe_cache_dir)
def remove_recipe_dir(self, alias): """ Remove a recipe alias and, if applicable, its cache. """ if not alias in self.cfg.get_named_recipe_dirs(): #self.log.error("Unknown recipe alias: {alias}".format(alias=alias)) remove_fail = 'Could not remove the recipe directory' self.color_strips(remove_fail, 'red') return False # Remove from config file cfg_filename = self.cfg.get_named_recipe_cfg_file(alias) cfg_file = PBConfigFile(cfg_filename) cfg_data = cfg_file.get() cfg_data['recipes'].pop(alias, None) cfg_file.save(cfg_data) recipe_cache_dir = os.path.join( os.path.split(cfg_filename)[0], self.cfg.recipe_cache_dir, alias, ) # If the recipe cache is not inside a PyBOMBS dir, don't delete it. if self.cfg.pybombs_dir not in recipe_cache_dir: remove_success = 'Successfully removed the recipe directory' self.color_strips(remove_success, 'blue') return True if os.path.exists(recipe_cache_dir): #self.log.info("Removing directory: {cdir}".format(cdir=recipe_cache_dir)) shutil.rmtree(recipe_cache_dir) remove_success = 'Successfully removed the recipe directory' self.color_strips(remove_success, 'blue') return True
def remove_recipe_dir(self, alias): """ Remove a recipe alias and, if applicable, its cache. """ if not alias in self.cfg.get_named_recipe_dirs(): self.log.error("Unknown recipe alias: {alias}".format(alias=alias)) return False # Remove from config file cfg_filename = self.cfg.get_named_recipe_cfg_file(alias) cfg_file = PBConfigFile(cfg_filename) cfg_data = cfg_file.get() cfg_data['recipes'].pop(alias, None) cfg_file.save(cfg_data) recipe_cache_dir = os.path.join( os.path.split(cfg_filename)[0], self.cfg.recipe_cache_dir, alias, ) # If the recipe cache is not inside a PyBOMBS dir, don't delete it. if self.cfg.pybombs_dir not in recipe_cache_dir: return True if os.path.exists(recipe_cache_dir): self.log.info("Removing directory: {cdir}".format(cdir=recipe_cache_dir)) shutil.rmtree(recipe_cache_dir) return True
def _run_config(self): " Handle `pybombs config' " if self.args.config_only: cfg_data = PBConfigFile(self.cfg_file).get('config') else: cfg_data = self.cfg keys = [self.args.key] if self.args.key is None: keys = cfg_data.keys() elif self.args.value is not None: self.cfg.set(self.args.key, self.args.value) self.cfg.update_cfg_file( new_data={'config': {self.args.key: self.args.value}}, cfg_file=self.cfg_file, ) for key in keys: print("{key}: {value}".format(key=key, value=cfg_data.get(key, ""))) print(" - {help}".format(help=self.cfg.get_help(key) or " Undocumented config option"))
def _append_cfg_from_file(self, cfg_filename): """ Load file filename, interpret it as a config file and append to cfg_cascade Returns True if loading the config file was successful. """ self.log.debug("Reading config info from file: {0}".format(cfg_filename)) try: cfg_data = PBConfigFile(cfg_filename).get() except Exception as e: self.log.debug("Parsing config file failed ({cfgf}).".format(cfgf=cfg_filename)) self.cfg_cascade.append({}) return False config_items = cfg_data.get('config', {}) self.log.debug('New config items: {items}'.format(items=config_items)) self.cfg_cascade.append(config_items) return True
def _lint_recipe(self, recipe_file): """ Check if recipe_file is a valid recipe """ print("Linting recipe `{0}'".format(recipe_file)) # Basic file checks try: recipe_dict = PBConfigFile(recipe_file).get() except IOError: self.log.error("Can't open `{0}'".format(recipe_file)) return -1 except AttributeError: self.log.error( "Can't parse contents of file `{0}'".format(recipe_file)) return -1 if not isinstance(recipe_dict, dict): self.log.error("Invalid recipe file. Not a dict.") return -1 # Try loading as recipe try: rec = recipe.Recipe(recipe_file) if not hasattr(rec, 'satisfy'): print("[HMM] - No satisfy rules declared") else: for pkgtype in rec.satisfy.keys(): rec.get_package_reqs(pkgtype) except PBException as ex: print("[VERY BAD] - Recipe error: " + str(ex)) # Check keys key_check = { 'HMM': ['source', 'depends'], 'BAD': ['inherit', 'category'], } for err_type, key_list in iteritems(key_check): for key in key_list: if not key in recipe_dict: print("[{err}] Recipe doesn't have key: {key}".format( err=err_type, key=key)) # Check dependencies is a list dependencies = recipe_dict.get('depends', []) if not isinstance(dependencies, list): print("[VERY BAD] Dependencies is not a list: {}".format( dependencies))
def _append_cfg_from_file(self, cfg_filename): """ Load file filename, interpret it as a config file and append to cfg_cascade Returns True if loading the config file was successful. """ self.log.debug( "Reading config info from file: {0}".format(cfg_filename)) try: cfg_data = PBConfigFile(cfg_filename).get() except Exception as e: self.log.debug("Parsing config file failed ({cfgf}).".format( cfgf=cfg_filename)) self.cfg_cascade.append({}) return False config_items = cfg_data.get('config', {}) self.log.debug('New config items: {items}'.format(items=config_items)) self.cfg_cascade.append(config_items) return True
def _lint_recipe(self, recipe_file): """ Check if recipe_file is a valid recipe """ print("Linting recipe `{0}'".format(recipe_file)) # Basic file checks try: recipe_dict = PBConfigFile(recipe_file).get() except IOError: self.log.error("Can't open `{0}'".format(recipe_file)) return -1 except AttributeError: self.log.error("Can't parse contents of file `{0}'".format(recipe_file)) return -1 if not isinstance(recipe_dict, dict): self.log.error("Invalid recipe file. Not a dict.") return -1 # Try loading as recipe try: rec = recipe.Recipe(recipe_file) if not hasattr(rec, 'satisfy'): print("[HMM] - No satisfy rules declared") else: for pkgtype in rec.satisfy.keys(): rec.get_package_reqs(pkgtype) except PBException as ex: print("[VERY BAD] - Recipe error: " + str(ex)) # Check keys key_check = { 'HMM': ['source', 'depends'], 'BAD': ['inherit', 'category'], } for err_type, key_list in iteritems(key_check): for key in key_list: if not key in recipe_dict: print("[{err}] Recipe doesn't have key: {key}".format(err=err_type, key=key)) # Check dependencies is a list dependencies = recipe_dict.get('depends', []) if not isinstance(dependencies, list): print("[VERY BAD] Dependencies is not a list: {}" .format(dependencies))
def _run_config(self): " Handle `pybombs config' " if self.args.config_only: cfg_data = PBConfigFile(self.cfg_file).get('config') else: cfg_data = self.cfg keys = [self.args.key] if self.args.key is None: keys = cfg_data.keys() elif self.args.value is not None: self.cfg.set(self.args.key, self.args.value) self.cfg.update_cfg_file( new_data={'config': { self.args.key: self.args.value }}, cfg_file=self.cfg_file, ) for key in keys: print("{key}: {value}".format(key=key, value=cfg_data.get(key, ""))) print(" - {help}".format( help=self.cfg.get_help(key) or " Undocumented config option"))
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()