Skip to content

Commit

Permalink
Merge pull request #71 from mschmidt87/enh/testsuite_temp_folder
Browse files Browse the repository at this point in the history
Enh/testsuite temp folder
  • Loading branch information
jakobj committed Mar 22, 2018
2 parents 58678ee + c80c92c commit 5562bfa
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 46 deletions.
12 changes: 9 additions & 3 deletions convert_h5file
Expand Up @@ -19,6 +19,7 @@ import docopt
import os
import importlib
import sys
import tempfile

import h5py_wrapper.wrapper as h5w
import h5py_wrapper.lib as h5w_lib
Expand All @@ -28,11 +29,16 @@ if __name__ == '__main__':
# First get release version used to create the file to be converted
version_stripped = args['--release'].replace('.', '')
release_base_name = '_'.join(('h5py_wrapper', version_stripped))
# This case distinction is necessary because the directory structure
# changes between versions
module = '.'.join((release_base_name, 'wrapper'))
try:
h5w_old = importlib.import_module('.'.join((release_base_name, 'wrapper')))
h5w_old = importlib.import_module(module)
except ImportError:
h5w_lib.get_previous_version(args['--release'])
h5w_old = importlib.import_module('.'.join((release_base_name, 'wrapper')))
tmpdir = tempfile.mkdtemp()
h5w_lib.get_previous_version(args['--release'], tmpdir)
sys.path.append(tmpdir)
h5w_old = importlib.import_module(module)

files = args['<files>'] or sys.stdin.read().splitlines()
for fn in files:
Expand Down
42 changes: 33 additions & 9 deletions h5py_wrapper/lib.py
Expand Up @@ -10,25 +10,49 @@
import tarfile


def get_previous_version(version):
def get_previous_version(version, path):
"""
Retrieves the given version of the wrapper from github as a tar
archive and extracts its contents to the current directory.
Parameters
----------
version : str
Version number in format 'X.X.X'
path : str
Path to store the files.
Returns
-------
package_dir : str
Path to package.
"""
base_url = "https://github.com/INM-6/h5py_wrapper/archive/v"
r = requests.get(''.join((base_url, version, ".tar.gz")))
base_url = "https://github.com/INM-6/h5py_wrapper/archive/"
if version == '0.0.1':
ver = 'v0.0.1'
else:
ver = version
r = requests.get(''.join((base_url, ver, ".tar.gz")))
# convert LocalPath object to str (if path to tmp dir is passed by py.test)
# to ensure that path can be handled by os.path.join()
path = str(path)
try:
r.raise_for_status()
fn = ''.join((os.path.join(os.getcwd(), version), '.tar.gz'))
fn = ''.join((os.path.join(path, version), '.tar.gz'))
with open(fn, 'wb') as f:
f.write(r.content)
with tarfile.open(fn) as f:
f.extract(''.join(('h5py_wrapper-', version, '/wrapper.py')))
f.extract(''.join(('h5py_wrapper-', version, '/__init__.py')))
os.rename('-'.join(('h5py_wrapper', version)),
'_'.join(('h5py_wrapper', version.replace('.', ''))))
os.remove(fn)
f.extractall(path=path)

# This case distinction is necessary because the directory structure
# changes between versions
if version == '0.0.1':
pkg_wrp_dir = os.path.join(path, '-'.join(('h5py_wrapper', version)))
elif version == '1.0.1':
pkg_wrp_dir = os.path.join(path, '-'.join(('h5py_wrapper', version)), 'h5py_wrapper')
package_dir = os.path.join(path, '_'.join(('h5py_wrapper', version.replace('.', ''))))
os.rename(pkg_wrp_dir, package_dir)
return package_dir
except requests.exceptions.HTTPError:
raise ImportError("Requested release version does not exist.")

Expand Down
93 changes: 59 additions & 34 deletions tests/test_wrapper.py
Expand Up @@ -5,6 +5,7 @@
"""

from future.builtins import str, range
import importlib
import os
import numpy as np
from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -157,6 +158,7 @@ def test_store_and_load_dictdata():
for key, val in dval.items():
assert(res[dkey][key] == val)


def test_store_and_load_numpy_datatypes():
res = {}
res['float64'] = np.float64(f0)
Expand All @@ -166,7 +168,8 @@ def test_store_and_load_numpy_datatypes():
res = h5w.load(fn)
assert(isinstance(res['float64'], np.float64))
assert(isinstance(res['int64'], np.int64))



def test_overwrite_dataset():
res = {'a': 5}
h5w.save(fn, res, write_mode='w')
Expand Down Expand Up @@ -317,41 +320,63 @@ def test_file_close_on_exception():
h5w.save(fn, res, write_mode='w')


# This test is only run for Python 2 because version v0.0.1
# and 1.0.1 are not compatible with Python 3.
# Once there are releases compatible with Python 3, we have to
# change this.
@pytest.mark.skipif(sys.version_info >= (3, 0, 0),
reason='Previous release requires Python2')
def test_conversion_script():
try:
import h5py_wrapper_001.wrapper as h5w_001
except ImportError:
h5w_lib.get_previous_version('0.0.1')
import h5py_wrapper_001.wrapper as h5w_001

res = {key: value for key, value in zip(simpledata_str, simpledata_val)}
res.update({key: value for key, value in zip(arraydata_str, arraydata_val)})
h5w_001.add_to_h5(fn, res)
h5w_001.add_to_h5(fn2, res)
with open('conversion_list.txt', 'w') as f:
f.write(fn)
f.write('\n')
f.write(fn2)
# Specify list on command line
os.system('./convert_h5file {} {} --release=0.0.1'.format(fn, fn2))
# Read list of files from file and pipe into conversion script
os.system('cat conversion_list.txt | ./convert_h5file --release=0.0.1')
os.remove('conversion_list.txt')
# Find files based on pattern using `find` and pipe into conversion script
os.system('find -name "data*.h5" | ./convert_h5file --release=0.0.1')

res2 = h5w.load(fn)
for key, value in res.items():
if isinstance(res2[key], np.ndarray):
assert_array_equal(res2[key], value)
else:
assert(res2[key] == value)
if isinstance(res2[key], str):
assert(isinstance(res2[key], type(str(value))))
else:
assert(isinstance(res2[key], type(value)))
def test_conversion_script(tmpdir):
# convert LocalPath object to str to ensure that path can be
# handled by os.path.join()
path = str(tmpdir)

previous_versions = ['0.0.1', '1.0.1']
for version in previous_versions:
print(version)
version_stripped = version.replace('.', '')
release_base_name = '_'.join(('h5py_wrapper', version_stripped))
module = '.'.join((release_base_name, 'wrapper'))
try:
h5w_prev = importlib.import_module(module)
except ImportError:
h5w_lib.get_previous_version(version, tmpdir)
sys.path.append(path)
print(sys.path[-1])
print(module)
h5w_prev = importlib.import_module(module)

tmp_fn = os.path.join(path, fn)
tmp_fn2 = os.path.join(path, fn2)
res = {key: value for key, value in zip(simpledata_str, simpledata_val)}
res.update({key: value for key, value in zip(arraydata_str, arraydata_val)})
h5w_prev.add_to_h5(tmp_fn, res)
h5w_prev.add_to_h5(tmp_fn2, res)
with open('conversion_list.txt', 'w') as f:
f.write(tmp_fn)
f.write('\n')
f.write(tmp_fn2)

# Specify list on command line
os.system('./../convert_h5file {} {} --release={}'.format(tmp_fn, tmp_fn2, version))
# Read list of files from file and pipe into conversion script
os.system('cat conversion_list.txt | ./../convert_h5file --release={}'.format(version))
os.remove('conversion_list.txt')
# Find files based on pattern using `find` and pipe into conversion script
os.system('find -name "data*.h5" | ./../convert_h5file --release={}'.format(version))

res2 = h5w.load(tmp_fn)
for key, value in res.items():
if isinstance(res2[key], np.ndarray):
assert_array_equal(res2[key], value)
else:
assert(res2[key] == value)
if isinstance(res2[key], str):
assert(isinstance(res2[key], type(str(value))))
else:
assert(isinstance(res2[key], type(value)))
os.remove(tmp_fn)
os.remove(tmp_fn2)


def test_raises_error_for_dictlabel_and_path():
Expand Down

0 comments on commit 5562bfa

Please sign in to comment.