# Buggy code
def url_channel(url):
    if url is None:
        return None, "<unknown>"
    channel = url.rsplit("/", 2)[0]
    schannel = canonical_channel_name(channel)
    if url.startswith("file://") and schannel != "local":
        channel = schannel = url.rsplit("/", 1)[0]
    return channel, schannel


# Traceback (KeyError)
[root@42d0710d45b6 /]# conda index /opt/packages/linux-64/
updating index in: /opt/packages/linux-64
[root@42d0710d45b6 /]# ls /opt/packages/linux-64/
numpy-1.11.0-py35_1.tar.bz2  repodata.json  repodata.json.bz2
[root@42d0710d45b6 /]# conda install -c file:///opt/packages --yes numpy
Fetching package metadata .........
Solving package specifications: ..........

Package plan for installation in environment /opt/conda:

The following packages will be downloaded:

package                    |            build
---------------------------|-----------------
numpy-1.11.0               |           py35_1         6.1 MB  file:///opt/packages

The following NEW packages will be INSTALLED:

numpy: 1.11.0-py35_1 file:///opt/packages

Pruning fetched packages from the cache ...
Fetching packages ...
numpy-1.11.0-p 100% |#########################################################################################################| Time: 0:00:00 164.40 MB/s
Extracting packages ...
An unexpected error has occurred, please consider sending the                                                                                      |   0%
following traceback to the conda GitHub issue tracker at:

https://github.com/conda/conda/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
File "/opt/conda/bin/conda", line 6, in <module>
sys.exit(main())
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main_install.py", line 69, in execute
install(args, parser, 'install')
File "/opt/conda/lib/python3.5/site-packages/conda/cli/install.py", line 407, in install
execute_actions(actions, index, verbose=not args.quiet)
File "/opt/conda/lib/python3.5/site-packages/conda/plan.py", line 589, in execute_actions
inst.execute_instructions(plan, index, verbose)
File "/opt/conda/lib/python3.5/site-packages/conda/instructions.py", line 137, in execute_instructions
cmd(state, arg)
File "/opt/conda/lib/python3.5/site-packages/conda/instructions.py", line 60, in EXTRACT_CMD
extract(arg)
File "/opt/conda/lib/python3.5/site-packages/conda/install.py", line 833, in extract
rec = package_cache()[dist]
KeyError: 'file:///opt/packages::numpy-1.11.0-py35_1'

# Fixed code
def url_channel(url):
    parts = (url or "").rsplit("/", 2)
    if len(parts) == 1:
        return "<unknown>", "<unknown>"
    if len(parts) == 2:
        return parts[0], parts[0]
    if url.startswith("file://") and parts[1] not in ("noarch", subdir):
        # Explicit file-based URLs are denoted with a '/' in the schannel
        channel = parts[0] + "/" + parts[1]
        schannel = channel + "/"
    else:
        channel = parts[0]
        schannel = canonical_channel_name(channel)
    return channel, schannel