kusano 7d535a
#!/usr/bin/env python3
kusano 7d535a
kusano 7d535a
import glob
kusano 7d535a
import subprocess
kusano 7d535a
import filecmp
kusano 7d535a
import os
kusano 7d535a
import shutil
kusano 7d535a
import sys
kusano 7d535a
import hashlib
kusano 7d535a
kusano 7d535a
repo_url = 'https://github.com/Cyan4973/lz4.git'
kusano 7d535a
tmp_dir_name = 'versionsTest/lz4test'
kusano 7d535a
make_cmd = 'make'
kusano 7d535a
git_cmd = 'git'
kusano 7d535a
test_dat_src = 'README.md'
kusano 7d535a
test_dat = 'test_dat'
kusano 7d535a
head = 'r999'
kusano 7d535a
kusano 7d535a
def proc(cmd_args, pipe=True, dummy=False):
kusano 7d535a
    if dummy:
kusano 7d535a
        return
kusano 7d535a
    if pipe:
kusano 7d535a
        subproc = subprocess.Popen(cmd_args,
kusano 7d535a
                                   stdout=subprocess.PIPE, 
kusano 7d535a
                                   stderr=subprocess.PIPE)
kusano 7d535a
    else:
kusano 7d535a
        subproc = subprocess.Popen(cmd_args)
kusano 7d535a
    return subproc.communicate()
kusano 7d535a
kusano 7d535a
def make(args, pipe=True):
kusano 7d535a
    return proc([make_cmd] + args, pipe)
kusano 7d535a
kusano 7d535a
def git(args, pipe=True):
kusano 7d535a
    return proc([git_cmd] + args, pipe)
kusano 7d535a
kusano 7d535a
def get_git_tags():
kusano 7d535a
    stdout, stderr = git(['tag', '-l', 'r[0-9][0-9][0-9]'])
kusano 7d535a
    tags = stdout.decode('utf-8').split()
kusano 7d535a
    return tags
kusano 7d535a
kusano 7d535a
# http://stackoverflow.com/a/19711609/2132223
kusano 7d535a
def sha1_of_file(filepath):
kusano 7d535a
    with open(filepath, 'rb') as f:
kusano 7d535a
        return hashlib.sha1(f.read()).hexdigest()
kusano 7d535a
kusano 7d535a
if __name__ == '__main__':
kusano 7d535a
    error_code = 0
kusano 7d535a
    base_dir = os.getcwd() + '/..'           # /path/to/lz4
kusano 7d535a
    tmp_dir = base_dir + '/' + tmp_dir_name  # /path/to/lz4/versionsTest/lz4test
kusano 7d535a
    clone_dir = tmp_dir + '/' + 'lz4'        # /path/to/lz4/versionsTest/lz4test/lz4
kusano 7d535a
    programs_dir = base_dir + '/programs'    # /path/to/lz4/programs
kusano 7d535a
    os.makedirs(tmp_dir, exist_ok=True)
kusano 7d535a
kusano 7d535a
    # since Travis clones limited depth, we should clone full repository
kusano 7d535a
    if not os.path.isdir(clone_dir):
kusano 7d535a
        git(['clone', repo_url, clone_dir])
kusano 7d535a
kusano 7d535a
    shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
kusano 7d535a
kusano 7d535a
    # Retrieve all release tags
kusano 7d535a
    print('Retrieve all release tags :')
kusano 7d535a
    os.chdir(clone_dir)
kusano 7d535a
    tags = [head] + get_git_tags()
kusano 7d535a
    print(tags);
kusano 7d535a
kusano 7d535a
    # Build all release lz4c and lz4c32
kusano 7d535a
    for tag in tags:
kusano 7d535a
        os.chdir(base_dir)
kusano 7d535a
        dst_lz4c   = '{}/lz4c.{}'  .format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c.<tag></tag>
kusano 7d535a
        dst_lz4c32 = '{}/lz4c32.{}'.format(tmp_dir, tag) # /path/to/lz4/test/lz4test/lz4c32.<tag></tag>
kusano 7d535a
        if not os.path.isfile(dst_lz4c) or not os.path.isfile(dst_lz4c32) or tag == head:
kusano 7d535a
            if tag != head:
kusano 7d535a
                r_dir = '{}/{}'.format(tmp_dir, tag)  # /path/to/lz4/test/lz4test/<tag></tag>
kusano 7d535a
                os.makedirs(r_dir, exist_ok=True)
kusano 7d535a
                os.chdir(clone_dir)
kusano 7d535a
                git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
kusano 7d535a
                os.chdir(r_dir + '/programs')  # /path/to/lz4/lz4test/<tag>/programs</tag>
kusano 7d535a
                make(['clean', 'lz4c', 'lz4c32'], False)
kusano 7d535a
            else:
kusano 7d535a
                os.chdir(programs_dir)
kusano 7d535a
                make(['lz4c', 'lz4c32'], False)
kusano 7d535a
            shutil.copy2('lz4c',   dst_lz4c)
kusano 7d535a
            shutil.copy2('lz4c32', dst_lz4c32)
kusano 7d535a
kusano 7d535a
    # Compress test.dat by all released lz4c and lz4c32
kusano 7d535a
    print('Compress test.dat by all released lz4c and lz4c32')
kusano 7d535a
    os.chdir(tmp_dir)
kusano 7d535a
    for lz4 in glob.glob("*.lz4"):
kusano 7d535a
        os.remove(lz4)
kusano 7d535a
    for tag in tags:
kusano 7d535a
        proc(['./lz4c.'   + tag, '-1fz', test_dat, test_dat + '_1_64_' + tag + '.lz4'])
kusano 7d535a
        proc(['./lz4c.'   + tag, '-9fz', test_dat, test_dat + '_9_64_' + tag + '.lz4'])
kusano 7d535a
        proc(['./lz4c32.' + tag, '-1fz', test_dat, test_dat + '_1_32_' + tag + '.lz4'])
kusano 7d535a
        proc(['./lz4c32.' + tag, '-9fz', test_dat, test_dat + '_9_32_' + tag + '.lz4'])
kusano 7d535a
kusano 7d535a
    print('Full list of compressed files')
kusano 7d535a
    lz4s = sorted(glob.glob('*.lz4'))
kusano 7d535a
    for lz4 in lz4s:
kusano 7d535a
        print(lz4 + ' : ' + repr(os.path.getsize(lz4)))
kusano 7d535a
kusano 7d535a
    # Remove duplicated .lz4 files
kusano 7d535a
    print('')
kusano 7d535a
    print('Duplicated files')
kusano 7d535a
    lz4s = sorted(glob.glob('*.lz4'))
kusano 7d535a
    for i, lz4 in enumerate(lz4s):
kusano 7d535a
        if not os.path.isfile(lz4):
kusano 7d535a
            continue
kusano 7d535a
        for j in range(i+1, len(lz4s)):
kusano 7d535a
            lz4t = lz4s[j]
kusano 7d535a
            if not os.path.isfile(lz4t):
kusano 7d535a
                continue
kusano 7d535a
            if filecmp.cmp(lz4, lz4t):
kusano 7d535a
                os.remove(lz4t)
kusano 7d535a
                print('{} == {}'.format(lz4, lz4t))
kusano 7d535a
kusano 7d535a
    print('Enumerate only different compressed files')
kusano 7d535a
    lz4s = sorted(glob.glob('*.lz4'))
kusano 7d535a
    for lz4 in lz4s:
kusano 7d535a
        print(lz4 + ' : ' + repr(os.path.getsize(lz4)) + ', ' + sha1_of_file(lz4))
kusano 7d535a
kusano 7d535a
    # Decompress remained .lz4 files by all released lz4c and lz4c32
kusano 7d535a
    print('Decompression tests and verifications')
kusano 7d535a
    lz4s = sorted(glob.glob('*.lz4'))
kusano 7d535a
    for dec in glob.glob("*.dec"):
kusano 7d535a
        os.remove(dec)
kusano 7d535a
    for lz4 in lz4s:
kusano 7d535a
        print(lz4, end=" ")
kusano 7d535a
        for tag in tags:
kusano 7d535a
            print(tag, end=" ")
kusano 7d535a
            proc(['./lz4c.'   + tag, '-df', lz4, lz4 + '_d64_' + tag + '.dec'])
kusano 7d535a
            proc(['./lz4c32.' + tag, '-df', lz4, lz4 + '_d32_' + tag + '.dec'])
kusano 7d535a
        print(' OK')   # well, here, decompression has worked; but file is not yet verified
kusano 7d535a
kusano 7d535a
    # Compare all '.dec' files with test_dat
kusano 7d535a
    decs = glob.glob('*.dec')
kusano 7d535a
    for dec in decs:
kusano 7d535a
        if not filecmp.cmp(dec, test_dat):
kusano 7d535a
            print('ERR : ' + dec)
kusano 7d535a
            error_code = 1
kusano 7d535a
        else:
kusano 7d535a
            print('OK  : ' + dec)
kusano 7d535a
            os.remove(dec)
kusano 7d535a
kusano 7d535a
    if error_code != 0:
kusano 7d535a
        print('ERROR')
kusano 7d535a
kusano 7d535a
    sys.exit(error_code)