hab minimale Veränderungen gemacht(Autor namens tim66613 hinzugefügt, encoding hinzugefügt, main function verändert) und das ganze mal kommentiert, nachdem hier einige noch die Fragen hatten was das ganze denn überhaupt machen würde.
PHP Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from itertools import chain
import sys
import os
import shutil
__author__ = 'tim66613'
# Simple class holding the info from the locale/*/map/index file
class AllowedMapList:
def __init__(self):
# initializes name_to_index_map dictionary ( dict() is equal to {} )
self.name_to_index_map = dict()
"""
used for loading our index file
"""
def LoadMapIndexFile(self, filename):
# using the opening process of our index file as file var
with open(filename) as file:
# run loop for every line in the opened file
for line in file:
# using spaces to split the map index and the map name apart(strip to remove f.e. \n)
index, name = line.strip().split()
# if our map name is already in the dict, append it to our array
if name in self.name_to_index_map:
self.name_to_index_map[name].append(index)
# else create sub variable in our dictionary and add an single element array ( [index] )
else:
self.name_to_index_map[name] = [index]
"""
used for getting our index vars from the created dictionary
"""
def GetIndexListFromNameList(self, names):
# not as easy to explain as the other script lines, here equal, but more inefficient script to explain it:
# initializing our tmp variable(empty array)
#tmp = []
# run the loop for every element in our names array
#for name in names:
# because the map can have multiple index numbers, using " ".join to get them in plain text and append them to our array
# tmp.append(" ".join(self.name_to_index_map[name]))
# return our created array
#return(tmp)
return [" ".join(self.name_to_index_map[name]) for name in names]
class TreeCreator:
"""
basic init function, automatically called if class is called
"""
def __init__(self):
# using our class we explained before as variable
self.allowed_map_list = AllowedMapList()
# initialize every other variable(defined later)
self.output_path = None
self.template_path = None
self.config_dict = None
"""
used for our configuration script
"""
def LoadConfigScript(self, filename):
# again equal to {}
self.config_dict = dict()
# execute our configuration file, to set variables, in this case the file "trunk_server_conf.py" and safe it in self.config_dict
execfile(filename, self.config_dict)
# define the variables we initialized in the init function with the dict we created from the configuration file
self.output_path = self.config_dict["kOutputPath"]
self.template_path = self.config_dict["kTemplatePath"]
# loading the index file(until now it was barely an unused class)
self.allowed_map_list.LoadMapIndexFile(self.config_dict["kMapIndexPath"])
# copy process
try:
shutil.copytree(self.template_path + "/base", self.output_path)
# exception if the path already exists, so remove it(can fail here again if python is using the path already(unhandled exception in this case))
except os.error:
print "Output directory already exists. Erasing..."
# remove path, exception case is missing
shutil.rmtree(self.output_path)
# create again
shutil.copytree(self.template_path + "/base", self.output_path)
# Channels
# run for every channel in our config_dict, the channel config var
# only used for writing the basic files, you should be able to know what this part is writing ;)
for channel in self.config_dict["kChannelConfig"]:
os.mkdir(self.output_path + "/" + channel)
for game in self.config_dict["kChannelConfig"][channel]:
shutil.copytree(self.template_path + "/game", self.output_path + "/" + channel + "/" + game)
merged_conf = dict(chain(self.config_dict["kBaseConfig"]["game"].iteritems(), self.config_dict["kChannelConfig"][channel][game].iteritems()))
open(self.output_path + "/" + channel + "/" + game + "/CONFIG", "wb").write(self.GetConfigString(merged_conf, self.ProcessGameConfigEntry))
# Special configs
merged_conf = dict(chain(self.config_dict["kBaseConfig"]["game"].iteritems(), self.config_dict["kBaseConfig"]["game99"].iteritems()))
open(self.output_path + "/game99/CONFIG", "wb").write(self.GetConfigString(merged_conf, self.ProcessGameConfigEntry))
open(self.output_path + "/auth/CONFIG", "wb").write(self.GetConfigString(self.config_dict["kBaseConfig"]["auth"], self.ProcessGameConfigEntry))
open(self.output_path + "/db/conf.txt", "wb").write(self.GetConfigString(self.config_dict["kBaseConfig"]["db"], self.ProcessDbConfigEntry))
# Script files
install_sh = open(self.output_path + "/install.sh", "ab")
run_sh = open(self.output_path + "/run.sh", "ab")
shutdown_sh = open(self.output_path + "/shutdown.sh", "ab")
clear_sh = open(self.output_path + "/clear.sh", "ab")
for channel in self.config_dict["kChannelConfig"]:
for game in self.config_dict["kChannelConfig"][channel]:
install_sh.write("cd $ROOT/%s && sh install.sh\n" % (channel + "/" + game))
run_sh.write("echo \"Starting %s\"\ncd $ROOT/%s && sh run.sh\n\n" % (channel + "/" + game, channel + "/" + game))
shutdown_sh.write("echo \"Stopping %s\"\ncd $ROOT/%s && sh shutdown.sh\n\n" % (channel + "/" + game, channel + "/" + game))
clear_sh.write("cd $ROOT/%s && sh clear.sh\n" % (channel + "/" + game))
install_sh.write("cd $ROOT/%s && sh install.sh\n" % ("game99"))
run_sh.write("echo \"Starting %s\"\ncd $ROOT/%s && sh run.sh\nsleep 2\n\n" % ("game99", "game99"))
shutdown_sh.write("echo \"Stopping %s\"\ncd $ROOT/%s && sh shutdown.sh\n\n" % ("game99", "game99"))
shutdown_sh.write("echo \"Stopping %s\"\ncd $ROOT/%s && sh shutdown.sh\n\n" % ("db", "db"))
clear_sh.write("cd $ROOT/%s && sh clear.sh\n" % ("game99"))
"""
function used to get the configuration as string
"""
def GetConfigString(self, list, fn):
# create array from our list with every key
conf_entries = [fn(key, list[key]) for key in list]
# sort it based on the key(kv.split(":")[0].lower()), basic sort
conf_entries.sort(key = lambda kv: kv.split(":")[0].lower())
# return as plain text, using "\n".join(conf_entries)
return '\n'.join(conf_entries)
"""
function used to get the proper db config entry
"""
def ProcessDbConfigEntry(self, key, value):
# simple if check, if value(!= 0, != False, != null, != "", != " ") and not key == "ITEM_ID_RANGE
if not value or (value.find(' ') != -1 and key != "ITEM_ID_RANGE"):
return key + " = \"" + value + "\""
# else case
else:
return key + " = " + value
"""
function used to get the proper game config entry
"""
def ProcessGameConfigEntry(self, key, value):
# check the key
if key == "MAP_ALLOW":
# check if value != > 32(max number of allowed maps for every core)
if len(value) > 32:
print value
print "MAP_ALLOW has too many values!"
else:
# using the other function the get a plain text string using for our map allow
return "MAP_ALLOW: " + " ".join(self.allowed_map_list.GetIndexListFromNameList(value))
else:
# else case
return key + ": " + value
"""
main function
"""
def main():
creator = TreeCreator()
creator.LoadConfigScript("trunk_server_conf.py")
if __name__ == "__main__":
main()