utils: change fork_call() to return a callable
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 4 Apr 2014 06:00:02 +0000 (09:00 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 8 Apr 2014 09:17:46 +0000 (12:17 +0300)
Hopefully making its usage easier to read.

Change-Id: I7facc91ace28b4e83783453d8f72ca14316b827a
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
obs_service_gbp/command.py
obs_service_gbp_utils/__init__.py
tests/test_obs_service_gbp_utils.py

index 41c877820a2ed52f94ac20d59e6b575a638d3b28..19fab5f20c94ee8fd4e0dd22a7b47223bae619cb 100644 (file)
@@ -126,7 +126,7 @@ def gbp_export(repo, args, config):
         if args.rpm == 'yes' or (args.rpm == 'auto' and specs_found):
             LOGGER.info('Exporting RPM packaging files with GBP')
             LOGGER.debug('git-buildpackage-rpm args: %s', ' '.join(rpm_args))
-            ret = fork_call(uid, gid, gbp_rpmrpm_args)
+            ret = fork_call(uid, gid, gbp_rpm)(rpm_args)
             if ret:
                 LOGGER.error('Git-buildpackage-rpm failed, unable to export '
                              'RPM packaging files')
@@ -134,7 +134,7 @@ def gbp_export(repo, args, config):
         if args.deb == 'yes' or (args.deb== 'auto' and os.path.isdir('debian')):
             LOGGER.info('Exporting Debian source package with GBP')
             LOGGER.debug('git-buildpackage args: %s', ' '.join(deb_args))
-            ret = fork_call(uid, gid, gbp_debdeb_args)
+            ret = fork_call(uid, gid, gbp_deb)(deb_args)
             if ret:
                 LOGGER.error('Git-buildpackage failed, unable to export Debian '
                              'sources package files')
index b4b50eee5859e1f5984f9a669e8ff3c76cee8aaa..f41534ad23adb0ce6d40ea03888c2f1975ad4fa0 100644 (file)
@@ -96,8 +96,8 @@ def sanitize_uid_gid(user, group):
         raise GbpServiceError('Unable to find UID/GID: %s' % err)
     return (uid, gid)
 
-def fork_call(user, group, func, *args, **kwargs):
-    """Fork and call a function. The function should return an integer"""
+def _fork_call(user, group, func, *args, **kwargs):
+    """Wrapper for actual logic of fork_call()"""
     # Get numerical uid and gid
     uid, gid = sanitize_uid_gid(user, group)
 
@@ -114,6 +114,10 @@ def fork_call(user, group, func, *args, **kwargs):
     else:
         raise ret_data
 
+def fork_call(user, group, func):
+    """Fork and call a function. The function should return an integer.
+       Returns a callable that runs the function."""
+    return partial(_fork_call, user, group, func)
 
 def _commit_info_in_json(repo, committish):
     """Get info about a committish in json-serializable format"""
index 7f4e2df70106ac2723ffa44128bd7209f4480a90..972c0b22b7f5fa88cd7636cf859b56ac2f65a4b2 100644 (file)
@@ -48,19 +48,22 @@ class TestForkCall(object):
         self._group = grp.getgrgid(self._gid).gr_name
 
     @staticmethod
-    def _no_fork_call(uid, gid, func, *args, **kwargs):
+    def _no_fork_call(uid, gid, func):
         """For testing demoted call without actually forking"""
-        data_q = Queue()
-        try:
-            _demoted_child_call(uid, gid, data_q,
-                                partial(func, *args, **kwargs))
-        except SystemExit as err:
-            ret_code = err.code
-        ret_data = data_q.get()
-        if ret_code == _RET_FORK_OK:
-            return ret_data
-        else:
-            raise ret_data
+        def _no_fork_call_runner(uid, gid, func, *args, **kwargs):
+            """Actual workhorse"""
+            data_q = Queue()
+            try:
+                _demoted_child_call(uid, gid, data_q,
+                                    partial(func, *args, **kwargs))
+            except SystemExit as err:
+                ret_code = err.code
+            ret_data = data_q.get()
+            if ret_code == _RET_FORK_OK:
+                return ret_data
+            else:
+                raise ret_data
+        return partial(_no_fork_call_runner, uid, gid, func)
 
     @staticmethod
     def _dummy_ok():
@@ -79,44 +82,44 @@ class TestForkCall(object):
 
     def test_success(self):
         """Basic test for successful call"""
-        eq_(fork_call(None, None, self._dummy_ok), 'ok')
+        eq_(fork_call(None, None, self._dummy_ok)(), 'ok')
 
-        eq_(fork_call(None, None, self._dummy_args1, '2', arg3='foo'),
+        eq_(fork_call(None, None, self._dummy_args)(1, '2', arg3='foo'),
             (1, '2', 'foo'))
 
-        ok_(os.getpid() != fork_call(None, None, os.getpid))
+        ok_(os.getpid() != fork_call(None, None, os.getpid)())
 
     def test_fail(self):
         """Tests for function call failures"""
         with assert_raises(GbpChildBTError) as exc:
-            fork_call(None, None, self._dummy_raise)
+            fork_call(None, None, self._dummy_raise)()
         eq_(exc.exception.typ, _DummyException)
 
         with assert_raises(GbpChildBTError) as exc:
-            fork_call(None, None, self._dummy_ok'unexptected_arg')
+            fork_call(None, None, self._dummy_ok)('unexptected_arg')
         eq_(exc.exception.typ, TypeError)
 
     def test_demoted_call_no(self):
         """Test running with different UID/GID"""
-        eq_(fork_call(self._name, self._group, self._dummy_ok), 'ok')
-        eq_(fork_call(self._uid, None, self._dummy_ok), 'ok')
-        eq_(fork_call(None, self._gid, self._dummy_ok), 'ok')
+        eq_(fork_call(self._name, self._group, self._dummy_ok)(), 'ok')
+        eq_(fork_call(self._uid, None, self._dummy_ok)(), 'ok')
+        eq_(fork_call(None, self._gid, self._dummy_ok)(), 'ok')
 
-        eq_(self._no_fork_call(self._uid, self._gid, self._dummy_ok), 'ok')
+        eq_(self._no_fork_call(self._uid, self._gid, self._dummy_ok)(), 'ok')
 
     def test_demoted_call_fail(self):
         """Test running with invalid UID/GID"""
         with assert_raises(GbpServiceError):
-            fork_call('_non_existent_user', None, self._dummy_ok)
+            fork_call('_non_existent_user', None, self._dummy_ok)()
         with assert_raises(GbpServiceError):
-            fork_call(None, '_non_existen_group', self._dummy_ok)
+            fork_call(None, '_non_existen_group', self._dummy_ok)()
 
         with assert_raises(GbpServiceError):
-            self._no_fork_call(99999, None, self._dummy_ok)
+            self._no_fork_call(99999, None, self._dummy_ok)()
         with assert_raises(GbpServiceError):
-            self._no_fork_call(None, 99999, self._dummy_ok)
+            self._no_fork_call(None, 99999, self._dummy_ok)()
         with assert_raises(GbpChildBTError) as exc:
-            self._no_fork_call(self._uid, self._gid, self._dummy_raise)
+            self._no_fork_call(self._uid, self._gid, self._dummy_raise)()
         eq_(exc.exception.typ, _DummyException)
 
 class TestGitMeta(UnitTestsBase):