__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import absolute_import
  2. from __future__ import with_statement
  3. import logging
  4. import os
  5. import sys
  6. import warnings
  7. from importlib import import_module
  8. config_module = os.environ.setdefault('CELERY_TEST_CONFIG_MODULE',
  9. 'celery.tests.config')
  10. os.environ.setdefault('CELERY_CONFIG_MODULE', config_module)
  11. os.environ['CELERY_LOADER'] = 'default'
  12. os.environ['EVENTLET_NOPATCH'] = 'yes'
  13. os.environ['GEVENT_NOPATCH'] = 'yes'
  14. os.environ['KOMBU_DISABLE_LIMIT_PROTECTION'] = 'yes'
  15. os.environ['CELERY_BROKER_URL'] = 'memory://'
  16. try:
  17. WindowsError = WindowsError # noqa
  18. except NameError:
  19. class WindowsError(Exception):
  20. pass
  21. def teardown():
  22. # Don't want SUBDEBUG log messages at finalization.
  23. try:
  24. from multiprocessing.util import get_logger
  25. except ImportError:
  26. pass
  27. else:
  28. get_logger().setLevel(logging.WARNING)
  29. # Make sure test database is removed.
  30. import os
  31. if os.path.exists('test.db'):
  32. try:
  33. os.remove('test.db')
  34. except WindowsError:
  35. pass
  36. # Make sure there are no remaining threads at shutdown.
  37. import threading
  38. remaining_threads = [thread for thread in threading.enumerate()
  39. if thread.getName() != 'MainThread']
  40. if remaining_threads:
  41. sys.stderr.write(
  42. '\n\n**WARNING**: Remaining threads at teardown: %r...\n' % (
  43. remaining_threads))
  44. def find_distribution_modules(name=__name__, file=__file__):
  45. current_dist_depth = len(name.split('.')) - 1
  46. current_dist = os.path.join(os.path.dirname(file),
  47. *([os.pardir] * current_dist_depth))
  48. abs = os.path.abspath(current_dist)
  49. dist_name = os.path.basename(abs)
  50. for dirpath, dirnames, filenames in os.walk(abs):
  51. package = (dist_name + dirpath[len(abs):]).replace('/', '.')
  52. if '__init__.py' in filenames:
  53. yield package
  54. for filename in filenames:
  55. if filename.endswith('.py') and filename != '__init__.py':
  56. yield '.'.join([package, filename])[:-3]
  57. def import_all_modules(name=__name__, file=__file__,
  58. skip=['celery.decorators', 'celery.contrib.batches']):
  59. for module in find_distribution_modules(name, file):
  60. if module not in skip:
  61. try:
  62. import_module(module)
  63. except ImportError:
  64. pass
  65. if os.environ.get('COVER_ALL_MODULES') or '--with-coverage3' in sys.argv:
  66. from celery.tests.utils import catch_warnings
  67. with catch_warnings(record=True):
  68. import_all_modules()
  69. warnings.resetwarnings()