builder.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. CODE_DIR = None
  19. # 脚本名称;
  20. SCRIPT_NAME = None
  21. # 编译前的参数
  22. PREV_ARG = None
  23. # 脚本参数
  24. SCRIPT_ARG = None
  25. # 编译后的参数
  26. AFTER_ARG = None
  27. # 源码目录名称;
  28. CODE_FOLDER_NAME = None
  29. # 解析json文件;
  30. def loadJson():
  31. global CODE_DIR
  32. global PREV_ARG
  33. global JSONFILE
  34. global SCRIPT_ARG
  35. global AFTER_ARG
  36. global SCRIPT_NAME
  37. global CODE_FOLDER_NAME
  38. print(u"loadJson")
  39. if JSONFILE is None or JSONFILE.__len__() == 0:
  40. print(u"use default josn arg")
  41. JSONFILE=sys.path[0]+"/arg.json"
  42. #JSONFILE = "arg.json"
  43. if os.path.exists(JSONFILE) is False:
  44. print(u"json file doesn't exist")
  45. return False
  46. try:
  47. data = open(JSONFILE, 'r')
  48. args = json.load(data)
  49. except Exception as e:
  50. print("An error occurred:"+e)
  51. return False
  52. print(args['pre-compile'])
  53. print(args['after-compile'])
  54. print(args['compile'])
  55. CODE_DIR = args['code-dir']
  56. CODE_FOLDER_NAME = CODE_DIR.split('/')[-1]
  57. SCRIPT_NAME = args['compile-script']
  58. PREV_ARG = args['pre-compile']
  59. SCRIPT_ARG = args['compile']
  60. AFTER_ARG = args['after-compile']
  61. return True
  62. # 不使用重定向;
  63. def cmdExecute2(cmd,dir=os.getcwd()):
  64. # 定义文件名称;
  65. file_name = str(time.time())
  66. proc = subprocess.Popen(cmd, shell=True, cwd=dir)
  67. # 等待完成;
  68. proc.wait()
  69. def cmdExecute(cmd, dir=None):
  70. print(cmd)
  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. return stdout
  79. def prev_compile():
  80. global PREV_ARG
  81. global CODE_DIR
  82. if PREV_ARG is None or PREV_ARG.__len__() == 0:
  83. print("prev args is empty")
  84. return False
  85. # 更新代码;
  86. commands=None
  87. if PREV_ARG['sync']:
  88. commands = str.format("cd %s;repo sync") % CODE_DIR
  89. cmdExecute(commands, CODE_DIR)
  90. # 重新下载;
  91. commands=[]
  92. if PREV_ARG['redownload']:
  93. commands.append(str.format("rm -rf %s") % CODE_DIR)
  94. commands.append(str.format("cd %s") % os.getcwd())
  95. commands.append(str.format("mkdir %s") % CODE_FOLDER_NAME)
  96. commands.append(str.format("cd %s") % CODE_DIR)
  97. commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr']))
  98. commands.append("repo sync")
  99. commandline=';'.join(commands)
  100. cmdExecute(commandline)
  101. def call_script():
  102. global SCRIPT_ARG
  103. if SCRIPT_ARG is None or SCRIPT_ARG.__len__() == 0:
  104. print("script args is empty")
  105. return False
  106. # 生成命令行参数;
  107. commandline = ''
  108. for item in SCRIPT_ARG:
  109. line = str.format("%s=%s ") % (item, SCRIPT_ARG[item])
  110. commandline = commandline + line
  111. print("script args is:"+commandline)
  112. if commandline.__len__() == 0 or commandline.index("=") == -1:
  113. print("commandline is empty or format error!")
  114. return False
  115. print("start to compile")
  116. cmdExecute(str.format("cd %s;chmod 777 %s;./%s %s")%(CODE_DIR,SCRIPT_NAME,SCRIPT_NAME,commandline), CODE_DIR)
  117. return True
  118. def after_compile():
  119. global AFTER_ARG
  120. if AFTER_ARG is None or AFTER_ARG.__len__() == 0:
  121. print("after args is empty")
  122. return False
  123. if __name__ == "__main__":
  124. # os.system('cd ~/2851M; ls -al; repo sync') # 可用命令格式;
  125. # subprocess.call('cd ~;ls -al;cd ~/2851M; ls -al; repo sync', shell=True)
  126. # subprocess.call(["cd ~", "ls -al","cd ~/2851M", "ls -al", "repo sync"], shell=False) 错误的执行方式;
  127. ret = loadJson()
  128. if ret is True:
  129. prev_compile()
  130. call_script()
  131. after_compile()
  132. else:
  133. print(u"doesn't build")