builder.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding:utf-8 -*-
  2. import time
  3. import sys
  4. import os
  5. import json
  6. import subprocess
  7. '''
  8. 配置全局变量
  9. '''
  10. JSONFILE = None
  11. SHELLFILE = None
  12. print(sys.argv[0]) # sys.argv[0] 类似于shell中的$0,但不是脚本名称,而是脚本的路径
  13. if len(sys.argv) == 2:
  14. JSONFILE = sys.argv[1] # 表示Json参数文件路径;
  15. if len(sys.argv) == 3:
  16. SHELLFILE = sys.argv[2] # 表示Shell脚本文件路径;
  17. # 输入开始时间;
  18. print("Start",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  19. # 源码路径
  20. CODE_DIR = None
  21. # 脚本名称;
  22. SCRIPT_NAME = None
  23. # 编译前的参数
  24. PREV_ARG = None
  25. # 脚本参数
  26. SCRIPT_ARG = None
  27. # 编译后的参数
  28. AFTER_ARG = None
  29. # 源码目录名称;
  30. CODE_FOLDER_NAME = None
  31. # 解析json文件;
  32. def loadJson():
  33. global CODE_DIR
  34. global PREV_ARG
  35. global JSONFILE
  36. global SCRIPT_ARG
  37. global AFTER_ARG
  38. global SCRIPT_NAME
  39. global CODE_FOLDER_NAME
  40. print(u"loadJson")
  41. if JSONFILE is None or JSONFILE.__len__() == 0:
  42. print(u"use default josn arg")
  43. JSONFILE=sys.path[0]+"/arg.json"
  44. #JSONFILE = "arg.json"
  45. if os.path.exists(JSONFILE) is False:
  46. print(u"json file doesn't exist")
  47. return False
  48. try:
  49. data = open(JSONFILE, 'r')
  50. args = json.load(data)
  51. except Exception as e:
  52. print("An error occurred:"+e)
  53. return False
  54. print(args['pre-compile'])
  55. print(args['after-compile'])
  56. print(args['compile'])
  57. CODE_DIR = args['code-dir']
  58. CODE_FOLDER_NAME = CODE_DIR.split('/')[-1]
  59. SCRIPT_NAME = args['compile-script']
  60. PREV_ARG = args['pre-compile']
  61. SCRIPT_ARG = args['compile']
  62. AFTER_ARG = args['after-compile']
  63. return True
  64. # 不使用重定向;
  65. def cmdExecute2(cmd, dir=None):
  66. print(str.format("cmd start:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  67. proc = subprocess.Popen(cmd, shell=True, cwd=dir)
  68. # 等待完成;
  69. proc.wait()
  70. print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  71. def cmdExecute(cmd, dir=None):
  72. print(str.format("cmd start:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  73. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=dir)
  74. # 等待完成;
  75. stdout,stderr = proc.communicate()
  76. if proc.returncode == 0:
  77. print(u'cmd finished')
  78. # 返回执行结果;
  79. print(stdout)
  80. print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  81. return stdout
  82. def prev_compile():
  83. global PREV_ARG
  84. global CODE_DIR
  85. print("prev_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  86. if PREV_ARG is None or PREV_ARG.__len__() == 0:
  87. print("prev args is empty")
  88. return False
  89. # Python无法将字符串'false'转为布尔类型;
  90. if type(PREV_ARG['sync']) == type(u'str'):
  91. PREV_ARG['sync'] = True if PREV_ARG['sync'].lower() == u'true' else False
  92. if type(PREV_ARG['redownload']) == type(u'str'):
  93. PREV_ARG['redownload'] = True if PREV_ARG['redownload'].lower() == u'true' else False
  94. print(type(PREV_ARG['sync']), PREV_ARG['sync'])
  95. print(type(PREV_ARG['redownload']), PREV_ARG['redownload'])
  96. # 更新代码;
  97. commands=None
  98. if 'sync' in PREV_ARG and PREV_ARG['sync']:
  99. print("start sync")
  100. commands = str.format("cd %s;repo sync") % CODE_DIR
  101. cmdExecute(commands, CODE_DIR)
  102. # 重新下载;
  103. commands=[]
  104. if PREV_ARG['redownload']:
  105. print("start redownload")
  106. commands.append(str.format("cd %s") % CODE_DIR)
  107. commands.append(str.format("rm -rf !(%s|%s|%s|%s)") % ("builder.py", "arg.json", "51M-CompileScript.sh", "ftp.py"))
  108. # 已在编译路径中,无须处理;
  109. #commands.append(str.format("cd %s") % os.getcwd())
  110. #commands.append(str.format("mkdir %s") % CODE_FOLDER_NAME)
  111. #commands.append(str.format("cd %s") % CODE_DIR)
  112. commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr']))
  113. commands.append("repo sync")
  114. commandline=';'.join(commands)
  115. cmdExecute(commandline)
  116. def call_script():
  117. global SCRIPT_ARG
  118. print("call_script", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  119. if SCRIPT_ARG is None or SCRIPT_ARG.__len__() == 0:
  120. print("script args is empty")
  121. return False
  122. # 生成命令行参数;
  123. commandline = ''
  124. for item in SCRIPT_ARG:
  125. line = str.format("%s=%s ") % (item, SCRIPT_ARG[item])
  126. commandline = commandline + line
  127. print("script args is:"+commandline)
  128. if commandline.__len__() == 0 or commandline.index("=") == -1:
  129. print("commandline is empty or format error!")
  130. return False
  131. print("start to compile")
  132. # 调用编译脚本时,不需要重定向,否则无法生成日志;
  133. cmdExecute2(str.format("cd %s;chmod 777 %s;./%s %s")%(CODE_DIR,SCRIPT_NAME,SCRIPT_NAME,commandline), CODE_DIR)
  134. return True
  135. def after_compile():
  136. global AFTER_ARG
  137. print("after_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  138. if AFTER_ARG is None or AFTER_ARG.__len__() == 0:
  139. print("after args is empty")
  140. return False
  141. if __name__ == "__main__":
  142. # os.system('cd ~/2851M; ls -al; repo sync') # 可用命令格式;
  143. # subprocess.call('cd ~;ls -al;cd ~/2851M; ls -al; repo sync', shell=True)
  144. # subprocess.call(["cd ~", "ls -al","cd ~/2851M", "ls -al", "repo sync"], shell=False) 错误的执行方式;
  145. ret = loadJson()
  146. if ret is True:
  147. prev_compile()
  148. call_script()
  149. after_compile()
  150. else:
  151. print(u"doesn't build")