__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 attributes
  13. ------------------
  14. ========= ====================================================================
  15. Attribute Description
  16. ========= ====================================================================
  17. PUBLIC If exists and is set to 'True', the backend is considered "public".
  18. ========= ====================================================================
  19. Backend functions
  20. -----------------
  21. ======== =======================================================================
  22. Function Description
  23. ======== =======================================================================
  24. <format> Called to generate the format- and backend-specific code for each of
  25. the specified events. If the function does not exist, the backend is
  26. considered not compatible with the given format.
  27. ======== =======================================================================
  28. """
  29. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  30. __copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
  31. __license__ = "GPL version 2 or (at your option) any later version"
  32. __maintainer__ = "Stefan Hajnoczi"
  33. __email__ = "stefanha@linux.vnet.ibm.com"
  34. import os
  35. import tracetool
  36. def get_list(only_public = False):
  37. """Get a list of (name, description) pairs."""
  38. res = [("nop", "Tracing disabled.")]
  39. modnames = []
  40. for filename in os.listdir(tracetool.backend.__path__[0]):
  41. if filename.endswith('.py') and filename != '__init__.py':
  42. modnames.append(filename.rsplit('.', 1)[0])
  43. for modname in modnames:
  44. module = tracetool.try_import("tracetool.backend." + modname)
  45. # just in case; should never fail unless non-module files are put there
  46. if not module[0]:
  47. continue
  48. module = module[1]
  49. public = getattr(module, "PUBLIC", False)
  50. if only_public and not public:
  51. continue
  52. doc = module.__doc__
  53. if doc is None:
  54. doc = ""
  55. doc = doc.strip().split("\n")[0]
  56. name = modname.replace("_", "-")
  57. res.append((name, doc))
  58. return res
  59. def exists(name):
  60. """Return whether the given backend exists."""
  61. if len(name) == 0:
  62. return False
  63. if name == "nop":
  64. return True
  65. name = name.replace("-", "_")
  66. return tracetool.try_import("tracetool.backend." + name)[1]
  67. def compatible(backend, format):
  68. """Whether a backend is compatible with the given format."""
  69. if not exists(backend):
  70. raise ValueError("unknown backend: %s" % backend)
  71. backend = backend.replace("-", "_")
  72. format = format.replace("-", "_")
  73. if backend == "nop":
  74. return True
  75. else:
  76. func = tracetool.try_import("tracetool.backend." + backend,
  77. format, None)[1]
  78. return func is not None
  79. def _empty(events):
  80. pass
  81. def generate(backend, format, events):
  82. """Generate the per-event output for the given (backend, format) pair."""
  83. if not compatible(backend, format):
  84. raise ValueError("backend '%s' not compatible with format '%s'" %
  85. (backend, format))
  86. backend = backend.replace("-", "_")
  87. format = format.replace("-", "_")
  88. if backend == "nop":
  89. func = tracetool.try_import("tracetool.format." + format,
  90. "nop", _empty)[1]
  91. else:
  92. func = tracetool.try_import("tracetool.backend." + backend,
  93. format, None)[1]
  94. func(events)