resource-compiler.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. from pycrown import Repository
  28. from pycrown import Compiler
  29. PERFECT_SEED_FILE = "seed.ini"
  30. # Helper for compiling resources
  31. class CompilerHelper:
  32. def __init__(self, root_path, platform_name):
  33. self.m_root_path = root_path
  34. # Define destination path name
  35. dest = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + "_" + platform_name
  36. self.m_dest_path = os.path.normpath(dest)
  37. # Repository needs a root path
  38. self.m_repository = Repository.Repository(root_path)
  39. self.m_compiler = Compiler.Compiler(self.m_repository, self.m_dest_path)
  40. # Returns the root path where the resources are read from
  41. def root_path(self):
  42. return self.m_root_path
  43. # Returns the destination folder where the compiled resources
  44. # will be put
  45. def dest_path(self):
  46. return self.m_dest_path
  47. # Creates the destination folder
  48. def create_dest_folder(self):
  49. if not os.path.exists(self.m_dest_path):
  50. os.makedirs(self.m_dest_path)
  51. # Write the perfect seed into the destination folder
  52. def write_perfect_seed(self):
  53. seed_file_name = self.dest_path() + "/" + PERFECT_SEED_FILE
  54. seed_file_name = os.path.normpath(seed_file_name)
  55. file = open(seed_file_name, "w")
  56. file.write(str(self.m_compiler.perfect_seed()))
  57. file.close()
  58. # Compiles the resources into the destination folder
  59. def compile(self):
  60. # Scan the repository
  61. print("Scanning folder:", self.m_repository.root_path())
  62. self.m_repository.scan()
  63. print("Resources:", len(self.m_repository.all_resources()))
  64. # Create the output folder
  65. print("Creating destination folder:", self.dest_path())
  66. self.create_dest_folder()
  67. print("Compiling resources...")
  68. self.m_compiler.compile_all()
  69. print("Writing perfect seed...")
  70. self.write_perfect_seed()
  71. print("Done.")
  72. #------------------------------------------------------------------------------
  73. def main():
  74. root_path = ""
  75. if (len(sys.argv) != 3):
  76. print("Usage: resource-compiler <root-path> <platform-name>")
  77. sys.exit(-1)
  78. root_path = sys.argv[1];
  79. platform_name = sys.argv[2];
  80. root_path = os.path.abspath(root_path)
  81. if not os.path.exists(root_path):
  82. print("The path does not exist.")
  83. sys.exit(-1)
  84. if (os.path.islink(root_path)):
  85. print("The path is a symbolic link.")
  86. sys.exit(-1)
  87. if not os.path.isdir(root_path):
  88. print("The path has to be a directory.")
  89. sys.exit(-1)
  90. helper = CompilerHelper(root_path, platform_name)
  91. helper.compile()
  92. main()