# -*- 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脚本文件路径; # 源码路径 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 cmdExecute2(cmd,dir=os.getcwd()): # 定义文件名称; file_name = str(time.time()) proc = subprocess.Popen(cmd, shell=True, cwd=dir) # 等待完成; proc.wait() def cmdExecute(cmd, dir=None): print(cmd) 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) return stdout def prev_compile(): global PREV_ARG global CODE_DIR if PREV_ARG is None or PREV_ARG.__len__() == 0: print("prev args is empty") return False # 更新代码; commands=None if PREV_ARG['sync']: commands = str.format("cd %s;repo sync") % CODE_DIR cmdExecute(commands, CODE_DIR) # 重新下载; commands=[] if PREV_ARG['redownload']: commands.append(str.format("rm -rf %s") % CODE_DIR) commands.append(str.format("cd %s") % os.getcwd()) commands.append(str.format("mkdir %s") % CODE_FOLDER_NAME) commands.append(str.format("cd %s" % CODE_DIR)) commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr'])) commands.append("repo sync") commandline=';'.join(commands) cmdExecute(commandline) def call_script(): global SCRIPT_ARG 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(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 if AFTER_ARG is None or AFTER_ARG.__len__() == 0: print("after args is empty") return False 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")