compat.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. """
  2. celery.utils.compat
  3. ===================
  4. Backward compatible implementations of features
  5. only available in later Python versions.
  6. """
  7. from __future__ import absolute_import
  8. ############## py3k #########################################################
  9. import sys
  10. try:
  11. from UserList import UserList # noqa
  12. except ImportError:
  13. from collections import UserList # noqa
  14. try:
  15. from UserDict import UserDict # noqa
  16. except ImportError:
  17. from collections import UserDict # noqa
  18. if sys.version_info >= (3, 0):
  19. from io import StringIO, BytesIO
  20. from .encoding import bytes_to_str
  21. class WhateverIO(StringIO):
  22. def write(self, data):
  23. StringIO.write(self, bytes_to_str(data))
  24. else:
  25. try:
  26. from cStringIO import StringIO # noqa
  27. except ImportError:
  28. from StringIO import StringIO # noqa
  29. BytesIO = WhateverIO = StringIO # noqa
  30. ############## collections.OrderedDict ######################################
  31. try:
  32. from collections import OrderedDict
  33. except ImportError:
  34. from ordereddict import OrderedDict # noqa
  35. ############## logging.LoggerAdapter ########################################
  36. import logging
  37. try:
  38. import multiprocessing
  39. except ImportError:
  40. multiprocessing = None # noqa
  41. import sys
  42. def _checkLevel(level):
  43. if isinstance(level, int):
  44. rv = level
  45. elif str(level) == level:
  46. if level not in logging._levelNames:
  47. raise ValueError("Unknown level: %r" % level)
  48. rv = logging._levelNames[level]
  49. else:
  50. raise TypeError("Level not an integer or a valid string: %r" % level)
  51. return rv
  52. class _CompatLoggerAdapter(object):
  53. def __init__(self, logger, extra):
  54. self.logger = logger
  55. self.extra = extra
  56. def setLevel(self, level):
  57. self.logger.level = _checkLevel(level)
  58. def process(self, msg, kwargs):
  59. kwargs["extra"] = self.extra
  60. return msg, kwargs
  61. def debug(self, msg, *args, **kwargs):
  62. self.log(logging.DEBUG, msg, *args, **kwargs)
  63. def info(self, msg, *args, **kwargs):
  64. self.log(logging.INFO, msg, *args, **kwargs)
  65. def warning(self, msg, *args, **kwargs):
  66. self.log(logging.WARNING, msg, *args, **kwargs)
  67. warn = warning
  68. def error(self, msg, *args, **kwargs):
  69. self.log(logging.ERROR, msg, *args, **kwargs)
  70. def exception(self, msg, *args, **kwargs):
  71. kwargs.setdefault("exc_info", 1)
  72. self.error(msg, *args, **kwargs)
  73. def critical(self, msg, *args, **kwargs):
  74. self.log(logging.CRITICAL, msg, *args, **kwargs)
  75. fatal = critical
  76. def log(self, level, msg, *args, **kwargs):
  77. if self.logger.isEnabledFor(level):
  78. msg, kwargs = self.process(msg, kwargs)
  79. self._log(level, msg, args, **kwargs)
  80. def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
  81. func=None, extra=None):
  82. rv = logging.LogRecord(name, level, fn, lno, msg, args, exc_info, func)
  83. if extra is not None:
  84. for key, value in extra.items():
  85. if key in ("message", "asctime") or key in rv.__dict__:
  86. raise KeyError(
  87. "Attempt to override %r in LogRecord" % key)
  88. rv.__dict__[key] = value
  89. if multiprocessing is not None:
  90. rv.processName = multiprocessing.current_process()._name
  91. else:
  92. rv.processName = ""
  93. return rv
  94. def _log(self, level, msg, args, exc_info=None, extra=None):
  95. defcaller = "(unknown file)", 0, "(unknown function)"
  96. if logging._srcfile:
  97. # IronPython doesn't track Python frames, so findCaller
  98. # throws an exception on some versions of IronPython.
  99. # We trap it here so that IronPython can use logging.
  100. try:
  101. fn, lno, func = self.logger.findCaller()
  102. except ValueError:
  103. fn, lno, func = defcaller
  104. else:
  105. fn, lno, func = defcaller
  106. if exc_info:
  107. if not isinstance(exc_info, tuple):
  108. exc_info = sys.exc_info()
  109. record = self.makeRecord(self.logger.name, level, fn, lno, msg,
  110. args, exc_info, func, extra)
  111. self.logger.handle(record)
  112. def isEnabledFor(self, level):
  113. return self.logger.isEnabledFor(level)
  114. def addHandler(self, hdlr):
  115. self.logger.addHandler(hdlr)
  116. def removeHandler(self, hdlr):
  117. self.logger.removeHandler(hdlr)
  118. @property
  119. def level(self):
  120. return self.logger.level
  121. try:
  122. from logging import LoggerAdapter
  123. except ImportError:
  124. LoggerAdapter = _CompatLoggerAdapter # noqa
  125. ############## itertools.izip_longest #######################################
  126. try:
  127. from itertools import izip_longest
  128. except ImportError:
  129. import itertools
  130. def izip_longest(*args, **kwds): # noqa
  131. fillvalue = kwds.get("fillvalue")
  132. def sentinel(counter=([fillvalue] * (len(args) - 1)).pop):
  133. yield counter() # yields the fillvalue, or raises IndexError
  134. fillers = itertools.repeat(fillvalue)
  135. iters = [itertools.chain(it, sentinel(), fillers)
  136. for it in args]
  137. try:
  138. for tup in itertools.izip(*iters):
  139. yield tup
  140. except IndexError:
  141. pass
  142. ############## itertools.chain.from_iterable ################################
  143. from itertools import chain
  144. def _compat_chain_from_iterable(iterables):
  145. for it in iterables:
  146. for element in it:
  147. yield element
  148. try:
  149. chain_from_iterable = getattr(chain, "from_iterable")
  150. except AttributeError:
  151. chain_from_iterable = _compat_chain_from_iterable
  152. ############## logging.handlers.WatchedFileHandler ##########################
  153. import os
  154. from stat import ST_DEV, ST_INO
  155. import platform as _platform
  156. if _platform.system() == "Windows":
  157. #since windows doesn't go with WatchedFileHandler use FileHandler instead
  158. WatchedFileHandler = logging.FileHandler
  159. else:
  160. try:
  161. from logging.handlers import WatchedFileHandler
  162. except ImportError:
  163. class WatchedFileHandler(logging.FileHandler): # noqa
  164. """
  165. A handler for logging to a file, which watches the file
  166. to see if it has changed while in use. This can happen because of
  167. usage of programs such as newsyslog and logrotate which perform
  168. log file rotation. This handler, intended for use under Unix,
  169. watches the file to see if it has changed since the last emit.
  170. (A file has changed if its device or inode have changed.)
  171. If it has changed, the old file stream is closed, and the file
  172. opened to get a new stream.
  173. This handler is not appropriate for use under Windows, because
  174. under Windows open files cannot be moved or renamed - logging
  175. opens the files with exclusive locks - and so there is no need
  176. for such a handler. Furthermore, ST_INO is not supported under
  177. Windows; stat always returns zero for this value.
  178. This handler is based on a suggestion and patch by Chad J.
  179. Schroeder.
  180. """
  181. def __init__(self, *args, **kwargs):
  182. logging.FileHandler.__init__(self, *args, **kwargs)
  183. if not os.path.exists(self.baseFilename):
  184. self.dev, self.ino = -1, -1
  185. else:
  186. stat = os.stat(self.baseFilename)
  187. self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
  188. def emit(self, record):
  189. """
  190. Emit a record.
  191. First check if the underlying file has changed, and if it
  192. has, close the old stream and reopen the file to get the
  193. current stream.
  194. """
  195. if not os.path.exists(self.baseFilename):
  196. stat = None
  197. changed = 1
  198. else:
  199. stat = os.stat(self.baseFilename)
  200. changed = ((stat[ST_DEV] != self.dev) or
  201. (stat[ST_INO] != self.ino))
  202. if changed and self.stream is not None:
  203. self.stream.flush()
  204. self.stream.close()
  205. self.stream = self._open()
  206. if stat is None:
  207. stat = os.stat(self.baseFilename)
  208. self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
  209. logging.FileHandler.emit(self, record)