def recipe_links(self): res = [ location for location in glob(os.path.join(self.location, "recipes", "*")) if location.endswith(".json") ] return res
def remove_with_legend(self): for settings_file in glob(self.location + "/{}*json".format(self.app_name)): content = open(settings_file, "rb").read().decode("utf-8") content = content.replace("--with-legend", "--legend") open(settings_file, "wb").write(content.encode("utf-8")) self.computed_location = None self.compute_settings() return True
def recipes_instead_of_settings_level(self): if self.isrecipe: return True recipes_dir = self.location + "/recipes/" def migrate_settings_to_recipe(settings_level_file, name): if open(settings_level_file, "rb").read().decode("utf-8").strip() == "{}": return False makedirs(recipes_dir + "/" + name) enabled = not json.load(open(settings_level_file)).get( "_self", {}).get("disabled", False) order = json.load(open(settings_level_file)).get("_self", {}).get( "order", 100) move(settings_level_file, recipes_dir + "/" + name + "/{}.json".format(self.app_name)) createfile(recipes_dir + "/" + name + "/version.txt", str(self.version + 1)) createfile(recipes_dir + "/" + name + ".json", json.dumps({ "enabled": enabled, "order": order, })) return True migrate_something = False for settings_level_file in glob(self.location + "/{}-*.json".format(self.app_name)): name = re.sub(".+{}-([a-zA-Z-]+).json$".format(self.app_name), r"\1", settings_level_file) name = name.replace("-", "_") if name == "private": continue migrate_something |= migrate_settings_to_recipe( settings_level_file, name + "_from_settings") private = self.location + "/{}-private.json".format(self.app_name) local = self.location + "/{}.json".format(self.app_name) if os.path.exists(private): if open(private, "rb").read().decode("utf-8").strip() != "{}": migrate_something = True else: rm(private) if migrate_something is True: name = "migrated_local" if os.path.exists(local) and not open( local, "rb").read().decode("utf-8").strip() == "{}": migrate_settings_to_recipe(local, name) if os.path.exists(private): move(private, local) with json_file(local) as values: recipes = values.get("recipe", {}) local_order = recipes.get(name, {}) local_order["order"] = 0 recipes[name] = local_order values["recipe"] = recipes self.computed_location = None self.compute_settings() return True
def __init__(self, location, app_name, dry_run=False, name=None): self.app_name = app_name self.migrate_from = [ self.alias_has_documentation, self.stack_of_git_records, self.recipes_instead_of_settings_level, self.recipes_instead_of_profiles, self.remove_with_legend, self.hooks_to_trigger, ] self._version = None self.location = location self.settings_path = os.path.join(self.location, "{}.json".format(self.app_name)) self.dry_run = dry_run self.name = name def file_name_if_exists(file_name): return os.path.exists(file_name) and file_name or None self.persist = True self.prevented_persistence = False self.pluginsdir = os.path.join(self.location, "plugins") self.read_only = False self.old_version = self.version if self.version > self.max_version: LOGGER.error("The profile at location {} is at version {}." " I can only manage till version {}." " It will be ignored." " Please upgrade {} and try again.".format( self.location, self.old_version, self.max_version, self.app_name, )) self.read_only = True self.computed_location = None self.compute_settings() self.backup_location = self.location + "_backup" if os.path.exists(self.backup_location): LOGGER.warning("The backup directory for profile" " in location {} already exist".format( self.location)) self.migration_impact = [ os.path.basename(self.version_file_name), ] + [ os.path.basename(f) for f in glob(self.location + "/{}*json".format(self.app_name)) ] + ["recipes"]
def stack_of_git_records(self): for settings_file in glob(self.location + "/{}*json".format(self.app_name)): with json_file(settings_file) as settings: if "git_record" not in settings: continue git_records = settings["git_record"] new_git_records = { key: [record] for key, record in six.iteritems(git_records) } settings["git_record"] = new_git_records self.computed_location = None self.compute_settings() return True
def recipes_instead_of_profiles(self): if self.isrecipe: return True recipes_dir = self.location + "/recipes/" warn = False for profile in glob(self.location + "/../.csm-*"): profile_name = re.sub("^.+csm-(.+)$", r"\1", profile) recipe_location = recipes_dir + "/" + profile_name + "_from_profile" move(profile, recipe_location) warn = True if warn: LOGGER.warning("The profiles were migrated as recipes." " As we could not maintain backward compatibility," " please see with SLO or GLE to understand how" " to make use of this new setup") return True
def recipes(self): if self.isrecipe: return [] recipes_dir = os.path.join(self.location, "recipes") if not os.path.exists(recipes_dir): return [] res = sorted([ ProfileFactory.get( location, name=self.name + "/" + os.path.basename(location), app_name=self.app_name, ) for location in glob(os.path.join(recipes_dir, "*")) if not location.endswith(".json") and not location.endswith("_backup") ], key=lambda r: r.name) return res
def alias_has_documentation(self): for settings_file in glob(self.location + "/{}*json".format(self.app_name)): with json_file(settings_file) as settings: if "alias" not in settings: continue aliases = settings["alias"] new_aliases = { alias: { "commands": commands, "documentation": None, } for alias, commands in six.iteritems(aliases) } settings["alias"] = new_aliases self.computed_location = None self.compute_settings() return True