[recipes] pass the directories of recipes as argument of create_recipes draft
authorDavid Douard <david.douard@logilab.fr>
Fri, 21 Nov 2014 00:50:25 +0100
changeset 1781 37db6358b486
parent 1780 d47dfaf9f065
child 1782 69394bafe4bd
[recipes] pass the directories of recipes as argument of create_recipes so it's usable as is from other cubes (eg. jpl)
migration/4.0.0_Any.py
migration/postcreate.py
recipes/__init__.py
test/test_functional.py
--- a/migration/4.0.0_Any.py	Thu Nov 20 16:14:11 2014 +0100
+++ b/migration/4.0.0_Any.py	Fri Nov 21 00:50:25 2014 +0100
@@ -18,7 +18,7 @@
 
 # add new or update basic recipes
 from cubes.apycot import recipes
-recipes.create_recipes(session, update=True)
+recipes.create_recipes(session, recipes.__file__, update=True)
 
 # add checkout recipes to repositories
 rql('SET REPO checkout_recipe CR WHERE CR name "apycot.checkout.mercurial", '
--- a/migration/postcreate.py	Thu Nov 20 16:14:11 2014 +0100
+++ b/migration/postcreate.py	Fri Nov 21 00:50:25 2014 +0100
@@ -16,5 +16,5 @@
 
 print " RECIPES ".center(60, "=")
 from cubes.apycot import recipes
-recipes.create_recipes(session)
+recipes.create_recipes(session, recipes.__file__)
 commit()
--- a/recipes/__init__.py	Thu Nov 20 16:14:11 2014 +0100
+++ b/recipes/__init__.py	Fri Nov 21 00:50:25 2014 +0100
@@ -1,18 +1,28 @@
 #
 from os import listdir
-from os.path import join, dirname
+from os.path import join, dirname, isfile
+
+def create_recipes(cnx, recipes_directory, update=False):
+    """Create Recipe entities from python files found in
+    `recipes_directory` (or the parent directory if it's a file).
 
-def create_recipes(cnx, update=False):
+    For each python file, add a Recipe which name is the python
+    filename (without its .py extension) and with the file's content
+    as Recipe.script value.
+
+    If `update` is True, existing Recipe are updated (matches
+    Recipe.name)
+    """
     recipes = []
-    rdir = dirname(__file__)
-    for recipe in listdir(rdir):
+    if isfile(recipes_directory):
+        recipes_directory = dirname(recipes_directory)
+    for recipe in listdir(recipes_directory):
         if recipe.endswith('.py') and not recipe.startswith('_'):
             try:
                 rname = recipe[:-3].decode('utf-8')
-                script = open(join(rdir, recipe)).read().decode('utf-8')
+                script = open(join(recipes_directory, 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)
@@ -22,6 +32,6 @@
                                           script=script)
                 recipes.append(r)
             except Exception as exc:
-                print "Argh", exc
+                print "ERROR:", exc
                 cnx.warning('Failed to create Recipe %s (%s)'%(rname, exc))
     return recipes
--- a/test/test_functional.py	Thu Nov 20 16:14:11 2014 +0100
+++ b/test/test_functional.py	Fri Nov 21 00:50:25 2014 +0100
@@ -7,7 +7,7 @@
 
 import cubicweb.devtools
 import cubes.apycot.testutils as utils
-from cubes.apycot.recipes import create_recipes
+from cubes.apycot import recipes
 class ApycotNoTestTC(utils.ApycotBaseTC):
     _repo_path = (u'project',)
 
@@ -31,7 +31,7 @@
             pe = cnx.find('ProjectEnvironment', name='pe_project').one()
 
             # ensure recipes content
-            create_recipes(cnx, update=True)
+            recipes.create_recipes(cnx, recipes.__file__, update=True)
             recipe = cnx.find('Recipe', name="apycot.python.full").one()
             self.assertTrue(cnx.find('Recipe', name="apycot.checkout.mercurial"))
             self.assertTrue(cnx.find('Recipe', name="apycot.setup.distutils"))