ntpath.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
  2. """Common pathname manipulations, WindowsNT/95 version.
  3. Instead of importing this module directly, import os and refer to this
  4. module as os.path.
  5. """
  6. import os
  7. import sys
  8. import stat
  9. import genericpath
  10. from genericpath import *
  11. __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
  12. "basename","dirname","commonprefix","getsize","getmtime",
  13. "getatime","getctime", "islink","exists","lexists","isdir","isfile",
  14. "ismount", "expanduser","expandvars","normpath","abspath",
  15. "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
  16. "extsep","devnull","realpath","supports_unicode_filenames","relpath",
  17. "samefile", "sameopenfile", "samestat", "commonpath"]
  18. # strings representing various path-related bits and pieces
  19. # These are primarily for export; internally, they are hardcoded.
  20. curdir = '.'
  21. pardir = '..'
  22. extsep = '.'
  23. sep = '\\'
  24. pathsep = ';'
  25. altsep = '/'
  26. defpath = '.;C:\\bin'
  27. if 'ce' in sys.builtin_module_names:
  28. defpath = '\\Windows'
  29. devnull = 'nul'
  30. def _get_bothseps(path):
  31. if isinstance(path, bytes):
  32. return b'\\/'
  33. else:
  34. return '\\/'
  35. # Normalize the case of a pathname and map slashes to backslashes.
  36. # Other normalizations (such as optimizing '../' away) are not done
  37. # (this is done by normpath).
  38. def normcase(s):
  39. """Normalize case of pathname.
  40. Makes all characters lowercase and all slashes into backslashes."""
  41. try:
  42. if isinstance(s, bytes):
  43. return s.replace(b'/', b'\\').lower()
  44. else:
  45. return s.replace('/', '\\').lower()
  46. except (TypeError, AttributeError):
  47. if not isinstance(s, (bytes, str)):
  48. raise TypeError("normcase() argument must be str or bytes, "
  49. "not %r" % s.__class__.__name__) from None
  50. raise
  51. # Return whether a path is absolute.
  52. # Trivial in Posix, harder on Windows.
  53. # For Windows it is absolute if it starts with a slash or backslash (current
  54. # volume), or if a pathname after the volume-letter-and-colon or UNC-resource
  55. # starts with a slash or backslash.
  56. def isabs(s):
  57. """Test whether a path is absolute"""
  58. s = splitdrive(s)[1]
  59. return len(s) > 0 and s[0] in _get_bothseps(s)
  60. # Join two (or more) paths.
  61. def join(path, *paths):
  62. if isinstance(path, bytes):
  63. sep = b'\\'
  64. seps = b'\\/'
  65. colon = b':'
  66. else:
  67. sep = '\\'
  68. seps = '\\/'
  69. colon = ':'
  70. try:
  71. if not paths:
  72. path[:0] + sep #23780: Ensure compatible data type even if p is null.
  73. result_drive, result_path = splitdrive(path)
  74. for p in paths:
  75. p_drive, p_path = splitdrive(p)
  76. if p_path and p_path[0] in seps:
  77. # Second path is absolute
  78. if p_drive or not result_drive:
  79. result_drive = p_drive
  80. result_path = p_path
  81. continue
  82. elif p_drive and p_drive != result_drive:
  83. if p_drive.lower() != result_drive.lower():
  84. # Different drives => ignore the first path entirely
  85. result_drive = p_drive
  86. result_path = p_path
  87. continue
  88. # Same drive in different case
  89. result_drive = p_drive
  90. # Second path is relative to the first
  91. if result_path and result_path[-1] not in seps:
  92. result_path = result_path + sep
  93. result_path = result_path + p_path
  94. ## add separator between UNC and non-absolute path
  95. if (result_path and result_path[0] not in seps and
  96. result_drive and result_drive[-1:] != colon):
  97. return result_drive + sep + result_path
  98. return result_drive + result_path
  99. except (TypeError, AttributeError, BytesWarning):
  100. genericpath._check_arg_types('join', path, *paths)
  101. raise
  102. # Split a path in a drive specification (a drive letter followed by a
  103. # colon) and the path specification.
  104. # It is always true that drivespec + pathspec == p
  105. def splitdrive(p):
  106. """Split a pathname into drive/UNC sharepoint and relative path specifiers.
  107. Returns a 2-tuple (drive_or_unc, path); either part may be empty.
  108. If you assign
  109. result = splitdrive(p)
  110. It is always true that:
  111. result[0] + result[1] == p
  112. If the path contained a drive letter, drive_or_unc will contain everything
  113. up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir")
  114. If the path contained a UNC path, the drive_or_unc will contain the host name
  115. and share up to but not including the fourth directory separator character.
  116. e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")
  117. Paths cannot contain both a drive letter and a UNC path.
  118. """
  119. if len(p) >= 2:
  120. if isinstance(p, bytes):
  121. sep = b'\\'
  122. altsep = b'/'
  123. colon = b':'
  124. else:
  125. sep = '\\'
  126. altsep = '/'
  127. colon = ':'
  128. normp = p.replace(altsep, sep)
  129. if (normp[0:2] == sep*2) and (normp[2:3] != sep):
  130. # is a UNC path:
  131. # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
  132. # \\machine\mountpoint\directory\etc\...
  133. # directory ^^^^^^^^^^^^^^^
  134. index = normp.find(sep, 2)
  135. if index == -1:
  136. return p[:0], p
  137. index2 = normp.find(sep, index + 1)
  138. # a UNC path can't have two slashes in a row
  139. # (after the initial two)
  140. if index2 == index + 1:
  141. return p[:0], p
  142. if index2 == -1:
  143. index2 = len(p)
  144. return p[:index2], p[index2:]
  145. if normp[1:2] == colon:
  146. return p[:2], p[2:]
  147. return p[:0], p
  148. # Parse UNC paths
  149. def splitunc(p):
  150. """Deprecated since Python 3.1. Please use splitdrive() instead;
  151. it now handles UNC paths.
  152. Split a pathname into UNC mount point and relative path specifiers.
  153. Return a 2-tuple (unc, rest); either part may be empty.
  154. If unc is not empty, it has the form '//host/mount' (or similar
  155. using backslashes). unc+rest is always the input path.
  156. Paths containing drive letters never have a UNC part.
  157. """
  158. import warnings
  159. warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead",
  160. DeprecationWarning, 2)
  161. drive, path = splitdrive(p)
  162. if len(drive) == 2:
  163. # Drive letter present
  164. return p[:0], p
  165. return drive, path
  166. # Split a path in head (everything up to the last '/') and tail (the
  167. # rest). After the trailing '/' is stripped, the invariant
  168. # join(head, tail) == p holds.
  169. # The resulting head won't end in '/' unless it is the root.
  170. def split(p):
  171. """Split a pathname.
  172. Return tuple (head, tail) where tail is everything after the final slash.
  173. Either part may be empty."""
  174. seps = _get_bothseps(p)
  175. d, p = splitdrive(p)
  176. # set i to index beyond p's last slash
  177. i = len(p)
  178. while i and p[i-1] not in seps:
  179. i -= 1
  180. head, tail = p[:i], p[i:] # now tail has no slashes
  181. # remove trailing slashes from head, unless it's all slashes
  182. head = head.rstrip(seps) or head
  183. return d + head, tail
  184. # Split a path in root and extension.
  185. # The extension is everything starting at the last dot in the last
  186. # pathname component; the root is everything before that.
  187. # It is always true that root + ext == p.
  188. def splitext(p):
  189. if isinstance(p, bytes):
  190. return genericpath._splitext(p, b'\\', b'/', b'.')
  191. else:
  192. return genericpath._splitext(p, '\\', '/', '.')
  193. splitext.__doc__ = genericpath._splitext.__doc__
  194. # Return the tail (basename) part of a path.
  195. def basename(p):
  196. """Returns the final component of a pathname"""
  197. return split(p)[1]
  198. # Return the head (dirname) part of a path.
  199. def dirname(p):
  200. """Returns the directory component of a pathname"""
  201. return split(p)[0]
  202. # Is a path a symbolic link?
  203. # This will always return false on systems where os.lstat doesn't exist.
  204. def islink(path):
  205. """Test whether a path is a symbolic link.
  206. This will always return false for Windows prior to 6.0.
  207. """
  208. try:
  209. st = os.lstat(path)
  210. except (OSError, AttributeError):
  211. return False
  212. return stat.S_ISLNK(st.st_mode)
  213. # Being true for dangling symbolic links is also useful.
  214. def lexists(path):
  215. """Test whether a path exists. Returns True for broken symbolic links"""
  216. try:
  217. st = os.lstat(path)
  218. except OSError:
  219. return False
  220. return True
  221. # Is a path a mount point?
  222. # Any drive letter root (eg c:\)
  223. # Any share UNC (eg \\server\share)
  224. # Any volume mounted on a filesystem folder
  225. #
  226. # No one method detects all three situations. Historically we've lexically
  227. # detected drive letter roots and share UNCs. The canonical approach to
  228. # detecting mounted volumes (querying the reparse tag) fails for the most
  229. # common case: drive letter roots. The alternative which uses GetVolumePathName
  230. # fails if the drive letter is the result of a SUBST.
  231. try:
  232. from nt import _getvolumepathname
  233. except ImportError:
  234. _getvolumepathname = None
  235. def ismount(path):
  236. """Test whether a path is a mount point (a drive root, the root of a
  237. share, or a mounted volume)"""
  238. seps = _get_bothseps(path)
  239. path = abspath(path)
  240. root, rest = splitdrive(path)
  241. if root and root[0] in seps:
  242. return (not rest) or (rest in seps)
  243. if rest in seps:
  244. return True
  245. if _getvolumepathname:
  246. return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps)
  247. else:
  248. return False
  249. # Expand paths beginning with '~' or '~user'.
  250. # '~' means $HOME; '~user' means that user's home directory.
  251. # If the path doesn't begin with '~', or if the user or $HOME is unknown,
  252. # the path is returned unchanged (leaving error reporting to whatever
  253. # function is called with the expanded path as argument).
  254. # See also module 'glob' for expansion of *, ? and [...] in pathnames.
  255. # (A function should also be defined to do full *sh-style environment
  256. # variable expansion.)
  257. def expanduser(path):
  258. """Expand ~ and ~user constructs.
  259. If user or $HOME is unknown, do nothing."""
  260. if isinstance(path, bytes):
  261. tilde = b'~'
  262. else:
  263. tilde = '~'
  264. if not path.startswith(tilde):
  265. return path
  266. i, n = 1, len(path)
  267. while i < n and path[i] not in _get_bothseps(path):
  268. i += 1
  269. if 'HOME' in os.environ:
  270. userhome = os.environ['HOME']
  271. elif 'USERPROFILE' in os.environ:
  272. userhome = os.environ['USERPROFILE']
  273. elif not 'HOMEPATH' in os.environ:
  274. return path
  275. else:
  276. try:
  277. drive = os.environ['HOMEDRIVE']
  278. except KeyError:
  279. drive = ''
  280. userhome = join(drive, os.environ['HOMEPATH'])
  281. if isinstance(path, bytes):
  282. userhome = os.fsencode(userhome)
  283. if i != 1: #~user
  284. userhome = join(dirname(userhome), path[1:i])
  285. return userhome + path[i:]
  286. # Expand paths containing shell variable substitutions.
  287. # The following rules apply:
  288. # - no expansion within single quotes
  289. # - '$$' is translated into '$'
  290. # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
  291. # - ${varname} is accepted.
  292. # - $varname is accepted.
  293. # - %varname% is accepted.
  294. # - varnames can be made out of letters, digits and the characters '_-'
  295. # (though is not verified in the ${varname} and %varname% cases)
  296. # XXX With COMMAND.COM you can use any characters in a variable name,
  297. # XXX except '^|<>='.
  298. def expandvars(path):
  299. """Expand shell variables of the forms $var, ${var} and %var%.
  300. Unknown variables are left unchanged."""
  301. if isinstance(path, bytes):
  302. if b'$' not in path and b'%' not in path:
  303. return path
  304. import string
  305. varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
  306. quote = b'\''
  307. percent = b'%'
  308. brace = b'{'
  309. rbrace = b'}'
  310. dollar = b'$'
  311. environ = getattr(os, 'environb', None)
  312. else:
  313. if '$' not in path and '%' not in path:
  314. return path
  315. import string
  316. varchars = string.ascii_letters + string.digits + '_-'
  317. quote = '\''
  318. percent = '%'
  319. brace = '{'
  320. rbrace = '}'
  321. dollar = '$'
  322. environ = os.environ
  323. res = path[:0]
  324. index = 0
  325. pathlen = len(path)
  326. while index < pathlen:
  327. c = path[index:index+1]
  328. if c == quote: # no expansion within single quotes
  329. path = path[index + 1:]
  330. pathlen = len(path)
  331. try:
  332. index = path.index(c)
  333. res += c + path[:index + 1]
  334. except ValueError:
  335. res += c + path
  336. index = pathlen - 1
  337. elif c == percent: # variable or '%'
  338. if path[index + 1:index + 2] == percent:
  339. res += c
  340. index += 1
  341. else:
  342. path = path[index+1:]
  343. pathlen = len(path)
  344. try:
  345. index = path.index(percent)
  346. except ValueError:
  347. res += percent + path
  348. index = pathlen - 1
  349. else:
  350. var = path[:index]
  351. try:
  352. if environ is None:
  353. value = os.fsencode(os.environ[os.fsdecode(var)])
  354. else:
  355. value = environ[var]
  356. except KeyError:
  357. value = percent + var + percent
  358. res += value
  359. elif c == dollar: # variable or '$$'
  360. if path[index + 1:index + 2] == dollar:
  361. res += c
  362. index += 1
  363. elif path[index + 1:index + 2] == brace:
  364. path = path[index+2:]
  365. pathlen = len(path)
  366. try:
  367. index = path.index(rbrace)
  368. except ValueError:
  369. res += dollar + brace + path
  370. index = pathlen - 1
  371. else:
  372. var = path[:index]
  373. try:
  374. if environ is None:
  375. value = os.fsencode(os.environ[os.fsdecode(var)])
  376. else:
  377. value = environ[var]
  378. except KeyError:
  379. value = dollar + brace + var + rbrace
  380. res += value
  381. else:
  382. var = path[:0]
  383. index += 1
  384. c = path[index:index + 1]
  385. while c and c in varchars:
  386. var += c
  387. index += 1
  388. c = path[index:index + 1]
  389. try:
  390. if environ is None:
  391. value = os.fsencode(os.environ[os.fsdecode(var)])
  392. else:
  393. value = environ[var]
  394. except KeyError:
  395. value = dollar + var
  396. res += value
  397. if c:
  398. index -= 1
  399. else:
  400. res += c
  401. index += 1
  402. return res
  403. # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
  404. # Previously, this function also truncated pathnames to 8+3 format,
  405. # but as this module is called "ntpath", that's obviously wrong!
  406. def normpath(path):
  407. """Normalize path, eliminating double slashes, etc."""
  408. if isinstance(path, bytes):
  409. sep = b'\\'
  410. altsep = b'/'
  411. curdir = b'.'
  412. pardir = b'..'
  413. special_prefixes = (b'\\\\.\\', b'\\\\?\\')
  414. else:
  415. sep = '\\'
  416. altsep = '/'
  417. curdir = '.'
  418. pardir = '..'
  419. special_prefixes = ('\\\\.\\', '\\\\?\\')
  420. if path.startswith(special_prefixes):
  421. # in the case of paths with these prefixes:
  422. # \\.\ -> device names
  423. # \\?\ -> literal paths
  424. # do not do any normalization, but return the path unchanged
  425. return path
  426. path = path.replace(altsep, sep)
  427. prefix, path = splitdrive(path)
  428. # collapse initial backslashes
  429. if path.startswith(sep):
  430. prefix += sep
  431. path = path.lstrip(sep)
  432. comps = path.split(sep)
  433. i = 0
  434. while i < len(comps):
  435. if not comps[i] or comps[i] == curdir:
  436. del comps[i]
  437. elif comps[i] == pardir:
  438. if i > 0 and comps[i-1] != pardir:
  439. del comps[i-1:i+1]
  440. i -= 1
  441. elif i == 0 and prefix.endswith(sep):
  442. del comps[i]
  443. else:
  444. i += 1
  445. else:
  446. i += 1
  447. # If the path is now empty, substitute '.'
  448. if not prefix and not comps:
  449. comps.append(curdir)
  450. return prefix + sep.join(comps)
  451. # Return an absolute path.
  452. try:
  453. from nt import _getfullpathname
  454. except ImportError: # not running on Windows - mock up something sensible
  455. def abspath(path):
  456. """Return the absolute version of a path."""
  457. if not isabs(path):
  458. if isinstance(path, bytes):
  459. cwd = os.getcwdb()
  460. else:
  461. cwd = os.getcwd()
  462. path = join(cwd, path)
  463. return normpath(path)
  464. else: # use native Windows method on Windows
  465. def abspath(path):
  466. """Return the absolute version of a path."""
  467. if path: # Empty path must return current working directory.
  468. try:
  469. path = _getfullpathname(path)
  470. except OSError:
  471. pass # Bad path - return unchanged.
  472. elif isinstance(path, bytes):
  473. path = os.getcwdb()
  474. else:
  475. path = os.getcwd()
  476. return normpath(path)
  477. # realpath is a no-op on systems without islink support
  478. realpath = abspath
  479. # Win9x family and earlier have no Unicode filename support.
  480. supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
  481. sys.getwindowsversion()[3] >= 2)
  482. def relpath(path, start=None):
  483. """Return a relative version of a path"""
  484. if isinstance(path, bytes):
  485. sep = b'\\'
  486. curdir = b'.'
  487. pardir = b'..'
  488. else:
  489. sep = '\\'
  490. curdir = '.'
  491. pardir = '..'
  492. if start is None:
  493. start = curdir
  494. if not path:
  495. raise ValueError("no path specified")
  496. try:
  497. start_abs = abspath(normpath(start))
  498. path_abs = abspath(normpath(path))
  499. start_drive, start_rest = splitdrive(start_abs)
  500. path_drive, path_rest = splitdrive(path_abs)
  501. if normcase(start_drive) != normcase(path_drive):
  502. raise ValueError("path is on mount %r, start on mount %r" % (
  503. path_drive, start_drive))
  504. start_list = [x for x in start_rest.split(sep) if x]
  505. path_list = [x for x in path_rest.split(sep) if x]
  506. # Work out how much of the filepath is shared by start and path.
  507. i = 0
  508. for e1, e2 in zip(start_list, path_list):
  509. if normcase(e1) != normcase(e2):
  510. break
  511. i += 1
  512. rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
  513. if not rel_list:
  514. return curdir
  515. return join(*rel_list)
  516. except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning):
  517. genericpath._check_arg_types('relpath', path, start)
  518. raise
  519. # Return the longest common sub-path of the sequence of paths given as input.
  520. # The function is case-insensitive and 'separator-insensitive', i.e. if the
  521. # only difference between two paths is the use of '\' versus '/' as separator,
  522. # they are deemed to be equal.
  523. #
  524. # However, the returned path will have the standard '\' separator (even if the
  525. # given paths had the alternative '/' separator) and will have the case of the
  526. # first path given in the sequence. Additionally, any trailing separator is
  527. # stripped from the returned path.
  528. def commonpath(paths):
  529. """Given a sequence of path names, returns the longest common sub-path."""
  530. if not paths:
  531. raise ValueError('commonpath() arg is an empty sequence')
  532. if isinstance(paths[0], bytes):
  533. sep = b'\\'
  534. altsep = b'/'
  535. curdir = b'.'
  536. else:
  537. sep = '\\'
  538. altsep = '/'
  539. curdir = '.'
  540. try:
  541. drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths]
  542. split_paths = [p.split(sep) for d, p in drivesplits]
  543. try:
  544. isabs, = set(p[:1] == sep for d, p in drivesplits)
  545. except ValueError:
  546. raise ValueError("Can't mix absolute and relative paths") from None
  547. # Check that all drive letters or UNC paths match. The check is made only
  548. # now otherwise type errors for mixing strings and bytes would not be
  549. # caught.
  550. if len(set(d for d, p in drivesplits)) != 1:
  551. raise ValueError("Paths don't have the same drive")
  552. drive, path = splitdrive(paths[0].replace(altsep, sep))
  553. common = path.split(sep)
  554. common = [c for c in common if c and c != curdir]
  555. split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
  556. s1 = min(split_paths)
  557. s2 = max(split_paths)
  558. for i, c in enumerate(s1):
  559. if c != s2[i]:
  560. common = common[:i]
  561. break
  562. else:
  563. common = common[:len(s1)]
  564. prefix = drive + sep if isabs else drive
  565. return prefix + sep.join(common)
  566. except (TypeError, AttributeError):
  567. genericpath._check_arg_types('commonpath', *paths)
  568. raise
  569. # determine if two files are in fact the same file
  570. try:
  571. # GetFinalPathNameByHandle is available starting with Windows 6.0.
  572. # Windows XP and non-Windows OS'es will mock _getfinalpathname.
  573. if sys.getwindowsversion()[:2] >= (6, 0):
  574. from nt import _getfinalpathname
  575. else:
  576. raise ImportError
  577. except (AttributeError, ImportError):
  578. # On Windows XP and earlier, two files are the same if their absolute
  579. # pathnames are the same.
  580. # Non-Windows operating systems fake this method with an XP
  581. # approximation.
  582. def _getfinalpathname(f):
  583. return normcase(abspath(f))
  584. try:
  585. # The genericpath.isdir implementation uses os.stat and checks the mode
  586. # attribute to tell whether or not the path is a directory.
  587. # This is overkill on Windows - just pass the path to GetFileAttributes
  588. # and check the attribute from there.
  589. from nt import _isdir as isdir
  590. except ImportError:
  591. # Use genericpath.isdir as imported above.
  592. pass