You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macpath.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """Pathname and path-related operations for the Macintosh."""
  2. import os
  3. from stat import *
  4. __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
  5. "basename","dirname","commonprefix","getsize","getmtime",
  6. "getatime","islink","exists","isdir","isfile",
  7. "walk","expanduser","expandvars","normpath","abspath"]
  8. # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
  9. def normcase(path):
  10. return path.lower()
  11. def isabs(s):
  12. """Return true if a path is absolute.
  13. On the Mac, relative paths begin with a colon,
  14. but as a special case, paths with no colons at all are also relative.
  15. Anything else is absolute (the string up to the first colon is the
  16. volume name)."""
  17. return ':' in s and s[0] != ':'
  18. def join(s, *p):
  19. path = s
  20. for t in p:
  21. if (not s) or isabs(t):
  22. path = t
  23. continue
  24. if t[:1] == ':':
  25. t = t[1:]
  26. if ':' not in path:
  27. path = ':' + path
  28. if path[-1:] != ':':
  29. path = path + ':'
  30. path = path + t
  31. return path
  32. def split(s):
  33. """Split a pathname into two parts: the directory leading up to the final
  34. bit, and the basename (the filename, without colons, in that directory).
  35. The result (s, t) is such that join(s, t) yields the original argument."""
  36. if ':' not in s: return '', s
  37. colon = 0
  38. for i in range(len(s)):
  39. if s[i] == ':': colon = i + 1
  40. path, file = s[:colon-1], s[colon:]
  41. if path and not ':' in path:
  42. path = path + ':'
  43. return path, file
  44. def splitext(p):
  45. """Split a path into root and extension.
  46. The extension is everything starting at the last dot in the last
  47. pathname component; the root is everything before that.
  48. It is always true that root + ext == p."""
  49. root, ext = '', ''
  50. for c in p:
  51. if c == ':':
  52. root, ext = root + ext + c, ''
  53. elif c == '.':
  54. if ext:
  55. root, ext = root + ext, c
  56. else:
  57. ext = c
  58. elif ext:
  59. ext = ext + c
  60. else:
  61. root = root + c
  62. return root, ext
  63. def splitdrive(p):
  64. """Split a pathname into a drive specification and the rest of the
  65. path. Useful on DOS/Windows/NT; on the Mac, the drive is always
  66. empty (don't use the volume name -- it doesn't have the same
  67. syntactic and semantic oddities as DOS drive letters, such as there
  68. being a separate current directory per drive)."""
  69. return '', p
  70. # Short interfaces to split()
  71. def dirname(s): return split(s)[0]
  72. def basename(s): return split(s)[1]
  73. def isdir(s):
  74. """Return true if the pathname refers to an existing directory."""
  75. try:
  76. st = os.stat(s)
  77. except os.error:
  78. return 0
  79. return S_ISDIR(st[ST_MODE])
  80. # Get size, mtime, atime of files.
  81. def getsize(filename):
  82. """Return the size of a file, reported by os.stat()."""
  83. st = os.stat(filename)
  84. return st[ST_SIZE]
  85. def getmtime(filename):
  86. """Return the last modification time of a file, reported by os.stat()."""
  87. st = os.stat(filename)
  88. return st[ST_MTIME]
  89. def getatime(filename):
  90. """Return the last access time of a file, reported by os.stat()."""
  91. st = os.stat(filename)
  92. return st[ST_ATIME]
  93. def islink(s):
  94. """Return true if the pathname refers to a symbolic link.
  95. Always false on the Mac, until we understand Aliases.)"""
  96. return 0
  97. def isfile(s):
  98. """Return true if the pathname refers to an existing regular file."""
  99. try:
  100. st = os.stat(s)
  101. except os.error:
  102. return 0
  103. return S_ISREG(st[ST_MODE])
  104. def exists(s):
  105. """Return true if the pathname refers to an existing file or directory."""
  106. try:
  107. st = os.stat(s)
  108. except os.error:
  109. return 0
  110. return 1
  111. # Return the longest prefix of all list elements.
  112. def commonprefix(m):
  113. "Given a list of pathnames, returns the longest common leading component"
  114. if not m: return ''
  115. prefix = m[0]
  116. for item in m:
  117. for i in range(len(prefix)):
  118. if prefix[:i+1] != item[:i+1]:
  119. prefix = prefix[:i]
  120. if i == 0: return ''
  121. break
  122. return prefix
  123. def expandvars(path):
  124. """Dummy to retain interface-compatibility with other operating systems."""
  125. return path
  126. def expanduser(path):
  127. """Dummy to retain interface-compatibility with other operating systems."""
  128. return path
  129. norm_error = 'macpath.norm_error: path cannot be normalized'
  130. def normpath(s):
  131. """Normalize a pathname. Will return the same result for
  132. equivalent paths."""
  133. if ":" not in s:
  134. return ":"+s
  135. comps = s.split(":")
  136. i = 1
  137. while i < len(comps)-1:
  138. if comps[i] == "" and comps[i-1] != "":
  139. if i > 1:
  140. del comps[i-1:i+1]
  141. i = i - 1
  142. else:
  143. # best way to handle this is to raise an exception
  144. raise norm_error, 'Cannot use :: immedeately after volume name'
  145. else:
  146. i = i + 1
  147. s = ":".join(comps)
  148. # remove trailing ":" except for ":" and "Volume:"
  149. if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
  150. s = s[:-1]
  151. return s
  152. def walk(top, func, arg):
  153. """Directory tree walk.
  154. For each directory under top (including top itself),
  155. func(arg, dirname, filenames) is called, where
  156. dirname is the name of the directory and filenames is the list
  157. of files (and subdirectories etc.) in the directory.
  158. The func may modify the filenames list, to implement a filter,
  159. or to impose a different order of visiting."""
  160. try:
  161. names = os.listdir(top)
  162. except os.error:
  163. return
  164. func(arg, top, names)
  165. for name in names:
  166. name = join(top, name)
  167. if isdir(name):
  168. walk(name, func, arg)
  169. def abspath(path):
  170. """Return an absolute path."""
  171. if not isabs(path):
  172. path = join(os.getcwd(), path)
  173. return normpath(path)