gen_db.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python3
  2. # -*- Coding: UTF-8 -*-
  3. # ---------------------------------------------------------------------------
  4. # Open Asset Import Library (ASSIMP)
  5. # ---------------------------------------------------------------------------
  6. #
  7. # Copyright (c) 2006-2020, ASSIMP Development Team
  8. #
  9. # All rights reserved.
  10. #
  11. # Redistribution and use of this software in source and binary forms,
  12. # with or without modification, are permitted provided that the following
  13. # conditions are met:
  14. #
  15. # * Redistributions of source code must retain the above
  16. # copyright notice, this list of conditions and the
  17. # following disclaimer.
  18. #
  19. # * Redistributions in binary form must reproduce the above
  20. # copyright notice, this list of conditions and the
  21. # following disclaimer in the documentation and/or other
  22. # materials provided with the distribution.
  23. #
  24. # * Neither the name of the ASSIMP team, nor the names of its
  25. # contributors may be used to endorse or promote products
  26. # derived from this software without specific prior
  27. # written permission of the ASSIMP Development Team.
  28. #
  29. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. # ---------------------------------------------------------------------------
  41. """
  42. Generate the regression database db.zip from the files in the <root>/test/models
  43. directory. Older databases are overwritten with no prompt but can be restored
  44. using Git as needed.
  45. Use --help for usage.
  46. On Windows, use ``py run.py <arguments>`` to make sure command line parameters
  47. are forwarded to the script.
  48. """
  49. import sys
  50. import os
  51. import subprocess
  52. import zipfile
  53. import settings
  54. import utils
  55. usage = """gen_db [assimp_binary] [-i=...] [-e=...] [-p] [-n]
  56. The assimp_cmd (or assimp) binary to use is specified by the first
  57. command line argument and defaults to ``assimp``.
  58. To build, set ``ASSIMP_BUILD_ASSIMP_TOOLS=ON`` in CMake. If generating
  59. configs for an IDE, make sure to build the assimp_cmd project.
  60. -i,--include: List of file extensions to update dumps for. If omitted,
  61. all file extensions are updated except those in `exclude`.
  62. Example: -ixyz,abc
  63. -i.xyz,.abc
  64. --include=xyz,abc
  65. -e,--exclude: Merged with settings.exclude_extensions to produce a
  66. list of all file extensions to ignore. If dumps exist,
  67. they are not altered. If not, theu are not created.
  68. -p,--preview: Preview list of file extensions touched by the update.
  69. Dont' change anything.
  70. -n,--nozip: Don't pack to ZIP archive. Keep all dumps in individual files.
  71. """
  72. # -------------------------------------------------------------------------------
  73. def process_dir(d, outfile, file_filter):
  74. """ Generate small dump records for all files in 'd' """
  75. print("Processing directory " + d)
  76. num = 0
  77. for f in os.listdir(d):
  78. fullp = os.path.join(d, f)
  79. if os.path.isdir(fullp) and not f == ".svn":
  80. num += process_dir(fullp, outfile, file_filter)
  81. continue
  82. if file_filter(f):
  83. for pp in settings.pp_configs_to_test:
  84. num += 1
  85. print("DUMP " + fullp + "\n post-processing: " + pp)
  86. outf = os.path.join(os.getcwd(), settings.database_name,
  87. utils.hashing(fullp, pp))
  88. cmd = [ assimp_bin_path, "dump", fullp, outf, "-b", "-s", "-l" ] + pp.split()
  89. outfile.write("assimp dump "+"-"*80+"\n")
  90. outfile.flush()
  91. if subprocess.call(cmd, stdout=outfile, stderr=outfile, shell=False):
  92. print("Failure processing " + fullp)
  93. # spit out an empty file to indicate that this failure is expected
  94. with open(outf,'wb') as f:
  95. pass
  96. return num
  97. # -------------------------------------------------------------------------------
  98. def make_zip():
  99. """Zip the contents of ./<settings.database_name>
  100. to <settings.database_name>.zip using DEFLATE
  101. compression to minimize the file size. """
  102. num = 0
  103. zipout = zipfile.ZipFile(settings.database_name + ".zip", "w", zipfile.ZIP_DEFLATED)
  104. for f in os.listdir(settings.database_name):
  105. p = os.path.join(settings.database_name, f)
  106. zipout.write(p, f)
  107. if settings.remove_old:
  108. os.remove(p)
  109. num += 1
  110. if settings.remove_old:
  111. os.rmdir(settings.database_name)
  112. bad = zipout.testzip()
  113. assert bad is None
  114. print("="*60)
  115. print("Database contains {0} entries".format(num))
  116. # -------------------------------------------------------------------------------
  117. def extract_zip():
  118. """Unzip <settings.database_name>.zip to
  119. ./<settings.database_name>"""
  120. try:
  121. zipout = zipfile.ZipFile(settings.database_name + ".zip", "r", 0)
  122. zipout.extractall(path=settings.database_name)
  123. except (RuntimeError,IOError) as r:
  124. print(r)
  125. print("failed to extract previous ZIP contents. "\
  126. "DB is generated from scratch.")
  127. # -------------------------------------------------------------------------------
  128. def gen_db(ext_list,outfile):
  129. """Generate the crash dump database in
  130. ./<settings.database_name>"""
  131. try:
  132. os.mkdir(settings.database_name)
  133. except OSError:
  134. pass
  135. num = 0
  136. for tp in settings.model_directories:
  137. num += process_dir(tp, outfile,
  138. lambda x: os.path.splitext(x)[1].lower() in ext_list and not x in settings.files_to_ignore)
  139. print("="*60)
  140. print("Updated {0} entries".format(num))
  141. # -------------------------------------------------------------------------------
  142. if __name__ == "__main__":
  143. def clean(f):
  144. f = f.strip("* \'")
  145. return "."+f if f[:1] != '.' else f
  146. if len(sys.argv) <= 1 or sys.argv[1] == "--help" or sys.argv[1] == "-h":
  147. print(usage)
  148. sys.exit(0)
  149. assimp_bin_path = sys.argv[1]
  150. ext_list, preview, nozip = None, False, False
  151. for m in sys.argv[2:]:
  152. if m[:10]=="--exclude=":
  153. settings.exclude_extensions += map(clean, m[10:].split(","))
  154. elif m[:2]=="-e":
  155. settings.exclude_extensions += map(clean, m[2:].split(","))
  156. elif m[:10]=="--include=":
  157. ext_list = m[10:].split(",")
  158. elif m[:2]=="-i":
  159. ext_list = m[2:].split(",")
  160. elif m=="-p" or m == "--preview":
  161. preview = True
  162. elif m=="-n" or m == "--nozip":
  163. nozip = True
  164. else:
  165. print("Unrecognized parameter: " + m)
  166. sys.exit(-1)
  167. outfile = open(os.path.join("..", "results", "gen_regression_db_output.txt"), "w")
  168. if ext_list is None:
  169. (ext_list, err) = subprocess.Popen([assimp_bin_path, "listext"],
  170. stdout=subprocess.PIPE).communicate()
  171. ext_list = str(ext_list.strip()).lower().split(";")
  172. # todo: Fix for multi dot extensions like .skeleton.xml
  173. ext_list = list(filter(lambda f: not f in settings.exclude_extensions,
  174. map(clean, ext_list)))
  175. print('File extensions processed: ' + ', '.join(ext_list))
  176. if preview:
  177. sys.exit(1)
  178. extract_zip()
  179. gen_db(ext_list,outfile)
  180. make_zip()
  181. print("="*60)
  182. input("Press any key to continue")
  183. sys.exit(0)
  184. # vim: ai ts=4 sts=4 et sw=4