allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [10/15] git commit: [#6456] Changed import_tool call to task in GC project importer
Date Mon, 22 Jul 2013 22:54:17 GMT
[#6456] Changed import_tool call to task in GC project importer

Signed-off-by: Cory Johns <cjohns@slashdotmedia.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/f091124b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f091124b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f091124b

Branch: refs/heads/cj/6456
Commit: f091124bcb4eea22389cafb96bd3b8753f1559dd
Parents: d67afd6
Author: Cory Johns <cjohns@slashdotmedia.com>
Authored: Mon Jul 22 20:33:44 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Mon Jul 22 20:33:44 2013 +0000

----------------------------------------------------------------------
 ForgeImporters/forgeimporters/base.py           | 22 ++++---
 ForgeImporters/forgeimporters/google/project.py |  4 +-
 ForgeImporters/forgeimporters/google/tasks.py   |  6 ++
 .../forgeimporters/tests/google/test_tasks.py   |  9 +++
 .../forgeimporters/tests/test_base.py           | 66 ++++++++++++--------
 5 files changed, 69 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f091124b/ForgeImporters/forgeimporters/base.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/base.py b/ForgeImporters/forgeimporters/base.py
index c3b6946..01e614a 100644
--- a/ForgeImporters/forgeimporters/base.py
+++ b/ForgeImporters/forgeimporters/base.py
@@ -68,10 +68,15 @@ class ProjectImporter(BaseController):
 
 
 class ToolImporter(object):
-    target_app = None
-    source = None
+    target_app = None  # app or list of apps
+    source = None  # string description of source, must match project importer
     controller = None
 
+    @classmethod
+    def by_name(self, name):
+        for ep in iter_entry_points('allura.importers', name):
+            return ep.load()()
+
     def import_tool(self, project=None, mount_point=None):
         """
         Override this method to perform the tool import.
@@ -96,15 +101,12 @@ class ToolsValidator(fev.Set):
         value = super(ToolsValidator, self).to_python(value, state)
         valid = []
         invalid = []
-        for i, v in enumerate(value):
-            try:
-                ep = iter_entry_points('allura.importers', v).next().load()
-            except StopIteration:
-                ep = None
-            if getattr(ep, 'source', None) != self.source:
-                invalid.append(v)
+        for name in value:
+            importer = ToolImporter.by_name(name)
+            if importer is not None and importer.source == self.source:
+                valid.append(name)
             else:
-                valid.append(ep())
+                invalid.append(name)
         if invalid:
             pl = 's' if len(invalid) > 1 else ''
             raise fev.Invalid('Invalid tool%s selected: %s' % (pl, ', '.join(invalid)), value,
state)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f091124b/ForgeImporters/forgeimporters/google/project.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/project.py b/ForgeImporters/forgeimporters/google/project.py
index ec8de53..caf1346 100644
--- a/ForgeImporters/forgeimporters/google/project.py
+++ b/ForgeImporters/forgeimporters/google/project.py
@@ -71,8 +71,8 @@ class GoogleCodeProjectImporter(base.ProjectImporter):
 
         c.project.set_tool_data('google-code', project_name=project_name)
         tasks.import_project_info.post()
-        for tool in tools:
-            tool.import_tool(c.project)
+        for importer_name in tools:
+            tasks.import_tool.post(importer_name)
 
         flash('Welcome to the SourceForge Project System! '
               'Your project data will be imported and should show up here shortly.')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f091124b/ForgeImporters/forgeimporters/google/tasks.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/tasks.py b/ForgeImporters/forgeimporters/google/tasks.py
index 79851c3..834dc9d 100644
--- a/ForgeImporters/forgeimporters/google/tasks.py
+++ b/ForgeImporters/forgeimporters/google/tasks.py
@@ -22,6 +22,7 @@ from ming.orm import ThreadLocalORMSession
 from allura.lib.decorators import task
 
 from . import GoogleCodeProjectExtractor
+from ..base import ToolImporter
 
 
 @task
@@ -31,3 +32,8 @@ def import_project_info():
     extractor.get_icon()
     extractor.get_license()
     ThreadLocalORMSession.flush_all()
+
+@task
+def import_tool(importer_name, mount_point=None, mount_label=None):
+    importer = ToolImporter.by_name(importer_name)
+    importer.import_tool(c.project, mount_point, mount_label)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f091124b/ForgeImporters/forgeimporters/tests/google/test_tasks.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/tests/google/test_tasks.py b/ForgeImporters/forgeimporters/tests/google/test_tasks.py
index a21d083..bb9319d 100644
--- a/ForgeImporters/forgeimporters/tests/google/test_tasks.py
+++ b/ForgeImporters/forgeimporters/tests/google/test_tasks.py
@@ -31,3 +31,12 @@ def test_import_project_info(c, session, gpe):
     gpe.return_value.get_icon.assert_called_once_with()
     gpe.return_value.get_license.assert_called_once_with()
     session.flush_all.assert_called_once_with()
+
+
+@mock.patch.object(tasks.ToolImporter, 'by_name')
+@mock.patch.object(tasks, 'c')
+def test_import_tool(c, by_name):
+    c.project = mock.Mock(name='project')
+    tasks.import_tool('importer_name', 'mount_point', 'mount_label')
+    by_name.assert_called_once_with('importer_name')
+    by_name.return_value.import_tool.assert_called_once_with(c.project, 'mount_point', 'mount_label')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f091124b/ForgeImporters/forgeimporters/tests/test_base.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/tests/test_base.py b/ForgeImporters/forgeimporters/tests/test_base.py
index 394723d..a6a4d18 100644
--- a/ForgeImporters/forgeimporters/tests/test_base.py
+++ b/ForgeImporters/forgeimporters/tests/test_base.py
@@ -28,6 +28,7 @@ def ep(name, source=None):
     mep.name = name
     mep.load.return_value.source = source
     mep.lv = mep.load.return_value.return_value
+    mep.lv.source = source
     return mep
 
 
@@ -50,7 +51,7 @@ class TestProjectImporter(TestCase):
         iep.assert_called_once_with('allura.importers')
 
 
-class TestImporter(TestCase):
+class TestToolImporter(TestCase):
     class TI1(base.ToolImporter):
         target_app = mock.Mock(tool_label='foo', tool_description='foo_desc')
 
@@ -59,6 +60,19 @@ class TestImporter(TestCase):
         tool_label = 'bar'
         tool_description = 'bar_desc'
 
+    @mock.patch('forgeimporters.base.iter_entry_points')
+    def test_by_name(self, iep):
+        eps = iep.return_value = [ep('my-name', 'my-source')]
+        importer = base.ToolImporter.by_name('my-name')
+        iep.assert_called_once_with('allura.importers', 'my-name')
+        self.assertEqual(importer, eps[0].lv)
+
+        iep.reset_mock()
+        iep.return_value = []
+        importer = base.ToolImporter.by_name('other-name')
+        iep.assert_called_once_with('allura.importers', 'other-name')
+        self.assertEqual(importer, None)
+
     def test_tool_label(self):
         self.assertEqual(self.TI1().tool_label, 'foo')
         self.assertEqual(self.TI2().tool_label, 'bar')
@@ -72,44 +86,44 @@ class TestToolsValidator(TestCase):
     def setUp(self):
         self.tv = base.ToolsValidator('good-source')
 
-    @mock.patch('forgeimporters.base.iter_entry_points')
-    def test_empty(self, iep):
+    @mock.patch.object(base.ToolImporter, 'by_name')
+    def test_empty(self, by_name):
         self.assertEqual(self.tv.to_python(''), [])
-        self.assertEqual(iep.call_count, 0)
+        self.assertEqual(by_name.call_count, 0)
 
-    @mock.patch('forgeimporters.base.iter_entry_points')
-    def test_no_ep(self, iep):
-        eps = iep.return_value.next.side_effect = StopIteration
+    @mock.patch.object(base.ToolImporter, 'by_name')
+    def test_no_ep(self, by_name):
+        eps = by_name.return_value = None
         with self.assertRaises(Invalid) as cm:
             self.tv.to_python('my-value')
         self.assertEqual(cm.exception.msg, 'Invalid tool selected: my-value')
-        iep.assert_called_once_with('allura.importers', 'my-value')
+        by_name.assert_called_once_with('my-value')
 
-    @mock.patch('forgeimporters.base.iter_entry_points')
-    def test_bad_source(self, iep):
-        eps = iep.return_value.next.side_effect = [ep('ep1', 'bad-source'), ep('ep2', 'good-source')]
+    @mock.patch.object(base.ToolImporter, 'by_name')
+    def test_bad_source(self, by_name):
+        eps = by_name.return_value = ep('ep1', 'bad-source').lv
         with self.assertRaises(Invalid) as cm:
             self.tv.to_python('my-value')
         self.assertEqual(cm.exception.msg, 'Invalid tool selected: my-value')
-        iep.assert_called_once_with('allura.importers', 'my-value')
+        by_name.assert_called_once_with('my-value')
 
-    @mock.patch('forgeimporters.base.iter_entry_points')
-    def test_multiple(self, iep):
-        eps = iep.return_value.next.side_effect = [ep('ep1', 'bad-source'), ep('ep2', 'good-source'),
ep('ep3', 'bad-source')]
+    @mock.patch.object(base.ToolImporter, 'by_name')
+    def test_multiple(self, by_name):
+        eps = by_name.side_effect = [ep('ep1', 'bad-source').lv, ep('ep2', 'good-source').lv,
ep('ep3', 'bad-source').lv]
         with self.assertRaises(Invalid) as cm:
             self.tv.to_python(['value1', 'value2', 'value3'])
         self.assertEqual(cm.exception.msg, 'Invalid tools selected: value1, value3')
-        self.assertEqual(iep.call_args_list, [
-                mock.call('allura.importers', 'value1'),
-                mock.call('allura.importers', 'value2'),
-                mock.call('allura.importers', 'value3'),
+        self.assertEqual(by_name.call_args_list, [
+                mock.call('value1'),
+                mock.call('value2'),
+                mock.call('value3'),
             ])
 
-    @mock.patch('forgeimporters.base.iter_entry_points')
-    def test_valid(self, iep):
-        eps = iep.return_value.next.side_effect = [ep('ep1', 'good-source'), ep('ep2', 'good-source'),
ep('ep3', 'bad-source')]
-        self.assertEqual(self.tv.to_python(['value1', 'value2']), [eps[0].lv, eps[1].lv])
-        self.assertEqual(iep.call_args_list, [
-                mock.call('allura.importers', 'value1'),
-                mock.call('allura.importers', 'value2'),
+    @mock.patch.object(base.ToolImporter, 'by_name')
+    def test_valid(self, by_name):
+        eps = by_name.side_effect = [ep('ep1', 'good-source').lv, ep('ep2', 'good-source').lv,
ep('ep3', 'bad-source').lv]
+        self.assertEqual(self.tv.to_python(['value1', 'value2']), ['value1', 'value2'])
+        self.assertEqual(by_name.call_args_list, [
+                mock.call('value1'),
+                mock.call('value2'),
             ])


Mime
View raw message