comparison mercurial/posix.py @ 49816:ae93ada06454

typing: add type hints to the rest of the posix module These methods either don't have an analog in the windows module, or are aliased in the windows module from something else (like os.path.xxx).
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 16 Dec 2022 18:27:15 -0500
parents 464fe8b8f474
children 3fd5824f1177
comparison
equal deleted inserted replaced
49815:464fe8b8f474 49816:ae93ada06454
22 from typing import ( 22 from typing import (
23 Any, 23 Any,
24 Iterable, 24 Iterable,
25 Iterator, 25 Iterator,
26 List, 26 List,
27 Match,
27 NoReturn, 28 NoReturn,
28 Optional, 29 Optional,
29 Sequence, 30 Sequence,
31 Tuple,
30 Union, 32 Union,
31 ) 33 )
32 34
33 from .i18n import _ 35 from .i18n import _
34 from .pycompat import ( 36 from .pycompat import (
63 65
64 readlink = os.readlink 66 readlink = os.readlink
65 unlink = os.unlink 67 unlink = os.unlink
66 rename = os.rename 68 rename = os.rename
67 removedirs = os.removedirs 69 removedirs = os.removedirs
68 expandglobs = False 70 expandglobs: bool = False
69 71
70 umask = os.umask(0) 72 umask: int = os.umask(0)
71 os.umask(umask) 73 os.umask(umask)
72 74
73 posixfile = open 75 posixfile = open
74 76
75 77
76 def split(p): 78 def split(p: bytes) -> Tuple[bytes, bytes]:
77 """Same as posixpath.split, but faster 79 """Same as posixpath.split, but faster
78 80
79 >>> import posixpath 81 >>> import posixpath
80 >>> for f in [b'/absolute/path/to/file', 82 >>> for f in [b'/absolute/path/to/file',
81 ... b'relative/path/to/file', 83 ... b'relative/path/to/file',
350 if inst.errno == errno.EIO and os.path.exists(name): 352 if inst.errno == errno.EIO and os.path.exists(name):
351 unlink(name) 353 unlink(name)
352 return False 354 return False
353 355
354 356
355 def checkosfilename(path): 357 def checkosfilename(path: bytes) -> Optional[bytes]:
356 """Check that the base-relative path is a valid filename on this platform. 358 """Check that the base-relative path is a valid filename on this platform.
357 Returns None if the path is ok, or a UI string describing the problem.""" 359 Returns None if the path is ok, or a UI string describing the problem."""
358 return None # on posix platforms, every path is ok 360 return None # on posix platforms, every path is ok
359 361
360 362
361 def getfsmountpoint(dirpath): 363 def getfsmountpoint(dirpath: bytes) -> Optional[bytes]:
362 """Get the filesystem mount point from a directory (best-effort) 364 """Get the filesystem mount point from a directory (best-effort)
363 365
364 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc. 366 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
365 """ 367 """
366 return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath) 368 return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath)
408 def normcase(path: bytes) -> bytes: 410 def normcase(path: bytes) -> bytes:
409 return path.lower() 411 return path.lower()
410 412
411 413
412 # what normcase does to ASCII strings 414 # what normcase does to ASCII strings
413 normcasespec = encoding.normcasespecs.lower 415 normcasespec: int = encoding.normcasespecs.lower
414 # fallback normcase function for non-ASCII strings 416 # fallback normcase function for non-ASCII strings
415 normcasefallback = normcase 417 normcasefallback = normcase
416 418
417 if pycompat.isdarwin: 419 if pycompat.isdarwin:
418 420
516 # Windows, with other native tools, or on shared volumes 518 # Windows, with other native tools, or on shared volumes
517 def checklink(path: bytes) -> bool: 519 def checklink(path: bytes) -> bool:
518 return False 520 return False
519 521
520 522
521 _needsshellquote = None 523 _needsshellquote: Optional[Match[bytes]] = None
522 524
523 525
524 def shellquote(s: bytes) -> bytes: 526 def shellquote(s: bytes) -> bytes:
525 if pycompat.sysplatform == b'OpenVMS': 527 if pycompat.sysplatform == b'OpenVMS':
526 return b'"%s"' % s 528 return b'"%s"' % s
645 647
646 def spawndetached(args: List[bytes]) -> int: 648 def spawndetached(args: List[bytes]) -> int:
647 return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), args[0], args) 649 return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), args[0], args)
648 650
649 651
650 def gethgcmd(): 652 def gethgcmd(): # TODO: convert to bytes, like on Windows?
651 return sys.argv[:1] 653 return sys.argv[:1]
652 654
653 655
654 def makedir(path: bytes, notindexed: bool) -> None: 656 def makedir(path: bytes, notindexed: bool) -> None:
655 os.mkdir(path) 657 os.mkdir(path)