__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. """Distributed Task Queue"""
  3. # :copyright: (c) 2009 - 2012 Ask Solem and individual contributors,
  4. # All rights reserved.
  5. # :copyright: (c) 2012-2013 GoPivotal, Inc., All rights reserved.
  6. # :license: BSD (3 Clause), see LICENSE for more details.
  7. from __future__ import absolute_import
  8. SERIES = 'Cipater'
  9. VERSION = (3, 1, 0, 'rc4')
  10. __version__ = '.'.join(str(p) for p in VERSION[0:3]) + ''.join(VERSION[3:])
  11. __author__ = 'Ask Solem'
  12. __contact__ = 'ask@celeryproject.org'
  13. __homepage__ = 'http://celeryproject.org'
  14. __docformat__ = 'restructuredtext'
  15. __all__ = [
  16. 'Celery', 'bugreport', 'shared_task', 'task',
  17. 'current_app', 'current_task',
  18. 'chain', 'chord', 'chunks', 'group', 'subtask',
  19. 'xmap', 'xstarmap', 'uuid', 'version', '__version__',
  20. ]
  21. VERSION_BANNER = '{0} ({1})'.format(__version__, SERIES)
  22. # -eof meta-
  23. import os
  24. import sys
  25. if os.environ.get('C_IMPDEBUG'): # pragma: no cover
  26. from .five import builtins
  27. real_import = builtins.__import__
  28. def debug_import(name, locals=None, globals=None,
  29. fromlist=None, level=-1):
  30. glob = globals or getattr(sys, 'emarfteg_'[::-1])(1).f_globals
  31. importer_name = glob and glob.get('__name__') or 'unknown'
  32. print('-- {0} imports {1}'.format(importer_name, name))
  33. return real_import(name, locals, globals, fromlist, level)
  34. builtins.__import__ = debug_import
  35. STATICA_HACK = True
  36. globals()['kcah_acitats'[::-1].upper()] = False
  37. if STATICA_HACK: # pragma: no cover
  38. # This is never executed, but tricks static analyzers (PyDev, PyCharm,
  39. # pylint, etc.) into knowing the types of these symbols, and what
  40. # they contain.
  41. from celery.app import shared_task # noqa
  42. from celery.app.base import Celery # noqa
  43. from celery.app.utils import bugreport # noqa
  44. from celery.app.task import Task # noqa
  45. from celery._state import current_app, current_task # noqa
  46. from celery.canvas import ( # noqa
  47. chain, chord, chunks, group, subtask, xmap, xstarmap,
  48. )
  49. from celery.utils import uuid # noqa
  50. # Lazy loading
  51. from .five import recreate_module
  52. old_module, new_module = recreate_module( # pragma: no cover
  53. __name__,
  54. by_module={
  55. 'celery.app': ['Celery', 'bugreport', 'shared_task'],
  56. 'celery.app.task': ['Task'],
  57. 'celery._state': ['current_app', 'current_task'],
  58. 'celery.canvas': ['chain', 'chord', 'chunks', 'group',
  59. 'subtask', 'xmap', 'xstarmap'],
  60. 'celery.utils': ['uuid'],
  61. },
  62. direct={'task': 'celery.task'},
  63. __package__='celery', __file__=__file__,
  64. __path__=__path__, __doc__=__doc__, __version__=__version__,
  65. __author__=__author__, __contact__=__contact__,
  66. __homepage__=__homepage__, __docformat__=__docformat__,
  67. VERSION=VERSION, SERIES=SERIES, VERSION_BANNER=VERSION_BANNER,
  68. )
  69. if sys.version_info[0:2] == (3, 2):
  70. # There is a problem in Python3's import system where it
  71. # returns the raw module object instead of the one
  72. # kept in ``sys.modules``.
  73. # This breaks our dynamically generated modules because we insert
  74. # them into sys.modules, and expect the import statement to return
  75. # that.
  76. # I'm not entirely sure of why, or when it happens, but this import hook
  77. # fixes the problem. The bug can be reproduced by disabling the hook
  78. # and doing the following:
  79. #
  80. # >>> import celery
  81. # >>> from celery.task import sets
  82. # >>> from celery import task
  83. # >>> type(celery.task)
  84. # <class 'celery.task'>
  85. # >>> import sys
  86. # >>> import celery
  87. # >>> sys.modules.pop('celery.task')
  88. # <module 'celery.task' from 'celery/task/__init__.py'>
  89. # >>> from celery.task import sets
  90. # Traceback (most recent call last):
  91. # File "<stdin>", line 1, in <module>
  92. # ImportError: cannot import name sets
  93. # >>> type(celery.task)
  94. # <class 'module'> # <-- where did this come from?!?
  95. # Note that popping the module from sys.modules is just a way to force
  96. # this to happen and I'm sure it happens in other cases too.
  97. # [ask]
  98. import imp
  99. class FixBrokenImportHook(object):
  100. generated_modules = ('celery', 'celery.task')
  101. def load_module(self, name, *args):
  102. try:
  103. return sys.modules[name]
  104. except KeyError:
  105. modname, path = name, None
  106. if '.' in name:
  107. modname, path = name.split('.')[-1], __path__
  108. module_info = imp.find_module(modname, path)
  109. imp.load_module(name, *module_info)
  110. return sys.modules[name]
  111. def find_module(self, name, path):
  112. if name in self.generated_modules:
  113. return self
  114. return None
  115. sys.meta_path.insert(0, FixBrokenImportHook())