瀏覽代碼

Moves gen_task_name to .utils.imports

Ask Solem 8 年之前
父節點
當前提交
7b93ce91b7

+ 1 - 2
celery/app/base.py

@@ -37,12 +37,11 @@ from celery.five import (
 from celery.loaders import get_loader_cls
 from celery.local import PromiseProxy, maybe_evaluate
 from celery.utils import abstract
-from celery.utils import gen_task_name
 from celery.utils.collections import AttributeDictMixin
 from celery.utils.dispatch import Signal
 from celery.utils.functional import first, maybe_list, head_from_fun
 from celery.utils.timeutils import timezone
-from celery.utils.imports import instantiate, symbol_by_name
+from celery.utils.imports import gen_task_name, instantiate, symbol_by_name
 from celery.utils.log import get_logger
 from celery.utils.objects import FallbackContext, mro_lookup
 

+ 3 - 3
celery/tests/app/test_app.py

@@ -407,8 +407,8 @@ class test_App(AppCase):
             check.assert_called_with(foo)
 
     def test_task_sets_main_name_MP_MAIN_FILE(self):
-        from celery import utils as _utils
-        _utils.MP_MAIN_FILE = __file__
+        from celery.utils import imports as _imports
+        _imports.MP_MAIN_FILE = __file__
         try:
             with self.Celery('xuzzy') as app:
 
@@ -418,7 +418,7 @@ class test_App(AppCase):
 
                 self.assertEqual(foo.name, 'xuzzy.foo')
         finally:
-            _utils.MP_MAIN_FILE = None
+            _imports.MP_MAIN_FILE = None
 
     def test_annotate_decorator(self):
         from celery.app.task import Task

+ 10 - 1
celery/tests/utils/test_imports.py

@@ -3,11 +3,12 @@ from __future__ import absolute_import, unicode_literals
 from celery.five import bytes_if_py2
 
 from celery.utils.imports import (
+    NotAPackage,
     qualname,
+    gen_task_name,
     reload_from_cwd,
     module_file,
     find_module,
-    NotAPackage,
 )
 
 from celery.tests.case import Case, Mock, patch
@@ -47,3 +48,11 @@ class test_import_utils(Case):
         m2 = Mock()
         m2.__file__ = '/opt/foo/xyz.py'
         self.assertEqual(module_file(m1), '/opt/foo/xyz.py')
+
+
+class test_gen_task_name(Case):
+
+    def test_no_module(self):
+        app = Mock()
+        app.name == '__main__'
+        self.assertTrue(gen_task_name(app, 'foo', 'axsadaewe'))

+ 0 - 9
celery/tests/utils/test_utils.py

@@ -11,7 +11,6 @@ from celery.utils import (
     isatty,
     is_iterable,
     cached_property,
-    gen_task_name,
     jsonify,
 )
 from celery.tests.case import Case, Mock
@@ -26,14 +25,6 @@ class test_isatty(Case):
         self.assertFalse(isatty(fh))
 
 
-class test_gen_task_name(Case):
-
-    def test_no_module(self):
-        app = Mock()
-        app.name == '__main__'
-        self.assertTrue(gen_task_name(app, 'foo', 'axsadaewe'))
-
-
 class test_jsonify(Case):
 
     def test_simple(self):

+ 1 - 23
celery/utils/__init__.py

@@ -162,34 +162,12 @@ def jsonify(obj,
         return unknown_type_filter(obj)
 
 
-def gen_task_name(app, name, module_name):
-    """Generate task name from name/module pair."""
-    module_name = module_name or '__main__'
-    try:
-        module = sys.modules[module_name]
-    except KeyError:
-        # Fix for manage.py shell_plus (Issue #366)
-        module = None
-
-    if module is not None:
-        module_name = module.__name__
-        # - If the task module is used as the __main__ script
-        # - we need to rewrite the module part of the task name
-        # - to match App.main.
-        if MP_MAIN_FILE and module.__file__ == MP_MAIN_FILE:
-            # - see comment about :envvar:`MP_MAIN_FILE` above.
-            module_name = '__main__'
-    if module_name == '__main__' and app.main:
-        return '.'.join([app.main, name])
-    return '.'.join(p for p in (module_name, name) if p)
-
-
 # ------------------------------------------------------------------------ #
 # > XXX Compat
 from .log import LOG_LEVELS     # noqa
 from .imports import (          # noqa
     qualname as get_full_cls_name, symbol_by_name as get_cls_by_name,
-    instantiate, import_from_cwd
+    instantiate, import_from_cwd, gen_task_name,
 )
 from .functional import chunks, noop                    # noqa
 from kombu.utils import cached_property, uuid   # noqa

+ 31 - 2
celery/utils/imports.py

@@ -19,9 +19,16 @@ from kombu.utils import symbol_by_name
 
 from celery.five import reload
 
+#: Billiard sets this when execv is enabled.
+#: We use it to find out the name of the original ``__main__``
+#: module, so that we can properly rewrite the name of the
+#: task to be that of ``App.main``.
+MP_MAIN_FILE = os.environ.get('MP_MAIN_FILE')
+
 __all__ = [
-    'NotAPackage', 'qualname', 'instantiate', 'symbol_by_name', 'cwd_in_path',
-    'find_module', 'import_from_cwd', 'reload_from_cwd', 'module_file',
+    'NotAPackage', 'qualname', 'instantiate', 'symbol_by_name',
+    'cwd_in_path', 'find_module', 'import_from_cwd',
+    'reload_from_cwd', 'module_file', 'gen_task_name',
 ]
 
 
@@ -112,3 +119,25 @@ def module_file(module):
     """Return the correct original file name of a module."""
     name = module.__file__
     return name[:-1] if name.endswith('.pyc') else name
+
+
+def gen_task_name(app, name, module_name):
+    """Generate task name from name/module pair."""
+    module_name = module_name or '__main__'
+    try:
+        module = sys.modules[module_name]
+    except KeyError:
+        # Fix for manage.py shell_plus (Issue #366)
+        module = None
+
+    if module is not None:
+        module_name = module.__name__
+        # - If the task module is used as the __main__ script
+        # - we need to rewrite the module part of the task name
+        # - to match App.main.
+        if MP_MAIN_FILE and module.__file__ == MP_MAIN_FILE:
+            # - see comment about :envvar:`MP_MAIN_FILE` above.
+            module_name = '__main__'
+    if module_name == '__main__' and app.main:
+        return '.'.join([app.main, name])
+    return '.'.join(p for p in (module_name, name) if p)