/versionsTest/test-lz4-versions.py

https://gitlab.com/adam.lukaitis/lz4
Python | 147 lines | 118 code | 20 blank | 9 comment | 28 complexity | 70a1beb8e3fd0ca4b0072a07c16a9e75 MD5 | raw file
  1. #!/usr/bin/env python3
  2. import glob
  3. import subprocess
  4. import filecmp
  5. import os
  6. import shutil
  7. import sys
  8. import hashlib
  9. repo_url = 'https://github.com/Cyan4973/lz4.git'
  10. tmp_dir_name = 'versionsTest/lz4test'
  11. make_cmd = 'make'
  12. git_cmd = 'git'
  13. test_dat_src = 'README.md'
  14. test_dat = 'test_dat'
  15. head = 'r999'
  16. def proc(cmd_args, pipe=True, dummy=False):
  17. if dummy:
  18. return
  19. if pipe:
  20. subproc = subprocess.Popen(cmd_args,
  21. stdout=subprocess.PIPE,
  22. stderr=subprocess.PIPE)
  23. else:
  24. subproc = subprocess.Popen(cmd_args)
  25. return subproc.communicate()
  26. def make(args, pipe=True):
  27. return proc([make_cmd] + args, pipe)
  28. def git(args, pipe=True):
  29. return proc([git_cmd] + args, pipe)
  30. def get_git_tags():
  31. stdout, stderr = git(['tag', '-l', 'r[0-9][0-9][0-9]'])
  32. tags = stdout.decode('utf-8').split()
  33. return tags
  34. # http://stackoverflow.com/a/19711609/2132223
  35. def sha1_of_file(filepath):
  36. with open(filepath, 'rb') as f:
  37. return hashlib.sha1(f.read()).hexdigest()
  38. if __name__ == '__main__':
  39. error_code = 0
  40. base_dir = os.getcwd() + '/..' # /path/to/lz4
  41. tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/lz4/versionsTest/lz4test
  42. clone_dir = tmp_dir + '/' + 'lz4' # /path/to/lz4/versionsTest/lz4test/lz4
  43. programs_dir = base_dir + '/programs' # /path/to/lz4/programs
  44. os.makedirs(tmp_dir, exist_ok=True)
  45. # since Travis clones limited depth, we should clone full repository
  46. if not os.path.isdir(clone_dir):
  47. git(['clone', repo_url, clone_dir])
  48. shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
  49. # Retrieve all release tags
  50. print('Retrieve all release tags :')
  51. os.chdir(clone_dir)
  52. tags = [head] + get_git_tags()
  53. print(tags);
  54. # Build all release lz4c and lz4c32
  55. for tag in tags:
  56. os.chdir(base_dir)
  57. dst_lz4c = '{}/lz4c.{}' .format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c.<TAG>
  58. dst_lz4c32 = '{}/lz4c32.{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c32.<TAG>
  59. if not os.path.isfile(dst_lz4c) or not os.path.isfile(dst_lz4c32) or tag == head:
  60. if tag != head:
  61. r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/<TAG>
  62. os.makedirs(r_dir, exist_ok=True)
  63. os.chdir(clone_dir)
  64. git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
  65. os.chdir(r_dir + '/programs') # /path/to/lz4/lz4test/<TAG>/programs
  66. make(['clean', 'lz4c', 'lz4c32'], False)
  67. else:
  68. os.chdir(programs_dir)
  69. make(['lz4c', 'lz4c32'], False)
  70. shutil.copy2('lz4c', dst_lz4c)
  71. shutil.copy2('lz4c32', dst_lz4c32)
  72. # Compress test.dat by all released lz4c and lz4c32
  73. print('Compress test.dat by all released lz4c and lz4c32')
  74. os.chdir(tmp_dir)
  75. for lz4 in glob.glob("*.lz4"):
  76. os.remove(lz4)
  77. for tag in tags:
  78. proc(['./lz4c.' + tag, '-1fz', test_dat, test_dat + '_1_64_' + tag + '.lz4'])
  79. proc(['./lz4c.' + tag, '-9fz', test_dat, test_dat + '_9_64_' + tag + '.lz4'])
  80. proc(['./lz4c32.' + tag, '-1fz', test_dat, test_dat + '_1_32_' + tag + '.lz4'])
  81. proc(['./lz4c32.' + tag, '-9fz', test_dat, test_dat + '_9_32_' + tag + '.lz4'])
  82. print('Full list of compressed files')
  83. lz4s = sorted(glob.glob('*.lz4'))
  84. for lz4 in lz4s:
  85. print(lz4 + ' : ' + repr(os.path.getsize(lz4)))
  86. # Remove duplicated .lz4 files
  87. print('')
  88. print('Duplicated files')
  89. lz4s = sorted(glob.glob('*.lz4'))
  90. for i, lz4 in enumerate(lz4s):
  91. if not os.path.isfile(lz4):
  92. continue
  93. for j in range(i+1, len(lz4s)):
  94. lz4t = lz4s[j]
  95. if not os.path.isfile(lz4t):
  96. continue
  97. if filecmp.cmp(lz4, lz4t):
  98. os.remove(lz4t)
  99. print('{} == {}'.format(lz4, lz4t))
  100. print('Enumerate only different compressed files')
  101. lz4s = sorted(glob.glob('*.lz4'))
  102. for lz4 in lz4s:
  103. print(lz4 + ' : ' + repr(os.path.getsize(lz4)) + ', ' + sha1_of_file(lz4))
  104. # Decompress remained .lz4 files by all released lz4c and lz4c32
  105. print('Decompression tests and verifications')
  106. lz4s = sorted(glob.glob('*.lz4'))
  107. for dec in glob.glob("*.dec"):
  108. os.remove(dec)
  109. for lz4 in lz4s:
  110. print(lz4, end=" ")
  111. for tag in tags:
  112. print(tag, end=" ")
  113. proc(['./lz4c.' + tag, '-df', lz4, lz4 + '_d64_' + tag + '.dec'])
  114. proc(['./lz4c32.' + tag, '-df', lz4, lz4 + '_d32_' + tag + '.dec'])
  115. print(' OK') # well, here, decompression has worked; but file is not yet verified
  116. # Compare all '.dec' files with test_dat
  117. decs = glob.glob('*.dec')
  118. for dec in decs:
  119. if not filecmp.cmp(dec, test_dat):
  120. print('ERR : ' + dec)
  121. error_code = 1
  122. else:
  123. print('OK : ' + dec)
  124. os.remove(dec)
  125. if error_code != 0:
  126. print('ERROR')
  127. sys.exit(error_code)