generate_not_user_triggered_actions.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright 2020 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Produces C++ file with sorted array of UMA User Actions strings."""
  5. import argparse
  6. import sys
  7. import os
  8. from xml.etree import ElementTree
  9. def not_user_triggered_actions(actions_file_path):
  10. """Generates list of not-user triggered and non-obsolete UMA User Actions.
  11. Args:
  12. actions_file_path: path to actions.xml file
  13. """
  14. actions = ElementTree.parse(actions_file_path).getroot()
  15. for action in actions:
  16. if action.find('obsolete') is not None:
  17. continue
  18. if action.attrib.get('not_user_triggered') == 'true':
  19. yield action.attrib['name']
  20. def main(actions_file_path, output_file_path):
  21. """Produces C++ file with sorted array of UMA User Actions strings.
  22. Array is a global kNotUserTriggeredActions constant in anonymous namespace.
  23. Args:
  24. actions_file_path: path to actions.xml file
  25. output_file_path: path to output C++ file
  26. """
  27. actions = not_user_triggered_actions(actions_file_path)
  28. if not actions:
  29. sys.stderr.write(
  30. 'There are no not-user triggered and non-obsolete in %s',
  31. actions_file_path)
  32. return -1
  33. with open(output_file_path ,'w') as output_file:
  34. output_file.write('// Generated by %s\n' % sys.argv[0])
  35. output_file.write('namespace {\n')
  36. output_file.write('const char* const kNotUserTriggeredActions[] = {\n')
  37. for action in sorted(actions):
  38. output_file.write(' "%s",\n' % action)
  39. output_file.write('};\n')
  40. output_file.write('} // namespace\n')
  41. if __name__ == '__main__':
  42. parser = argparse.ArgumentParser(description=__doc__)
  43. parser.add_argument(
  44. '-a',
  45. '--actions',
  46. help='path to actions.xml file')
  47. parser.add_argument(
  48. '-o',
  49. '--output',
  50. help='path to output source file')
  51. args = parser.parse_args()
  52. sys.exit(main(args.actions, args.output))