recipes/__init__.py
author David Douard <david.douard@logilab.fr>
Sun, 09 Nov 2014 20:07:17 +0100
changeset 1772 139e0c756385
parent 1740 f3137466457a
child 1622 84913fb4d2e7
permissions -rw-r--r--
[recipes] allow update_recipes() to update an existing one of the same name Useful for the migration script.

#
from os import listdir
from os.path import join, dirname

def create_recipes(cnx, update=False):
    recipes = []
    rdir = dirname(__file__)
    for recipe in listdir(rdir):
        if recipe.endswith('.py') and not recipe.startswith('_'):
            try:
                rname = recipe[:-3].decode('utf-8')
                script = open(join(rdir, recipe)).read().decode('utf-8')
                rset = cnx.execute('Recipe R WHERE R name %(name)s',
                                   {'name': rname})
                print "rset=", rset, "for", rname
                if rset and update:
                    r = rset.one()
                    r.cw_set(script=script)
                else:
                    r = cnx.create_entity('Recipe',
                                          name=rname,
                                          script=script)
                recipes.append(r)
            except Exception as exc:
                print "Argh", exc
                cnx.warning('Failed to create Recipe %s (%s)'%(rname, exc))
    return recipes