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

Data Structures

class  ConfiguredBuildBackendHookCaller
 
class  HiddenText
 
class  StreamWrapper
 

Functions

str get_pip_version ()
 
Tuple[int, int, int] normalize_version_info (Tuple[int,...] py_version_info)
 
None ensure_dir (str path)
 
str get_prog ()
 
None rmtree (str dir, bool ignore_errors=False)
 
None rmtree_errorhandler (Callable[..., Any] func, str path, Union[ExcInfo, BaseException] exc_info)
 
str display_path (str path)
 
str backup_dir (str dir, str ext=".bak")
 
str ask_path_exists (str message, Iterable[str] options)
 
None _check_no_input (str message)
 
str ask (str message, Iterable[str] options)
 
str ask_input (str message)
 
str ask_password (str message)
 
int strtobool (str val)
 
str format_size (float bytes)
 
Tuple[List[str], List[int]] tabulate (Iterable[Iterable[Any]] rows)
 
bool is_installable_dir (str path)
 
Generator[bytes, None, Noneread_chunks (BinaryIO file, int size=io.DEFAULT_BUFFER_SIZE)
 
str normalize_path (str path, bool resolve_symlinks=True)
 
Tuple[str, str] splitext (str path)
 
None renames (str old, str new)
 
bool is_local (str path)
 
None write_output (Any msg, *Any args)
 
Generator[StreamWrapper, None, Nonecaptured_output (str stream_name)
 
ContextManager[StreamWrappercaptured_stdout ()
 
ContextManager[StreamWrappercaptured_stderr ()
 
Type[Any] enum (*Any sequential, **Any named)
 
str build_netloc (str host, Optional[int] port)
 
str build_url_from_netloc (str netloc, str scheme="https")
 
Tuple[Optional[str], Optional[int]] parse_netloc (str netloc)
 
NetlocTuple split_auth_from_netloc (str netloc)
 
str redact_netloc (str netloc)
 
Tuple[str, NetlocTuple_transform_url (str url, Callable[[str], Tuple[Any,...]] transform_netloc)
 
NetlocTuple _get_netloc (str netloc)
 
Tuple[str] _redact_netloc (str netloc)
 
Tuple[str, str, Tuple[Optional[str], Optional[str]]] split_auth_netloc_from_url (str url)
 
str remove_auth_from_url (str url)
 
str redact_auth_from_url (str url)
 
HiddenText hide_value (str value)
 
HiddenText hide_url (str url)
 
None protect_pip_from_modification_on_windows (bool modifying_pip)
 
None check_externally_managed ()
 
bool is_console_interactive ()
 
Tuple[Any, int] hash_file (str path, int blocksize=1<< 20)
 
Iterator[Tuple[Any, Any]] pairwise (Iterable[Any] iterable)
 
Tuple[Iterable[T], Iterable[T]] partition (Callable[[T], bool] pred, Iterable[T] iterable)
 

Variables

 logger = logging.getLogger(__name__)
 
 T = TypeVar("T")
 
 ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
 
 VersionInfo = Tuple[int, int, int]
 
 NetlocTuple = Tuple[str, Tuple[Optional[str], Optional[str]]]
 

Function Documentation

◆ _check_no_input()

None _check_no_input ( str  message)
protected
Raise an error if no input is allowed.

Definition at line 182 of file misc.py.

182def _check_no_input(message: str) -> None:
183 """Raise an error if no input is allowed."""
184 if os.environ.get("PIP_NO_INPUT"):
185 raise Exception(
186 f"No input was expected ($PIP_NO_INPUT set); question: {message}"
187 )
188
189
for i

References i.

Referenced by pip._internal.utils.misc.ask(), pip._internal.utils.misc.ask_input(), and pip._internal.utils.misc.ask_password().

Here is the caller graph for this function:

◆ _get_netloc()

NetlocTuple _get_netloc ( str  netloc)
protected

Definition at line 503 of file misc.py.

503def _get_netloc(netloc: str) -> NetlocTuple:
504 return split_auth_from_netloc(netloc)
505
506

◆ _redact_netloc()

Tuple[str] _redact_netloc ( str  netloc)
protected

Definition at line 507 of file misc.py.

507def _redact_netloc(netloc: str) -> Tuple[str]:
508 return (redact_netloc(netloc),)
509
510

References pip._internal.utils.misc.redact_netloc().

Here is the call graph for this function:

◆ _transform_url()

Tuple[str, NetlocTuple] _transform_url ( str  url,
Callable[[str], Tuple[Any, ...]]   transform_netloc 
)
protected
Transform and replace netloc in a url.

transform_netloc is a function taking the netloc and returning a
tuple. The first element of this tuple is the new netloc. The
entire tuple is returned.

Returns a tuple containing the transformed url as item 0 and the
original tuple returned by transform_netloc as item 1.

Definition at line 483 of file misc.py.

485) -> Tuple[str, NetlocTuple]:
486 """Transform and replace netloc in a url.
487
488 transform_netloc is a function taking the netloc and returning a
489 tuple. The first element of this tuple is the new netloc. The
490 entire tuple is returned.
491
492 Returns a tuple containing the transformed url as item 0 and the
493 original tuple returned by transform_netloc as item 1.
494 """
495 purl = urllib.parse.urlsplit(url)
496 netloc_tuple = transform_netloc(purl.netloc)
497 # stripped url
498 url_pieces = (purl.scheme, netloc_tuple[0], purl.path, purl.query, purl.fragment)
499 surl = urllib.parse.urlunsplit(url_pieces)
500 return surl, cast("NetlocTuple", netloc_tuple)
501
502

References i.

Referenced by pip._internal.utils.misc.redact_auth_from_url(), pip._internal.utils.misc.remove_auth_from_url(), and pip._internal.utils.misc.split_auth_netloc_from_url().

Here is the caller graph for this function:

◆ ask()

str ask ( str  message,
Iterable[str]  options 
)
Ask the message interactively, with the given possible responses

Definition at line 190 of file misc.py.

190def ask(message: str, options: Iterable[str]) -> str:
191 """Ask the message interactively, with the given possible responses"""
192 while 1:
193 _check_no_input(message)
194 response = input(message)
195 response = response.strip().lower()
196 if response not in options:
197 print(
198 "Your response ({!r}) was not one of the expected responses: "
199 "{}".format(response, ", ".join(options))
200 )
201 else:
202 return response
203
204

References pip._internal.utils.misc._check_no_input(), and i.

Here is the call graph for this function:

◆ ask_input()

str ask_input ( str  message)
Ask for input interactively.

Definition at line 205 of file misc.py.

205def ask_input(message: str) -> str:
206 """Ask for input interactively."""
207 _check_no_input(message)
208 return input(message)
209
210

References pip._internal.utils.misc._check_no_input().

Here is the call graph for this function:

◆ ask_password()

str ask_password ( str  message)
Ask for a password interactively.

Definition at line 211 of file misc.py.

211def ask_password(message: str) -> str:
212 """Ask for a password interactively."""
213 _check_no_input(message)
214 return getpass.getpass(message)
215
216

References pip._internal.utils.misc._check_no_input(), and i.

Here is the call graph for this function:

◆ ask_path_exists()

str ask_path_exists ( str  message,
Iterable[str]  options 
)

Definition at line 175 of file misc.py.

175def ask_path_exists(message: str, options: Iterable[str]) -> str:
176 for action in os.environ.get("PIP_EXISTS_ACTION", "").split():
177 if action in options:
178 return action
179 return ask(message, options)
180
181

References i.

◆ backup_dir()

str backup_dir ( str  dir,
str   ext = ".bak" 
)
Figure out the name of a directory to back up the given dir to
(adding .bak, .bak2, etc)

Definition at line 164 of file misc.py.

164def backup_dir(dir: str, ext: str = ".bak") -> str:
165 """Figure out the name of a directory to back up the given dir to
166 (adding .bak, .bak2, etc)"""
167 n = 1
168 extension = ext
169 while os.path.exists(dir + extension):
170 n += 1
171 extension = ext + str(n)
172 return dir + extension
173
174

References i.

◆ build_netloc()

str build_netloc ( str  host,
Optional[int]  port 
)
Build a netloc from a host-port pair

Definition at line 401 of file misc.py.

401def build_netloc(host: str, port: Optional[int]) -> str:
402 """
403 Build a netloc from a host-port pair
404 """
405 if port is None:
406 return host
407 if ":" in host:
408 # Only wrap host with square brackets when it is IPv6
409 host = f"[{host}]"
410 return f"{host}:{port}"
411
412

◆ build_url_from_netloc()

str build_url_from_netloc ( str  netloc,
str   scheme = "https" 
)
Build a full URL from a netloc.

Definition at line 413 of file misc.py.

413def build_url_from_netloc(netloc: str, scheme: str = "https") -> str:
414 """
415 Build a full URL from a netloc.
416 """
417 if netloc.count(":") >= 2 and "@" not in netloc and "[" not in netloc:
418 # It must be a bare IPv6 address, so wrap it with brackets.
419 netloc = f"[{netloc}]"
420 return f"{scheme}://{netloc}"
421
422

References i.

◆ captured_output()

Generator[StreamWrapper, None, None] captured_output ( str  stream_name)
Return a context manager used by captured_stdout/stdin/stderr
that temporarily replaces the sys stream *stream_name* with a StringIO.

Taken from Lib/support/__init__.py in the CPython repo.

Definition at line 360 of file misc.py.

360def captured_output(stream_name: str) -> Generator[StreamWrapper, None, None]:
361 """Return a context manager used by captured_stdout/stdin/stderr
362 that temporarily replaces the sys stream *stream_name* with a StringIO.
363
364 Taken from Lib/support/__init__.py in the CPython repo.
365 """
366 orig_stdout = getattr(sys, stream_name)
367 setattr(sys, stream_name, StreamWrapper.from_stream(orig_stdout))
368 try:
369 yield getattr(sys, stream_name)
370 finally:
371 setattr(sys, stream_name, orig_stdout)
372
373

References i.

Referenced by pip._internal.utils.misc.captured_stderr(), and pip._internal.utils.misc.captured_stdout().

Here is the caller graph for this function:

◆ captured_stderr()

ContextManager[StreamWrapper] captured_stderr ( )
See captured_stdout().

Definition at line 386 of file misc.py.

386def captured_stderr() -> ContextManager[StreamWrapper]:
387 """
388 See captured_stdout().
389 """
390 return captured_output("stderr")
391
392
393# Simulates an enum

References pip._internal.utils.misc.captured_output().

Here is the call graph for this function:

◆ captured_stdout()

ContextManager[StreamWrapper] captured_stdout ( )
Capture the output of sys.stdout:

   with captured_stdout() as stdout:
       print('hello')
   self.assertEqual(stdout.getvalue(), 'hello\n')

Taken from Lib/support/__init__.py in the CPython repo.

Definition at line 374 of file misc.py.

374def captured_stdout() -> ContextManager[StreamWrapper]:
375 """Capture the output of sys.stdout:
376
377 with captured_stdout() as stdout:
378 print('hello')
379 self.assertEqual(stdout.getvalue(), 'hello\n')
380
381 Taken from Lib/support/__init__.py in the CPython repo.
382 """
383 return captured_output("stdout")
384
385

References pip._internal.utils.misc.captured_output().

Here is the call graph for this function:

◆ check_externally_managed()

None check_externally_managed ( )
Check whether the current environment is externally managed.

If the ``EXTERNALLY-MANAGED`` config file is found, the current environment
is considered externally managed, and an ExternallyManagedEnvironment is
raised.

Definition at line 591 of file misc.py.

591def check_externally_managed() -> None:
592 """Check whether the current environment is externally managed.
593
594 If the ``EXTERNALLY-MANAGED`` config file is found, the current environment
595 is considered externally managed, and an ExternallyManagedEnvironment is
596 raised.
597 """
598 if running_under_virtualenv():
599 return
600 marker = os.path.join(sysconfig.get_path("stdlib"), "EXTERNALLY-MANAGED")
601 if not os.path.isfile(marker):
602 return
604
605

References i.

◆ display_path()

str display_path ( str  path)
Gives the display value for a given path, making it relative to cwd
if possible.

Definition at line 155 of file misc.py.

155def display_path(path: str) -> str:
156 """Gives the display value for a given path, making it relative to cwd
157 if possible."""
160 path = "." + path[len(os.getcwd()) :]
161 return path
162
163

References i.

◆ ensure_dir()

None ensure_dir ( str  path)
os.path.makedirs without EEXIST.

Definition at line 101 of file misc.py.

101def ensure_dir(path: str) -> None:
102 """os.path.makedirs without EEXIST."""
103 try:
104 os.makedirs(path)
105 except OSError as e:
106 # Windows can raise spurious ENOTEMPTY errors. See #6426.
108 raise
109
110

References i.

◆ enum()

Type[Any] enum ( *Any  sequential,
**Any  named 
)

Definition at line 394 of file misc.py.

394def enum(*sequential: Any, **named: Any) -> Type[Any]:
395 enums = dict(zip(sequential, range(len(sequential))), **named)
396 reverse = {value: key for key, value in enums.items()}
397 enums["reverse_mapping"] = reverse
398 return type("Enum", (), enums)
399
400

References i.

◆ format_size()

str format_size ( float  bytes)

Definition at line 233 of file misc.py.

233def format_size(bytes: float) -> str:
234 if bytes > 1000 * 1000:
235 return "{:.1f} MB".format(bytes / 1000.0 / 1000)
236 elif bytes > 10 * 1000:
237 return "{} kB".format(int(bytes / 1000))
238 elif bytes > 1000:
239 return "{:.1f} kB".format(bytes / 1000.0)
240 else:
241 return "{} bytes".format(int(bytes))
242
243

◆ get_pip_version()

str get_pip_version ( )

Definition at line 71 of file misc.py.

71def get_pip_version() -> str:
72 pip_pkg_dir = os.path.join(os.path.dirname(__file__), "..", "..")
73 pip_pkg_dir = os.path.abspath(pip_pkg_dir)
74
75 return "pip {} from {} (python {})".format(
76 __version__,
77 pip_pkg_dir,
78 get_major_minor_version(),
79 )
80
81

References i.

◆ get_prog()

str get_prog ( )

Definition at line 111 of file misc.py.

111def get_prog() -> str:
112 try:
113 prog = os.path.basename(sys.argv[0])
114 if prog in ("__main__.py", "-c"):
115 return f"{sys.executable} -m pip"
116 else:
117 return prog
118 except (AttributeError, TypeError, IndexError):
119 pass
120 return "pip"
121
122
123# Retry every half second for up to 3 seconds
124# Tenacity raises RetryError by default, explicitly raise the original exception
125@retry(reraise=True, stop=stop_after_delay(3), wait=wait_fixed(0.5))

References i.

◆ hash_file()

Tuple[Any, int] hash_file ( str  path,
int   blocksize = 1 << 20 
)
Return (hash, length) for path using hashlib.sha256()

Definition at line 611 of file misc.py.

611def hash_file(path: str, blocksize: int = 1 << 20) -> Tuple[Any, int]:
612 """Return (hash, length) for path using hashlib.sha256()"""
613
614 h = hashlib.sha256()
615 length = 0
616 with open(path, "rb") as f:
617 for block in read_chunks(f, size=blocksize):
618 length += len(block)
619 h.update(block)
620 return h, length
621
622

References i.

◆ hide_url()

HiddenText hide_url ( str  url)

Definition at line 560 of file misc.py.

560def hide_url(url: str) -> HiddenText:
561 redacted = redact_auth_from_url(url)
562 return HiddenText(url, redacted=redacted)
563
564

◆ hide_value()

HiddenText hide_value ( str  value)

Definition at line 556 of file misc.py.

556def hide_value(value: str) -> HiddenText:
557 return HiddenText(value, redacted="****")
558
559

◆ is_console_interactive()

bool is_console_interactive ( )
Is this console interactive?

Definition at line 606 of file misc.py.

606def is_console_interactive() -> bool:
607 """Is this console interactive?"""
608 return sys.stdin is not None and sys.stdin.isatty()
609
610

References i.

◆ is_installable_dir()

bool is_installable_dir ( str  path)
Is path is a directory containing pyproject.toml or setup.py?

If pyproject.toml exists, this is a PEP 517 project. Otherwise we look for
a legacy setuptools layout by identifying setup.py. We don't check for the
setup.cfg because using it without setup.py is only available for PEP 517
projects, which are already covered by the pyproject.toml check.

Definition at line 258 of file misc.py.

258def is_installable_dir(path: str) -> bool:
259 """Is path is a directory containing pyproject.toml or setup.py?
260
261 If pyproject.toml exists, this is a PEP 517 project. Otherwise we look for
262 a legacy setuptools layout by identifying setup.py. We don't check for the
263 setup.cfg because using it without setup.py is only available for PEP 517
264 projects, which are already covered by the pyproject.toml check.
265 """
266 if not os.path.isdir(path):
267 return False
268 if os.path.isfile(os.path.join(path, "pyproject.toml")):
269 return True
270 if os.path.isfile(os.path.join(path, "setup.py")):
271 return True
272 return False
273
274

References i.

◆ is_local()

bool is_local ( str  path)
Return True if path is within sys.prefix, if we're running in a virtualenv.

If we're not in a virtualenv, all paths are considered "local."

Caution: this function assumes the head of path has been normalized
with normalize_path.

Definition at line 325 of file misc.py.

325def is_local(path: str) -> bool:
326 """
327 Return True if path is within sys.prefix, if we're running in a virtualenv.
328
329 If we're not in a virtualenv, all paths are considered "local."
330
331 Caution: this function assumes the head of path has been normalized
332 with normalize_path.
333 """
334 if not running_under_virtualenv():
335 return True
336 return path.startswith(normalize_path(sys.prefix))
337
338

References i.

◆ normalize_path()

str normalize_path ( str  path,
bool   resolve_symlinks = True 
)
Convert a path to its canonical, case-normalized, absolute version.

Definition at line 286 of file misc.py.

286def normalize_path(path: str, resolve_symlinks: bool = True) -> str:
287 """
288 Convert a path to its canonical, case-normalized, absolute version.
289
290 """
291 path = os.path.expanduser(path)
292 if resolve_symlinks:
293 path = os.path.realpath(path)
294 else:
295 path = os.path.abspath(path)
296 return os.path.normcase(path)
297
298

References i.

◆ normalize_version_info()

Tuple[int, int, int] normalize_version_info ( Tuple[int, ...]  py_version_info)
Convert a tuple of ints representing a Python version to one of length
three.

:param py_version_info: a tuple of ints representing a Python version,
    or None to specify no version. The tuple can have any length.

:return: a tuple of length three if `py_version_info` is non-None.
    Otherwise, return `py_version_info` unchanged (i.e. None).

Definition at line 82 of file misc.py.

82def normalize_version_info(py_version_info: Tuple[int, ...]) -> Tuple[int, int, int]:
83 """
84 Convert a tuple of ints representing a Python version to one of length
85 three.
86
87 :param py_version_info: a tuple of ints representing a Python version,
88 or None to specify no version. The tuple can have any length.
89
90 :return: a tuple of length three if `py_version_info` is non-None.
91 Otherwise, return `py_version_info` unchanged (i.e. None).
92 """
93 if len(py_version_info) < 3:
94 py_version_info += (3 - len(py_version_info)) * (0,)
95 elif len(py_version_info) > 3:
96 py_version_info = py_version_info[:3]
97
98 return cast("VersionInfo", py_version_info)
99
100

References i.

◆ pairwise()

Iterator[Tuple[Any, Any]] pairwise ( Iterable[Any]  iterable)
Return paired elements.

For example:
    s -> (s0, s1), (s2, s3), (s4, s5), ...

Definition at line 623 of file misc.py.

623def pairwise(iterable: Iterable[Any]) -> Iterator[Tuple[Any, Any]]:
624 """
625 Return paired elements.
626
627 For example:
628 s -> (s0, s1), (s2, s3), (s4, s5), ...
629 """
630 iterable = iter(iterable)
631 return zip_longest(iterable, iterable)
632
633

◆ parse_netloc()

Tuple[Optional[str], Optional[int]] parse_netloc ( str  netloc)
Return the host-port pair from a netloc.

Definition at line 423 of file misc.py.

423def parse_netloc(netloc: str) -> Tuple[Optional[str], Optional[int]]:
424 """
425 Return the host-port pair from a netloc.
426 """
427 url = build_url_from_netloc(netloc)
428 parsed = urllib.parse.urlparse(url)
430
431

References i.

◆ partition()

Tuple[Iterable[T], Iterable[T]] partition ( Callable[[T], bool]  pred,
Iterable[T iterable 
)
Use a predicate to partition entries into false entries and true entries,
like

    partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9

Definition at line 634 of file misc.py.

637) -> Tuple[Iterable[T], Iterable[T]]:
638 """
639 Use a predicate to partition entries into false entries and true entries,
640 like
641
642 partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
643 """
644 t1, t2 = tee(iterable)
645 return filterfalse(pred, t1), filter(pred, t2)
646
647

◆ protect_pip_from_modification_on_windows()

None protect_pip_from_modification_on_windows ( bool  modifying_pip)
Protection of pip.exe from modification on Windows

On Windows, any operation modifying pip should be run as:
    python -m pip ...

Definition at line 565 of file misc.py.

565def protect_pip_from_modification_on_windows(modifying_pip: bool) -> None:
566 """Protection of pip.exe from modification on Windows
567
568 On Windows, any operation modifying pip should be run as:
569 python -m pip ...
570 """
571 pip_names = [
572 "pip",
573 f"pip{sys.version_info.major}",
574 f"pip{sys.version_info.major}.{sys.version_info.minor}",
575 ]
576
577 # See https://github.com/pypa/pip/issues/1299 for more discussion
578 should_show_use_python_msg = (
579 modifying_pip and WINDOWS and os.path.basename(sys.argv[0]) in pip_names
580 )
581
582 if should_show_use_python_msg:
583 new_command = [sys.executable, "-m", "pip"] + sys.argv[1:]
584 raise CommandError(
585 "To modify pip, please run the following command:\n{}".format(
586 " ".join(new_command)
587 )
588 )
589
590

References i.

◆ read_chunks()

Generator[bytes, None, None] read_chunks ( BinaryIO  file,
int   size = io.DEFAULT_BUFFER_SIZE 
)
Yield pieces of data from a file-like object until EOF.

Definition at line 275 of file misc.py.

277) -> Generator[bytes, None, None]:
278 """Yield pieces of data from a file-like object until EOF."""
279 while True:
280 chunk = file.read(size)
281 if not chunk:
282 break
283 yield chunk
284
285

References i.

◆ redact_auth_from_url()

str redact_auth_from_url ( str  url)
Replace the password in a given url with ****.

Definition at line 530 of file misc.py.

530def redact_auth_from_url(url: str) -> str:
531 """Replace the password in a given url with ****."""
532 return _transform_url(url, _redact_netloc)[0]
533
534

References pip._internal.utils.misc._transform_url().

Here is the call graph for this function:

◆ redact_netloc()

str redact_netloc ( str  netloc)
Replace the sensitive data in a netloc with "****", if it exists.

For example:
    - "user:pass@example.com" returns "user:****@example.com"
    - "accesstoken@example.com" returns "****@example.com"

Definition at line 461 of file misc.py.

461def redact_netloc(netloc: str) -> str:
462 """
463 Replace the sensitive data in a netloc with "****", if it exists.
464
465 For example:
466 - "user:pass@example.com" returns "user:****@example.com"
467 - "accesstoken@example.com" returns "****@example.com"
468 """
469 netloc, (user, password) = split_auth_from_netloc(netloc)
470 if user is None:
471 return netloc
472 if password is None:
473 user = "****"
474 password = ""
475 else:
476 user = urllib.parse.quote(user)
477 password = ":****"
478 return "{user}{password}@{netloc}".format(
479 user=user, password=password, netloc=netloc
480 )
481
482

References i.

Referenced by pip._internal.utils.misc._redact_netloc().

Here is the caller graph for this function:

◆ remove_auth_from_url()

str remove_auth_from_url ( str  url)
Return a copy of url with 'username:password@' removed.

Definition at line 523 of file misc.py.

523def remove_auth_from_url(url: str) -> str:
524 """Return a copy of url with 'username:password@' removed."""
525 # username/pass params are passed to subversion through flags
526 # and are not recognized in the url.
527 return _transform_url(url, _get_netloc)[0]
528
529

References pip._internal.utils.misc._transform_url().

Here is the call graph for this function:

◆ renames()

None renames ( str  old,
str  new 
)
Like os.renames(), but handles renaming across devices.

Definition at line 308 of file misc.py.

308def renames(old: str, new: str) -> None:
309 """Like os.renames(), but handles renaming across devices."""
310 # Implementation borrowed from os.renames().
311 head, tail = os.path.split(new)
312 if head and tail and not os.path.exists(head):
313 os.makedirs(head)
314
315 shutil.move(old, new)
316
317 head, tail = os.path.split(old)
318 if head and tail:
319 try:
320 os.removedirs(head)
321 except OSError:
322 pass
323
324

References i.

◆ rmtree()

None rmtree ( str  dir,
bool   ignore_errors = False 
)

Definition at line 126 of file misc.py.

126def rmtree(dir: str, ignore_errors: bool = False) -> None:
127 if sys.version_info >= (3, 12):
128 shutil.rmtree(dir, ignore_errors=ignore_errors, onexc=rmtree_errorhandler)
129 else:
130 shutil.rmtree(dir, ignore_errors=ignore_errors, onerror=rmtree_errorhandler)
131
132

References i.

◆ rmtree_errorhandler()

None rmtree_errorhandler ( Callable[..., Any]  func,
str  path,
Union[ExcInfo, BaseException]   exc_info 
)
On Windows, the files in .svn are read-only, so when rmtree() tries to
remove them, an exception is thrown.  We catch that here, remove the
read-only attribute, and hopefully continue without problems.

Definition at line 133 of file misc.py.

135) -> None:
136 """On Windows, the files in .svn are read-only, so when rmtree() tries to
137 remove them, an exception is thrown. We catch that here, remove the
138 read-only attribute, and hopefully continue without problems."""
139 try:
140 has_attr_readonly = not (os.stat(path).st_mode & stat.S_IWRITE)
141 except OSError:
142 # it's equivalent to os.path.exists
143 return
144
145 if has_attr_readonly:
146 # convert to read/write
148 # use the original function to repeat the operation
149 func(path)
150 return
151 else:
152 raise
153
154

References i.

◆ split_auth_from_netloc()

NetlocTuple split_auth_from_netloc ( str  netloc)
Parse out and remove the auth information from a netloc.

Returns: (netloc, (username, password)).

Definition at line 432 of file misc.py.

432def split_auth_from_netloc(netloc: str) -> NetlocTuple:
433 """
434 Parse out and remove the auth information from a netloc.
435
436 Returns: (netloc, (username, password)).
437 """
438 if "@" not in netloc:
439 return netloc, (None, None)
440
441 # Split from the right because that's how urllib.parse.urlsplit()
442 # behaves if more than one @ is present (which can be checked using
443 # the password attribute of urlsplit()'s return value).
444 auth, netloc = netloc.rsplit("@", 1)
445 pw: Optional[str] = None
446 if ":" in auth:
447 # Split from the left because that's how urllib.parse.urlsplit()
448 # behaves if more than one : is present (which again can be checked
449 # using the password attribute of the return value)
450 user, pw = auth.split(":", 1)
451 else:
452 user, pw = auth, None
453
454 user = urllib.parse.unquote(user)
455 if pw is not None:
456 pw = urllib.parse.unquote(pw)
457
458 return netloc, (user, pw)
459
460

References i.

◆ split_auth_netloc_from_url()

Tuple[str, str, Tuple[Optional[str], Optional[str]]] split_auth_netloc_from_url ( str  url)
Parse a url into separate netloc, auth, and url with no auth.

Returns: (url_without_auth, netloc, (username, password))

Definition at line 511 of file misc.py.

513) -> Tuple[str, str, Tuple[Optional[str], Optional[str]]]:
514 """
515 Parse a url into separate netloc, auth, and url with no auth.
516
517 Returns: (url_without_auth, netloc, (username, password))
518 """
519 url_without_auth, (netloc, auth) = _transform_url(url, _get_netloc)
520 return url_without_auth, netloc, auth
521
522

References pip._internal.utils.misc._transform_url().

Here is the call graph for this function:

◆ splitext()

Tuple[str, str] splitext ( str  path)
Like os.path.splitext, but take off .tar too

Definition at line 299 of file misc.py.

299def splitext(path: str) -> Tuple[str, str]:
300 """Like os.path.splitext, but take off .tar too"""
301 base, ext = posixpath.splitext(path)
302 if base.lower().endswith(".tar"):
303 ext = base[-4:] + ext
304 base = base[:-4]
305 return base, ext
306
307

References i.

◆ strtobool()

int strtobool ( str  val)
Convert a string representation of truth to true (1) or false (0).

True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
'val' is anything else.

Definition at line 217 of file misc.py.

217def strtobool(val: str) -> int:
218 """Convert a string representation of truth to true (1) or false (0).
219
220 True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
221 are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
222 'val' is anything else.
223 """
224 val = val.lower()
225 if val in ("y", "yes", "t", "true", "on", "1"):
226 return 1
227 elif val in ("n", "no", "f", "false", "off", "0"):
228 return 0
229 else:
230 raise ValueError(f"invalid truth value {val!r}")
231
232

References i.

◆ tabulate()

Tuple[List[str], List[int]] tabulate ( Iterable[Iterable[Any]]  rows)
Return a list of formatted rows and a list of column sizes.

For example::

>>> tabulate([['foobar', 2000], [0xdeadbeef]])
(['foobar     2000', '3735928559'], [10, 4])

Definition at line 244 of file misc.py.

244def tabulate(rows: Iterable[Iterable[Any]]) -> Tuple[List[str], List[int]]:
245 """Return a list of formatted rows and a list of column sizes.
246
247 For example::
248
249 >>> tabulate([['foobar', 2000], [0xdeadbeef]])
250 (['foobar 2000', '3735928559'], [10, 4])
251 """
252 rows = [tuple(map(str, row)) for row in rows]
253 sizes = [max(map(len, col)) for col in zip_longest(*rows, fillvalue="")]
254 table = [" ".join(map(str.ljust, row, sizes)).rstrip() for row in rows]
255 return table, sizes
256
257

References i.

◆ write_output()

None write_output ( Any  msg,
*Any  args 
)

Definition at line 339 of file misc.py.

339def write_output(msg: Any, *args: Any) -> None:
340 logger.info(msg, *args)
341
342

References i.

Variable Documentation

◆ ExcInfo

ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]

Definition at line 66 of file misc.py.

◆ logger

logger = logging.getLogger(__name__)

Definition at line 63 of file misc.py.

◆ NetlocTuple

NetlocTuple = Tuple[str, Tuple[Optional[str], Optional[str]]]

Definition at line 68 of file misc.py.

◆ T

T = TypeVar("T")

Definition at line 65 of file misc.py.

◆ VersionInfo

VersionInfo = Tuple[int, int, int]

Definition at line 67 of file misc.py.