build-qt-resources.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/python -u
  2. #
  3. # This creates a Qt resources XML file.
  4. #
  5. # Usage:
  6. #
  7. # build-qt-resources.py output.qrc virtualpath1=realpath1 virtualpath2=realpath2 ... virtualpathN=realpathN
  8. #
  9. # "output.qrc" will be overwritten! Each argument after the output file
  10. # consists of a virtual path, which defines the resource prefix, and a real
  11. # path (i.e. an actual file system path), which sets the source file or
  12. # directory. If the real path is a file, the virtual path sets the full
  13. # virtual file name. If the real path is a directory, the directory is
  14. # scanned recursively, and all files are added, using the virtual path as
  15. # prefix.
  16. import sys
  17. import os
  18. import stat
  19. import io
  20. if len(sys.argv) < 2:
  21. sys.exit(1)
  22. result = "<RCC>\n"
  23. def add_files(virtualpath, filepath):
  24. global result
  25. fstat = os.stat(filepath)
  26. if stat.S_ISDIR(fstat.st_mode):
  27. for item in os.listdir(filepath):
  28. add_files(os.path.join(virtualpath, item), os.path.join(filepath, item))
  29. else:
  30. dirname, fname = os.path.split(virtualpath)
  31. result += (('<qresource prefix=\"%s\">\n' +
  32. ' <file alias=\"%s\">%s</file>\n' +
  33. '</qresource>\n') % (dirname, fname, filepath))
  34. for item in sys.argv[2:]:
  35. virtualpath, filepath = item.split("=", 1)
  36. add_files(virtualpath, filepath)
  37. result += "</RCC>"
  38. result = result.encode("utf8")
  39. try:
  40. with open(sys.argv[1], "rb") as infile:
  41. if infile.read() == result:
  42. # They're the same -> prevent cmake from rebuilding resources.
  43. sys.exit(0)
  44. except IOError:
  45. pass
  46. outfile = open(sys.argv[1], "wb")
  47. outfile.write(result)