oe-trim-schemas 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #! /usr/bin/env python3
  2. #
  3. # Copyright OpenEmbedded Contributors
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import sys
  8. try:
  9. import xml.etree.cElementTree as etree
  10. except:
  11. import xml.etree.ElementTree as etree
  12. def child (elem, name):
  13. for e in elem.getchildren():
  14. if e.tag == name:
  15. return e
  16. return None
  17. def children (elem, name=None):
  18. l = elem.getchildren()
  19. if name:
  20. l = [e for e in l if e.tag == name]
  21. return l
  22. if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
  23. print('oe-trim-schemas: error: the following arguments are required: schema\n'
  24. 'Usage: oe-trim-schemas schema\n\n'
  25. 'OpenEmbedded trim schemas - remove unneeded schema locale translations\n'
  26. ' from gconf schema files\n\n'
  27. 'arguments:\n'
  28. ' schema gconf schema file to trim\n')
  29. sys.exit(2)
  30. xml = etree.parse(sys.argv[1])
  31. for schema in child(xml.getroot(), "schemalist").getchildren():
  32. e = child(schema, "short")
  33. if e is not None:
  34. schema.remove(e)
  35. e = child(schema, "long")
  36. if e is not None:
  37. schema.remove(e)
  38. for locale in children(schema, "locale"):
  39. # One locale must exist so leave C locale...
  40. a = locale.attrib.get("name")
  41. if a == 'C':
  42. continue
  43. e = child(locale, "default")
  44. if e is None:
  45. schema.remove(locale)
  46. else:
  47. e = child(locale, "short")
  48. if e is not None:
  49. locale.remove(e)
  50. e = child(locale, "long")
  51. if e is not None:
  52. locale.remove(e)
  53. xml.write(sys.stdout, "UTF-8")