# Buggy code
def path_shortener(path, short_paths):
    """short_paths is 4-state:
    False: Never shorten the path
    True: Always shorten the path, create link if not existing
    None: Use shorten path only if already exists, not create
    """
    if short_paths is False or os.getenv("CONAN_USER_HOME_SHORT") == "None":
        return path
    link = os.path.join(path, CONAN_LINK)
    if os.path.exists(link):
        return load(link)
    elif short_paths is None:
        return path

    short_home = os.getenv("CONAN_USER_HOME_SHORT")
    if not short_home:
        drive = os.path.splitdrive(path)[0]
        short_home = drive + "/.conan"
    mkdir(short_home)

    # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid
    # access problems in cygwin/msys2 windows subsystems when using short_home folder
    try:
        cmd = r'cacls %s /E /G "%s\%s":F' % (
            short_home,
            os.environ["USERDOMAIN"],
            os.environ["USERNAME"],
        )
        subprocess.check_output(
            cmd, stderr=subprocess.STDOUT
        )  # Ignoring any returned output, make command quiet
    except subprocess.CalledProcessError as e:
        # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.
        pass

    redirect = tempfile.mkdtemp(dir=short_home, prefix="")
    # This "1" is the way to have a non-existing directory, so commands like
    # shutil.copytree() to it, works. It can be removed without compromising the
    # temp folder generator and conan-links consistency
    redirect = os.path.join(redirect, "1")
    save(link, redirect)
    return redirect


# Traceback (KeyError)
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\conans\client\command.py", line 1187, in run
method(args[0][1:])
File "C:\Python27\lib\site-packages\conans\client\command.py", line 304, in install
install_folder=args.install_folder)
File "C:\Python27\lib\site-packages\conans\client\conan_api.py", line 61, in wrapper
return f(*args, **kwargs)
File "C:\Python27\lib\site-packages\conans\client\conan_api.py", line 444, in install
no_imports=no_imports)
File "C:\Python27\lib\site-packages\conans\client\manager.py", line 395, in install
installer.install(deps_graph, profile.build_requires, keep_build)
File "C:\Python27\lib\site-packages\conans\client\installer.py", line 262, in install
nodes_to_process = self._get_nodes(nodes_by_level, skip_private_nodes)
File "C:\Python27\lib\site-packages\conans\client\installer.py", line 501, in _get_nodes
check_outdated)
File "C:\Python27\lib\site-packages\conans\client\proxy.py", line 47, in package_available
package_folder = self._client_cache.package(package_ref, short_paths=short_paths)
File "C:\Python27\lib\site-packages\conans\paths.py", line 162, in package
return path_shortener(p, short_paths)
File "C:\Python27\lib\site-packages\conans\util\windows.py", line 57, in path_shortener
cmd = r'cacls %s /E /G "%s\%s":F' % (short_home, os.environ['USERDOMAIN'], os.environ['USERNAME'])
File "C:\Python27\lib\os.py", line 425, in __getitem__
return self.data[key.upper()]
KeyError: 'USERDOMAIN'

# Fixed code
def path_shortener(path, short_paths):
    """short_paths is 4-state:
    False: Never shorten the path
    True: Always shorten the path, create link if not existing
    None: Use shorten path only if already exists, not create
    """
    if short_paths is False or os.getenv("CONAN_USER_HOME_SHORT") == "None":
        return path
    link = os.path.join(path, CONAN_LINK)
    if os.path.exists(link):
        return load(link)
    elif short_paths is None:
        return path

    short_home = os.getenv("CONAN_USER_HOME_SHORT")
    if not short_home:
        drive = os.path.splitdrive(path)[0]
        short_home = drive + "/.conan"
    mkdir(short_home)

    # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid
    # access problems in cygwin/msys2 windows subsystems when using short_home folder
    try:
        username = os.getenv("USERDOMAIN")
        domainname = (
            "%s\%s" % (username, os.environ["USERNAME"])
            if username
            else os.environ["USERNAME"]
        )
        cmd = r'cacls %s /E /G "%s":F' % (short_home, domainname)
        subprocess.check_output(
            cmd, stderr=subprocess.STDOUT
        )  # Ignoring any returned output, make command quiet
    except subprocess.CalledProcessError:
        # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.
        pass

    redirect = tempfile.mkdtemp(dir=short_home, prefix="")
    # This "1" is the way to have a non-existing directory, so commands like
    # shutil.copytree() to it, works. It can be removed without compromising the
    # temp folder generator and conan-links consistency
    redirect = os.path.join(redirect, "1")
    save(link, redirect)
    return redirect