# -*- coding:utf-8 -*- import time import sys import os import json import subprocess ''' 配置全局变量 ''' JSONFILE = None SHELLFILE = None print(sys.argv[0]) # sys.argv[0] 类似于shell中的$0,但不是脚本名称,而是脚本的路径 if len(sys.argv) == 2: JSONFILE = sys.argv[1] # 表示Json参数文件路径; if len(sys.argv) == 3: SHELLFILE = sys.argv[2] # 表示Shell脚本文件路径; # 输入开始时间; print("Start",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 源码路径 CODE_DIR = None # 脚本名称; SCRIPT_NAME = None # 编译前的参数 PREV_ARG = None # 脚本参数 SCRIPT_ARG = None # 编译后的参数 AFTER_ARG = None # 源码目录名称; CODE_FOLDER_NAME = None # 解析json文件; def loadJson(): global CODE_DIR global PREV_ARG global JSONFILE global SCRIPT_ARG global AFTER_ARG global SCRIPT_NAME global CODE_FOLDER_NAME print(u"loadJson") if JSONFILE is None or JSONFILE.__len__() == 0: print(u"use default josn arg") JSONFILE=sys.path[0]+"/arg.json" #JSONFILE = "arg.json" if os.path.exists(JSONFILE) is False: print(u"json file doesn't exist") return False try: data = open(JSONFILE, 'r') args = json.load(data) except Exception as e: print("An error occurred:"+e) return False print(args['pre-compile']) print(args['after-compile']) print(args['compile']) CODE_DIR = args['code-dir'] CODE_FOLDER_NAME = CODE_DIR.split('/')[-1] SCRIPT_NAME = args['compile-script'] PREV_ARG = args['pre-compile'] SCRIPT_ARG = args['compile'] AFTER_ARG = args['after-compile'] return True # 不使用重定向; def cmdExecute_NoRidrect(cmd, dir=None): print(str.format("cmd start:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) proc = subprocess.Popen(cmd, shell=True, cwd=dir) # 等待完成; proc.wait() print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) # 重定向标准输出、标准错误; def cmdExecute_Ridrect(cmd, dir=None): print(str.format("cmd start:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=dir) # 等待完成; stdout,stderr = proc.communicate() if proc.returncode == 0: print(u'cmd finished') # 返回执行结果; print("stdout=",stdout) print("stderr=",stderr) print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) return stdout def prev_compile(): global PREV_ARG global CODE_DIR print("prev_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) if PREV_ARG is None or PREV_ARG.__len__() == 0: print("prev args is empty") return False # Python无法将字符串'false'转为布尔类型; if 'clean' in PREV_ARG: if type(PREV_ARG['clean']) == type(u'str'): PREV_ARG['clean'] = True if PREV_ARG['clean'].lower() == u'true' else False print(type(PREV_ARG['clean']), PREV_ARG['clean']) if 'sync' in PREV_ARG: if type(PREV_ARG['sync']) == type(u'str'): PREV_ARG['sync'] = True if PREV_ARG['sync'].lower() == u'true' else False print(type(PREV_ARG['sync']), PREV_ARG['sync']) if 'redownload' in PREV_ARG: if type(PREV_ARG['redownload']) == type(u'str'): PREV_ARG['redownload'] = True if PREV_ARG['redownload'].lower() == u'true' else False print(type(PREV_ARG['redownload']), PREV_ARG['redownload']) # 更新代码; commands=[] commands.append(str.format("cd %s") % CODE_DIR) commands.append("rm -rf Target") # Java创建会话终端(导入了所有环境变量),无须再export,解决repo:commnand not found的问题; #commands.append("export PATH=$(echo $PATH)") if 'clean' in PREV_ARG and PREV_ARG['clean']: print("enable clean") commands.append("repo forall -c \"pwd &&git clean -xfd && git checkout .\"") # 更新代码; if 'sync' in PREV_ARG and PREV_ARG['sync']: print("enable sync") commands.append("repo sync") # 重新下载; if 'redownload' in PREV_ARG and PREV_ARG['redownload']: print("enable redownload") commands.append(str.format("rm -rf !(%s|%s|%s|%s)") % ("builder.py", "arg.json", SCRIPT_NAME, "ftp.py")) commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr'])) commands.append("repo sync") # 执行所有命令; commandline=';'.join(commands) cmdExecute_Ridrect(commandline) def call_script(): global SCRIPT_ARG print("call_script", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) if SCRIPT_ARG is None or SCRIPT_ARG.__len__() == 0: print("script args is empty") return False # 生成命令行参数; commandline = '' for item in SCRIPT_ARG: line = str.format("%s=%s ") % (item, SCRIPT_ARG[item]) commandline = commandline + line print("script args is:"+commandline) if commandline.__len__() == 0 or commandline.index("=") == -1: print("commandline is empty or format error!") return False print("start to compile") # 调用编译脚本时,不需要重定向,否则无法生成日志; cmdExecute_NoRidrect(str.format("cd %s;chmod 777 %s;./%s %s")%(CODE_DIR,SCRIPT_NAME,SCRIPT_NAME,commandline), CODE_DIR) return True def after_compile(): global AFTER_ARG print("after_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) if AFTER_ARG is None or AFTER_ARG.__len__() == 0: print("after args is empty") return False if 'Scp2OtherServer' in AFTER_ARG: if type(AFTER_ARG['Scp2OtherServer']) == type(u'str'): AFTER_ARG['Scp2OtherServer'] = True if AFTER_ARG['Scp2OtherServer'].lower() == u'true' else False print(type(AFTER_ARG['Scp2OtherServer']), AFTER_ARG['Scp2OtherServer']) # 上传到其他服务器; if 'Scp2OtherServer' in AFTER_ARG and AFTER_ARG['Scp2OtherServer']: commands=[] commands.append(str.format("cd %s/Target/") % CODE_DIR) commands.append("cd $(ls|grep V8)") commands.append("pwd") commands.append("ls") # 创建目录; ScpDir=AFTER_ARG['ScpServerPath']+time.strftime("%Y%m%d%H%M%S") commands.append(str.format('ssh %s@%s "[ -d %s ] && echo ok || mkdir -p %s"') % (AFTER_ARG['ScpUserName'], AFTER_ARG['ScpServerName'], ScpDir, ScpDir)) commands.append(str.format("scp -r OTA %s@%s:%s") % (AFTER_ARG['ScpUserName'], AFTER_ARG['ScpServerName'], ScpDir)) commands.append(str.format("scp -r Reports/* %s@%s:%s") % (AFTER_ARG['ScpUserName'], AFTER_ARG['ScpServerName'], ScpDir)) commands.append(str.format("scp -r USB/* %s@%s:%s") % (AFTER_ARG['ScpUserName'], AFTER_ARG['ScpServerName'], ScpDir)) # 执行所有命令; commandline=';'.join(commands) cmdExecute_Ridrect(commandline) if __name__ == "__main__": # os.system('cd ~/2851M; ls -al; repo sync') # 可用命令格式; # subprocess.call('cd ~;ls -al;cd ~/2851M; ls -al; repo sync', shell=True) # subprocess.call(["cd ~", "ls -al","cd ~/2851M", "ls -al", "repo sync"], shell=False) 错误的执行方式; ret = loadJson() if ret is True: prev_compile() call_script() after_compile() else: print(u"doesn't build")