利用PythonQT,定制服务器启动工具
在开发过程中经常需要修改游戏服务器的各种配置参数,以方便在冒烟环境,开发环境和预发布环境中进行调试。我们以前使用ini文件进行服务器配置,所以经常需要关掉服务器,修改ini文件,然后再重新把服务器拉起来。最近有点时间,就开始考虑做个服务器启动小工具,能方便服务器在不同环境下的部署。
开始想用QT来写这个小工具,但发现建立一个QT工程其实有点小麻烦,后来干脆使用PythonQT来做。一个100多行的小脚本就能满足需求,以后交给其他人扩展功能也很方便。
主要是想做两个功能
1:可以方便的对服务器在不同环境下进行部署
2:可以在服务器启动时设置系统时间,因为经常需要测试一些时间性的业务。比如元旦节的活动。以前都是通过改操作系统时间来测的,这样每台机器就只能启动一个服务器了,如果想测两个不同时间的业务,就要多找一台机器。
下面是代码,可以很简单的修改成自己需要的工具
#!/usr/bin/python
#-*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we create a skeleton
of a calculator using a QtGui.QGridLayout.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
import time
import threading
from PyQt4 import QtGui,QtCore
from xml.dom.minidom import parse,parseString
import os
version = u"服务器启动工具 V0.1"
class CommandThread(threading.Thread):
def setCommand(self,command):
self.command = command
def run(self):
os.system(self.command)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
label1 = QtGui.QLabel(self)
label1.setText(u'选择服务器执行文件')
label1.move(20,20)
label2 = QtGui.QLabel(self)
label2.setText(u'选择服务器启动配置')
label2.move(20,80)
label3 = QtGui.QLabel(self)
label3.setText(u'选择服务器启动时间')
label3.move(20,150)
self.label4 = QtGui.QLabel(self)
self.label4.setText(u'差异时间')
self.label4.move(120,365)
self.text_serverbin = u"未选择文件"
self.label_serverfilename = QtGui.QLabel(self)
self.label_serverfilename.setText(self.text_serverbin)
self.label_serverfilename.move(20,50)
btn_selserverbin = QtGui.QPushButton('...',self)
btn_selserverbin.setGeometry(270,45,20,20)
btn_selserverbin.clicked[bool].connect(self.event_selserverbin)
self.dlg_selserverbin = QtGui.QFileDialog(self)
#配置选择
combo_serverConfig = QtGui.QComboBox(self)
xml_file = "./Data/Config/ServerConfig.xml"
doc = parse(xml_file)
serverConfig = doc.getElementsByTagName("serverConfig")
for config in serverConfig:
descript = config.getAttribute("description")
index = config.getAttribute("index")
combo_serverConfig.addItem(descript,index)
combo_serverConfig.move(20,100)
combo_serverConfig.activated[int].connect(self.event_selserverconfig)
self.serverconfigindex = 0
#日历
timer=QtCore.QTimer(self)
timer.start(60000)
self.connect(timer,QtCore.SIGNAL("timeout()"),self.event_update)
self.cal_serverdate = QtGui.QCalendarWidget(self)
self.cal_serverdate.setGridVisible(True)
self.cal_serverdate.move(20, 180)
self.cal_serverdate.clicked[QtCore.QDate].connect(self.event_selserverdate)
self.seldate = QtCore.QDate.currentDate();
self.time_servertime = QtGui.QTimeEdit(self)
self.time_servertime.move(20, 360)
self.time_servertime.setTime(QtCore.QTime.currentTime())
self.time_servertime.timeChanged.connect(self.event_selservertime)
self.seltime = QtCore.QTime.currentTime();
btn_start = QtGui.QPushButton(u'启动服务器',self);
btn_start.move(180,420)
btn_start.clicked.connect(self.start_server)
btn_close = QtGui.QPushButton(u'退出',self);
btn_close.move(260,420)
btn_close.clicked.connect(self.close)
self.setGeometry(300, 300,350,450)
self.setWindowTitle(version)
self.show()
def event_selserverbin(self, clicked):
self.text_serverbin = self.dlg_selserverbin.getOpenFileName(self,
"Open EXE", "", "EXE Files (*.exe)");
self.label_serverfilename.setText(self.text_serverbin)
self.label_serverfilename.adjustSize()
def event_selserverconfig(self,selindex):
self.serverconfigindex = selindex
def event_selserverdate(self,date):
self.seldate = date
self.update_deltaTime()
def event_selservertime(self,time):
self.seltime = time
self.update_deltaTime()
def event_update(self):
self.time_servertime.setTime(QtCore.QTime.currentTime())
def update_deltaTime(self):
currentDateTime = QtCore.QDateTime()
currentDateTime.setDate(QtCore.QDate.currentDate());
currentDateTime.setTime(QtCore.QTime.currentTime());
selDateTime = QtCore.QDateTime()
selDateTime.setDate(self.seldate)
selDateTime.setTime(self.seltime)
self.label4.setText(u'差异时间: ' +
str(currentDateTime.secsTo(selDateTime)) + u'秒')
self.label4.adjustSize()
def start_server(self):
serverfilename = self.label_serverfilename.text()
serverconfigindex = self.serverconfigindex
currentDateTime = QtCore.QDateTime()
currentDateTime.setDate(QtCore.QDate.currentDate());
currentDateTime.setTime(QtCore.QTime.currentTime());
selDateTime = QtCore.QDateTime()
selDateTime.setDate(self.seldate)
selDateTime.setTime(self.seltime)
command = str(serverfilename)
command += ' -c ' + str(serverconfigindex)
command += ' -t ' + str(currentDateTime.secsTo(selDateTime))
# print command
commandTD = CommandThread()
commandTD.setDaemon(True)
commandTD.setCommand(command)
commandTD.start()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
上面的小工具在启动服务器时是阻塞式的,一旦服务器启动,小工具就会处于假死阶段,直到关闭服务器。
可以使用Thread库,建立一个线程来启动服务器即可。
最后使用py2exe ,把上述脚本编译成EXE后就可以在团队内部发布了。