We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pip.utils.filesystem.check_path_owner should treat group membership properly
I have two users who share same .pip/cache directory: `a` and `b`. Both users are members of the group `pip`. There is third user, `pip` who is also member of group `pip` and that user handles all download and caching for users a and b. `.pip/cache` directory configured to have group writeable permission and sticky bit, to make sure all newly created files and folders are owned by group `pip`. 

Function [check_path_owner](https://github.com/pypa/pip/blob/6.0.6/pip/utils/filesystem.py#L7) only checks if owner is same user, but it is somewhat wrong. You should check group membership as well. 

Another option, is to create new command-line option, which will allow to ignore this check. 

Either solution will work.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pip/utils/filesystem.py`
Content:
```
1 import os
2 import os.path
3 
4 from pip.compat import get_path_uid
5 
6 
7 def check_path_owner(path):
8     # If we don't have a way to check the effective uid of this process, then
9     # we'll just assume that we own the directory.
10     if not hasattr(os, "geteuid"):
11         return True
12 
13     previous = None
14     while path != previous:
15         if os.path.lexists(path):
16             # Actually do the ownership check
17             try:
18                 if get_path_uid(path) != os.geteuid():
19                     return False
20             except OSError:
21                 return False
22             return True
23         else:
24             previous, path = path, os.path.dirname(path)
25
```
--- 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()

```

diff --git a/pip/utils/filesystem.py b/pip/utils/filesystem.py
--- a/pip/utils/filesystem.py
+++ b/pip/utils/filesystem.py
@@ -13,12 +13,16 @@
     previous = None
     while path != previous:
         if os.path.lexists(path):
-            # Actually do the ownership check
-            try:
-                if get_path_uid(path) != os.geteuid():
+            # Check if path is writable by current user.
+            if os.geteuid() == 0:
+                # Special handling for root user in order to handle properly
+                # cases where users use sudo without -H flag.
+                try:
+                    path_uid = get_path_uid(path)
+                except OSError:
                     return False
-            except OSError:
-                return False
-            return True
+                return path_uid == 0
+            else:
+                return os.access(path, os.W_OK)
         else:
             previous, path = path, os.path.dirname(path)

{"golden_diff": "diff --git a/pip/utils/filesystem.py b/pip/utils/filesystem.py\n--- a/pip/utils/filesystem.py\n+++ b/pip/utils/filesystem.py\n@@ -13,12 +13,16 @@\n     previous = None\n     while path != previous:\n         if os.path.lexists(path):\n-            # Actually do the ownership check\n-            try:\n-                if get_path_uid(path) != os.geteuid():\n+            # Check if path is writable by current user.\n+            if os.geteuid() == 0:\n+                # Special handling for root user in order to handle properly\n+                # cases where users use sudo without -H flag.\n+                try:\n+                    path_uid = get_path_uid(path)\n+                except OSError:\n                     return False\n-            except OSError:\n-                return False\n-            return True\n+                return path_uid == 0\n+            else:\n+                return os.access(path, os.W_OK)\n         else:\n             previous, path = path, os.path.dirname(path)\n", "issue": "pip.utils.filesystem.check_path_owner should treat group membership properly\nI have two users who share same .pip/cache directory: `a` and `b`. Both users are members of the group `pip`. There is third user, `pip` who is also member of group `pip` and that user handles all download and caching for users a and b. `.pip/cache` directory configured to have group writeable permission and sticky bit, to make sure all newly created files and folders are owned by group `pip`. \n\nFunction [check_path_owner](https://github.com/pypa/pip/blob/6.0.6/pip/utils/filesystem.py#L7) only checks if owner is same user, but it is somewhat wrong. You should check group membership as well. \n\nAnother option, is to create new command-line option, which will allow to ignore this check. \n\nEither solution will work.\n\n", "before_files": [{"content": "import os\nimport os.path\n\nfrom pip.compat import get_path_uid\n\n\ndef check_path_owner(path):\n    # If we don't have a way to check the effective uid of this process, then\n    # we'll just assume that we own the directory.\n    if not hasattr(os, \"geteuid\"):\n        return True\n\n    previous = None\n    while path != previous:\n        if os.path.lexists(path):\n            # Actually do the ownership check\n            try:\n                if get_path_uid(path) != os.geteuid():\n                    return False\n            except OSError:\n                return False\n            return True\n        else:\n            previous, path = path, os.path.dirname(path)\n", "path": "pip/utils/filesystem.py"}], "after_files": [{"content": "import os\nimport os.path\n\nfrom pip.compat import get_path_uid\n\n\ndef check_path_owner(path):\n    # If we don't have a way to check the effective uid of this process, then\n    # we'll just assume that we own the directory.\n    if not hasattr(os, \"geteuid\"):\n        return True\n\n    previous = None\n    while path != previous:\n        if os.path.lexists(path):\n            # Check if path is writable by current user.\n            if os.geteuid() == 0:\n                # Special handling for root user in order to handle properly\n                # cases where users use sudo without -H flag.\n                try:\n                    path_uid = get_path_uid(path)\n                except OSError:\n                    return False\n                return path_uid == 0\n            else:\n                return os.access(path, os.W_OK)\n        else:\n            previous, path = path, os.path.dirname(path)\n", "path": "pip/utils/filesystem.py"}]}