__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. import sys
  5. import warnings
  6. from importlib import import_module
  7. PYPY3 = getattr(sys, 'pypy_version_info', None) and sys.version_info[0] > 3
  8. try:
  9. WindowsError = WindowsError # noqa
  10. except NameError:
  11. class WindowsError(Exception):
  12. pass
  13. def setup():
  14. using_coverage = (
  15. os.environ.get('COVER_ALL_MODULES') or '--with-coverage' in sys.argv
  16. )
  17. os.environ.update(
  18. # warn if config module not found
  19. C_WNOCONF='yes',
  20. KOMBU_DISABLE_LIMIT_PROTECTION='yes',
  21. )
  22. if using_coverage and not PYPY3:
  23. from warnings import catch_warnings
  24. with catch_warnings(record=True):
  25. import_all_modules()
  26. warnings.resetwarnings()
  27. from celery.tests.case import Trap
  28. from celery._state import set_default_app
  29. set_default_app(Trap())
  30. def teardown():
  31. # Don't want SUBDEBUG log messages at finalization.
  32. try:
  33. from multiprocessing.util import get_logger
  34. except ImportError:
  35. pass
  36. else:
  37. get_logger().setLevel(logging.WARNING)
  38. # Make sure test database is removed.
  39. import os
  40. if os.path.exists('test.db'):
  41. try:
  42. os.remove('test.db')
  43. except WindowsError:
  44. pass
  45. # Make sure there are no remaining threads at shutdown.
  46. import threading
  47. remaining_threads = [thread for thread in threading.enumerate()
  48. if thread.getName() != 'MainThread']
  49. if remaining_threads:
  50. sys.stderr.write(
  51. '\n\n**WARNING**: Remaining threads at teardown: %r...\n' % (
  52. remaining_threads))
  53. def find_distribution_modules(name=__name__, file=__file__):
  54. current_dist_depth = len(name.split('.')) - 1
  55. current_dist = os.path.join(os.path.dirname(file),
  56. *([os.pardir] * current_dist_depth))
  57. abs = os.path.abspath(current_dist)
  58. dist_name = os.path.basename(abs)
  59. for dirpath, dirnames, filenames in os.walk(abs):
  60. package = (dist_name + dirpath[len(abs):]).replace('/', '.')
  61. if '__init__.py' in filenames:
  62. yield package
  63. for filename in filenames:
  64. if filename.endswith('.py') and filename != '__init__.py':
  65. yield '.'.join([package, filename])[:-3]
  66. def import_all_modules(name=__name__, file=__file__,
  67. skip=('celery.decorators',
  68. 'celery.contrib.batches',
  69. 'celery.task')):
  70. for module in find_distribution_modules(name, file):
  71. if not module.startswith(skip):
  72. try:
  73. import_module(module)
  74. except ImportError:
  75. pass
  76. except OSError as exc:
  77. warnings.warn(UserWarning(
  78. 'Ignored error importing module {0}: {1!r}'.format(
  79. module, exc,
  80. )))