From 9fc973366f69d32c1e66da182f6fb8bbe560d7cc Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 30 Jan 2018 13:52:05 -0800 Subject: [PATCH] Move creating a frozen recipe into recipes.py Also adds .package_names and .module_names properties. Call recipe.freeze with a list of NEVRA dependencies and it will return a new Recipe object with all of the packages and modules set to the depsolved version. --- src/pylorax/api/recipes.py | 33 +++++++++++++++++++++++++++++++++ src/pylorax/api/v0.py | 23 +++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/pylorax/api/recipes.py b/src/pylorax/api/recipes.py index d4e89989..4f05c758 100644 --- a/src/pylorax/api/recipes.py +++ b/src/pylorax/api/recipes.py @@ -25,6 +25,7 @@ import os import pytoml as toml import semantic_version as semver +from pylorax.api.projects import dep_evra from pylorax.base import DataHolder from pylorax.sysutils import joinpaths @@ -62,6 +63,16 @@ class Recipe(dict): modules=modules, packages=packages) + @property + def package_names(self): + """Return the names of the packages""" + return map(lambda p: p["name"], self["packages"] or []) + + @property + def module_names(self): + """Return the names of the modules""" + return map(lambda m: m["name"], self["modules"] or []) + @property def filename(self): """Return the Recipe's filename @@ -108,6 +119,28 @@ class Recipe(dict): # Return the new version return str(semver.Version(self["version"])) + def freeze(self, deps): + """ Return a new Recipe with full module and package NEVRA + + :param deps: A list of dependency NEVRA to use to fill in the modules and packages + :type deps: list( + :returns: A new Recipe object + :rtype: Recipe + """ + module_names = self.module_names + package_names = self.package_names + + new_modules = [] + new_packages = [] + for dep in deps: + if dep["name"] in package_names: + new_packages.append(RecipePackage(dep["name"], dep_evra(dep))) + elif dep["name"] in module_names: + new_modules.append(RecipeModule(dep["name"], dep_evra(dep))) + + return Recipe(self["name"], self["description"], self["version"], + new_modules, new_packages) + class RecipeModule(dict): def __init__(self, name, version): dict.__init__(self, name=name, version=version) diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 74972164..aeb1784d 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -620,11 +620,11 @@ from flask import jsonify, request from pylorax.api.compose import start_build, compose_types from pylorax.api.crossdomain import crossdomain -from pylorax.api.projects import projects_list, projects_info, projects_depsolve, dep_evra +from pylorax.api.projects import projects_list, projects_info, projects_depsolve from pylorax.api.projects import modules_list, modules_info, ProjectsError from pylorax.api.recipes import list_branch_files, read_recipe_commit, recipe_filename, list_commits from pylorax.api.recipes import recipe_from_dict, recipe_from_toml, commit_recipe, delete_recipe, revert_recipe -from pylorax.api.recipes import tag_recipe_commit, recipe_diff, Recipe, RecipePackage, RecipeModule +from pylorax.api.recipes import tag_recipe_commit, recipe_diff from pylorax.api.workspace import workspace_read, workspace_write, workspace_delete # The API functions don't actually get called by any code here @@ -918,8 +918,8 @@ def v0_api(api): # Combine modules and packages and depsolve the list # TODO include the version/glob in the depsolving - module_names = map(lambda m: m["name"], recipe["modules"] or []) - package_names = map(lambda p: p["name"], recipe["packages"] or []) + module_names = recipe.module_names + package_names = recipe.package_names projects = sorted(set(module_names+package_names), key=lambda n: n.lower()) deps = [] try: @@ -929,20 +929,7 @@ def v0_api(api): errors.append({"recipe":recipe_name, "msg":str(e)}) log.error("(v0_recipes_freeze) %s", str(e)) - # Change the recipe's modules and packages to use the depsolved version - new_modules = [] - new_packages = [] - for dep in deps: - if dep["name"] in package_names: - new_packages.append(RecipePackage(dep["name"], dep_evra(dep))) - elif dep["name"] in module_names: - new_modules.append(RecipeModule(dep["name"], dep_evra(dep))) - - recipes.append({"recipe":Recipe(recipe["name"], - recipe["description"], - recipe["version"], - new_modules, - new_packages)}) + recipes.append({"recipe": recipe.freeze(deps)}) return jsonify(recipes=recipes, errors=errors)