__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. All the following functions are optional, and no output will be generated if
  22. they do not exist.
  23. =============================== ==============================================
  24. Function Description
  25. =============================== ==============================================
  26. generate_<format>_begin(events) Generate backend- and format-specific file
  27. header contents.
  28. generate_<format>_end(events) Generate backend- and format-specific file
  29. footer contents.
  30. generate_<format>(event) Generate backend- and format-specific contents
  31. for the given event.
  32. =============================== ==============================================
  33. """
  34. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  35. __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
  36. __license__ = "GPL version 2 or (at your option) any later version"
  37. __maintainer__ = "Stefan Hajnoczi"
  38. __email__ = "stefanha@linux.vnet.ibm.com"
  39. import os
  40. import tracetool
  41. def get_list(only_public = False):
  42. """Get a list of (name, description) pairs."""
  43. res = [("nop", "Tracing disabled.")]
  44. modnames = []
  45. for filename in os.listdir(tracetool.backend.__path__[0]):
  46. if filename.endswith('.py') and filename != '__init__.py':
  47. modnames.append(filename.rsplit('.', 1)[0])
  48. for modname in sorted(modnames):
  49. module = tracetool.try_import("tracetool.backend." + modname)
  50. # just in case; should never fail unless non-module files are put there
  51. if not module[0]:
  52. continue
  53. module = module[1]
  54. public = getattr(module, "PUBLIC", False)
  55. if only_public and not public:
  56. continue
  57. doc = module.__doc__
  58. if doc is None:
  59. doc = ""
  60. doc = doc.strip().split("\n")[0]
  61. name = modname.replace("_", "-")
  62. res.append((name, doc))
  63. return res
  64. def exists(name):
  65. """Return whether the given backend exists."""
  66. if len(name) == 0:
  67. return False
  68. if name == "nop":
  69. return True
  70. name = name.replace("-", "_")
  71. return tracetool.try_import("tracetool.backend." + name)[1]
  72. class Wrapper:
  73. def __init__(self, backends, format):
  74. self._backends = [backend.replace("-", "_") for backend in backends]
  75. self._format = format.replace("-", "_")
  76. for backend in self._backends:
  77. assert exists(backend)
  78. assert tracetool.format.exists(self._format)
  79. def _run_function(self, name, *args, **kwargs):
  80. for backend in self._backends:
  81. func = tracetool.try_import("tracetool.backend." + backend,
  82. name % self._format, None)[1]
  83. if func is not None:
  84. func(*args, **kwargs)
  85. def generate_begin(self, events):
  86. self._run_function("generate_%s_begin", events)
  87. def generate(self, event):
  88. self._run_function("generate_%s", event)
  89. def generate_end(self, events):
  90. self._run_function("generate_%s_end", events)