builder.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  69. def cmdExecute(cmd, dir=None):
  70. print(str.format("cmd start:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  71. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=dir)
  72. # 等待完成;
  73. stdout,stderr = proc.communicate()
  74. if proc.returncode == 0:
  75. print(u'cmd finished')
  76. # 返回执行结果;
  77. print(stdout)
  78. print(str.format("cmd end:%s, %s") % (cmd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
  79. return stdout
  80. def prev_compile():
  81. global PREV_ARG
  82. global CODE_DIR
  83. print("prev_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  84. if PREV_ARG is None or PREV_ARG.__len__() == 0:
  85. print("prev args is empty")
  86. return False
  87. # Python无法将字符串'false'转为布尔类型;
  88. if type(PREV_ARG['sync']) == type(u'str'):
  89. PREV_ARG['sync'] = True if PREV_ARG['sync'].lower() == u'true' else False
  90. if type(PREV_ARG['redownload']) == type(u'str'):
  91. PREV_ARG['redownload'] = True if PREV_ARG['redownload'].lower() == u'true' else False
  92. print(type(PREV_ARG['sync']), PREV_ARG['sync'])
  93. print(type(PREV_ARG['redownload']), PREV_ARG['redownload'])
  94. # 更新代码;
  95. commands=None
  96. if 'sync' in PREV_ARG and PREV_ARG['sync']:
  97. print("start sync")
  98. commands = str.format("cd %s;repo sync") % CODE_DIR
  99. cmdExecute(commands, CODE_DIR)
  100. # 重新下载;
  101. commands=[]
  102. if PREV_ARG['redownload']:
  103. print("start redownload")
  104. commands.append(str.format("cd %s") % CODE_DIR)
  105. commands.append(str.format("rm -rf !(%s|%s|%s|%s)") % ("builder.py", "arg.json", "51M-CompileScript.sh", "ftp.py"))
  106. # 已在编译路径中,无须处理;
  107. #commands.append(str.format("cd %s") % os.getcwd())
  108. #commands.append(str.format("mkdir %s") % CODE_FOLDER_NAME)
  109. #commands.append(str.format("cd %s") % CODE_DIR)
  110. commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr']))
  111. commands.append("repo sync")
  112. commandline=';'.join(commands)
  113. cmdExecute(commandline)
  114. def call_script():
  115. global SCRIPT_ARG
  116. print("call_script", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  117. if SCRIPT_ARG is None or SCRIPT_ARG.__len__() == 0:
  118. print("script args is empty")
  119. return False
  120. # 生成命令行参数;
  121. commandline = ''
  122. for item in SCRIPT_ARG:
  123. line = str.format("%s=%s ") % (item, SCRIPT_ARG[item])
  124. commandline = commandline + line
  125. print("script args is:"+commandline)
  126. if commandline.__len__() == 0 or commandline.index("=") == -1:
  127. print("commandline is empty or format error!")
  128. return False
  129. print("start to compile")
  130. # 调用编译脚本时,不需要重定向,否则无法生成日志;
  131. cmdExecute2(str.format("cd %s;chmod 777 %s;./%s %s")%(CODE_DIR,SCRIPT_NAME,SCRIPT_NAME,commandline), CODE_DIR)
  132. return True
  133. def after_compile():
  134. global AFTER_ARG
  135. print("after_compile", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  136. if AFTER_ARG is None or AFTER_ARG.__len__() == 0:
  137. print("after args is empty")
  138. return False
  139. if __name__ == "__main__":
  140. # os.system('cd ~/2851M; ls -al; repo sync') # 可用命令格式;
  141. # subprocess.call('cd ~;ls -al;cd ~/2851M; ls -al; repo sync', shell=True)
  142. # subprocess.call(["cd ~", "ls -al","cd ~/2851M", "ls -al", "repo sync"], shell=False) 错误的执行方式;
  143. ret = loadJson()
  144. if ret is True:
  145. prev_compile()
  146. call_script()
  147. after_compile()
  148. else:
  149. print(u"doesn't build")