def needs_rebuild(spec, mirror_url, rebuild_on_errors=False):
    if (not spec.concrete):
        raise ValueError('spec must be concrete to check against mirror')
    pkg_name = spec.name
    pkg_version = spec.version
    pkg_hash = spec.dag_hash()
    pkg_full_hash = spec.full_hash()
    tty.debug('Checking {0}-{1}, dag_hash = {2}, full_hash = {3}'.format(pkg_name, pkg_version, pkg_hash, pkg_full_hash))
    tty.debug(spec.tree())
    cache_prefix = build_cache_prefix(mirror_url)
    spec_yaml_file_name = tarball_name(spec, '.spec.yaml')
    file_path = os.path.join(cache_prefix, spec_yaml_file_name)
    result_of_error = 'Package ({0}) will {1}be rebuilt'.format(spec.short_spec, ('' if rebuild_on_errors else 'not '))
    try:
        (_, _, yaml_file) = web_util.read_from_url(file_path)
        yaml_contents = codecs.getreader('utf-8')(yaml_file).read()
    except (URLError, web_util.SpackWebError) as url_err:
        err_msg = ['Unable to determine whether {0} needs rebuilding,', ' caught exception attempting to read from {1}.']
        tty.error(''.join(err_msg).format(spec.short_spec, file_path))
        tty.debug(url_err)
        tty.warn(result_of_error)
        return rebuild_on_errors
    if (not yaml_contents):
        tty.error('Reading {0} returned nothing'.format(file_path))
        tty.warn(result_of_error)
        return rebuild_on_errors
    spec_yaml = syaml.load(yaml_contents)
    if (('full_hash' not in spec_yaml) or (spec_yaml['full_hash'] != pkg_full_hash)):
        if ('full_hash' in spec_yaml):
            reason = 'hash mismatch, remote = {0}, local = {1}'.format(spec_yaml['full_hash'], pkg_full_hash)
        else:
            reason = 'full_hash was missing from remote spec.yaml'
        tty.msg('Rebuilding {0}, reason: {1}'.format(spec.short_spec, reason))
        tty.msg(spec.tree())
        return True
    return False
def needs_rebuild(spec, mirror_url, rebuild_on_errors=False):
    if (not spec.concrete):
        raise ValueError('spec must be concrete to check against mirror')
    pkg_name = spec.name
    pkg_version = spec.version
    pkg_hash = spec.dag_hash()
    pkg_full_hash = spec.full_hash()
    tty.debug('Checking {0}-{1}, dag_hash = {2}, full_hash = {3}'.format(pkg_name, pkg_version, pkg_hash, pkg_full_hash))
    tty.debug(spec.tree())
    cache_prefix = build_cache_prefix(mirror_url)
    spec_yaml_file_name = tarball_name(spec, '.spec.yaml')
    file_path = os.path.join(cache_prefix, spec_yaml_file_name)
    result_of_error = 'Package ({0}) will {1}be rebuilt'.format(spec.short_spec, ( if rebuild_on_errors else 'not '))
    try:
        (_, _, yaml_file) = web_util.read_from_url(file_path)
        yaml_contents = codecs.getreader('utf-8')(yaml_file).read()
    except (URLError, web_util.SpackWebError) as url_err:
        err_msg = ['Unable to determine whether {0} needs rebuilding,', ' caught exception attempting to read from {1}.']
        tty.error(.join(err_msg).format(spec.short_spec, file_path))
        tty.debug(url_err)
        tty.warn(result_of_error)
        return rebuild_on_errors
    if (not yaml_contents):
        tty.error('Reading {0} returned nothing'.format(file_path))
        tty.warn(result_of_error)
        return rebuild_on_errors
    spec_yaml = syaml.load(yaml_contents)
    if (('full_hash' not in spec_yaml) or (spec_yaml['full_hash'] != pkg_full_hash)):
        if ('full_hash' in spec_yaml):
            reason = 'hash mismatch, remote = {0}, local = {1}'.format(spec_yaml['full_hash'], pkg_full_hash)
        else:
            reason = 'full_hash was missing from remote spec.yaml'
        tty.msg('Rebuilding {0}, reason: {1}'.format(spec.short_spec, reason))
        tty.msg(spec.tree())
        return True
    return False