[recipes] allow update_recipes() to update an existing one of the same name draft
authorDavid Douard <david.douard@logilab.fr>
Sun, 09 Nov 2014 20:07:17 +0100
changeset 1772 139e0c756385
parent 1771 3673586bc3d6
child 1773 35a2edaa84e5
[recipes] allow update_recipes() to update an existing one of the same name Useful for the migration script.
recipes/__init__.py
--- a/recipes/__init__.py	Sun Nov 09 20:07:23 2014 +0100
+++ b/recipes/__init__.py	Sun Nov 09 20:07:17 2014 +0100
@@ -2,18 +2,26 @@
 from os import listdir
 from os.path import join, dirname
 
-def create_recipes(cnx):
+def create_recipes(cnx, update=False):
     recipes = []
     rdir = dirname(__file__)
     for recipe in listdir(rdir):
         if recipe.endswith('.py') and not recipe.startswith('_'):
-            rname = recipe[:-3]
             try:
-                script = open(join(rdir, recipe)).read()
-                r = cnx.create_entity('Recipe',
-                                      name=rname.decode('utf-8'),
-                                      script=script.decode('utf-8'))
+                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