__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Backend management.
  5. Creating new backends
  6. ---------------------
  7. A new backend named 'foo-bar' corresponds to Python module
  8. 'tracetool/backend/foo_bar.py'.
  9. A backend module should provide a docstring, whose first non-empty line will be
  10. considered its short description.
  11. All backends must generate their contents through the 'tracetool.out' routine.
  12. Backend functions
  13. -----------------
  14. ======== =======================================================================
  15. Function Description
  16. ======== =======================================================================
  17. <format> Called to generate the format- and backend-specific code for each of
  18. the specified events. If the function does not exist, the backend is
  19. considered not compatible with the given format.
  20. ======== =======================================================================
  21. """
  22. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  23. __copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
  24. __license__ = "GPL version 2 or (at your option) any later version"
  25. __maintainer__ = "Stefan Hajnoczi"
  26. __email__ = "stefanha@linux.vnet.ibm.com"
  27. import os
  28. import tracetool
  29. def get_list():
  30. """Get a list of (name, description) pairs."""
  31. res = [("nop", "Tracing disabled.")]
  32. modnames = []
  33. for filename in os.listdir(tracetool.backend.__path__[0]):
  34. if filename.endswith('.py') and filename != '__init__.py':
  35. modnames.append(filename.rsplit('.', 1)[0])
  36. for modname in modnames:
  37. module = tracetool.try_import("tracetool.backend." + modname)
  38. # just in case; should never fail unless non-module files are put there
  39. if not module[0]:
  40. continue
  41. module = module[1]
  42. doc = module.__doc__
  43. if doc is None:
  44. doc = ""
  45. doc = doc.strip().split("\n")[0]
  46. name = modname.replace("_", "-")
  47. res.append((name, doc))
  48. return res
  49. def exists(name):
  50. """Return whether the given backend exists."""
  51. if len(name) == 0:
  52. return False
  53. if name == "nop":
  54. return True
  55. name = name.replace("-", "_")
  56. return tracetool.try_import("tracetool.backend." + name)[1]
  57. def compatible(backend, format):
  58. """Whether a backend is compatible with the given format."""
  59. if not exists(backend):
  60. raise ValueError("unknown backend: %s" % backend)
  61. backend = backend.replace("-", "_")
  62. format = format.replace("-", "_")
  63. if backend == "nop":
  64. return True
  65. else:
  66. func = tracetool.try_import("tracetool.backend." + backend,
  67. format, None)[1]
  68. return func is not None
  69. def _empty(events):
  70. pass
  71. def generate(backend, format, events):
  72. """Generate the per-event output for the given (backend, format) pair."""
  73. if not compatible(backend, format):
  74. raise ValueError("backend '%s' not compatible with format '%s'" %
  75. (backend, format))
  76. backend = backend.replace("-", "_")
  77. format = format.replace("-", "_")
  78. if backend == "nop":
  79. func = tracetool.try_import("tracetool.format." + format,
  80. "nop", _empty)[1]
  81. else:
  82. func = tracetool.try_import("tracetool.backend." + backend,
  83. format, None)[1]
  84. func(events)