Sfoglia il codice sorgente

Improve python resource compiler

Daniele Bartolini 12 anni fa
parent
commit
a9d47fb304
1 ha cambiato i file con 102 aggiunte e 34 eliminazioni
  1. 102 34
      tools/compilers/resource-compiler.py

+ 102 - 34
tools/compilers/resource-compiler.py

@@ -1,57 +1,125 @@
-#!/bin/python
-
+#!/usr/bin/python
+
+# Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+# Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+# 
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+import sys
 import os
 
 from crown.resources import Repository
 from crown.resources import Compiler
 
-root_path = "/home/daniele/resources"
+DEST_PATH_SUFFIX = "_compiled"
+PERFECT_SEED_FILE = "seed.ini"
 
-#------------------------------------------------------------------------------
-def create_output_folder(root_path):
-	output_path = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + "_compiled"
-	output_path = os.path.normpath(output_path)
+# Helper for compiling resources
+class CompilerHelper:
+	def __init__(self, root_path):
+		self.m_root_path = root_path
 
-	if not os.path.exists(output_path):
-		os.makedirs(output_path)
-		
-	return output_path
+		# Define destination path name
+		dest = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + DEST_PATH_SUFFIX
+		self.m_dest_path = os.path.normpath(dest)
 
-#------------------------------------------------------------------------------
-def write_perfect_seed(dest_path, perfect_seed):
-	output_path = dest_path + "/" + "seed.ini"
-	output_path = os.path.normpath(output_path)
+		# Repository needs a root path
+		self.m_repository = Repository.Repository(root_path)
+
+		self.m_compiler = Compiler.Compiler(self.m_repository, self.m_dest_path)
+
+	# Returns the root path where the resources are read from
+	def root_path(self):
+		return self.m_root_path
+
+	# Returns the destination folder where the compiled resources
+	# will be put
+	def dest_path(self):
+		return self.m_dest_path
+
+	# Creates the destination folder
+	def create_dest_folder(self):
+		if not os.path.exists(self.m_dest_path):
+			os.makedirs(self.m_dest_path)
 
-	file = open(output_path, "w");
-	file.write(str(perfect_seed))
-	file.close()
+	# Write the perfect seed into the destination folder
+	def write_perfect_seed(self):
+		seed_file_name = self.dest_path() + "/" + PERFECT_SEED_FILE
+		seed_file_name = os.path.normpath(seed_file_name)
 
-repository = Repository.Repository(root_path)
+		file = open(seed_file_name, "w")
+		file.write(str(self.m_compiler.perfect_seed()))
+		file.close()
+
+ 	# Compiles the resources into the destination folder
+	def compile(self):
+		# Scan the repository
+		print("Scanning folder:", self.m_repository.root_path())
+		self.m_repository.scan()
+		print("Resources:", len(self.m_repository.all_resources()))
+
+		# Create the output folder
+		print("Creating destination folder:", self.dest_path())
+		self.create_dest_folder()
+
+		print("Compiling resources...")
+		self.m_compiler.compile_all()
+
+		print("Writing perfect seed...")
+		self.write_perfect_seed()
+
+		print("Done.")
 
 #------------------------------------------------------------------------------
-print("Reading resources...")
-repository.scan()
+def print_help_message():
+	print("usage: resource-compiler <root-path>")
 
-print("Found", len(repository.all_resources()), "resources.")
+#------------------------------------------------------------------------------
+def main():
+	root_path = ""
 
-print("Creating output folder...")
-dest_path = create_output_folder(root_path)
+	if (len(sys.argv) != 2):
+		print("Usage: resource-compiler <root-path>")
+		sys.exit(-1)
 
-compiler = Compiler.Compiler(repository, dest_path)
-compiler.compute_perfect_seed()
+	root_path = sys.argv[1];
 
-print("Compiling resources...")
+	root_path = os.path.abspath(root_path)
 
-print("Perfect seed is: ", compiler.perfect_seed())
+	if not os.path.exists(root_path):
+		print("The path does not exist.")
+		sys.exit(-1)
 
-print("Writing resources...")
-compiler.compile_all()
+	if (os.path.islink(root_path)):
+		print("The path is a symbolic link.")
+		sys.exit(-1)
 
-print("Writing perfect seed...")
-write_perfect_seed(dest_path, compiler.perfect_seed())
+	if not os.path.isdir(root_path):
+		print("The path has to be a directory.")
+		sys.exit(-1)
 
-print("\n")
+	helper = CompilerHelper(root_path)
 
-print("Done.")
+	helper.compile()
 
+main()