def relocate_package(spec, allow_root):
    '\n    Relocate the given package\n    '
    workdir = str(spec.prefix)
    buildinfo = read_buildinfo_file(workdir)
    new_layout_root = str(spack.store.layout.root)
    new_prefix = str(spec.prefix)
    new_rel_prefix = str(os.path.relpath(new_prefix, new_layout_root))
    new_spack_prefix = str(spack.paths.prefix)
    old_layout_root = str(buildinfo['buildpath'])
    old_spack_prefix = str(buildinfo.get('spackprefix'))
    old_rel_prefix = buildinfo.get('relative_prefix')
    old_prefix = os.path.join(old_layout_root, old_rel_prefix)
    rel = buildinfo.get('relative_rpaths')
    prefix_to_hash = buildinfo.get('prefix_to_hash', None)
    if ((old_rel_prefix != new_rel_prefix) and (not prefix_to_hash)):
        msg = 'Package tarball was created from an install '
        msg += 'prefix with a different directory layout and an older '
        msg += 'buildcache create implementation. It cannot be relocated.'
        raise NewLayoutException(msg)
    if (not prefix_to_hash):
        prefix_to_hash = dict()
    hash_to_prefix = dict()
    hash_to_prefix[spec.format('{hash}')] = str(spec.package.prefix)
    new_deps = spack.build_environment.get_rpath_deps(spec.package)
    for d in new_deps:
        hash_to_prefix[d.format('{hash}')] = str(d.prefix)
    prefix_to_prefix = dict()
    for (orig_prefix, hash) in prefix_to_hash.items():
        prefix_to_prefix[orig_prefix] = hash_to_prefix.get(hash, None)
    prefix_to_prefix[old_prefix] = new_prefix
    prefix_to_prefix[old_layout_root] = new_layout_root
    tty.debug('Relocating package from', ('%s to %s.' % (old_layout_root, new_layout_root)))

    def is_backup_file(file):
        return file.endswith('~')
    text_names = list()
    for filename in buildinfo['relocate_textfiles']:
        text_name = os.path.join(workdir, filename)
        if (not is_backup_file(text_name)):
            text_names.append(text_name)
    if (old_layout_root != new_layout_root):
        paths_to_relocate = [old_spack_prefix, old_layout_root]
        paths_to_relocate.extend(prefix_to_hash.keys())
        files_to_relocate = list(filter((lambda pathname: (not relocate.file_is_relocatable(pathname, paths_to_relocate=paths_to_relocate))), map((lambda filename: os.path.join(workdir, filename)), buildinfo['relocate_binaries'])))
        if ((spec.architecture.platform == 'darwin') or ((spec.architecture.platform == 'test') and (platform.system().lower() == 'darwin'))):
            relocate.relocate_macho_binaries(files_to_relocate, old_layout_root, new_layout_root, prefix_to_prefix, rel, old_prefix, new_prefix)
        if ((spec.architecture.platform == 'linux') or ((spec.architecture.platform == 'test') and (platform.system().lower() == 'linux'))):
            relocate.relocate_elf_binaries(files_to_relocate, old_layout_root, new_layout_root, prefix_to_prefix, rel, old_prefix, new_prefix)
            link_names = [linkname for linkname in buildinfo.get('relocate_links', [])]
            relocate.relocate_links(link_names, old_layout_root, new_layout_root, old_prefix, new_prefix, prefix_to_prefix)
        relocate.relocate_text(text_names, old_layout_root, new_layout_root, old_prefix, new_prefix, old_spack_prefix, new_spack_prefix, prefix_to_prefix)
        relocate.relocate_text_bin(files_to_relocate, old_layout_root, new_layout_root, old_prefix, new_prefix, old_spack_prefix, new_spack_prefix, prefix_to_prefix)
Relocate the given package
def relocate_package(spec, allow_root):
    '\n    \n    '
    workdir = str(spec.prefix)
    buildinfo = read_buildinfo_file(workdir)
    new_layout_root = str(spack.store.layout.root)
    new_prefix = str(spec.prefix)
    new_rel_prefix = str(os.path.relpath(new_prefix, new_layout_root))
    new_spack_prefix = str(spack.paths.prefix)
    old_layout_root = str(buildinfo['buildpath'])
    old_spack_prefix = str(buildinfo.get('spackprefix'))
    old_rel_prefix = buildinfo.get('relative_prefix')
    old_prefix = os.path.join(old_layout_root, old_rel_prefix)
    rel = buildinfo.get('relative_rpaths')
    prefix_to_hash = buildinfo.get('prefix_to_hash', None)
    if ((old_rel_prefix != new_rel_prefix) and (not prefix_to_hash)):
        msg = 'Package tarball was created from an install '
        msg += 'prefix with a different directory layout and an older '
        msg += 'buildcache create implementation. It cannot be relocated.'
        raise NewLayoutException(msg)
    if (not prefix_to_hash):
        prefix_to_hash = dict()
    hash_to_prefix = dict()
    hash_to_prefix[spec.format('{hash}')] = str(spec.package.prefix)
    new_deps = spack.build_environment.get_rpath_deps(spec.package)
    for d in new_deps:
        hash_to_prefix[d.format('{hash}')] = str(d.prefix)
    prefix_to_prefix = dict()
    for (orig_prefix, hash) in prefix_to_hash.items():
        prefix_to_prefix[orig_prefix] = hash_to_prefix.get(hash, None)
    prefix_to_prefix[old_prefix] = new_prefix
    prefix_to_prefix[old_layout_root] = new_layout_root
    tty.debug('Relocating package from', ('%s to %s.' % (old_layout_root, new_layout_root)))

    def is_backup_file(file):
        return file.endswith('~')
    text_names = list()
    for filename in buildinfo['relocate_textfiles']:
        text_name = os.path.join(workdir, filename)
        if (not is_backup_file(text_name)):
            text_names.append(text_name)
    if (old_layout_root != new_layout_root):
        paths_to_relocate = [old_spack_prefix, old_layout_root]
        paths_to_relocate.extend(prefix_to_hash.keys())
        files_to_relocate = list(filter((lambda pathname: (not relocate.file_is_relocatable(pathname, paths_to_relocate=paths_to_relocate))), map((lambda filename: os.path.join(workdir, filename)), buildinfo['relocate_binaries'])))
        if ((spec.architecture.platform == 'darwin') or ((spec.architecture.platform == 'test') and (platform.system().lower() == 'darwin'))):
            relocate.relocate_macho_binaries(files_to_relocate, old_layout_root, new_layout_root, prefix_to_prefix, rel, old_prefix, new_prefix)
        if ((spec.architecture.platform == 'linux') or ((spec.architecture.platform == 'test') and (platform.system().lower() == 'linux'))):
            relocate.relocate_elf_binaries(files_to_relocate, old_layout_root, new_layout_root, prefix_to_prefix, rel, old_prefix, new_prefix)
            link_names = [linkname for linkname in buildinfo.get('relocate_links', [])]
            relocate.relocate_links(link_names, old_layout_root, new_layout_root, old_prefix, new_prefix, prefix_to_prefix)
        relocate.relocate_text(text_names, old_layout_root, new_layout_root, old_prefix, new_prefix, old_spack_prefix, new_spack_prefix, prefix_to_prefix)
        relocate.relocate_text_bin(files_to_relocate, old_layout_root, new_layout_root, old_prefix, new_prefix, old_spack_prefix, new_spack_prefix, prefix_to_prefix)