gen_source.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in
  13. # the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Intel Corporation nor the names of its
  16. # contributors may be used to endorse or promote products derived
  17. # from this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #
  31. #
  32. # This script is used to copy file from src to dest as descript in a BOM file
  33. # example: python gen_source.py --bom=PATH_TO_BOM [--cleanup=false] [--deliverydir=DIR_RELATIVE_TO_gen_source.py]
  34. #
  35. import sys
  36. import os
  37. import shutil
  38. import getopt
  39. def parse_cmd(argc, argv):
  40. global bom_file
  41. global cleanup
  42. global deliverydir
  43. bom_file = ""
  44. cleanup = True
  45. deliverydir = ""
  46. local_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
  47. try:
  48. opts, args = getopt.getopt(sys.argv[1:], "", ["bom=", "cleanup=", "deliverydir="])
  49. except getopt.GetoptError as err:
  50. print str(err)
  51. return False
  52. for option, value in opts:
  53. if option == "--bom":
  54. bom_file = value
  55. if not os.path.exists(os.path.join(local_path, bom_file)):
  56. return False
  57. if option == "--cleanup":
  58. cleanup = value
  59. if cleanup.lower() != "true" and cleanup.lower() != "false":
  60. return False
  61. if cleanup.lower() == "false":
  62. cleanup = False
  63. if option == "--deliverydir":
  64. deliverydir = value
  65. if bom_file == "":
  66. return False
  67. return True
  68. ################################################################################
  69. # Name: copy_folder_unrecursively() #
  70. # Usage: #
  71. # return: True if the operation is successful. False otherwise. #
  72. ################################################################################
  73. def copy_folder_unrecursively(src_dir, dest_dir):
  74. if os.path.isdir(src_dir) == True :
  75. print "Warning: The src is a fold ......"
  76. return False
  77. #print "copy" + src_dir + "->" + dest_dir
  78. try:
  79. shutil.copy(src_dir, dest_dir)
  80. return True
  81. except:
  82. print "Error !!!: can't find file:" + src_dir
  83. return False
  84. ################################################################################
  85. # Name: copy_folder_recursively() #
  86. # Usage: #
  87. ################################################################################
  88. def copy_folder_recursively(src_dir, dest_dir):
  89. if os.path.isdir(src_dir) == True:
  90. #copy the folder
  91. if os.path.exists(dest_dir) == False:
  92. os.makedirs(dest_dir)
  93. for item in os.listdir(src_dir):
  94. #print (os.path.join(src_dir, item)).replace("\\", "/")
  95. copy_folder_recursively((os.path.join(src_dir, item)).replace("\\", "/"), (os.path.join(dest_dir, item)).replace("\\", "/"))
  96. continue
  97. else:
  98. #copy the file
  99. print "copy file from " + src_dir + " to " + dest_dir
  100. shutil.copyfile(src_dir, dest_dir)
  101. #if os.path.isdir(src_dir) == False or os.path.isdir(dest_dir) == False:
  102. # print "Bom file error at copying " + src_dir + " to " + dest_dir
  103. # exit(-1)
  104. ################################################################################
  105. # Name: read_BOM() #
  106. # Usage: read bom_file #
  107. # return: the contents in bom_file #
  108. ################################################################################
  109. def read_BOM(local_path):
  110. #get the BOM file path that need to open
  111. file_list = local_path + "/" + bom_file
  112. f = open(file_list, 'r')
  113. #read the content in BOM file
  114. lines = f.readlines()
  115. f.close()
  116. return lines
  117. ################################################################################
  118. # Name: copy_files() #
  119. # Usage: copy file from src to dest #
  120. # return: #
  121. ################################################################################
  122. def copy_txt_files(local_path):
  123. home_path = os.path.realpath(os.path.join(local_path, "..", "..", "..", ".."))
  124. #read BOM file contents
  125. lines = read_BOM(local_path)
  126. for line in lines[1:]:
  127. if line == "\n":
  128. continue
  129. src = line.split('\t')[0]
  130. dest = line.split('\t')[1]
  131. src = src.replace("\\", "/")
  132. dest = dest.replace("\\", "/")
  133. if deliverydir == "":
  134. src = src.replace("<deliverydir>/", home_path + "/")
  135. else:
  136. src = src.replace("<deliverydir>/", deliverydir + "/")
  137. dest = dest.replace("<installdir>/", local_path + "/output/")
  138. #print "copy folder from " + src + " to " + dest
  139. if os.path.exists(src) == True:
  140. #check whether the src is a fold or file
  141. if os.path.isdir(src) == False :
  142. #the src is a file
  143. if os.path.exists(os.path.dirname(dest)) == False:
  144. os.makedirs(os.path.dirname(dest))
  145. ret = copy_folder_unrecursively(src, dest)
  146. if ret == False:
  147. break
  148. else:
  149. #the src is a fold
  150. copy_folder_recursively(src, dest)
  151. else:
  152. #although the src file isn't exist, create the dest folder
  153. if os.path.exists(os.path.dirname(dest)) == False:
  154. os.makedirs(os.path.dirname(dest))
  155. print "Error !!!: src folder not exist" + src
  156. if __name__ == "__main__":
  157. ret = parse_cmd(len(sys.argv), sys.argv)
  158. if ret == False:
  159. print "Usage:"
  160. print "python gen_source.py --bom=PATH_TO_BOM [--cleanup=false] [--deliverydir=DIR_RELATIVE_TO_gen_source.py]"
  161. exit(1)
  162. #script locate direction
  163. local_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
  164. local_dir = os.path.basename(local_path)
  165. local_path = local_path.replace("\\", "/")
  166. ret = os.path.exists(local_path + "/output")
  167. if ret == True:
  168. if cleanup == True:
  169. print "clean the dest dir"
  170. shutil.rmtree(local_path + "/output")
  171. os.mkdir(local_path + "/output")
  172. else:
  173. print "Create the dest dir"
  174. os.mkdir(local_path + "/output")
  175. #cpoy the files
  176. copy_txt_files(local_path)
  177. print "Copy files finished ......"
  178. exit(0)