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