resource-compiler.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python
  2. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. #
  5. # Permission is hereby granted, free of charge, to any person
  6. # obtaining a copy of this software and associated documentation
  7. # files (the "Software"), to deal in the Software without
  8. # restriction, including without limitation the rights to use,
  9. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the
  11. # Software is furnished to do so, subject to the following
  12. # conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. # OTHER DEALINGS IN THE SOFTWARE.
  25. import sys
  26. import os
  27. import shutil
  28. from pycrown import Repository
  29. from pycrown import Compiler
  30. PERFECT_SEED_FILE = "seed.ini"
  31. GAME_LIBRARY_FILE = "libgame.so"
  32. # Helper for compiling resources
  33. class CompilerHelper:
  34. def __init__(self, root_path, platform_name):
  35. self.m_root_path = root_path
  36. # Define destination path name
  37. dest = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + "_" + platform_name
  38. self.m_dest_path = os.path.normpath(dest)
  39. # Repository needs a root path
  40. self.m_repository = Repository.Repository(root_path)
  41. self.m_compiler = Compiler.Compiler(self.m_repository, self.m_dest_path)
  42. # Returns the root path where the resources are read from
  43. def root_path(self):
  44. return self.m_root_path
  45. # Returns the destination folder where the compiled resources
  46. # will be put
  47. def dest_path(self):
  48. return self.m_dest_path
  49. # Creates the destination folder
  50. def create_dest_folder(self):
  51. if not os.path.exists(self.m_dest_path):
  52. os.makedirs(self.m_dest_path)
  53. # Write the perfect seed into the destination folder
  54. def write_perfect_seed(self):
  55. seed_file_name = self.dest_path() + "/" + PERFECT_SEED_FILE
  56. seed_file_name = os.path.normpath(seed_file_name)
  57. file = open(seed_file_name, "w")
  58. file.write(str(self.m_compiler.perfect_seed()))
  59. file.close()
  60. # Compiles the resources into the destination folder
  61. def compile(self):
  62. # Scan the repository
  63. print("Scanning folder:", self.m_repository.root_path())
  64. self.m_repository.scan()
  65. print("Resources:", len(self.m_repository.all_resources()))
  66. # Create the output folder
  67. print("Creating destination folder:", self.dest_path())
  68. self.create_dest_folder()
  69. print("Compiling resources...")
  70. self.m_compiler.compile_all()
  71. print("Writing perfect seed...")
  72. self.write_perfect_seed()
  73. print("Done.")
  74. #------------------------------------------------------------------------------
  75. def main():
  76. root_path = ""
  77. if (len(sys.argv) != 3):
  78. print("Usage: resource-compiler <root-path> <platform-name>")
  79. sys.exit(-1)
  80. root_path = sys.argv[1];
  81. platform_name = sys.argv[2];
  82. root_path = os.path.abspath(root_path)
  83. if not os.path.exists(root_path):
  84. print("The path does not exist.")
  85. sys.exit(-1)
  86. if (os.path.islink(root_path)):
  87. print("The path is a symbolic link.")
  88. sys.exit(-1)
  89. if not os.path.isdir(root_path):
  90. print("The path has to be a directory.")
  91. sys.exit(-1)
  92. helper = CompilerHelper(root_path, platform_name)
  93. helper.compile()
  94. main()