Skip to content
Snippets Groups Projects

tests compare against git hash, eliminate cached .h5

Merged Jameson Rollins requested to merge jameson.rollins/pygwinc:test-git-ref into master
All threads resolved!
Files
2
+ 91
17
import os
import sys
import glob
import shutil
import signal
import logging
import tempfile
@@ -28,13 +29,64 @@ logging.basicConfig(
FREQ = np.logspace(np.log10(5), np.log10(6000), 3000)
TOLERANCE = 1e-6
CACHE_LIMIT = 5
def test_path(*args):
"""Return path to package file."""
return os.path.join(os.path.dirname(__file__), *args)
def gen_cache(ref_hash, path):
def git_ref_resolve_hash(git_ref):
"""Resolve a git reference into its hash string."""
try:
return subprocess.run(
['git', 'show', '-s', '--format=format:%H', git_ref],
capture_output=True, universal_newlines=True,
).stdout
except subprocess.CalledProcessError:
return None
def write_ref_hash(ref_hash):
"""Write ref hash to reference file
"""
with open(test_path('ref_hash'), 'w') as f:
f.write('{}\n'.format(ref_hash))
def load_ref_hash():
"""Load the current reference git hash.
"""
try:
with open(test_path('ref_hash')) as f:
return f.read().strip()
except IOError:
return None
def prune_cache_dir():
"""Prune all but the N most recently accessed caches.
"""
cache_dir = test_path('cache')
if not os.path.exists(cache_dir):
return
expired_paths = sorted(
[os.path.join(cache_dir, path) for path in os.listdir(cache_dir)],
key=lambda path: os.stat(path).st_atime,
)[CACHE_LIMIT:]
if not expired_paths:
return
logging.info("pruning {} old caches...".format(len(expired_paths)))
for path in expired_paths:
logging.debug("pruning {}...".format(path))
shutil.rmtree(path)
def gen_cache_for_ref(ref_hash, path):
"""generate cache from git reference
The ref_hash should be a git hash, and path should be the location
@@ -47,7 +99,8 @@ def gen_cache(ref_hash, path):
"""
logging.info("creating new cache from reference {}...".format(ref_hash))
subprocess.run(
[test_path('gen_cache.sh'), ref_hash, path]
[test_path('gen_cache.sh'), ref_hash, path],
check=True,
)
@@ -65,7 +118,7 @@ def load_cache(path):
ref_hash = f.read().strip()
else:
ref_hash = None
logging.info("cache git hash: {}".format(ref_hash))
logging.debug("cache hash: {}".format(ref_hash))
cache['ref_hash'] = ref_hash
cache['ifos'] = {}
for f in sorted(os.listdir(path)):
@@ -213,31 +266,52 @@ def main():
help='specific ifos to test (default all)')
args = parser.parse_args()
# get the current hash of HEAD
head_hash = git_ref_resolve_hash('HEAD')
if not head_hash:
logging.warning("could not determine git HEAD hash.")
# update the reference if specified
if args.update_ref:
if args.update_ref == 'HEAD':
ref_hash = subprocess.run(
['git', 'show', '-s', '--format=format:%H', 'HEAD'],
capture_output=True, universal_newlines=True,
).stdout
if not head_hash:
sys.exit("Could not update reference to head.")
logging.info("updating reference to HEAD...")
ref_hash = head_hash
else:
ref_hash = args.update_ref
logging.info("updating reference git hash to {}...".format(ref_hash))
with open(test_path('ref_hash'), 'w') as f:
f.write('{}\n'.format(ref_hash))
ref_hash = git_ref_resolve_hash(args.update_ref)
logging.info("updating reference git hash: {}".format(ref_hash))
write_ref_hash(ref_hash)
sys.exit()
# get the reference hash
if args.git_ref:
ref_hash = args.git_ref
elif os.path.exists(test_path('ref_hash')):
with open(test_path('ref_hash')) as f:
ref_hash = f.read().strip()
ref_hash = git_ref_resolve_hash(args.git_ref)
else:
sys.exit("Unspecified reference git hash, could not run test.")
ref_hash = load_ref_hash()
if not ref_hash:
pass
try:
with open(test_path('ref_hash')) as f:
ref_hash = f.read().strip()
except IOError:
logging.warning("could not open reference")
sys.exit("Unspecified reference git hash, could not run test.")
logging.info("head hash: {}".format(head_hash))
logging.info("ref hash: {}".format(ref_hash))
# don't bother test if hashes match
if ref_hash == head_hash:
logging.info("HEAD matches reference, not bothering to calculate.")
logging.info("Use --git-ref to compare against an arbitrary git commit.")
sys.exit()
# load the cache
cache_path = test_path('cache', ref_hash)
if not os.path.exists(cache_path):
gen_cache(ref_hash, cache_path)
prune_cache_dir()
gen_cache_for_ref(ref_hash, cache_path)
cache = load_cache(cache_path)
if args.report:
Loading