# Module 'os2emxpath' -- common operations on OS/2 pathnames
"""Common pathname manipulations, OS/2 EMX version.
Instead of importing this module directly, import os and refer to this
__all__
= ["normcase","isabs","join","splitdrive","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
"ismount","walk","expanduser","expandvars","normpath","abspath",
"splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
"extsep","devnull","realpath","supports_unicode_filenames"]
# strings representing various path-related bits and pieces
# Normalize the case of a pathname and map slashes to backslashes.
# Other normalizations (such as optimizing '../' away) are not done
# (this is done by normpath).
"""Normalize case of pathname.
Makes all characters lowercase and all altseps into seps."""
return s
.replace('\\', '/').lower()
# Return whether a path is absolute.
# Trivial in Posix, harder on the Mac or MS-DOS.
# For DOS it is absolute if it starts with a slash or backslash (current
# volume), or if a pathname after the volume letter and colon / UNC resource
# starts with a slash or backslash.
"""Test whether a path is absolute"""
return s
!= '' and s
[:1] in '/\\'
# Join two (or more) paths.
"""Join two or more pathname components, inserting sep as needed"""
elif path
== '' or path
[-1:] in '/\\:':
# Split a path in a drive specification (a drive letter followed by a
# colon) and the path specification.
# It is always true that drivespec + pathspec == p
"""Split a pathname into drive and path specifiers. Returns a 2-tuple
"(drive,path)"; either part may be empty"""
"""Split a pathname into UNC mount point and relative path specifiers.
Return a 2-tuple (unc, rest); either part may be empty.
If unc is not empty, it has the form '//host/mount' (or similar
using backslashes). unc+rest is always the input path.
Paths containing drive letters never have an UNC part.
return '', p
# Drive letter present
if firstTwo
== '/' * 2 or firstTwo
== '\\' * 2:
# vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
# \\machine\mountpoint\directories...
# directory ^^^^^^^^^^^^^^^
index
= normp
.find('/', 2)
##raise RuntimeError, 'illegal UNC path: "' + p + '"'
index
= normp
.find('/', index
+ 1)
return p
[:index
], p
[index
:]
# Split a path in head (everything up to the last '/') and tail (the
# rest). After the trailing '/' is stripped, the invariant
# join(head, tail) == p holds.
# The resulting head won't end in '/' unless it is the root.
Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty."""
# set i to index beyond p's last slash
while i
and p
[i
-1] not in '/\\':
head
, tail
= p
[:i
], p
[i
:] # now tail has no slashes
# remove trailing slashes from head, unless it's all slashes
while head2
and head2
[-1] in '/\\':
# Split a path in root and extension.
# The extension is everything starting at the last dot in the last
# pathname component; the root is everything before that.
# It is always true that root + ext == p.
"""Split the extension from a pathname.
Extension is everything from the last dot to the end.
Return (root, ext), either part may be empty."""
root
, ext
= root
+ ext
+ c
, ''
root
, ext
= root
+ ext
, c
# Return the tail (basename) part of a path.
"""Returns the final component of a pathname"""
# Return the head (dirname) part of a path.
"""Returns the directory component of a pathname"""
# Return the longest prefix of all list elements.
"Given a list of pathnames, returns the longest common leading component"
for i
in range(len(prefix
)):
if prefix
[:i
+1] != item
[:i
+1]:
# Get size, mtime, atime of files.
"""Return the size of a file, reported by os.stat()"""
return os
.stat(filename
).st_size
"""Return the last modification time of a file, reported by os.stat()"""
return os
.stat(filename
).st_mtime
"""Return the last access time of a file, reported by os.stat()"""
return os
.stat(filename
).st_atime
"""Return the creation time of a file, reported by os.stat()."""
return os
.stat(filename
).st_ctime
# Is a path a symbolic link?
# This will always return false on systems where posix.lstat doesn't exist.
"""Test for symbolic link. On OS/2 always returns false"""
# This is false for dangling symbolic links.
"""Test whether a path exists"""
"""Test whether a path is a directory"""
return stat
.S_ISDIR(st
.st_mode
)
# Is a path a regular file?
# This follows symbolic links, so both islink() and isdir() can be true
"""Test whether a path is a regular file"""
return stat
.S_ISREG(st
.st_mode
)
# Is a path a mount point? Either a root (with or without drive letter)
# or an UNC path with at most a / or \ after the mount point.
"""Test whether a path is a mount point (defined as root of drive)"""
unc
, rest
= splitunc(path
)
return rest
in ("", "/", "\\")
return len(p
) == 1 and p
[0] in '/\\'
# For each directory under top (including top itself, but excluding
# '.' and '..'), func(arg, dirname, filenames) is called, where
# dirname is the name of the directory and filenames is the list
# of files (and subdirectories etc.) in the directory.
# The func may modify the filenames list, to implement a filter,
# or to impose a different order of visiting.
def walk(top
, func
, arg
):
"""Directory tree walk whth callback function.
walk(top, func, arg) calls func(arg, d, files) for each directory d
in the tree rooted at top (including top itself); files is a list
of all the files and subdirs in directory d."""
if name
not in exceptions
:
# Expand paths beginning with '~' or '~user'.
# '~' means $HOME; '~user' means that user's home directory.
# If the path doesn't begin with '~', or if the user or $HOME is unknown,
# the path is returned unchanged (leaving error reporting to whatever
# function is called with the expanded path as argument).
# See also module 'glob' for expansion of *, ? and [...] in pathnames.
# (A function should also be defined to do full *sh-style environment
"""Expand ~ and ~user constructs.
If user or $HOME is unknown, do nothing."""
while i
< n
and path
[i
] not in '/\\':
userhome
= os
.environ
['HOME']
elif not 'HOMEPATH' in os
.environ
:
drive
= os
.environ
['HOMEDRIVE']
userhome
= join(drive
, os
.environ
['HOMEPATH'])
return userhome
+ path
[i
:]
# Expand paths containing shell variable substitutions.
# The following rules apply:
# - no expansion within single quotes
# - no escape character, except for '$$' which is translated into '$'
# - ${varname} is accepted.
# - varnames can be made out of letters, digits and the character '_'
# XXX With COMMAND.COM you can use any characters in a variable name,
"""Expand shell variables of form $var and ${var}.
Unknown variables are left unchanged."""
varchars
= string
.letters
+ string
.digits
+ '_-'
if c
== '\'': # no expansion within single quotes
res
= res
+ '\'' + path
[:index
+ 1]
elif c
== '$': # variable or '$$'
if path
[index
+ 1:index
+ 2] == '$':
elif path
[index
+ 1:index
+ 2] == '{':
res
= res
+ os
.environ
[var
]
c
= path
[index
:index
+ 1]
while c
!= '' and c
in varchars
:
c
= path
[index
:index
+ 1]
res
= res
+ os
.environ
[var
]
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
"""Normalize path, eliminating double slashes, etc."""
path
= path
.replace('\\', '/')
prefix
, path
= splitdrive(path
)
elif comps
[i
] == '..' and i
> 0 and comps
[i
-1] not in ('', '..'):
elif comps
[i
] == '' and i
> 0 and comps
[i
-1] != '':
# If the path is now empty, substitute '.'
if not prefix
and not comps
:
return prefix
+ '/'.join(comps
)
# Return an absolute path.
"""Return the absolute version of a path"""
path
= join(os
.getcwd(), path
)
# realpath is a no-op on systems without islink support
supports_unicode_filenames
= False