builder.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Python无法将字符串'false'转为布尔类型;
  86. if type(PREV_ARG['sync']) == type(u'str'):
  87. PREV_ARG['sync'] = True if PREV_ARG['sync'].lower() == u'true' else False
  88. if type(PREV_ARG['redownload']) == type(u'str'):
  89. PREV_ARG['redownload'] = True if PREV_ARG['redownload'].lower() == u'true' else False
  90. print(type(PREV_ARG['sync']), PREV_ARG['sync'])
  91. print(type(PREV_ARG['redownload']), PREV_ARG['redownload'])
  92. # 更新代码;
  93. commands=None
  94. if PREV_ARG['sync']:
  95. commands = str.format("cd %s;repo sync") % CODE_DIR
  96. cmdExecute(commands, CODE_DIR)
  97. # 重新下载;
  98. commands=[]
  99. if PREV_ARG['redownload']:
  100. commands.append(str.format("rm -rf %s") % CODE_DIR)
  101. commands.append(str.format("cd %s") % os.getcwd())
  102. commands.append(str.format("mkdir %s") % CODE_FOLDER_NAME)
  103. commands.append(str.format("cd %s") % CODE_DIR)
  104. commands.append(str.format("repo init -u %s") % (PREV_ARG['branch-addr']))
  105. commands.append("repo sync")
  106. commandline=';'.join(commands)
  107. cmdExecute(commandline)
  108. def call_script():
  109. global SCRIPT_ARG
  110. if SCRIPT_ARG is None or SCRIPT_ARG.__len__() == 0:
  111. print("script args is empty")
  112. return False
  113. # 生成命令行参数;
  114. commandline = ''
  115. for item in SCRIPT_ARG:
  116. line = str.format("%s=%s ") % (item, SCRIPT_ARG[item])
  117. commandline = commandline + line
  118. print("script args is:"+commandline)
  119. if commandline.__len__() == 0 or commandline.index("=") == -1:
  120. print("commandline is empty or format error!")
  121. return False
  122. print("start to compile")
  123. cmdExecute(str.format("cd %s;chmod 777 %s;./%s %s")%(CODE_DIR,SCRIPT_NAME,SCRIPT_NAME,commandline), CODE_DIR)
  124. return True
  125. def after_compile():
  126. global AFTER_ARG
  127. if AFTER_ARG is None or AFTER_ARG.__len__() == 0:
  128. print("after args is empty")
  129. return False
  130. if __name__ == "__main__":
  131. # os.system('cd ~/2851M; ls -al; repo sync') # 可用命令格式;
  132. # subprocess.call('cd ~;ls -al;cd ~/2851M; ls -al; repo sync', shell=True)
  133. # subprocess.call(["cd ~", "ls -al","cd ~/2851M", "ls -al", "repo sync"], shell=False) 错误的执行方式;
  134. ret = loadJson()
  135. if ret is True:
  136. prev_compile()
  137. call_script()
  138. after_compile()
  139. else:
  140. print(u"doesn't build")