gen_source.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011-2018 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 folder ......")
  76. return False
  77. try:
  78. shutil.copy(src_dir, dest_dir)
  79. return True
  80. except:
  81. print ("Error !!!: can't find file: {}".format(src_dir))
  82. return False
  83. ################################################################################
  84. # Name: copy_folder_recursively() #
  85. # Usage: #
  86. ################################################################################
  87. def copy_folder_recursively(src_dir, dest_dir):
  88. if os.path.isdir(src_dir) == True:
  89. #copy the folder
  90. if os.path.exists(dest_dir) == False:
  91. os.makedirs(dest_dir)
  92. for item in os.listdir(src_dir):
  93. copy_folder_recursively((os.path.join(src_dir, item)).replace("\\", "/"), (os.path.join(dest_dir, item)).replace("\\", "/"))
  94. continue
  95. else:
  96. #copy the file
  97. print ("copy file from {0} to {1}".format(src_dir, dest_dir))
  98. shutil.copyfile(src_dir, dest_dir)
  99. #if os.path.isdir(src_dir) == False or os.path.isdir(dest_dir) == False:
  100. # print "Bom file error at copying " + src_dir + " to " + dest_dir
  101. # exit(-1)
  102. ################################################################################
  103. # Name: read_BOM() #
  104. # Usage: read bom_file #
  105. # return: the contents in bom_file #
  106. ################################################################################
  107. def read_BOM(local_path):
  108. #get the BOM file path that need to open
  109. file_list = local_path + "/" + bom_file
  110. f = open(file_list, 'r')
  111. #read the content in BOM file
  112. lines = f.readlines()
  113. f.close()
  114. return lines
  115. ################################################################################
  116. # Name: copy_files() #
  117. # Usage: copy file from src to dest #
  118. # return: #
  119. ################################################################################
  120. def copy_txt_files(local_path):
  121. home_path = os.path.realpath(os.path.join(local_path, "..", "..", "..", ".."))
  122. #read BOM file contents
  123. lines = read_BOM(local_path)
  124. for line in lines[1:]:
  125. if line == "\n":
  126. continue
  127. src = line.split('\t')[0]
  128. dest = line.split('\t')[1]
  129. src = src.replace("\\", "/")
  130. dest = dest.replace("\\", "/")
  131. if deliverydir == "":
  132. src = src.replace("<deliverydir>/", home_path + "/")
  133. else:
  134. src = src.replace("<deliverydir>/", deliverydir + "/")
  135. dest = dest.replace("<installdir>/", local_path + "/output/")
  136. if os.path.exists(src) == True:
  137. #check whether the src is a folder or file
  138. if os.path.isdir(src) == False :
  139. #the src is a file
  140. if os.path.exists(os.path.dirname(dest)) == False:
  141. os.makedirs(os.path.dirname(dest))
  142. ret = copy_folder_unrecursively(src, dest)
  143. if ret == False:
  144. exit(1)
  145. else:
  146. #the src is a folder
  147. copy_folder_recursively(src, dest)
  148. else:
  149. #although the src file isn't exist, create the dest folder
  150. if os.path.exists(os.path.dirname(dest)) == False:
  151. os.makedirs(os.path.dirname(dest))
  152. if os.path.isdir(src) == False :
  153. print ("Error !!!: src file not exist {}".format(src))
  154. else:
  155. print ("Error !!!: src folder not exist {}".format(src))
  156. exit(1)
  157. if __name__ == "__main__":
  158. ret = parse_cmd(len(sys.argv), sys.argv)
  159. if ret == False:
  160. print ("Usage:")
  161. print ("python gen_source.py --bom=PATH_TO_BOM [--cleanup=false] [--deliverydir=DIR_RELATIVE_TO_gen_source.py]")
  162. exit(1)
  163. #script locate direction
  164. local_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
  165. local_dir = os.path.basename(local_path)
  166. local_path = local_path.replace("\\", "/")
  167. ret = os.path.exists(local_path + "/output")
  168. if ret == True:
  169. if cleanup == True:
  170. print ("clean the dest dir")
  171. shutil.rmtree(local_path + "/output")
  172. os.mkdir(local_path + "/output")
  173. else:
  174. print ("Create the dest dir")
  175. os.mkdir(local_path + "/output")
  176. #cpoy the files
  177. copy_txt_files(local_path)
  178. print ("Copy files finished ......")
  179. exit(0)