__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. """
  3. Backend management.
  4. Creating new backends
  5. ---------------------
  6. A new backend named 'foo-bar' corresponds to Python module
  7. 'tracetool/backend/foo_bar.py'.
  8. A backend module should provide a docstring, whose first non-empty line will be
  9. considered its short description.
  10. All backends must generate their contents through the 'tracetool.out' routine.
  11. Backend attributes
  12. ------------------
  13. ========= ====================================================================
  14. Attribute Description
  15. ========= ====================================================================
  16. PUBLIC If exists and is set to 'True', the backend is considered "public".
  17. ========= ====================================================================
  18. Backend functions
  19. -----------------
  20. All the following functions are optional, and no output will be generated if
  21. they do not exist.
  22. =============================== ==============================================
  23. Function Description
  24. =============================== ==============================================
  25. generate_<format>_begin(events) Generate backend- and format-specific file
  26. header contents.
  27. generate_<format>_end(events) Generate backend- and format-specific file
  28. footer contents.
  29. generate_<format>(event) Generate backend- and format-specific contents
  30. for the given event.
  31. =============================== ==============================================
  32. """
  33. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  34. __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
  35. __license__ = "GPL version 2 or (at your option) any later version"
  36. __maintainer__ = "Stefan Hajnoczi"
  37. __email__ = "stefanha@redhat.com"
  38. import os
  39. import tracetool
  40. def get_list(only_public = False):
  41. """Get a list of (name, description) pairs."""
  42. res = [("nop", "Tracing disabled.")]
  43. modnames = []
  44. for filename in os.listdir(tracetool.backend.__path__[0]):
  45. if filename.endswith('.py') and filename != '__init__.py':
  46. modnames.append(filename.rsplit('.', 1)[0])
  47. for modname in sorted(modnames):
  48. module = tracetool.try_import("tracetool.backend." + modname)
  49. # just in case; should never fail unless non-module files are put there
  50. if not module[0]:
  51. continue
  52. module = module[1]
  53. public = getattr(module, "PUBLIC", False)
  54. if only_public and not public:
  55. continue
  56. doc = module.__doc__
  57. if doc is None:
  58. doc = ""
  59. doc = doc.strip().split("\n")[0]
  60. name = modname.replace("_", "-")
  61. res.append((name, doc))
  62. return res
  63. def exists(name):
  64. """Return whether the given backend exists."""
  65. if len(name) == 0:
  66. return False
  67. if name == "nop":
  68. return True
  69. name = name.replace("-", "_")
  70. return tracetool.try_import("tracetool.backend." + name)[1]
  71. class Wrapper:
  72. def __init__(self, backends, format):
  73. self._backends = [backend.replace("-", "_") for backend in backends]
  74. self._format = format.replace("-", "_")
  75. for backend in self._backends:
  76. assert exists(backend)
  77. assert tracetool.format.exists(self._format)
  78. def _run_function(self, name, *args, **kwargs):
  79. for backend in self._backends:
  80. func = tracetool.try_import("tracetool.backend." + backend,
  81. name % self._format, None)[1]
  82. if func is not None:
  83. func(*args, **kwargs)
  84. def generate_begin(self, events, group):
  85. self._run_function("generate_%s_begin", events, group)
  86. def generate(self, event, group):
  87. self._run_function("generate_%s", event, group)
  88. def generate_backend_dstate(self, event, group):
  89. self._run_function("generate_%s_backend_dstate", event, group)
  90. def generate_end(self, events, group):
  91. self._run_function("generate_%s_end", events, group)