prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  KeyError USERDOMAIN on `conan install`
  We have at least two machines for which `conan install` errors out with the message:
  
  ```
  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'
  ```
  
  Defining an environment variable `USERDOMAIN` to any value fixes the problem.
  
  Both machines are Windows 7, with Conan 1.2.3.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `conans/util/windows.py`
  Content:
  ```
  1 import os
  2 import subprocess
  3 
  4 from conans.util.files import load, mkdir, save, rmdir
  5 import tempfile
  6 
  7 
  8 CONAN_LINK = ".conan_link"
  9 
  10 
  11 def conan_expand_user(path):
  12     """ wrapper to the original expanduser function, to workaround python returning
  13     verbatim %USERPROFILE% when some other app (git for windows) sets HOME envvar
  14     """
  15     # In win these variables should exist and point to user directory, which
  16     # must exist. Using context to avoid permanent modification of os.environ
  17     old_env = dict(os.environ)
  18     try:
  19         home = os.environ.get("HOME")
  20         # Problematic cases of wrong HOME variable
  21         # - HOME = %USERPROFILE% verbatim, as messed by some other tools
  22         # - MSYS console, that defines a different user home in /c/mingw/msys/users/xxx
  23         # In these cases, it is safe to remove it and rely on USERPROFILE directly
  24         if home and (not os.path.exists(home) or
  25                      (os.getenv("MSYSTEM") and os.getenv("USERPROFILE"))):
  26             del os.environ["HOME"]
  27         result = os.path.expanduser(path)
  28     finally:
  29         os.environ.clear()
  30         os.environ.update(old_env)
  31     return result
  32 
  33 
  34 def path_shortener(path, short_paths):
  35     """ short_paths is 4-state:
  36     False: Never shorten the path
  37     True: Always shorten the path, create link if not existing
  38     None: Use shorten path only if already exists, not create
  39     """
  40     if short_paths is False or os.getenv("CONAN_USER_HOME_SHORT") == "None":
  41         return path
  42     link = os.path.join(path, CONAN_LINK)
  43     if os.path.exists(link):
  44         return load(link)
  45     elif short_paths is None:
  46         return path
  47 
  48     short_home = os.getenv("CONAN_USER_HOME_SHORT")
  49     if not short_home:
  50         drive = os.path.splitdrive(path)[0]
  51         short_home = drive + "/.conan"
  52     mkdir(short_home)
  53 
  54     # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid
  55     # access problems in cygwin/msys2 windows subsystems when using short_home folder
  56     try:
  57         cmd = r'cacls %s /E /G "%s\%s":F' % (short_home, os.environ['USERDOMAIN'], os.environ['USERNAME'])
  58         subprocess.check_output(cmd, stderr=subprocess.STDOUT)  # Ignoring any returned output, make command quiet
  59     except subprocess.CalledProcessError as e:
  60         # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.
  61         pass
  62 
  63     redirect = tempfile.mkdtemp(dir=short_home, prefix="")
  64     # This "1" is the way to have a non-existing directory, so commands like
  65     # shutil.copytree() to it, works. It can be removed without compromising the
  66     # temp folder generator and conan-links consistency
  67     redirect = os.path.join(redirect, "1")
  68     save(link, redirect)
  69     return redirect
  70 
  71 
  72 def ignore_long_path_files(src_folder, build_folder, output):
  73     def _filter(src, files):
  74         filtered_files = []
  75         for the_file in files:
  76             source_path = os.path.join(src, the_file)
  77             # Without storage path, just relative
  78             rel_path = os.path.relpath(source_path, src_folder)
  79             dest_path = os.path.normpath(os.path.join(build_folder, rel_path))
  80             # it is NOT that "/" is counted as "\\" so it counts double
  81             # seems a bug in python, overflows paths near the limit of 260,
  82             if len(dest_path) >= 249:
  83                 filtered_files.append(the_file)
  84                 output.warn("Filename too long, file excluded: %s" % dest_path)
  85         return filtered_files
  86     return _filter
  87 
  88 
  89 def rm_conandir(path):
  90     """removal of a directory that might contain a link to a short path"""
  91     link = os.path.join(path, CONAN_LINK)
  92     if os.path.exists(link):
  93         short_path = load(link)
  94         rmdir(os.path.dirname(short_path))
  95     rmdir(path)
  96
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/conans/util/windows.py b/conans/util/windows.py
  --- a/conans/util/windows.py
  +++ b/conans/util/windows.py
  @@ -54,9 +54,11 @@
       # 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'])
  +        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 as e:
  +    except subprocess.CalledProcessError:
           # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.
           pass
  
verification_info: {"golden_diff": "diff --git a/conans/util/windows.py b/conans/util/windows.py\n--- a/conans/util/windows.py\n+++ b/conans/util/windows.py\n@@ -54,9 +54,11 @@\n     # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid\n     # access problems in cygwin/msys2 windows subsystems when using short_home folder\n     try:\n-        cmd = r'cacls %s /E /G \"%s\\%s\":F' % (short_home, os.environ['USERDOMAIN'], os.environ['USERNAME'])\n+        username = os.getenv(\"USERDOMAIN\")\n+        domainname = \"%s\\%s\" % (username, os.environ[\"USERNAME\"]) if username else os.environ[\"USERNAME\"]\n+        cmd = r'cacls %s /E /G \"%s\":F' % (short_home, domainname)\n         subprocess.check_output(cmd, stderr=subprocess.STDOUT)  # Ignoring any returned output, make command quiet\n-    except subprocess.CalledProcessError as e:\n+    except subprocess.CalledProcessError:\n         # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.\n         pass\n", "issue": "KeyError USERDOMAIN on `conan install`\nWe have at least two machines for which `conan install` errors out with the message:\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\command.py\", line 1187, in run\r\n    method(args[0][1:])\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\command.py\", line 304, in install\r\n    install_folder=args.install_folder)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\conan_api.py\", line 61, in wrapper\r\n    return f(*args, **kwargs)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\conan_api.py\", line 444, in install\r\n    no_imports=no_imports)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\manager.py\", line 395, in install\r\n    installer.install(deps_graph, profile.build_requires, keep_build)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\installer.py\", line 262, in install\r\n    nodes_to_process = self._get_nodes(nodes_by_level, skip_private_nodes)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\installer.py\", line 501, in _get_nodes\r\n    check_outdated)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\client\\proxy.py\", line 47, in package_available\r\n    package_folder = self._client_cache.package(package_ref, short_paths=short_paths)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\paths.py\", line 162, in package\r\n    return path_shortener(p, short_paths)\r\n  File \"C:\\Python27\\lib\\site-packages\\conans\\util\\windows.py\", line 57, in path_shortener\r\n    cmd = r'cacls %s /E /G \"%s\\%s\":F' % (short_home, os.environ['USERDOMAIN'], os.environ['USERNAME'])\r\n  File \"C:\\Python27\\lib\\os.py\", line 425, in __getitem__\r\n    return self.data[key.upper()]\r\nKeyError: 'USERDOMAIN'\r\n```\r\n\r\nDefining an environment variable `USERDOMAIN` to any value fixes the problem.\r\n\r\nBoth machines are Windows 7, with Conan 1.2.3.\n", "before_files": [{"content": "import os\nimport subprocess\n\nfrom conans.util.files import load, mkdir, save, rmdir\nimport tempfile\n\n\nCONAN_LINK = \".conan_link\"\n\n\ndef conan_expand_user(path):\n    \"\"\" wrapper to the original expanduser function, to workaround python returning\n    verbatim %USERPROFILE% when some other app (git for windows) sets HOME envvar\n    \"\"\"\n    # In win these variables should exist and point to user directory, which\n    # must exist. Using context to avoid permanent modification of os.environ\n    old_env = dict(os.environ)\n    try:\n        home = os.environ.get(\"HOME\")\n        # Problematic cases of wrong HOME variable\n        # - HOME = %USERPROFILE% verbatim, as messed by some other tools\n        # - MSYS console, that defines a different user home in /c/mingw/msys/users/xxx\n        # In these cases, it is safe to remove it and rely on USERPROFILE directly\n        if home and (not os.path.exists(home) or\n                     (os.getenv(\"MSYSTEM\") and os.getenv(\"USERPROFILE\"))):\n            del os.environ[\"HOME\"]\n        result = os.path.expanduser(path)\n    finally:\n        os.environ.clear()\n        os.environ.update(old_env)\n    return result\n\n\ndef path_shortener(path, short_paths):\n    \"\"\" short_paths is 4-state:\n    False: Never shorten the path\n    True: Always shorten the path, create link if not existing\n    None: Use shorten path only if already exists, not create\n    \"\"\"\n    if short_paths is False or os.getenv(\"CONAN_USER_HOME_SHORT\") == \"None\":\n        return path\n    link = os.path.join(path, CONAN_LINK)\n    if os.path.exists(link):\n        return load(link)\n    elif short_paths is None:\n        return path\n\n    short_home = os.getenv(\"CONAN_USER_HOME_SHORT\")\n    if not short_home:\n        drive = os.path.splitdrive(path)[0]\n        short_home = drive + \"/.conan\"\n    mkdir(short_home)\n\n    # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid\n    # access problems in cygwin/msys2 windows subsystems when using short_home folder\n    try:\n        cmd = r'cacls %s /E /G \"%s\\%s\":F' % (short_home, os.environ['USERDOMAIN'], os.environ['USERNAME'])\n        subprocess.check_output(cmd, stderr=subprocess.STDOUT)  # Ignoring any returned output, make command quiet\n    except subprocess.CalledProcessError as e:\n        # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.\n        pass\n\n    redirect = tempfile.mkdtemp(dir=short_home, prefix=\"\")\n    # This \"1\" is the way to have a non-existing directory, so commands like\n    # shutil.copytree() to it, works. It can be removed without compromising the\n    # temp folder generator and conan-links consistency\n    redirect = os.path.join(redirect, \"1\")\n    save(link, redirect)\n    return redirect\n\n\ndef ignore_long_path_files(src_folder, build_folder, output):\n    def _filter(src, files):\n        filtered_files = []\n        for the_file in files:\n            source_path = os.path.join(src, the_file)\n            # Without storage path, just relative\n            rel_path = os.path.relpath(source_path, src_folder)\n            dest_path = os.path.normpath(os.path.join(build_folder, rel_path))\n            # it is NOT that \"/\" is counted as \"\\\\\" so it counts double\n            # seems a bug in python, overflows paths near the limit of 260,\n            if len(dest_path) >= 249:\n                filtered_files.append(the_file)\n                output.warn(\"Filename too long, file excluded: %s\" % dest_path)\n        return filtered_files\n    return _filter\n\n\ndef rm_conandir(path):\n    \"\"\"removal of a directory that might contain a link to a short path\"\"\"\n    link = os.path.join(path, CONAN_LINK)\n    if os.path.exists(link):\n        short_path = load(link)\n        rmdir(os.path.dirname(short_path))\n    rmdir(path)\n", "path": "conans/util/windows.py"}], "after_files": [{"content": "import os\nimport subprocess\n\nfrom conans.util.files import load, mkdir, save, rmdir\nimport tempfile\n\n\nCONAN_LINK = \".conan_link\"\n\n\ndef conan_expand_user(path):\n    \"\"\" wrapper to the original expanduser function, to workaround python returning\n    verbatim %USERPROFILE% when some other app (git for windows) sets HOME envvar\n    \"\"\"\n    # In win these variables should exist and point to user directory, which\n    # must exist. Using context to avoid permanent modification of os.environ\n    old_env = dict(os.environ)\n    try:\n        home = os.environ.get(\"HOME\")\n        # Problematic cases of wrong HOME variable\n        # - HOME = %USERPROFILE% verbatim, as messed by some other tools\n        # - MSYS console, that defines a different user home in /c/mingw/msys/users/xxx\n        # In these cases, it is safe to remove it and rely on USERPROFILE directly\n        if home and (not os.path.exists(home) or\n                     (os.getenv(\"MSYSTEM\") and os.getenv(\"USERPROFILE\"))):\n            del os.environ[\"HOME\"]\n        result = os.path.expanduser(path)\n    finally:\n        os.environ.clear()\n        os.environ.update(old_env)\n    return result\n\n\ndef path_shortener(path, short_paths):\n    \"\"\" short_paths is 4-state:\n    False: Never shorten the path\n    True: Always shorten the path, create link if not existing\n    None: Use shorten path only if already exists, not create\n    \"\"\"\n    if short_paths is False or os.getenv(\"CONAN_USER_HOME_SHORT\") == \"None\":\n        return path\n    link = os.path.join(path, CONAN_LINK)\n    if os.path.exists(link):\n        return load(link)\n    elif short_paths is None:\n        return path\n\n    short_home = os.getenv(\"CONAN_USER_HOME_SHORT\")\n    if not short_home:\n        drive = os.path.splitdrive(path)[0]\n        short_home = drive + \"/.conan\"\n    mkdir(short_home)\n\n    # Workaround for short_home living in NTFS file systems. Give full control permission to current user to avoid\n    # access problems in cygwin/msys2 windows subsystems when using short_home folder\n    try:\n        username = os.getenv(\"USERDOMAIN\")\n        domainname = \"%s\\%s\" % (username, os.environ[\"USERNAME\"]) if username else os.environ[\"USERNAME\"]\n        cmd = r'cacls %s /E /G \"%s\":F' % (short_home, domainname)\n        subprocess.check_output(cmd, stderr=subprocess.STDOUT)  # Ignoring any returned output, make command quiet\n    except subprocess.CalledProcessError:\n        # cmd can fail if trying to set ACL in non NTFS drives, ignoring it.\n        pass\n\n    redirect = tempfile.mkdtemp(dir=short_home, prefix=\"\")\n    # This \"1\" is the way to have a non-existing directory, so commands like\n    # shutil.copytree() to it, works. It can be removed without compromising the\n    # temp folder generator and conan-links consistency\n    redirect = os.path.join(redirect, \"1\")\n    save(link, redirect)\n    return redirect\n\n\ndef ignore_long_path_files(src_folder, build_folder, output):\n    def _filter(src, files):\n        filtered_files = []\n        for the_file in files:\n            source_path = os.path.join(src, the_file)\n            # Without storage path, just relative\n            rel_path = os.path.relpath(source_path, src_folder)\n            dest_path = os.path.normpath(os.path.join(build_folder, rel_path))\n            # it is NOT that \"/\" is counted as \"\\\\\" so it counts double\n            # seems a bug in python, overflows paths near the limit of 260,\n            if len(dest_path) >= 249:\n                filtered_files.append(the_file)\n                output.warn(\"Filename too long, file excluded: %s\" % dest_path)\n        return filtered_files\n    return _filter\n\n\ndef rm_conandir(path):\n    \"\"\"removal of a directory that might contain a link to a short path\"\"\"\n    link = os.path.join(path, CONAN_LINK)\n    if os.path.exists(link):\n        short_path = load(link)\n        rmdir(os.path.dirname(short_path))\n    rmdir(path)\n", "path": "conans/util/windows.py"}]}