gen_builders.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # Copyright 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """A utility for generating builder classes for UKM entries.
  6. It takes as input a ukm.xml file describing all of the entries and metrics,
  7. and produces a c++ header and implementation file exposing builders for those
  8. entries and metrics.
  9. """
  10. import argparse
  11. import sys
  12. import ukm_model
  13. import builders_template
  14. import decode_template
  15. parser = argparse.ArgumentParser(description='Generate UKM entry builders')
  16. parser.add_argument('--input', help='Path to ukm.xml')
  17. parser.add_argument('--output', help='Path to generated files.')
  18. def main(argv):
  19. args = parser.parse_args()
  20. data = ReadFilteredData(args.input)
  21. relpath = 'services/metrics/public/cpp/'
  22. builders_template.WriteFiles(args.output, relpath, data)
  23. decode_template.WriteFiles(args.output, relpath, data)
  24. return 0
  25. def ReadFilteredData(path):
  26. """Reads data from path and filters out any obsolete metrics.
  27. Parses data from given path and removes all nodes that contain an
  28. <obsolete> tag. First iterates through <event> nodes, then <metric>
  29. nodes within them.
  30. Args:
  31. path: The path of the XML data source.
  32. Returns:
  33. A dict of the data not including any obsolete events or metrics.
  34. """
  35. with open(path) as ukm_file:
  36. data = ukm_model.UKM_XML_TYPE.Parse(ukm_file.read())
  37. event_tag = ukm_model._EVENT_TYPE.tag
  38. metric_tag = ukm_model._METRIC_TYPE.tag
  39. data[event_tag] = list(filter(ukm_model.IsNotObsolete, data[event_tag]))
  40. for event in data[event_tag]:
  41. event[metric_tag] = list(
  42. filter(ukm_model.IsNotObsolete, event[metric_tag]))
  43. return data
  44. if '__main__' == __name__:
  45. sys.exit(main(sys.argv))