Skip to content

Commit

Permalink
Merge pull request #302 from qwhelan/hash_uniqueness
Browse files Browse the repository at this point in the history
Add utility function to enforce hash prefix uniqueness
  • Loading branch information
pv committed Aug 27, 2015
2 parents 96c49b7 + fcb65e5 commit 8819998
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
33 changes: 31 additions & 2 deletions asv/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ def iter_results_for_machine_and_hash(results, machine_name, commit):
Iterate over all of the result files with a given hash for a
particular machine.
"""
full_commit = get_result_hash_from_prefix(results, machine_name, commit)

for (root, filename) in iter_results_paths(
os.path.join(results, machine_name)):
results_commit = filename.split('-')[0]
cmp_len = min(len(commit), len(results_commit))
if results_commit[:cmp_len] == commit[:cmp_len]:
if results_commit == full_commit:
yield Results.load(os.path.join(root, filename))


Expand All @@ -80,6 +81,34 @@ def get_existing_hashes(results):
return hashes


def get_result_hash_from_prefix(results, machine_name, commit_prefix):
"""
Get the 8-char result commit identifier from a potentially shorter
prefix. Only considers the set of commits that have had
results computed.
Returns None if there are no matches. Raises a UserError
if the prefix is non-unique.
"""
commits = set([])

for (root, filename) in iter_results_paths(os.path.join(results,
machine_name)):
results_commit = filename.split('-')[0]
cmp_len = min(len(commit_prefix), len(results_commit))
if results_commit[:cmp_len] == commit_prefix[:cmp_len]:
commits.add(results_commit)

if len(commits) > 1:
commit_list_str = ', '.join(sorted(commits))
raise util.UserError('Git hash prefix could represent one of ' +
'multiple commits: {0}'.format(commit_list_str))
elif len(commits) == 1:
return list(commits)[0]
else:
return None


def find_latest_result_hash(machine, root, hashes=None):
"""
Find the latest result for the given machine.
Expand Down
26 changes: 25 additions & 1 deletion test/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
from os.path import join

import six

from asv import results
from asv import results, util
import pytest


def test_results(tmpdir):
Expand All @@ -35,3 +37,25 @@ def test_results(tmpdir):
assert r2._results == r._results
assert r2.date == r.date
assert r2.commit_hash == r.commit_hash


def test_get_result_hash_from_prefix(tmpdir):
results_dir = tmpdir.mkdir('results')
machine_dir = results_dir.mkdir('machine')

for f in ['e5b6cdbc', 'e5bfoo12']:
open(join(str(machine_dir), '{0}-py2.7-Cython-numpy1.8.json'.format(f)), 'a').close()

# check unique, valid case
full_commit = results.get_result_hash_from_prefix(str(results_dir), 'machine', 'e5b6')
assert full_commit == 'e5b6cdbc'

# check invalid hash case
bad_commit = results.get_result_hash_from_prefix(str(results_dir), 'machine', 'foobar')
assert bad_commit is None

# check non-unique case
with pytest.raises(util.UserError) as excinfo:
results.get_result_hash_from_prefix(str(results_dir), 'machine', 'e')

assert 'one of multiple commits' in str(excinfo.value)

0 comments on commit 8819998

Please sign in to comment.