Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._internal.utils.filesystem Namespace Reference

Functions

bool check_path_owner (str path)
 
Generator[BinaryIO, None, Noneadjacent_tmp_file (str path, **Any kwargs)
 
bool test_writable_dir (str path)
 
bool _test_writable_dir_win (str path)
 
List[str] find_files (str path, str pattern)
 
Union[int, float] file_size (str path)
 
str format_file_size (str path)
 
Union[int, float] directory_size (str path)
 
str format_directory_size (str path)
 

Variables

 _replace_retry = retry(reraise=True, stop=stop_after_delay(1), wait=wait_fixed(0.25))
 
 replace = _replace_retry(os.replace)
 

Function Documentation

◆ _test_writable_dir_win()

bool _test_writable_dir_win ( str  path)
protected

Definition at line 94 of file filesystem.py.

94def _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"
99 for _ in range(10):
100 name = basename + "".join(random.choice(alphabet) for _ in range(6))
101 file = os.path.join(path, name)
102 try:
103 fd = os.open(file, os.O_RDWR | os.O_CREAT | os.O_EXCL)
104 except FileExistsError:
105 pass
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.
112 return False
113 else:
114 os.close(fd)
115 os.unlink(file)
116 return True
117
118 # This should never be reached
119 raise OSError("Unexpected condition testing for writable directory")
120
121
for i

References i.

Referenced by pip._internal.utils.filesystem.test_writable_dir().

Here is the caller graph for this function:

◆ adjacent_tmp_file()

Generator[BinaryIO, None, None] adjacent_tmp_file ( str  path,
**Any  kwargs 
)
Return a file-like object pointing to a tmp file next to path.

The file is created securely and is ensured to be written to disk
after the context reaches its end.

kwargs will be passed to tempfile.NamedTemporaryFile to control
the way the temporary file will be opened.

Definition at line 44 of file filesystem.py.

44def 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.
46
47 The file is created securely and is ensured to be written to disk
48 after the context reaches its end.
49
50 kwargs will be passed to tempfile.NamedTemporaryFile to control
51 the way the temporary file will be opened.
52 """
53 with NamedTemporaryFile(
54 delete=False,
55 dir=os.path.dirname(path),
56 prefix=os.path.basename(path),
57 suffix=".tmp",
58 **kwargs,
59 ) as f:
60 result = cast(BinaryIO, f)
61 try:
62 yield result
63 finally:
66
67
68# Tenacity raises RetryError by default, explicitly raise the original exception

References i.

◆ check_path_owner()

bool check_path_owner ( str  path)

Definition at line 16 of file filesystem.py.

16def 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"):
20 return True
21
22 assert os.path.isabs(path)
23
24 previous = None
25 while path != previous:
26 if os.path.lexists(path):
27 # Check if path is writable by current user.
28 if os.geteuid() == 0:
29 # Special handling for root user in order to handle properly
30 # cases where users use sudo without -H flag.
31 try:
32 path_uid = get_path_uid(path)
33 except OSError:
34 return False
35 return path_uid == 0
36 else:
37 return os.access(path, os.W_OK)
38 else:
39 previous, path = path, os.path.dirname(path)
40 return False # assume we don't own the path
41
42
43@contextmanager

References i.

◆ directory_size()

Union[int, float] directory_size ( str  path)

Definition at line 143 of file filesystem.py.

143def directory_size(path: str) -> Union[int, float]:
144 size = 0.0
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)
149 return size
150
151

References pip._internal.utils.filesystem.file_size(), and i.

Referenced by pip._internal.utils.filesystem.format_directory_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file_size()

Union[int, float] file_size ( str  path)

Definition at line 132 of file filesystem.py.

132def file_size(path: str) -> Union[int, float]:
133 # If it's a symlink, return 0.
134 if os.path.islink(path):
135 return 0
136 return os.path.getsize(path)
137
138

References i.

Referenced by pip._internal.utils.filesystem.directory_size(), and pip._internal.utils.filesystem.format_file_size().

Here is the caller graph for this function:

◆ find_files()

List[str] find_files ( str  path,
str  pattern 
)
Returns a list of absolute paths of files beneath path, recursively,
with filenames which match the UNIX-style shell glob pattern.

Definition at line 122 of file filesystem.py.

122def 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)
129 return result
130
131

References i.

◆ format_directory_size()

str format_directory_size ( str  path)

Definition at line 152 of file filesystem.py.

152def format_directory_size(path: str) -> str:
153 return format_size(directory_size(path))

References pip._internal.utils.filesystem.directory_size().

Here is the call graph for this function:

◆ format_file_size()

str format_file_size ( str  path)

Definition at line 139 of file filesystem.py.

139def format_file_size(path: str) -> str:
140 return format_size(file_size(path))
141
142

References pip._internal.utils.filesystem.file_size().

Here is the call graph for this function:

◆ test_writable_dir()

bool test_writable_dir ( str  path)
Check if a directory is writable.

Uses os.access() on POSIX, tries creating files on Windows.

Definition at line 76 of file filesystem.py.

76def test_writable_dir(path: str) -> bool:
77 """Check if a directory is writable.
78
79 Uses os.access() on POSIX, tries creating files on Windows.
80 """
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)
84 if parent == path:
85 break # Should never get here, but infinite loops are bad
86 path = parent
87
88 if os.name == "posix":
89 return os.access(path, os.W_OK)
90
91 return _test_writable_dir_win(path)
92
93

References pip._internal.utils.filesystem._test_writable_dir_win(), and i.

Here is the call graph for this function:

Variable Documentation

◆ _replace_retry

_replace_retry = retry(reraise=True, stop=stop_after_delay(1), wait=wait_fixed(0.25))
protected

Definition at line 69 of file filesystem.py.

◆ replace

replace = _replace_retry(os.replace)

Definition at line 71 of file filesystem.py.