/scripts/mkflags.py

https://github.com/nyetwurk/mumble · Python · 111 lines · 56 code · 25 blank · 30 comment · 14 complexity · 1f59a7b87e13733dc1343f9adf4c2940 MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2005-2017 The Mumble Developers. All rights reserved.
  5. # Use of this source code is governed by a BSD-style license
  6. # that can be found in the LICENSE file at the root of the
  7. # Mumble source tree or at <https://www.mumble.info/LICENSE>.
  8. # mkflags.py generates .pri and .qrc files from Mumble's
  9. # flag SVGs, such that the flags can be included as Qt
  10. # resources.
  11. #
  12. # The script splits the flags into multiple .qrc files once
  13. # a single .qrc file exceeds a given threshold.
  14. #
  15. # This is because older compilers (and old hardware, too!)
  16. # can have problems with large source files. When Qt embeds
  17. # a .qrc file, it generates a .cpp file and compiles it. Some
  18. # of our flag SVGs can be quite large, and if we only use a
  19. # single .qrc file, it is (as of this writing) 32MB+ large.
  20. from __future__ import (unicode_literals, print_function, division)
  21. import os
  22. import shutil
  23. import codecs
  24. import collections
  25. # Container for an on-disk flag SVG. Contains size and filename.
  26. OnDiskFlag = collections.namedtuple('OnDiskFlag', ['size', 'filename'])
  27. # Once a .qrc file's content exceeds this size, the
  28. # file will be considered full.
  29. MAX_SIZE = 1024*1024
  30. def main():
  31. # Get a list of all flag SVGs, and sort them by size.
  32. flags = []
  33. flagsDir = os.path.join('icons', 'flags')
  34. flagFns = os.listdir(flagsDir)
  35. for fn in flagFns:
  36. if not fn.lower().endswith('svg'):
  37. continue
  38. with open(os.path.join(flagsDir, fn), 'r') as f:
  39. buf = f.read()
  40. sz = len(buf)
  41. flags.append(OnDiskFlag(size=sz, filename=fn))
  42. flags = sorted(flags) # Sort by first tuple index (size).
  43. # Figure out the .qrc target of the individual
  44. # SVG files. Once a .qrc target exceeds MAX_SIZE,
  45. # we add a new file.
  46. flagsOut = []
  47. curFileContent = []
  48. curFileSz = 0
  49. for flag in flags:
  50. sz = flag.size
  51. fn = flag.filename
  52. curFileSz += sz
  53. curFileContent.append(fn)
  54. if curFileSz > MAX_SIZE:
  55. flagsOut.append(curFileContent)
  56. curFileContent = []
  57. curFileSz = 0
  58. if len(curFileContent) > 0:
  59. flagsOut.append(curFileContent)
  60. # Remove old flags qrc file.
  61. oldFlagsQrc = os.path.join('src', 'mumble', 'mumble_flags.qrc')
  62. if os.path.exists(oldFlagsQrc):
  63. os.remove(oldFlagsQrc)
  64. # Remove existing flags dir in src/mumble.
  65. flagsOutDir = os.path.join('src', 'mumble', 'flags')
  66. if os.path.exists(flagsOutDir):
  67. shutil.rmtree(flagsOutDir)
  68. os.mkdir(flagsOutDir)
  69. # Generate output files.
  70. for idx, content in enumerate(flagsOut):
  71. fn = 'mumble_flags_{0}.qrc'.format(idx)
  72. with codecs.open(os.path.join(flagsOutDir, fn), "w", "utf-8") as f:
  73. f.write('<!DOCTYPE RCC>\n')
  74. f.write('<RCC version="1.0">\n')
  75. f.write('<qresource>\n')
  76. for fn in content:
  77. f.write('<file alias="{0}">{1}</file>\n'.format('flags/' + fn, '../../../icons/flags/' + fn))
  78. f.write('</qresource>\n')
  79. f.write('</RCC>\n')
  80. # Generate .pri file for flags.
  81. with codecs.open(os.path.join(flagsOutDir, 'mumble_flags.pri'), "w", "utf-8") as f:
  82. for idx, _ in enumerate(flagsOut):
  83. fn = 'mumble_flags_{0}.qrc'.format(idx)
  84. f.write('RESOURCES *= flags/{0}\n'.format(fn))
  85. if __name__ == '__main__':
  86. main()