m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

669 lines
24 KiB

6 months ago
  1. # Human friendly input/output in Python.
  2. #
  3. # Author: Peter Odding <peter@peterodding.com>
  4. # Last Change: March 6, 2020
  5. # URL: https://humanfriendly.readthedocs.io
  6. """
  7. Utility classes and functions that make it easy to write :mod:`unittest` compatible test suites.
  8. Over the years I've developed the habit of writing test suites for Python
  9. projects using the :mod:`unittest` module. During those years I've come to know
  10. :pypi:`pytest` and in fact I use :pypi:`pytest` to run my test suites (due to
  11. its much better error reporting) but I've yet to publish a test suite that
  12. *requires* :pypi:`pytest`. I have several reasons for doing so:
  13. - It's nice to keep my test suites as simple and accessible as possible and
  14. not requiring a specific test runner is part of that attitude.
  15. - Whereas :mod:`unittest` is quite explicit, :pypi:`pytest` contains a lot of
  16. magic, which kind of contradicts the Python mantra "explicit is better than
  17. implicit" (IMHO).
  18. """
  19. # Standard library module
  20. import functools
  21. import logging
  22. import os
  23. import pipes
  24. import shutil
  25. import sys
  26. import tempfile
  27. import time
  28. import unittest
  29. # Modules included in our package.
  30. from humanfriendly.compat import StringIO
  31. from humanfriendly.text import random_string
  32. # Initialize a logger for this module.
  33. logger = logging.getLogger(__name__)
  34. # A unique object reference used to detect missing attributes.
  35. NOTHING = object()
  36. # Public identifiers that require documentation.
  37. __all__ = (
  38. 'CallableTimedOut',
  39. 'CaptureBuffer',
  40. 'CaptureOutput',
  41. 'ContextManager',
  42. 'CustomSearchPath',
  43. 'MockedProgram',
  44. 'PatchedAttribute',
  45. 'PatchedItem',
  46. 'TemporaryDirectory',
  47. 'TestCase',
  48. 'configure_logging',
  49. 'make_dirs',
  50. 'retry',
  51. 'run_cli',
  52. 'skip_on_raise',
  53. 'touch',
  54. )
  55. def configure_logging(log_level=logging.DEBUG):
  56. """configure_logging(log_level=logging.DEBUG)
  57. Automatically configure logging to the terminal.
  58. :param log_level: The log verbosity (a number, defaults
  59. to :mod:`logging.DEBUG <logging>`).
  60. When :mod:`coloredlogs` is installed :func:`coloredlogs.install()` will be
  61. used to configure logging to the terminal. When this fails with an
  62. :exc:`~exceptions.ImportError` then :func:`logging.basicConfig()` is used
  63. as a fall back.
  64. """
  65. try:
  66. import coloredlogs
  67. coloredlogs.install(level=log_level)
  68. except ImportError:
  69. logging.basicConfig(
  70. level=log_level,
  71. format='%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s',
  72. datefmt='%Y-%m-%d %H:%M:%S')
  73. def make_dirs(pathname):
  74. """
  75. Create missing directories.
  76. :param pathname: The pathname of a directory (a string).
  77. """
  78. if not os.path.isdir(pathname):
  79. os.makedirs(pathname)
  80. def retry(func, timeout=60, exc_type=AssertionError):
  81. """retry(func, timeout=60, exc_type=AssertionError)
  82. Retry a function until assertions no longer fail.
  83. :param func: A callable. When the callable returns
  84. :data:`False` it will also be retried.
  85. :param timeout: The number of seconds after which to abort (a number,
  86. defaults to 60).
  87. :param exc_type: The type of exceptions to retry (defaults
  88. to :exc:`~exceptions.AssertionError`).
  89. :returns: The value returned by `func`.
  90. :raises: Once the timeout has expired :func:`retry()` will raise the
  91. previously retried assertion error. When `func` keeps returning
  92. :data:`False` until `timeout` expires :exc:`CallableTimedOut`
  93. will be raised.
  94. This function sleeps between retries to avoid claiming CPU cycles we don't
  95. need. It starts by sleeping for 0.1 second but adjusts this to one second
  96. as the number of retries grows.
  97. """
  98. pause = 0.1
  99. timeout += time.time()
  100. while True:
  101. try:
  102. result = func()
  103. if result is not False:
  104. return result
  105. except exc_type:
  106. if time.time() > timeout:
  107. raise
  108. else:
  109. if time.time() > timeout:
  110. raise CallableTimedOut()
  111. time.sleep(pause)
  112. if pause < 1:
  113. pause *= 2
  114. def run_cli(entry_point, *arguments, **options):
  115. """
  116. Test a command line entry point.
  117. :param entry_point: The function that implements the command line interface
  118. (a callable).
  119. :param arguments: Any positional arguments (strings) become the command
  120. line arguments (:data:`sys.argv` items 1-N).
  121. :param options: The following keyword arguments are supported:
  122. **capture**
  123. Whether to use :class:`CaptureOutput`. Defaults
  124. to :data:`True` but can be disabled by passing
  125. :data:`False` instead.
  126. **input**
  127. Refer to :class:`CaptureOutput`.
  128. **merged**
  129. Refer to :class:`CaptureOutput`.
  130. **program_name**
  131. Used to set :data:`sys.argv` item 0.
  132. :returns: A tuple with two values:
  133. 1. The return code (an integer).
  134. 2. The captured output (a string).
  135. """
  136. # Add the `program_name' option to the arguments.
  137. arguments = list(arguments)
  138. arguments.insert(0, options.pop('program_name', sys.executable))
  139. # Log the command line arguments (and the fact that we're about to call the
  140. # command line entry point function).
  141. logger.debug("Calling command line entry point with arguments: %s", arguments)
  142. # Prepare to capture the return code and output even if the command line
  143. # interface raises an exception (whether the exception type is SystemExit
  144. # or something else).
  145. returncode = 0
  146. stdout = None
  147. stderr = None
  148. try:
  149. # Temporarily override sys.argv.
  150. with PatchedAttribute(sys, 'argv', arguments):
  151. # Manipulate the standard input/output/error streams?
  152. options['enabled'] = options.pop('capture', True)
  153. with CaptureOutput(**options) as capturer:
  154. try:
  155. # Call the command line interface.
  156. entry_point()
  157. finally:
  158. # Get the output even if an exception is raised.
  159. stdout = capturer.stdout.getvalue()
  160. stderr = capturer.stderr.getvalue()
  161. # Reconfigure logging to the terminal because it is very
  162. # likely that the entry point function has changed the
  163. # configured log level.
  164. configure_logging()
  165. except BaseException as e:
  166. if isinstance(e, SystemExit):
  167. logger.debug("Intercepting return code %s from SystemExit exception.", e.code)
  168. returncode = e.code
  169. else:
  170. logger.warning("Defaulting return code to 1 due to raised exception.", exc_info=True)
  171. returncode = 1
  172. else:
  173. logger.debug("Command line entry point returned successfully!")
  174. # Always log the output captured on stdout/stderr, to make it easier to
  175. # diagnose test failures (but avoid duplicate logging when merged=True).
  176. is_merged = options.get('merged', False)
  177. merged_streams = [('merged streams', stdout)]
  178. separate_streams = [('stdout', stdout), ('stderr', stderr)]
  179. streams = merged_streams if is_merged else separate_streams
  180. for name, value in streams:
  181. if value:
  182. logger.debug("Output on %s:\n%s", name, value)
  183. else:
  184. logger.debug("No output on %s.", name)
  185. return returncode, stdout
  186. def skip_on_raise(*exc_types):
  187. """
  188. Decorate a test function to translation specific exception types to :exc:`unittest.SkipTest`.
  189. :param exc_types: One or more positional arguments give the exception
  190. types to be translated to :exc:`unittest.SkipTest`.
  191. :returns: A decorator function specialized to `exc_types`.
  192. """
  193. def decorator(function):
  194. @functools.wraps(function)
  195. def wrapper(*args, **kw):
  196. try:
  197. return function(*args, **kw)
  198. except exc_types as e:
  199. logger.debug("Translating exception to unittest.SkipTest ..", exc_info=True)
  200. raise unittest.SkipTest("skipping test because %s was raised" % type(e))
  201. return wrapper
  202. return decorator
  203. def touch(filename):
  204. """
  205. The equivalent of the UNIX :man:`touch` program in Python.
  206. :param filename: The pathname of the file to touch (a string).
  207. Note that missing directories are automatically created using
  208. :func:`make_dirs()`.
  209. """
  210. make_dirs(os.path.dirname(filename))
  211. with open(filename, 'a'):
  212. os.utime(filename, None)
  213. class CallableTimedOut(Exception):
  214. """Raised by :func:`retry()` when the timeout expires."""
  215. class ContextManager(object):
  216. """Base class to enable composition of context managers."""
  217. def __enter__(self):
  218. """Enable use as context managers."""
  219. return self
  220. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  221. """Enable use as context managers."""
  222. class PatchedAttribute(ContextManager):
  223. """Context manager that temporary replaces an object attribute using :func:`setattr()`."""
  224. def __init__(self, obj, name, value):
  225. """
  226. Initialize a :class:`PatchedAttribute` object.
  227. :param obj: The object to patch.
  228. :param name: An attribute name.
  229. :param value: The value to set.
  230. """
  231. self.object_to_patch = obj
  232. self.attribute_to_patch = name
  233. self.patched_value = value
  234. self.original_value = NOTHING
  235. def __enter__(self):
  236. """
  237. Replace (patch) the attribute.
  238. :returns: The object whose attribute was patched.
  239. """
  240. # Enable composition of context managers.
  241. super(PatchedAttribute, self).__enter__()
  242. # Patch the object's attribute.
  243. self.original_value = getattr(self.object_to_patch, self.attribute_to_patch, NOTHING)
  244. setattr(self.object_to_patch, self.attribute_to_patch, self.patched_value)
  245. return self.object_to_patch
  246. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  247. """Restore the attribute to its original value."""
  248. # Enable composition of context managers.
  249. super(PatchedAttribute, self).__exit__(exc_type, exc_value, traceback)
  250. # Restore the object's attribute.
  251. if self.original_value is NOTHING:
  252. delattr(self.object_to_patch, self.attribute_to_patch)
  253. else:
  254. setattr(self.object_to_patch, self.attribute_to_patch, self.original_value)
  255. class PatchedItem(ContextManager):
  256. """Context manager that temporary replaces an object item using :meth:`~object.__setitem__()`."""
  257. def __init__(self, obj, item, value):
  258. """
  259. Initialize a :class:`PatchedItem` object.
  260. :param obj: The object to patch.
  261. :param item: The item to patch.
  262. :param value: The value to set.
  263. """
  264. self.object_to_patch = obj
  265. self.item_to_patch = item
  266. self.patched_value = value
  267. self.original_value = NOTHING
  268. def __enter__(self):
  269. """
  270. Replace (patch) the item.
  271. :returns: The object whose item was patched.
  272. """
  273. # Enable composition of context managers.
  274. super(PatchedItem, self).__enter__()
  275. # Patch the object's item.
  276. try:
  277. self.original_value = self.object_to_patch[self.item_to_patch]
  278. except KeyError:
  279. self.original_value = NOTHING
  280. self.object_to_patch[self.item_to_patch] = self.patched_value
  281. return self.object_to_patch
  282. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  283. """Restore the item to its original value."""
  284. # Enable composition of context managers.
  285. super(PatchedItem, self).__exit__(exc_type, exc_value, traceback)
  286. # Restore the object's item.
  287. if self.original_value is NOTHING:
  288. del self.object_to_patch[self.item_to_patch]
  289. else:
  290. self.object_to_patch[self.item_to_patch] = self.original_value
  291. class TemporaryDirectory(ContextManager):
  292. """
  293. Easy temporary directory creation & cleanup using the :keyword:`with` statement.
  294. Here's an example of how to use this:
  295. .. code-block:: python
  296. with TemporaryDirectory() as directory:
  297. # Do something useful here.
  298. assert os.path.isdir(directory)
  299. """
  300. def __init__(self, **options):
  301. """
  302. Initialize a :class:`TemporaryDirectory` object.
  303. :param options: Any keyword arguments are passed on to
  304. :func:`tempfile.mkdtemp()`.
  305. """
  306. self.mkdtemp_options = options
  307. self.temporary_directory = None
  308. def __enter__(self):
  309. """
  310. Create the temporary directory using :func:`tempfile.mkdtemp()`.
  311. :returns: The pathname of the directory (a string).
  312. """
  313. # Enable composition of context managers.
  314. super(TemporaryDirectory, self).__enter__()
  315. # Create the temporary directory.
  316. self.temporary_directory = tempfile.mkdtemp(**self.mkdtemp_options)
  317. return self.temporary_directory
  318. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  319. """Cleanup the temporary directory using :func:`shutil.rmtree()`."""
  320. # Enable composition of context managers.
  321. super(TemporaryDirectory, self).__exit__(exc_type, exc_value, traceback)
  322. # Cleanup the temporary directory.
  323. if self.temporary_directory is not None:
  324. shutil.rmtree(self.temporary_directory)
  325. self.temporary_directory = None
  326. class MockedHomeDirectory(PatchedItem, TemporaryDirectory):
  327. """
  328. Context manager to temporarily change ``$HOME`` (the current user's profile directory).
  329. This class is a composition of the :class:`PatchedItem` and
  330. :class:`TemporaryDirectory` context managers.
  331. """
  332. def __init__(self):
  333. """Initialize a :class:`MockedHomeDirectory` object."""
  334. PatchedItem.__init__(self, os.environ, 'HOME', os.environ.get('HOME'))
  335. TemporaryDirectory.__init__(self)
  336. def __enter__(self):
  337. """
  338. Activate the custom ``$PATH``.
  339. :returns: The pathname of the directory that has
  340. been added to ``$PATH`` (a string).
  341. """
  342. # Get the temporary directory.
  343. directory = TemporaryDirectory.__enter__(self)
  344. # Override the value to patch now that we have
  345. # the pathname of the temporary directory.
  346. self.patched_value = directory
  347. # Temporary patch $HOME.
  348. PatchedItem.__enter__(self)
  349. # Pass the pathname of the temporary directory to the caller.
  350. return directory
  351. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  352. """Deactivate the custom ``$HOME``."""
  353. super(MockedHomeDirectory, self).__exit__(exc_type, exc_value, traceback)
  354. class CustomSearchPath(PatchedItem, TemporaryDirectory):
  355. """
  356. Context manager to temporarily customize ``$PATH`` (the executable search path).
  357. This class is a composition of the :class:`PatchedItem` and
  358. :class:`TemporaryDirectory` context managers.
  359. """
  360. def __init__(self, isolated=False):
  361. """
  362. Initialize a :class:`CustomSearchPath` object.
  363. :param isolated: :data:`True` to clear the original search path,
  364. :data:`False` to add the temporary directory to the
  365. start of the search path.
  366. """
  367. # Initialize our own instance variables.
  368. self.isolated_search_path = isolated
  369. # Selectively initialize our superclasses.
  370. PatchedItem.__init__(self, os.environ, 'PATH', self.current_search_path)
  371. TemporaryDirectory.__init__(self)
  372. def __enter__(self):
  373. """
  374. Activate the custom ``$PATH``.
  375. :returns: The pathname of the directory that has
  376. been added to ``$PATH`` (a string).
  377. """
  378. # Get the temporary directory.
  379. directory = TemporaryDirectory.__enter__(self)
  380. # Override the value to patch now that we have
  381. # the pathname of the temporary directory.
  382. self.patched_value = (
  383. directory if self.isolated_search_path
  384. else os.pathsep.join([directory] + self.current_search_path.split(os.pathsep))
  385. )
  386. # Temporary patch the $PATH.
  387. PatchedItem.__enter__(self)
  388. # Pass the pathname of the temporary directory to the caller
  389. # because they may want to `install' custom executables.
  390. return directory
  391. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  392. """Deactivate the custom ``$PATH``."""
  393. super(CustomSearchPath, self).__exit__(exc_type, exc_value, traceback)
  394. @property
  395. def current_search_path(self):
  396. """The value of ``$PATH`` or :data:`os.defpath` (a string)."""
  397. return os.environ.get('PATH', os.defpath)
  398. class MockedProgram(CustomSearchPath):
  399. """
  400. Context manager to mock the existence of a program (executable).
  401. This class extends the functionality of :class:`CustomSearchPath`.
  402. """
  403. def __init__(self, name, returncode=0, script=None):
  404. """
  405. Initialize a :class:`MockedProgram` object.
  406. :param name: The name of the program (a string).
  407. :param returncode: The return code that the program should emit (a
  408. number, defaults to zero).
  409. :param script: Shell script code to include in the mocked program (a
  410. string or :data:`None`). This can be used to mock a
  411. program that is expected to generate specific output.
  412. """
  413. # Initialize our own instance variables.
  414. self.program_name = name
  415. self.program_returncode = returncode
  416. self.program_script = script
  417. self.program_signal_file = None
  418. # Initialize our superclasses.
  419. super(MockedProgram, self).__init__()
  420. def __enter__(self):
  421. """
  422. Create the mock program.
  423. :returns: The pathname of the directory that has
  424. been added to ``$PATH`` (a string).
  425. """
  426. directory = super(MockedProgram, self).__enter__()
  427. self.program_signal_file = os.path.join(directory, 'program-was-run-%s' % random_string(10))
  428. pathname = os.path.join(directory, self.program_name)
  429. with open(pathname, 'w') as handle:
  430. handle.write('#!/bin/sh\n')
  431. handle.write('echo > %s\n' % pipes.quote(self.program_signal_file))
  432. if self.program_script:
  433. handle.write('%s\n' % self.program_script.strip())
  434. handle.write('exit %i\n' % self.program_returncode)
  435. os.chmod(pathname, 0o755)
  436. return directory
  437. def __exit__(self, *args, **kw):
  438. """
  439. Ensure that the mock program was run.
  440. :raises: :exc:`~exceptions.AssertionError` when
  441. the mock program hasn't been run.
  442. """
  443. try:
  444. assert self.program_signal_file and os.path.isfile(self.program_signal_file), \
  445. ("It looks like %r was never run!" % self.program_name)
  446. finally:
  447. return super(MockedProgram, self).__exit__(*args, **kw)
  448. class CaptureOutput(ContextManager):
  449. """
  450. Context manager that captures what's written to :data:`sys.stdout` and :data:`sys.stderr`.
  451. .. attribute:: stdin
  452. The :class:`~humanfriendly.compat.StringIO` object used to feed the standard input stream.
  453. .. attribute:: stdout
  454. The :class:`CaptureBuffer` object used to capture the standard output stream.
  455. .. attribute:: stderr
  456. The :class:`CaptureBuffer` object used to capture the standard error stream.
  457. """
  458. def __init__(self, merged=False, input='', enabled=True):
  459. """
  460. Initialize a :class:`CaptureOutput` object.
  461. :param merged: :data:`True` to merge the streams,
  462. :data:`False` to capture them separately.
  463. :param input: The data that reads from :data:`sys.stdin`
  464. should return (a string).
  465. :param enabled: :data:`True` to enable capturing (the default),
  466. :data:`False` otherwise. This makes it easy to
  467. unconditionally use :class:`CaptureOutput` in
  468. a :keyword:`with` block while preserving the
  469. choice to opt out of capturing output.
  470. """
  471. self.stdin = StringIO(input)
  472. self.stdout = CaptureBuffer()
  473. self.stderr = self.stdout if merged else CaptureBuffer()
  474. self.patched_attributes = []
  475. if enabled:
  476. self.patched_attributes.extend(
  477. PatchedAttribute(sys, name, getattr(self, name))
  478. for name in ('stdin', 'stdout', 'stderr')
  479. )
  480. def __enter__(self):
  481. """Start capturing what's written to :data:`sys.stdout` and :data:`sys.stderr`."""
  482. super(CaptureOutput, self).__enter__()
  483. for context in self.patched_attributes:
  484. context.__enter__()
  485. return self
  486. def __exit__(self, exc_type=None, exc_value=None, traceback=None):
  487. """Stop capturing what's written to :data:`sys.stdout` and :data:`sys.stderr`."""
  488. super(CaptureOutput, self).__exit__(exc_type, exc_value, traceback)
  489. for context in self.patched_attributes:
  490. context.__exit__(exc_type, exc_value, traceback)
  491. def get_lines(self):
  492. """Get the contents of :attr:`stdout` split into separate lines."""
  493. return self.get_text().splitlines()
  494. def get_text(self):
  495. """Get the contents of :attr:`stdout` as a Unicode string."""
  496. return self.stdout.get_text()
  497. def getvalue(self):
  498. """Get the text written to :data:`sys.stdout`."""
  499. return self.stdout.getvalue()
  500. class CaptureBuffer(StringIO):
  501. """
  502. Helper for :class:`CaptureOutput` to provide an easy to use API.
  503. The two methods defined by this subclass were specifically chosen to match
  504. the names of the methods provided by my :pypi:`capturer` package which
  505. serves a similar role as :class:`CaptureOutput` but knows how to simulate
  506. an interactive terminal (tty).
  507. """
  508. def get_lines(self):
  509. """Get the contents of the buffer split into separate lines."""
  510. return self.get_text().splitlines()
  511. def get_text(self):
  512. """Get the contents of the buffer as a Unicode string."""
  513. return self.getvalue()
  514. class TestCase(unittest.TestCase):
  515. """Subclass of :class:`unittest.TestCase` with automatic logging and other miscellaneous features."""
  516. def __init__(self, *args, **kw):
  517. """
  518. Initialize a :class:`TestCase` object.
  519. Any positional and/or keyword arguments are passed on to the
  520. initializer of the superclass.
  521. """
  522. super(TestCase, self).__init__(*args, **kw)
  523. def setUp(self, log_level=logging.DEBUG):
  524. """setUp(log_level=logging.DEBUG)
  525. Automatically configure logging to the terminal.
  526. :param log_level: Refer to :func:`configure_logging()`.
  527. The :func:`setUp()` method is automatically called by
  528. :class:`unittest.TestCase` before each test method starts.
  529. It does two things:
  530. - Logging to the terminal is configured using
  531. :func:`configure_logging()`.
  532. - Before the test method starts a newline is emitted, to separate the
  533. name of the test method (which will be printed to the terminal by
  534. :mod:`unittest` or :pypi:`pytest`) from the first line of logging
  535. output that the test method is likely going to generate.
  536. """
  537. # Configure logging to the terminal.
  538. configure_logging(log_level)
  539. # Separate the name of the test method (printed by the superclass
  540. # and/or py.test without a newline at the end) from the first line of
  541. # logging output that the test method is likely going to generate.
  542. sys.stderr.write("\n")