记个python 脚本笔记,click命令行模块实例

午休时间撸了个超级低端的 python 脚本。。为了给策划 发个 exe 版本用。做个笔记

# -*- coding:utf-8 -*-

import yaml,os

yamlConfig = None;
currentPath = None;

def dOut(str):
	print(str)
	'''
	do nothing
	'''
# 读 yaml 路径配置
def initPath():
	print("init path ...")
	global yamlConfig;
	global currentPath;
	currentPath = os.path.abspath(os.path.dirname(__file__))
	# dOut(currentPath)
	cfgFilePath = currentPath + "\\publishall.yaml"
	# dOut(cfgFilePath)
	cfg = open(cfgFilePath);
	yamlConfig = yaml.load(cfg);
	# dOut(yamlConfig["vc_path"])
	print("init path OK")


# 更新 C
def pull_c_repository():
	print("pull_c_repository ...");
	global yamlConfig;
	theDir = yamlConfig["C_repository"];
	os.chdir(theDir);
	os.system("git pull");
	branchName = yamlConfig["c_branch"]
	os.system("git checkout "+branchName);
	print("c reposity branch:")
	os.system("git branch");
	print("pull_c_repository OK")

# 更新 lua
def pull_lua_repository():
	print("pull_lua_repository ...");
	global yamlConfig;
	theDir = yamlConfig["LUA_repository"];
	os.chdir(theDir);
	os.system("git pull");
	branchName = yamlConfig["lua_branch"]
	os.system("git checkout "+branchName);
	print("lua reposity branch:")
	os.system("git branch");
	print("pull_lua_repository OK")


# 更新 asset
def update_asset_repository():
	print("update_asset_repository ... ");
	global yamlConfig;
	theDir = yamlConfig["asset_path"];
	os.chdir(theDir);
	os.system("svn up");
	print("update_asset_repository OK ");

# 更新 config
def update_config_repository():
	print("update_config_repository ... ");
	global yamlConfig;
	theDir = yamlConfig["cfg_path"];
	os.chdir(theDir);
	os.system("svn up");
	print("update_config_repository OK ");


# 准备各种资源
def prepare_all_res():
	print("prepare_all_res ... ");
	os.system("mtl ccs -p -c");
	os.system("mtl res");
	os.system("mtl sres");
	os.system("mtl cfg");
	print("prepare_all_res OK ");

# build exe
def build_exe():
	print("build_exe ... ");	
	global yamlConfig;
	projectDir = yamlConfig["C_repository"]
	vcDir = yamlConfig["vc_path"];
	os.chdir(vcDir)
	#buildParam = projectDir + "runtime-src\proj.win32\dzm2.sln /build \"Release|Win32\""
	#buildParam = projectDir + "runtime-src\proj.win32\dzm2.sln /build \"Debug|Mixed Platform\""	# Mixed Platform有空格,语法不支持。。
	buildParam = projectDir + "runtime-src\proj.win32\dzm2.sln /build \"Debug|Win32\""
	os.system(buildCmd + " " + buildParam)
	print("build_exe OK ");	

# 拷贝到 目标目录之前 ,先 update 一次避免冲突
def pre_update():
	print("pre_update ... ");
	global currentPath
	os.chdir(currentPath)
	# print(os.getcwd())
	os.system("svn up");
	print("pre_update OK ");






def copy_res():

	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\asset")
	cmd = "xcopy /S /Y /D " + resPath + "asset " + currentPath + "\\asset\\"
	# print(cmd);
	os.system(cmd);

def copy_config():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\config")
	cmd = "xcopy /S /Y /D " + resPath + "config " + currentPath + "\\config\\"
	# print(cmd);
	os.system(cmd);	

def copy_script():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\script")
	cmd = "xcopy /S /Y /D " + resPath + "script " + currentPath + "\\script\\"
	# print(cmd);
	os.system(cmd);		


'''
删除目录
rd /S /Q E:\gameClient\script
rd /S /Q E:\gameClient\luac

拷贝 asset,config,lua, 其他 lua 文件
xcopy /S /Y /D svn\Resources\asset E:\gameClient\asset
xcopy /S /Y /D svn\Resources\config\*.lua E:\gameClient\config
xcopy /S /Y /D svn\Resources\lua_compile E:\gameClient\lua_compile
copy  svn\Resources\script\config.lua E:\gameClient\lua_compile /Y
copy  svn\Resources\script\dzm2\language\LanguageConfig_zhcn.lua E:\gameClient\lua_compile\dzm2\language /Y


拷贝 各种 dll
xcopy /S /Y /D runtime-src\proj.win32\Release.win32\*.dll E:\gameClient\
拷贝 exe
copy runtime-src\proj.win32\Release.win32\dzm2.exe E:\gameClient\dzm2.exe

# 清除所有 *.animxml
# rem del /S /Q *.animxml

'''
def copy_exe():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];	

	os.chdir(currentPath)
	os.system("del *.dll")

	# cmd = "xcopy /S /Y /D runtime-src\proj.win32\Release.win32\*.dll E:\gameClient\\"
	cmd = "xcopy /S /Y /D " + exePath + "*.dll " + currentPath + "\\"
	os.system(cmd)
	cmd = "copy " + exePath + "\dzm2.exe " + currentPath + "\dzm2.exe"
	os.system(cmd)

# copy exe libs res script static
def copy_all():
	print("copy_all ...")
	copy_res();
	copy_config();
	copy_script();
	copy_exe();
	print("copy_all OK");

# 把所有改动提交
def submitAll():
	print("submitAll ... ")
	'''
	e:
	cd g:/gameClient
	svn update
	svn add .  --force
	svn commit -m "+update pc client"
	'''

	global currentPath
	os.chdir(currentPath)
	# os.system("svn update");
	os.system("svn add . --force");
	os.system("svn commit -m '+update debug exe'");

	print("submitAll OK ")
	


initPath();
pull_c_repository();
pull_lua_repository();
update_asset_repository();
update_config_repository();
prepare_all_res();
build_exe();
pre_update();
copy_all();
copy_script()
submitAll();
print("-------- ALL JOB DONE -------")





下面这个 使用了 click 模块。click模块真是python 处理命令行神器

# -*- coding:utf-8 -*-

import yaml,os,click

yamlConfig = None;
currentPath = None;

def dOut(str):
	print(str)
	'''
	do nothing
	'''
# 读 yaml 路径配置
def initPath():
	print("init path ...")
	global yamlConfig;
	global currentPath;
	currentPath = os.path.abspath(os.path.dirname(__file__))
	# dOut(currentPath)
	cfgFilePath = currentPath + "\\pathsetting.yaml"
	# dOut(cfgFilePath)
	cfg = open(cfgFilePath);
	yamlConfig = yaml.load(cfg);
	# dOut(yamlConfig["vc_path"])
	print("init path OK")


def copy_res():

	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\asset")
	cmd = "xcopy /S /Y /D " + resPath + "asset " + currentPath + "\\asset\\"
	# print(cmd);
	os.system(cmd);

def copy_config():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\config")
	cmd = "xcopy /S /Y /D " + resPath + "config " + currentPath + "\\config\\"
	# print(cmd);
	os.system(cmd);	

def copy_script():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];

	os.chdir(currentPath)
	os.system("rd /S /Q " + currentPath + "\\script")
	cmd = "xcopy /S /Y /D " + resPath + "script " + currentPath + "\\script\\"
	# print(cmd);
	os.system(cmd);		

'''
删除目录
rd /S /Q E:\gameClient\script
rd /S /Q E:\gameClient\luac

拷贝 asset,config,lua, 其他 lua 文件
xcopy /S /Y /D svn\Resources\asset E:\gameClient\asset
xcopy /S /Y /D svn\Resources\config\*.lua E:\gameClient\config
xcopy /S /Y /D svn\Resources\lua_compile E:\gameClient\lua_compile
copy  svn\Resources\script\config.lua E:\gameClient\lua_compile /Y
copy  svn\Resources\script\dzm2\language\LanguageConfig_zhcn.lua E:\gameClient\lua_compile\dzm2\language /Y


拷贝 各种 dll
xcopy /S /Y /D runtime-src\proj.win32\Release.win32\*.dll E:\gameClient\
拷贝 exe
copy runtime-src\proj.win32\Release.win32\dzm2.exe E:\gameClient\dzm2.exe

# 清除所有 *.animxml
# rem del /S /Q *.animxml

'''
def copy_exe():
	global yamlConfig
	global currentPath
	exePath = yamlConfig["exe_path"];
	resPath = yamlConfig["rumtime_res_path"];	

	os.chdir(currentPath)
	# os.system("del *.dll")

	# cmd = "xcopy /S /Y /D runtime-src\proj.win32\Release.win32\*.dll E:\gameClient\\"
	cmd = "xcopy /S /Y /D " + exePath + "*.dll " + currentPath + "\\"
	os.system(cmd)
	cmd = "copy " + exePath + "\dzm2.exe " + currentPath + "\dzm2.exe"
	os.system(cmd)


def doUpdate():
	os.system("svn add * --force");
	os.system("svn commit -m \"+siege client\"")

@click.command()
# @click.option('-d/-nd', '--debug', default=False, help=u'debug模式')
# @click.option('-name', prompt='Your name', help='The person to greet.')
@click.option("-e/-ne","--exe",default = False,help=u"Shall copy exe")
@click.option("-r/-nr","--res",default = False,help=u"Shall copy res")
@click.option("-c/-nc","--cfg",default = False,help=u"Shall copy cfg")
@click.option("-l/-nl","--lua",default = False,help=u"Shall copy lua")
@click.option("-u/-nu","--up",default = False,help=u"Shall update change to svn")
@click.option("-a/-na","--all",default = False,help=u"Go all workflow")
def doJob(**options):
	# for key,value in options.items():
	# 	print(str(key) + ":" + str(value));

	initPath();
	if options["all"]:
		copy_exe();
		copy_res();
		copy_config();
		copy_script();
		doUpdate();		
	else:
		if options["exe"]:
			copy_exe();
		if options["res"]:
			copy_res();
		if options["cfg"]:
			copy_config();
		if options["lua"]:
			copy_script();
		if options["up"]:
			doUpdate();
	print("-------- JOB DONE -------")


doJob();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值