def update_cache(package_root, remote_packages, local_package_path,
                 refresh=False):
    """Updates the package cache at package_root for the given dictionary
    of packages as well as packages in the given local package path.
    """
    requires_wipe = False
    if refresh:
        click.echo('Force package cache refresh.')
        requires_wipe = True

    manifest_file = os.path.join(package_root, 'lektor-packages.manifest')
    local_packages = list_local_packages(local_package_path)

    old_manifest = load_manifest(manifest_file)
    to_install = []

    all_packages = dict(remote_packages)
    all_packages.update((x, None) for x in local_packages)

    # step 1: figure out which remote packages to install.
    for package, version in iteritems(remote_packages):
        old_version = old_manifest.pop(package, None)
        if old_version is None:
            to_install.append((package, version))
        elif old_version != version:
            requires_wipe = True

    # step 2: figure out which local packages to install
    for package in local_packages:
        if old_manifest.pop(package, False) is False:
            to_install.append((package, None))

    # Bad news, we need to wipe everything
    if requires_wipe or old_manifest:
        try:
            shutil.rmtree(package_root)
        except OSError:
            pass
        to_install = iteritems(all_packages)

    if to_install:
        click.echo('Updating packages in %s for project' % package_root)
        try:
            os.makedirs(package_root)
        except __HOLE__:
            pass
        for package, version in to_install:
            if package[:1] == '@':
                install_local_package(package_root,
                    os.path.join(local_package_path, package[1:]))
            else:
                download_and_install_package(package_root, package, version)
        write_manifest(manifest_file, all_packages)
dataset/ETHPy150Open lektor/lektor/lektor/packages.py/update_cache