]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_internal/utils/filesystem.py
6 from contextlib
import contextmanager
7 from tempfile
import NamedTemporaryFile
8 from typing
import Any
, BinaryIO
, Generator
, List
, Union
, cast
10 from pip
._vendor
.tenacity
import retry
, stop_after_delay
, wait_fixed
12 from pip
._internal
.utils
.compat
import get_path_uid
13 from pip
._internal
.utils
.misc
import format_size
16 def check_path_owner(path
: str) -> bool:
17 # If we don't have a way to check the effective uid of this process, then
18 # we'll just assume that we own the directory.
19 if sys
.platform
== "win32" or not hasattr(os
, "geteuid"):
22 assert os
.path
.isabs(path
)
25 while path
!= previous
:
26 if os
.path
.lexists(path
):
27 # Check if path is writable by current user.
29 # Special handling for root user in order to handle properly
30 # cases where users use sudo without -H flag.
32 path_uid
= get_path_uid(path
)
37 return os
.access(path
, os
.W_OK
)
39 previous
, path
= path
, os
.path
.dirname(path
)
40 return False # assume we don't own the path
44 def adjacent_tmp_file(path
: str, **kwargs
: Any
) -> Generator
[BinaryIO
, None, None]:
45 """Return a file-like object pointing to a tmp file next to path.
47 The file is created securely and is ensured to be written to disk
48 after the context reaches its end.
50 kwargs will be passed to tempfile.NamedTemporaryFile to control
51 the way the temporary file will be opened.
53 with NamedTemporaryFile(
55 dir=os
.path
.dirname(path
),
56 prefix
=os
.path
.basename(path
),
60 result
= cast(BinaryIO
, f
)
65 os
.fsync(result
.fileno())
68 # Tenacity raises RetryError by default, explicitly raise the original exception
69 _replace_retry
= retry(reraise
=True, stop
=stop_after_delay(1), wait
=wait_fixed(0.25))
71 replace
= _replace_retry(os
.replace
)
74 # test_writable_dir and _test_writable_dir_win are copied from Flit,
75 # with the author's agreement to also place them under pip's license.
76 def test_writable_dir(path
: str) -> bool:
77 """Check if a directory is writable.
79 Uses os.access() on POSIX, tries creating files on Windows.
81 # If the directory doesn't exist, find the closest parent that does.
82 while not os
.path
.isdir(path
):
83 parent
= os
.path
.dirname(path
)
85 break # Should never get here, but infinite loops are bad
88 if os
.name
== "posix":
89 return os
.access(path
, os
.W_OK
)
91 return _test_writable_dir_win(path
)
94 def _test_writable_dir_win(path
: str) -> bool:
95 # os.access doesn't work on Windows: http://bugs.python.org/issue2528
96 # and we can't use tempfile: http://bugs.python.org/issue22107
97 basename
= "accesstest_deleteme_fishfingers_custard_"
98 alphabet
= "abcdefghijklmnopqrstuvwxyz0123456789"
100 name
= basename
+ "".join(random
.choice(alphabet
) for _
in range(6))
101 file = os
.path
.join(path
, name
)
103 fd
= os
.open(file, os
.O_RDWR | os
.O_CREAT | os
.O_EXCL
)
104 except FileExistsError
:
106 except PermissionError
:
107 # This could be because there's a directory with the same name.
108 # But it's highly unlikely there's a directory called that,
109 # so we'll assume it's because the parent dir is not writable.
110 # This could as well be because the parent dir is not readable,
111 # due to non-privileged user access.
118 # This should never be reached
119 raise OSError("Unexpected condition testing for writable directory")
122 def find_files(path
: str, pattern
: str) -> List
[str]:
123 """Returns a list of absolute paths of files beneath path, recursively,
124 with filenames which match the UNIX-style shell glob pattern."""
125 result
: List
[str] = []
126 for root
, _
, files
in os
.walk(path
):
127 matches
= fnmatch
.filter(files
, pattern
)
128 result
.extend(os
.path
.join(root
, f
) for f
in matches
)
132 def file_size(path
: str) -> Union
[int, float]:
133 # If it's a symlink, return 0.
134 if os
.path
.islink(path
):
136 return os
.path
.getsize(path
)
139 def format_file_size(path
: str) -> str:
140 return format_size(file_size(path
))
143 def directory_size(path
: str) -> Union
[int, float]:
145 for root
, _dirs
, files
in os
.walk(path
):
146 for filename
in files
:
147 file_path
= os
.path
.join(root
, filename
)
148 size
+= file_size(file_path
)
152 def format_directory_size(path
: str) -> str:
153 return format_size(directory_size(path
))