test-zstd-versions.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/usr/bin/env python3
  2. """Test zstd interoperability between versions"""
  3. # ################################################################
  4. # Copyright (c) Yann Collet, Facebook, Inc.
  5. # All rights reserved.
  6. #
  7. # This source code is licensed under both the BSD-style license (found in the
  8. # LICENSE file in the root directory of this source tree) and the GPLv2 (found
  9. # in the COPYING file in the root directory of this source tree).
  10. # You may select, at your option, one of the above-listed licenses.
  11. # ################################################################
  12. import filecmp
  13. import glob
  14. import hashlib
  15. import os
  16. import shutil
  17. import sys
  18. import subprocess
  19. from subprocess import Popen, PIPE
  20. repo_url = 'https://github.com/facebook/zstd.git'
  21. tmp_dir_name = 'tests/versionsTest'
  22. make_cmd = 'make'
  23. make_args = ['-j','CFLAGS=-O1']
  24. git_cmd = 'git'
  25. test_dat_src = 'README.md'
  26. test_dat = 'test_dat'
  27. head = 'vdevel'
  28. dict_source = 'dict_source'
  29. dict_files = './zstd/programs/*.c ./zstd/lib/common/*.c ./zstd/lib/compress/*.c ./zstd/lib/decompress/*.c ./zstd/lib/dictBuilder/*.c ./zstd/lib/legacy/*.c '
  30. dict_files += './zstd/programs/*.h ./zstd/lib/common/*.h ./zstd/lib/compress/*.h ./zstd/lib/dictBuilder/*.h ./zstd/lib/legacy/*.h'
  31. def execute(command, print_output=False, print_error=True, param_shell=False):
  32. popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=param_shell)
  33. stdout_lines, stderr_lines = popen.communicate()
  34. stderr_lines = stderr_lines.decode("utf-8")
  35. stdout_lines = stdout_lines.decode("utf-8")
  36. if print_output:
  37. print(stdout_lines)
  38. print(stderr_lines)
  39. if popen.returncode is not None and popen.returncode != 0:
  40. if not print_output and print_error:
  41. print(stderr_lines)
  42. return popen.returncode
  43. def proc(cmd_args, pipe=True, dummy=False):
  44. if dummy:
  45. return
  46. if pipe:
  47. subproc = Popen(cmd_args, stdout=PIPE, stderr=PIPE)
  48. else:
  49. subproc = Popen(cmd_args)
  50. return subproc.communicate()
  51. def make(targets, pipe=True):
  52. cmd = [make_cmd] + make_args + targets
  53. cmd_str = str(cmd)
  54. print('compilation command : ' + cmd_str)
  55. return proc(cmd, pipe)
  56. def git(args, pipe=True):
  57. return proc([git_cmd] + args, pipe)
  58. def get_git_tags():
  59. stdout, stderr = git(['tag', '-l', 'v[0-9].[0-9].[0-9]'])
  60. tags = stdout.decode('utf-8').split()
  61. return tags
  62. def create_dict(tag, dict_source_path):
  63. dict_name = 'dict.' + tag
  64. if not os.path.isfile(dict_name):
  65. cFiles = glob.glob(dict_source_path + "/*.c")
  66. hFiles = glob.glob(dict_source_path + "/*.h")
  67. if tag == 'v0.5.0':
  68. result = execute('./dictBuilder.' + tag + ' ' + ' '.join(cFiles) + ' ' + ' '.join(hFiles) + ' -o ' + dict_name, print_output=False, param_shell=True)
  69. else:
  70. result = execute('./zstd.' + tag + ' -f --train ' + ' '.join(cFiles) + ' ' + ' '.join(hFiles) + ' -o ' + dict_name, print_output=False, param_shell=True)
  71. if result == 0:
  72. print(dict_name + ' created')
  73. else:
  74. print('ERROR: creating of ' + dict_name + ' failed')
  75. else:
  76. print(dict_name + ' already exists')
  77. def dict_compress_sample(tag, sample):
  78. dict_name = 'dict.' + tag
  79. DEVNULL = open(os.devnull, 'wb')
  80. if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-f', sample], stderr=DEVNULL) == 0:
  81. os.rename(sample + '.zst', sample + '_01_64_' + tag + '_dictio.zst')
  82. if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-5f', sample], stderr=DEVNULL) == 0:
  83. os.rename(sample + '.zst', sample + '_05_64_' + tag + '_dictio.zst')
  84. if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-9f', sample], stderr=DEVNULL) == 0:
  85. os.rename(sample + '.zst', sample + '_09_64_' + tag + '_dictio.zst')
  86. if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-15f', sample], stderr=DEVNULL) == 0:
  87. os.rename(sample + '.zst', sample + '_15_64_' + tag + '_dictio.zst')
  88. if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-18f', sample], stderr=DEVNULL) == 0:
  89. os.rename(sample + '.zst', sample + '_18_64_' + tag + '_dictio.zst')
  90. # zstdFiles = glob.glob("*.zst*")
  91. # print(zstdFiles)
  92. print(tag + " : dict compression completed")
  93. def compress_sample(tag, sample):
  94. DEVNULL = open(os.devnull, 'wb')
  95. if subprocess.call(['./zstd.' + tag, '-f', sample], stderr=DEVNULL) == 0:
  96. os.rename(sample + '.zst', sample + '_01_64_' + tag + '_nodict.zst')
  97. if subprocess.call(['./zstd.' + tag, '-5f', sample], stderr=DEVNULL) == 0:
  98. os.rename(sample + '.zst', sample + '_05_64_' + tag + '_nodict.zst')
  99. if subprocess.call(['./zstd.' + tag, '-9f', sample], stderr=DEVNULL) == 0:
  100. os.rename(sample + '.zst', sample + '_09_64_' + tag + '_nodict.zst')
  101. if subprocess.call(['./zstd.' + tag, '-15f', sample], stderr=DEVNULL) == 0:
  102. os.rename(sample + '.zst', sample + '_15_64_' + tag + '_nodict.zst')
  103. if subprocess.call(['./zstd.' + tag, '-18f', sample], stderr=DEVNULL) == 0:
  104. os.rename(sample + '.zst', sample + '_18_64_' + tag + '_nodict.zst')
  105. # zstdFiles = glob.glob("*.zst*")
  106. # print(zstdFiles)
  107. print(tag + " : compression completed")
  108. # http://stackoverflow.com/a/19711609/2132223
  109. def sha1_of_file(filepath):
  110. with open(filepath, 'rb') as f:
  111. return hashlib.sha1(f.read()).hexdigest()
  112. def remove_duplicates():
  113. list_of_zst = sorted(glob.glob('*.zst'))
  114. for i, ref_zst in enumerate(list_of_zst):
  115. if not os.path.isfile(ref_zst):
  116. continue
  117. for j in range(i + 1, len(list_of_zst)):
  118. compared_zst = list_of_zst[j]
  119. if not os.path.isfile(compared_zst):
  120. continue
  121. if filecmp.cmp(ref_zst, compared_zst):
  122. os.remove(compared_zst)
  123. print('duplicated : {} == {}'.format(ref_zst, compared_zst))
  124. def decompress_zst(tag):
  125. dec_error = 0
  126. list_zst = sorted(glob.glob('*_nodict.zst'))
  127. for file_zst in list_zst:
  128. print(file_zst, end=' ')
  129. print(tag, end=' ')
  130. file_dec = file_zst + '_d64_' + tag + '.dec'
  131. if tag <= 'v0.5.0':
  132. params = ['./zstd.' + tag, '-df', file_zst, file_dec]
  133. else:
  134. params = ['./zstd.' + tag, '-df', file_zst, '-o', file_dec]
  135. if execute(params) == 0:
  136. if not filecmp.cmp(file_dec, test_dat):
  137. print('ERR !! ')
  138. dec_error = 1
  139. else:
  140. print('OK ')
  141. else:
  142. print('command does not work')
  143. dec_error = 1
  144. return dec_error
  145. def decompress_dict(tag):
  146. dec_error = 0
  147. list_zst = sorted(glob.glob('*_dictio.zst'))
  148. for file_zst in list_zst:
  149. dict_tag = file_zst[0:len(file_zst)-11] # remove "_dictio.zst"
  150. if head in dict_tag: # find vdevel
  151. dict_tag = head
  152. else:
  153. dict_tag = dict_tag[dict_tag.rfind('v'):]
  154. if tag == 'v0.6.0' and dict_tag < 'v0.6.0':
  155. continue
  156. dict_name = 'dict.' + dict_tag
  157. print(file_zst + ' ' + tag + ' dict=' + dict_tag, end=' ')
  158. file_dec = file_zst + '_d64_' + tag + '.dec'
  159. if tag <= 'v0.5.0':
  160. params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, file_dec]
  161. else:
  162. params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, '-o', file_dec]
  163. if execute(params) == 0:
  164. if not filecmp.cmp(file_dec, test_dat):
  165. print('ERR !! ')
  166. dec_error = 1
  167. else:
  168. print('OK ')
  169. else:
  170. print('command does not work')
  171. dec_error = 1
  172. return dec_error
  173. if __name__ == '__main__':
  174. error_code = 0
  175. base_dir = os.getcwd() + '/..' # /path/to/zstd
  176. tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/zstd/tests/versionsTest
  177. clone_dir = tmp_dir + '/' + 'zstd' # /path/to/zstd/tests/versionsTest/zstd
  178. dict_source_path = tmp_dir + '/' + dict_source # /path/to/zstd/tests/versionsTest/dict_source
  179. programs_dir = base_dir + '/programs' # /path/to/zstd/programs
  180. os.makedirs(tmp_dir, exist_ok=True)
  181. # since Travis clones limited depth, we should clone full repository
  182. if not os.path.isdir(clone_dir):
  183. git(['clone', repo_url, clone_dir])
  184. shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
  185. # Retrieve all release tags
  186. print('Retrieve all release tags :')
  187. os.chdir(clone_dir)
  188. alltags = get_git_tags() + [head]
  189. tags = [t for t in alltags if t >= 'v0.5.0']
  190. print(tags)
  191. # Build all release zstd
  192. for tag in tags:
  193. os.chdir(base_dir)
  194. dst_zstd = '{}/zstd.{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/zstd.<TAG>
  195. if not os.path.isfile(dst_zstd) or tag == head:
  196. if tag != head:
  197. print('-----------------------------------------------')
  198. print('compiling ' + tag)
  199. print('-----------------------------------------------')
  200. r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/<TAG>
  201. os.makedirs(r_dir, exist_ok=True)
  202. os.chdir(clone_dir)
  203. git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
  204. if tag == 'v0.5.0':
  205. os.chdir(r_dir + '/dictBuilder') # /path/to/zstd/tests/versionsTest/v0.5.0/dictBuilder
  206. make(['clean'], False) # separate 'clean' target to allow parallel build
  207. make(['dictBuilder'], False)
  208. shutil.copy2('dictBuilder', '{}/dictBuilder.{}'.format(tmp_dir, tag))
  209. os.chdir(r_dir + '/programs') # /path/to/zstd/tests/versionsTest/<TAG>/programs
  210. make(['clean'], False) # separate 'clean' target to allow parallel build
  211. make(['zstd'], False)
  212. else:
  213. os.chdir(programs_dir)
  214. print('-----------------------------------------------')
  215. print('compiling head')
  216. print('-----------------------------------------------')
  217. make(['zstd'], False)
  218. shutil.copy2('zstd', dst_zstd)
  219. # remove any remaining *.zst and *.dec from previous test
  220. os.chdir(tmp_dir)
  221. for compressed in glob.glob("*.zst"):
  222. os.remove(compressed)
  223. for dec in glob.glob("*.dec"):
  224. os.remove(dec)
  225. # copy *.c and *.h to a temporary directory ("dict_source")
  226. if not os.path.isdir(dict_source_path):
  227. os.mkdir(dict_source_path)
  228. print('cp ' + dict_files + ' ' + dict_source_path)
  229. execute('cp ' + dict_files + ' ' + dict_source_path, param_shell=True)
  230. print('-----------------------------------------------')
  231. print('Compress test.dat by all released zstd')
  232. print('-----------------------------------------------')
  233. error_code = 0
  234. for tag in tags:
  235. print(tag)
  236. if tag >= 'v0.5.0':
  237. create_dict(tag, dict_source_path)
  238. dict_compress_sample(tag, test_dat)
  239. remove_duplicates()
  240. error_code += decompress_dict(tag)
  241. compress_sample(tag, test_dat)
  242. remove_duplicates()
  243. error_code += decompress_zst(tag)
  244. print('')
  245. print('Enumerate different compressed files')
  246. zstds = sorted(glob.glob('*.zst'))
  247. for zstd in zstds:
  248. print(zstd + ' : ' + repr(os.path.getsize(zstd)) + ', ' + sha1_of_file(zstd))
  249. if error_code != 0:
  250. print('====== ERROR !!! =======')
  251. sys.exit(error_code)