[Python-checkins] python/nondist/sandbox/path path.py, NONE, 1.1 test_path.py, NONE, 1.1

birkenfeld@users.sourceforge.net birkenfeld at users.sourceforge.net
Mon Jul 18 14:09:28 CEST 2005


Update of /cvsroot/python/python/nondist/sandbox/path
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17007

Added Files:
	path.py test_path.py 
Log Message:
Adding first revision of revised Path module for the hopefully upcoming PEP.



--- NEW FILE: path.py ---
""" path.py - An object representing a path to a file or directory.

Example:

from os.path import Path
d = Path('/home/guido/bin')
for f in d.files('*.py'):
    f.chmod(0755)

Author:  Jason Orendorff <jason at jorendorff.com> (and others)
Date:    7 Mar 2004

Adapted for stdlib by: Reinhold Birkenfeld, July 2005
"""


# TODO
#   - Bug in write_text().  It doesn't support Universal newline mode.
#   - Better error message in listdir() when self isn't a
#     directory. (On Windows, the error message really sucks.)
#   - Add methods for regex find and replace.
#   - guess_content_type() method?

# TODO (standard module)
# - add more standard header?
# - shutil functions should accept a Path as "dst"
# - Make sure everything has a good docstring.


import sys, os, fnmatch, glob, shutil, codecs

__version__ = "$Id: path.py,v 1.1 2005/07/18 12:09:26 birkenfeld Exp $"
__all__ = ['Path']


# Universal newline support
_textmode = 'r'
if hasattr(file, 'newlines'):
    _textmode = 'U'

# helper function: if given string/unicode, return it
#                  if given path, return path string
def _strpath(thing):
    if isinstance(thing, Path):
        return thing._p
    return thing

class Path(object):
    """ Represents a filesystem path.

    Path is an immutable object.

    For documentation on individual methods, consult their
    counterparts in os.path.
    """

    # --- Special Python methods.

    def __init__(self, path=os.curdir):
        """ Initialize a Path instance.

        The path argument can be either a string or an existing Path object.
        """
        if isinstance(path, Path):
            self._p = path._p
        else:
            self._p = path

    def __repr__(self):
        return 'Path(%s)' % repr(self._p)

    def __str__(self):
        return str(self._p)

    # Adding path and string yields a path
    def __add__(self, other):
        if isinstance(other, (str, unicode)):
            return Path(self._p + other)
        elif isinstance(other, Path):
            return Path(self._p + other._p)
        return NotImplemented

    def __radd__(self, other):
        if isinstance(other, (str, unicode)):
            return Path(other + self._p)
        elif isinstance(other, Path):
            return Path(other._p + self._p)
        return NotImplemented

    # The / joins paths
    def __div__(self, other):
        return self.joinwith(other)
    
    __truediv__ = __div__
    
    # Rich comparison
    def __eq__(self, other):
        return isinstance(other, Path) and (self._p == other._p)
    
    def __ne__(self, other):
        return (not isinstance(other, Path)) or (self._p != other._p)

    def __lt__(self, other):
        if isinstance(other, Path):
            return self._p < other._p
        return NotImplemented
    def __le__(self, other):
        if isinstance(other, Path):
            return self._p <= other._p
        return NotImplemented
    def __gt__(self, other):
        if isinstance(other, Path):
            return self._p > other._p
        return NotImplemented
    def __ge__(self, other):
        if isinstance(other, Path):
            return self._p >= other._p
        return NotImplemented

    # Hash like strings
    def __hash__(self):
        return hash(self._p) + 1

    # Alternative constructor.
    
    @staticmethod
    def cwd():
        """ Return the current working directory as a path object. """
        if os.path.supports_unicode_filenames:
            return Path(os.getcwdu())
        else:
            return Path(os.getcwd())


    # --- Operations which yield strings

    def _get_basename(self):
        return os.path.basename(self._p)
    
    def _get_namebase(self):
        base, ext = os.path.splitext(self.basename)
        return base

    def _get_ext(self):
        f, ext = os.path.splitext(self._p)
        return ext

    def _get_drive(self):
        drive, r = os.path.splitdrive(self._p)
        return drive

    basename = property(
        _get_basename, None, None,
        """ The name of this file or directory without the full path.

        For example, path('/usr/local/lib/libpython.so').basename == 'libpython.so'
        """)

    namebase = property(
        _get_namebase, None, None,
        """ The same as Path.basename, but with one file extension stripped off.

        For example, path('/home/guido/python.tar.gz').basename == 'python.tar.gz',
        but          path('/home/guido/python.tar.gz').namebase == 'python.tar'
        """)

    ext = property(
        _get_ext, None, None,
        """ The file extension, for example '.py'. """)

    drive = property(
        _get_drive, None, None,
        """ The drive specifier, for example 'C:'.
        This is always empty on systems that don't use drive specifiers.
        """)

    # --- Operations which yield Path objects

    def abspath(self):
        return Path(os.path.abspath(self._p))
    
    def normcase(self):
        return Path(os.path.normcase(self._p))
    
    def normpath(self):
        return Path(os.path.normpath(self._p))
    
    def realpath(self):
        return Path(os.path.realpath(self._p))
    
    def expanduser(self):
        return Path(os.path.expanduser(self._p))
    
    def expandvars(self):
        return Path(os.path.expandvars(self._p))
    
    def expand(self):
        """ Clean up a filename by calling expandvars(),
        expanduser(), and normpath() on it.

        This is commonly everything needed to clean up a filename
        read from a configuration file, for example.
        """
        return self.expandvars().expanduser().normpath()

    def _get_directory(self):
        return Path(os.path.dirname(self._p))

    directory = property(
        _get_directory, None, None,
        """ This path's parent directory, as a new path object.

        For example, Path('/usr/local/lib/libpython.so').directory == Path('/usr/local/lib')
        """)

    def stripext(self):
        """ p.stripext() -> Remove one file extension from the path.

        For example, path('/home/guido/python.tar.gz').stripext()
        returns path('/home/guido/python.tar').
        """
        return self.splitext()[0]

    # --- Operations which yield Paths and strings
    
    def splitpath(self):
        """ p.splitpath() -> Return (p.directory, p.basename). """
        parent, child = os.path.split(self._p)
        return Path(parent), child

    def splitdrive(self):
        """ p.splitdrive() -> Return (Path(p.drive), <the rest of p>).

        Split the drive specifier from this path.  If there is
        no drive specifier, p.drive is empty, so the return value
        is simply (Path(''), p).  This is always the case on Unix.
        """
        drive, rel = os.path.splitdrive(self._p)
        return Path(drive), rel

    def splitext(self):
        """ p.splitext() -> Return (p.stripext(), p.ext).

        Split the filename extension from this path and return
        the two parts.  Either part may be empty.

        The extension is everything from '.' to the end of the
        last path segment.
        """
        filename, ext = os.path.splitext(self._p)
        return Path(filename), ext

    if hasattr(os.path, 'splitunc'):
        def splitunc(self):
            unc, rest = os.path.splitunc(self._p)
            return Path(unc), rest

        def _get_uncshare(self):
            unc, r = os.path.splitunc(self._p)
            return Path(unc)

        uncshare = property(
            _get_uncshare, None, None,
            """ The UNC mount point for this path.
            This is empty for paths on local drives. """)

    @staticmethod
    def join(*args):
        if args:
            return Path(args[0]).joinwith(args[1:])
        return Path('')
    
    def joinwith(self, *args):
        """ Join two or more path components, adding a separator
        character (os.sep) if needed.  Returns a new path
        object.
        """
        return Path(os.path.join(self._p, *map(str, args)))

    def parts(self):
        """ Return a list of the path components in this path.

        The first item in the list will be a path.  Its value will be
        either os.curdir, os.pardir, empty, or the root directory of
        this path (for example, '/' or 'C:\\').  The other items in
        the list will be strings.

        Path.join(*result) will yield the original path.
        """
        parts = []
        loc = self
        while loc != os.curdir and loc != os.pardir:
            prev = loc
            loc, child = prev.splitpath()
            if loc == prev:
                break
            parts.append(child)
        parts.append(loc)
        parts.reverse()
        return parts

    def relpath(self):
        """ Return this path as a relative path,
        based from the current working directory.
        """
        return Path.cwd().relpathto(self)

    def relpathto(self, dest):
        """ Return a relative path from self to dest.

        If there is no relative path from self to dest, for example if
        they reside on different drives in Windows, then this returns
        dest.abspath().
        """
        origin = self.abspath()
        dest = Path(dest).abspath()

        orig_list = origin.normcase().parts()
        # Don't normcase dest!  We want to preserve the case.
        dest_list = dest.parts()

        if orig_list[0] != os.path.normcase(dest_list[0]):
            # Can't get here from there.
            return dest

        # Find the location where the two paths start to differ.
        i = 0
        for start_seg, dest_seg in zip(orig_list, dest_list):
            if start_seg != os.path.normcase(dest_seg):
                break
            i += 1

        # Now i is the point where the two paths diverge.
        # Need a certain number of "os.pardir"s to work up
        # from the origin to the point of divergence.
        segments = [os.pardir] * (len(orig_list) - i)
        # Need to add the diverging part of dest_list.
        segments += dest_list[i:]
        if len(segments) == 0:
            # If they happen to be identical, use os.curdir.
            return Path(os.curdir)
        else:
            return Path(os.path.join(*segments))


    # --- Listing, searching, walking, and matching

    def listdir(self):
        return [Path(p) for p in os.listdir(self._p)]
    
    def children(self, pattern=None):
        """ D.children() -> List of items in this directory,
        with this path prepended to them.
        
        Use D.files() or D.subdirs() instead if you want a listing
        of just files or just subdirectories.
        
        The elements of the list are path objects.
        
        With the optional 'pattern' argument, this only lists
        items whose names match the given pattern.
        """
        names = os.listdir(self._p)
        if pattern is not None:
            names = fnmatch.filter(names, pattern)
        return [self / child for child in names]

    def subdirs(self, pattern=None):
        """ D.subdirs() -> List of this directory's subdirectories.

        The elements of the list are path objects.
        This does not walk recursively into subdirectories
        (but see path.walkdirs).

        With the optional 'pattern' argument, this only lists
        directories whose names match the given pattern.  For
        example, d.subdirs('build-*').
        """
        return [p for p in self.children(pattern) if p.isdir()]

    def files(self, pattern=None):
        """ D.files() -> List of the files in this directory.

        The elements of the list are path objects.
        This does not walk into subdirectories (see path.walkfiles).

        With the optional 'pattern' argument, this only lists files
        whose names match the given pattern.  For example,
        d.files('*.pyc').
        """
        
        return [p for p in self.children(pattern) if p.isfile()]

    def walk(self, pattern=None):
        """ D.walk() -> iterator over files and subdirs, recursively.

        The iterator yields path objects naming each child item of
        this directory and its descendants, including the starting
        path. This requires that D.isdir().

        This performs a depth-first traversal of the directory tree.
        Each directory is returned just before all its children.
        """
        for child in self.children():
            if pattern is None or child.fnmatch(pattern):
                yield child
            if child.isdir():
                for item in child.walk(pattern):
                    yield item

    def walkdirs(self, pattern=None):
        """ D.walkdirs() -> iterator over subdirs, recursively.
        
        With the optional 'pattern' argument, this yields only
        directories whose names match the given pattern.  For
        example, mydir.walkdirs('*test') yields only directories
        with names ending in 'test'.
        """
        for child in self.subdirs():
            if pattern is None or child.fnmatch(pattern):
                yield child
            for subsubdir in child.walkdirs(pattern):
                yield subsubdir

    def walkfiles(self, pattern=None):
        """ D.walkfiles() -> iterator over files in D, recursively.

        The optional argument, pattern, limits the results to files
        with names that match the pattern.  For example,
        mydir.walkfiles('*.tmp') yields only files with the .tmp
        extension.
        """
        for child in self.children():
            if child.isfile():
                if pattern is None or child.fnmatch(pattern):
                    yield child
            elif child.isdir():
                for f in child.walkfiles(pattern):
                    yield f

    def fnmatch(self, pattern):
        """ Return True if self.name matches the given pattern.

        pattern - A filename pattern with wildcards,
            for example '*.py'.
        """
        return fnmatch.fnmatch(self.basename, pattern)

    def glob(self, pattern):
        """ Return a list of path objects that match the pattern.

        pattern - a path relative to this directory, with wildcards.

        For example, path('/users').glob('*/bin/*') returns a list
        of all the files users have in their bin directories.
        """
        return map(Path, glob.glob((self / pattern)._p))


    # --- Reading or writing an entire file at once.

    def open(self, mode='r'):
        """ Open this file.  Return a file object. """
        return file(self._p, mode)

    def get_file_bytes(self):
        """ Open this file, read all bytes, return them as a string. """
        f = self.open('rb')
        try:
            return f.read()
        finally:
            f.close()

    def write_file_bytes(self, bytes, append=False):
        """ Open this file and write the given bytes to it.

        Default behavior is to overwrite any existing file.
        Call this with write_bytes(bytes, append=True) to append instead.
        """
        if append:
            mode = 'ab'
        else:
            mode = 'wb'
        f = self.open(mode)
        try:
            f.write(bytes)
        finally:
            f.close()

    def get_file_text(self, encoding=None, errors='strict'):
        """ Open this file, read it in, return the content as a string.

        This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r'
        are automatically translated to '\n'.

        Optional arguments:

        encoding - The Unicode encoding (or character set) of
            the file.  If present, the content of the file is
            decoded and returned as a unicode object; otherwise
            it is returned as an 8-bit str.
        errors - How to handle Unicode errors; see help(str.decode)
            for the options.  Default is 'strict'.
        """
        if encoding is None:
            # 8-bit
            f = self.open(_textmode)
            try:
                return f.read()
            finally:
                f.close()
        else:
            # Unicode
            f = codecs.open(self._p, 'r', encoding, errors)
            # (Note - Can't use 'U' mode here, since codecs.open
            # doesn't support 'U' mode, even in Python 2.3.)
            try:
                t = f.read()
            finally:
                f.close()
            return (t.replace(u'\r\n', u'\n')
                     .replace(u'\r\x85', u'\n')
                     .replace(u'\r', u'\n')
                     .replace(u'\x85', u'\n')
                     .replace(u'\u2028', u'\n'))

    def write_file_text(self, text, encoding=None, errors='strict', linesep=os.linesep, append=False):
        """ Write the given text to this file.

        The default behavior is to overwrite any existing file;
        to append instead, use the 'append=True' keyword argument.

        There are two differences between path.write_file_text() and
        path.write_file_bytes(): newline handling and Unicode handling.
        See below.

        Parameters:

          - text - str/unicode - The text to be written.

          - encoding - str - The Unicode encoding that will be used.
            This is ignored if 'text' isn't a Unicode string.

          - errors - str - How to handle Unicode encoding errors.
            Default is 'strict'.  See help(unicode.encode) for the
            options.  This is ignored if 'text' isn't a Unicode
            string.

          - linesep - keyword argument - str/unicode - The sequence of
            characters to be used to mark end-of-line.  The default is
            os.linesep.  You can also specify None; this means to
            leave all newlines as they are in 'text'.

          - append - keyword argument - bool - Specifies what to do if
            the file already exists (True: append to the end of it;
            False: overwrite it.)  The default is False.


        --- Newline handling.

        write_text() converts all standard end-of-line sequences
        ('\n', '\r', and '\r\n') to your platform's default end-of-line
        sequence (see os.linesep; on Windows, for example, the
        end-of-line marker is '\r\n').

        If you don't like your platform's default, you can override it
        using the 'linesep=' keyword argument.  If you specifically want
        write_text() to preserve the newlines as-is, use 'linesep=None'.

        This applies to Unicode text the same as to 8-bit text, except
        there are three additional standard Unicode end-of-line sequences:
        u'\x85', u'\r\x85', and u'\u2028'.

        (This is slightly different from when you open a file for
        writing with fopen(filename, "w") in C or file(filename, 'w')
        in Python.)


        --- Unicode

        If 'text' isn't Unicode, then apart from newline handling, the
        bytes are written verbatim to the file.  The 'encoding' and
        'errors' arguments are not used and must be omitted.

        If 'text' is Unicode, it is first converted to bytes using the
        specified 'encoding' (or the default encoding if 'encoding'
        isn't specified).  The 'errors' argument applies only to this
        conversion.

        """
        if isinstance(text, unicode):
            if linesep is not None:
                # Convert all standard end-of-line sequences to
                # ordinary newline characters.
                text = (text.replace(u'\r\n', u'\n')
                            .replace(u'\r\x85', u'\n')
                            .replace(u'\r', u'\n')
                            .replace(u'\x85', u'\n')
                            .replace(u'\u2028', u'\n'))
                text = text.replace(u'\n', linesep)
            if encoding is None:
                encoding = sys.getdefaultencoding()
            bytes = text.encode(encoding, errors)
        else:
            # It is an error to specify an encoding if 'text' is
            # an 8-bit string.
            assert encoding is None

            if linesep is not None:
                text = (text.replace('\r\n', '\n')
                            .replace('\r', '\n'))
                bytes = text.replace('\n', linesep)

        self.write_file_bytes(bytes, append)

    def get_file_lines(self, encoding=None, errors='strict', retain=True):
        """ Open this file, read all lines, return them in a list.

        Optional arguments:
            encoding - The Unicode encoding (or character set) of
                the file.  The default is None, meaning the content
                of the file is read as 8-bit characters and returned
                as a list of (non-Unicode) str objects.
            errors - How to handle Unicode errors; see help(str.decode)
                for the options.  Default is 'strict'
            retain - If true, retain newline characters; but all newline
                character combinations ('\r', '\n', '\r\n') are
                translated to '\n'.  If false, newline characters are
                stripped off.  Default is True.

        This uses 'U' mode in Python 2.3 and later.
        """
        if encoding is None and retain:
            f = self.open(_textmode)
            try:
                return f.readlines()
            finally:
                f.close()
        else:
            return self.get_file_text(encoding, errors).splitlines(retain)

    def write_file_lines(self, lines, encoding=None, errors='strict',
                         linesep=os.linesep, append=False):
        """ Write the given lines of text to this file.

        By default this overwrites any existing file at this path.

        This puts a platform-specific newline sequence on every line.
        See 'linesep' below.

        lines - A list of strings.

        encoding - A Unicode encoding to use.  This applies only if
            'lines' contains any Unicode strings.

        errors - How to handle errors in Unicode encoding.  This
            also applies only to Unicode strings.

        linesep - The desired line-ending.  This line-ending is
            applied to every line.  If a line already has any
            standard line ending ('\r', '\n', '\r\n', u'\x85',
            u'\r\x85', u'\u2028'), that will be stripped off and
            this will be used instead.  The default is os.linesep,
            which is platform-dependent ('\r\n' on Windows, '\n' on
            Unix, etc.)  Specify None to write the lines as-is,
            like file.writelines().

        Use the keyword argument append=True to append lines to the
        file.  The default is to overwrite the file.  Warning:
        When you use this with Unicode data, if the encoding of the
        existing data in the file is different from the encoding
        you specify with the encoding= parameter, the result is
        mixed-encoding data, which can really confuse someone trying
        to read the file later.
        """
        if append:
            mode = 'ab'
        else:
            mode = 'wb'
        f = self.open(mode)
        try:
            for line in lines:
                isUnicode = isinstance(line, unicode)
                if linesep is not None:
                    # Strip off any existing line-end and add the
                    # specified linesep string.
                    if isUnicode:
                        if line[-2:] in (u'\r\n', u'\x0d\x85'):
                            line = line[:-2]
                        elif line[-1:] in (u'\r', u'\n',
                                           u'\x85', u'\u2028'):
                            line = line[:-1]
                    else:
                        if line[-2:] == '\r\n':
                            line = line[:-2]
                        elif line[-1:] in ('\r', '\n'):
                            line = line[:-1]
                    line += linesep
                if isUnicode:
                    if encoding is None:
                        encoding = sys.getdefaultencoding()
                    line = line.encode(encoding, errors)
                f.write(line)
        finally:
            f.close()


    # --- Methods for querying the filesystem.

    def exists(self):
        return os.path.exists(self._p)
    
    def isabs(self):
        return os.path.isabs(self._p)
    
    def isdir(self):
        return os.path.isdir(self._p)

    def isfile(self):
        return os.path.isfile(self._p)

    def islink(self):
        return os.path.islink(self._p)

    def ismount(self):
        return os.path.ismount(self._p)

    if hasattr(os.path, 'samefile'):
        def samefile(self):
            return os.path.samefile(self._p)

    def atime(self):
        return os.path.getatime(self._p)
    
    def mtime(self):
        return os.path.getmtime(self._p)

    if hasattr(os.path, 'getctime'):
        def ctime(self):
            return os.path.getctime(self._p)

    def getsize(self):
        return os.path.getsize(self._p)

    if hasattr(os, 'access'):
        def access(self, mode):
            """ Return true if current user has access to this path.

            mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK
            """
            return os.access(self._p, mode)

    def stat(self):
        """ Perform a stat() system call on this path. """
        return os.stat(self._p)

    def lstat(self):
        """ Like path.stat(), but do not follow symbolic links. """
        return os.lstat(self._p)

    if hasattr(os, 'statvfs'):
        def statvfs(self):
            """ Perform a statvfs() system call on this path. """
            return os.statvfs(self._p)

    if hasattr(os, 'pathconf'):
        def pathconf(self, name):
            return os.pathconf(self._p, name)


    # --- Modifying operations on files and directories

    def utime(self, times):
        """ Set the access and modified times of this file. """
        os.utime(self._p, times)

    def chmod(self, mode):
        os.chmod(self._p, mode)

    if hasattr(os, 'chown'):
        def chown(self, uid, gid):
            os.chown(self._p, uid, gid)

    def rename(self, new):
        os.rename(self._p, _strpath(new))

    def renames(self, new):
        os.renames(self._p, _strpath(new))


    # --- Create/delete operations on directories

    def mkdir(self, mode=0777):
        os.mkdir(self._p, mode)

    def makedirs(self, mode=0777):
        os.makedirs(self._p, mode)

    def rmdir(self):
        os.rmdir(self._p)

    def removedirs(self):
        os.removedirs(self._p)


    # --- Modifying operations on files

    def touch(self, mode=None):
        """ Set the access/modified times of this file to the current time.
        Create the file if it does not exist.

        The file mode is only set if the file must be created.
        The mode argument can be None, in which case the current umask is used.
        """
        if mode is None:
            mode = os.umask(0)
            os.umask(mode)
            mode = mode ^ 0777
        fd = os.open(self._p, os.O_WRONLY | os.O_CREAT, mode)
        os.close(fd)
        self.utime(None)

    def remove(self):
        os.remove(self._p)

    def unlink(self):
        os.unlink(self._p)


    # --- Links

    if hasattr(os, 'link'):
        def link(self, newpath):
            """ Create a hard link at 'newpath', pointing to this file. """
            os.link(self._p, _strpath(newpath))

    if hasattr(os, 'symlink'):
        def symlink(self, newlink):
            """ Create a symbolic link at 'newlink', pointing here. """
            os.symlink(self._p, _strpath(newlink))

    if hasattr(os, 'readlink'):
        def readlink(self):
            """ Return the path to which this symbolic link points.

            The result may be an absolute or a relative path.
            """
            return Path(os.readlink(self._p))

        def readlinkabs(self):
            """ Return the path to which this symbolic link points.

            The result is always an absolute path.
            """
            p = self.readlink()
            if p.isabs():
                return p
            else:
                return (self.directory / p).abspath()

    # --- High-level shutils functions

    def copyfile(self, dst):
        return shutil.copyfile(self._p, _strpath(dst))

    def copymode(self, dst):
        return shutil.copymode(self._p, _strpath(dst))

    def copystat(self, dst):
        return shutil.copystat(self._p, _strpath(dst))

    def copy(self, dst):
        return shutil.copy(self._p, _strpath(dst))

    def copy2(self, dst):
        return shutil.copy2(self._p, _strpath(dst))

    def copytree(self, dst, symlinks=False):
        return shutil.copytree(self._p, _strpath(dst), symlinks)

    def rmtree(self, ignore_errors=False, onerror=None):
        return shutil.rmtree(self._p, ignore_errors, onerror)

    def move(self, dst):
        return shutil.move(self._p, _strpath(dst))
    
    # --- Special stuff from os

    if hasattr(os, 'chroot'):
        def chroot(self):
            os.chroot(self._p)

    if hasattr(os, 'startfile'):
        def startfile(self):
            os.startfile(self._p)


--- NEW FILE: test_path.py ---
""" test_path.py - Test the path module.

This only runs on Posix and NT right now.  I would like to have more
tests.  You can help!  Just add appropriate pathnames for your
platform (os.name) in each place where the p() function is called.
Then send me the result.  If you can't get the test to run at all on
your platform, there's probably a bug in path.py -- please let me
know!

TempDirTestCase.testTouch() takes a while to run.  It sleeps a few
seconds to allow some time to pass between calls to check the modify
time on files.

URL:     http://www.jorendorff.com/articles/python/path
Author:  Jason Orendorff <jason at jorendorff.com>
Date:    7 Mar 2004

"""

import unittest
import codecs, os, random, shutil, tempfile, time
from path import Path

def p(**choices):
    """ Choose a value from several possible values, based on os.name """
    return Path(choices[os.name])

class BasicTestCase(unittest.TestCase):
    def testRelpath(self):
        root = p(nt='C:\\', posix='/')
        
        foo = root / 'foo'
        quux =        foo / 'quux'
        bar =         foo / 'bar'
        boz =                bar / 'Baz' / 'Boz'
        up = Path(os.pardir)

        # basics
        self.assertEqual(root.relpathto(boz), Path('foo')/'bar'/'Baz'/'Boz')
        self.assertEqual(bar.relpathto(boz), Path('Baz')/'Boz')
        self.assertEqual(quux.relpathto(boz), up/'bar'/'Baz'/'Boz')
        self.assertEqual(boz.relpathto(quux), up/up/up/'quux')
        self.assertEqual(boz.relpathto(bar), up/up)

        # x.relpathto(x) == curdir
        self.assertEqual(root.relpathto(root), Path())
        self.assertEqual(boz.relpathto(boz), Path())
        # Make sure case is properly noted (or ignored)
        self.assertEqual(boz.relpathto(boz.normcase()), Path())

        # relpath()
        cwd = Path.cwd()
        self.assertEqual(boz.relpath(), cwd.relpathto(boz))

        if os.name == 'nt':
            # Check relpath across drives.
            d = Path('D:\\')
            self.assertEqual(d.relpathto(boz), boz)

    def testProperties(self):
        # Create sample Path object.
        f = p(nt='C:\\Program Files\\Python\\Lib\\xyzzy.py',
              posix='/usr/local/python/lib/xyzzy.py')

        # .directory
        self.assertEqual(f.directory, p(nt='C:\\Program Files\\Python\\Lib',
                                     posix='/usr/local/python/lib'))

        # .basename
        self.assertEqual(f.basename, 'xyzzy.py')
        self.assertEqual(f.directory.basename, str(p(nt='Lib', posix='lib')))

        # .ext
        self.assertEqual(f.ext, '.py')
        self.assertEqual(f.directory.ext, '')

        # .drive
        self.assertEqual(f.drive, str(p(nt='C:', posix='')))

    def testMethods(self):
        # .abspath()
        self.assertEqual(Path().abspath(), Path.cwd())

        # .getcwd()
        cwd = Path.cwd()
        self.assert_(isinstance(cwd, Path))
        self.assertEqual(cwd, Path.cwd())

    def testUNC(self):
        if hasattr(os.path, 'splitunc'):
            p = Path(r'\\python1\share1\dir1\file1.txt')
            self.assert_(p.uncshare == r'\\python1\share1')
            self.assert_(p.splitunc() == os.path.splitunc(str(p)))

class TempDirTestCase(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory.
        f = tempfile.mktemp()
        system_tmp_dir = os.path.dirname(f)
        my_dir = 'testpath_tempdir_' + str(random.random())[2:]
        self.tempdir = os.path.join(system_tmp_dir, my_dir)
        os.mkdir(self.tempdir)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def testTouch(self):
        # NOTE: This test takes a long time to run (~10 seconds).
        # It sleeps several seconds because on Windows, the resolution
        # of a file's mtime and ctime is about 2 seconds.
        #
        # atime isn't tested because on Windows the resolution of atime
        # is something like 24 hours.

        d = Path(self.tempdir)
        f = d / 'test.txt'
        t0 = time.time() - 3
        f.touch()
        t1 = time.time() + 3
        try:
            self.assert_(f.exists())
            self.assert_(f.isfile())
            self.assertEqual(f.getsize(), 0)
            self.assert_(t0 <= f.mtime() <= t1)
            if hasattr(os.path, 'getctime'):
                ct = f.ctime()
                self.assert_(t0 <= ct <= t1)

            time.sleep(4)
            fobj = f.open('ab')
            fobj.write('some bytes')
            fobj.close()

            time.sleep(4)
            t2 = time.time() - 3
            f.touch()
            t3 = time.time() + 3

            assert t0 <= t1 < t2 <= t3  # sanity check

            self.assert_(f.exists())
            self.assert_(f.isfile())
            self.assertEqual(f.getsize(), 10)
            self.assert_(t2 <= f.mtime() <= t3)
            if hasattr(os.path, 'getctime'):
                ct2 = f.ctime()
                if os.name == 'nt':
                    # On Windows, "ctime" is CREATION time
                    self.assertEqual(ct, ct2)
                    self.assert_(ct2 < t2)
                else:
                    # On other systems, it might be the CHANGE time 
                    # (especially on Unix, time of inode changes)
                    self.failUnless(ct == ct2 or ct2 == f.mtime())
        finally:
            f.remove()

    def testListing(self):
        d = Path(self.tempdir)
        self.assertEqual(d.listdir(), [])
        
        f = 'testfile.txt'
        af = d / f
        self.assertEqual(str(af), os.path.join(str(d), f))
        af.touch()
        try:
            self.assert_(af.exists())

            self.assertEqual(d.listdir(), [Path(f)])

            # .glob()
            self.assertEqual(d.glob('testfile.txt'), [af])
            self.assertEqual(d.glob('test*.txt'), [af])
            self.assertEqual(d.glob('*.txt'), [af])
            self.assertEqual(d.glob('*txt'), [af])
            self.assertEqual(d.glob('*'), [af])
            self.assertEqual(d.glob('*.html'), [])
            self.assertEqual(d.glob('testfile'), [])
        finally:
            af.remove()

        # Try a test with 20 files
        files = [d / ('%d.txt' % i) for i in range(20)]
        for f in files:
            fobj = f.open('w')
            fobj.write('some text\n')
            fobj.close()
        try:
            files2 = d.children()
            files.sort()
            files2.sort()
            self.assertEqual(files, files2)
        finally:
            for f in files:
                try:
                    f.remove()
                except:
                    pass

    def testMakeDirs(self):
        d = Path(self.tempdir)

        # Placeholder file so that when removedirs() is called,
        # it doesn't remove the temporary directory itself.
        tempf = d / 'temp.txt'
        tempf.touch()
        try:
            foo = d / 'foo'
            boz =      foo / 'bar' / 'baz' / 'boz'
            boz.makedirs()
            try:
                self.assert_(boz.isdir())
            finally:
                boz.removedirs()
            self.failIf(foo.exists())
            self.assert_(d.exists())

            foo.mkdir(0750)
            boz.makedirs(0700)
            try:
                self.assert_(boz.isdir())
            finally:
                boz.removedirs()
            self.failIf(foo.exists())
            self.assert_(d.exists())
        finally:
            os.remove(str(tempf))

    def assertSetsEqual(self, a, b):
        ad = {}
        for i in a: ad[i] = None
        bd = {}
        for i in b: bd[i] = None
        self.assertEqual(ad, bd)

    def testShutil(self):
        # Note: This only tests the methods exist and do roughly what
        # they should, neglecting the details as they are shutil's
        # responsibility.

        d = Path(self.tempdir)
        testDir = d / 'testdir'
        testFile = testDir / 'testfile.txt'
        testA = testDir / 'A'
        testCopy = testA / 'testcopy.txt'
        testLink = testA / 'testlink.txt'
        testB = testDir / 'B'
        testC = testB / 'C'
        testCopyOfLink = testC / testA.relpathto(testLink)

        # Create test dirs and a file
        testDir.mkdir()
        testA.mkdir()
        testB.mkdir()

        f = testFile.open('w')
        f.write('x' * 10000)
        f.close()

        # Test simple file copying.
        testFile.copyfile(testCopy)
        self.assert_(testCopy.isfile())
        self.assert_(testFile.get_file_bytes() == testCopy.get_file_bytes())

        # Test copying into a directory.
        testCopy2 = testA / testFile.basename
        testFile.copy(testA)
        self.assert_(testCopy2.isfile())
        self.assert_(testFile.get_file_bytes() == testCopy2.get_file_bytes())

        # Make a link for the next test to use.
        if hasattr(os, 'symlink'):
            testFile.symlink(testLink)
        else:
            testFile.copy(testLink)  # fallback

        # Test copying directory tree.
        testA.copytree(testC)
        self.assert_(testC.isdir())
        self.assertSetsEqual(
            testC.children(),
            [testC / testCopy.basename,
             testC / testFile.basename,
             testCopyOfLink])
        self.assert_(not testCopyOfLink.islink())

        # Clean up for another try.
        testC.rmtree()
        self.assert_(not testC.exists())

        # Copy again, preserving symlinks.
        testA.copytree(testC, True)
        self.assert_(testC.isdir())
        self.assertSetsEqual(
            testC.children(),
            [testC / testCopy.basename,
             testC / testFile.basename,
             testCopyOfLink])
        if hasattr(os, 'symlink'):
            self.assert_(testCopyOfLink.islink())
            self.assert_(testCopyOfLink.readlink() == testFile)

        # Clean up.
        testDir.rmtree()
        self.assert_(not testDir.exists())
        self.assertList(d.children(), [])

    def assertList(self, listing, expected):
        listing = list(listing)
        listing.sort()
        expected = list(expected)
        expected.sort()
        self.assertEqual(listing, expected)

    def testPatterns(self):
        d = Path(self.tempdir)
        names = [ 'x.tmp', 'x.xtmp', 'x2g', 'x22', 'x.txt' ]
        dirs = [d, d/'xdir', d/'xdir.tmp', d/'xdir.tmp'/'xsubdir']

        for e in dirs:
            if not e.isdir():  e.makedirs()
            for name in names:
                (e/name).touch()
        self.assertList(d.children('*.tmp'), [d/'x.tmp', d/'xdir.tmp'])
        self.assertList(d.files('*.tmp'), [d/'x.tmp'])
        self.assertList(d.subdirs('*.tmp'), [d/'xdir.tmp'])
        self.assertList(d.walk(), [e for e in dirs if e != d] + [e/n for e in dirs for n in names])
        self.assertList(d.walk('*.tmp'),
                        [e/'x.tmp' for e in dirs] + [d/'xdir.tmp'])
        self.assertList(d.walkfiles('*.tmp'), [e/'x.tmp' for e in dirs])
        self.assertList(d.walkdirs('*.tmp'), [d/'xdir.tmp'])

    def testUnicode(self):
        d = Path(self.tempdir)
        p = d/'unicode.txt'

        def test(enc):
            """ Test that Path works with the specified encoding,
            which must be capable of representing the entire range of
            Unicode codepoints.
            """

            given = (u'Hello world\n'
                     u'\u0d0a\u0a0d\u0d15\u0a15\r\n'
                     u'\u0d0a\u0a0d\u0d15\u0a15\x85'
                     u'\u0d0a\u0a0d\u0d15\u0a15\u2028'
                     u'\r'
                     u'hanging')
            clean = (u'Hello world\n'
                     u'\u0d0a\u0a0d\u0d15\u0a15\n'
                     u'\u0d0a\u0a0d\u0d15\u0a15\n'
                     u'\u0d0a\u0a0d\u0d15\u0a15\n'
                     u'\n'
                     u'hanging')
            givenLines = [
                u'Hello world\n',
                u'\u0d0a\u0a0d\u0d15\u0a15\r\n',
                u'\u0d0a\u0a0d\u0d15\u0a15\x85',
                u'\u0d0a\u0a0d\u0d15\u0a15\u2028',
                u'\r',
                u'hanging']
            expectedLines = [
                u'Hello world\n',
                u'\u0d0a\u0a0d\u0d15\u0a15\n',
                u'\u0d0a\u0a0d\u0d15\u0a15\n',
                u'\u0d0a\u0a0d\u0d15\u0a15\n',
                u'\n',
                u'hanging']
            expectedLines2 = [
                u'Hello world',
                u'\u0d0a\u0a0d\u0d15\u0a15',
                u'\u0d0a\u0a0d\u0d15\u0a15',
                u'\u0d0a\u0a0d\u0d15\u0a15',
                u'',
                u'hanging']

            # write bytes manually to file
            f = codecs.open(str(p), 'w', enc)
            f.write(given)
            f.close()

            # test all 3 path read-fully functions, including
            # Path.lines() in unicode mode.
            self.assertEqual(p.get_file_bytes(), given.encode(enc))
            self.assertEqual(p.get_file_text(enc), clean)
            self.assertEqual(p.get_file_lines(enc), expectedLines)
            self.assertEqual(p.get_file_lines(enc, retain=False), expectedLines2)

            # If this is UTF-16, that's enough.
            # The rest of these will unfortunately fail because append=True mode
            # causes an extra BOM to be written in the middle of the file.
            # UTF-16 is the only encoding that has this problem.
            if enc == 'UTF-16':
                return

            # Write Unicode to file using Path.write_text().
            cleanNoHanging = clean + u'\n'  # This test doesn't work with a hanging line.
            p.write_file_text(cleanNoHanging, enc)
            p.write_file_text(cleanNoHanging, enc, append=True)
            # Check the result.
            expectedBytes = 2 * cleanNoHanging.replace('\n', os.linesep).encode(enc)
            expectedLinesNoHanging = expectedLines[:]
            expectedLinesNoHanging[-1] += '\n'
            self.assertEqual(p.get_file_bytes(), expectedBytes)
            self.assertEqual(p.get_file_text(enc), 2 * cleanNoHanging)
            self.assertEqual(p.get_file_lines(enc), 2 * expectedLinesNoHanging)
            self.assertEqual(p.get_file_lines(enc, retain=False), 2 * expectedLines2)

            # Write Unicode to file using Path.write_file_lines().
            # The output in the file should be exactly the same as last time.
            p.write_file_lines(expectedLines, enc)
            p.write_file_lines(expectedLines2, enc, append=True)
            # Check the result.
            self.assertEqual(p.get_file_bytes(), expectedBytes)

            # Now: same test, but using various newline sequences.
            # If linesep is being properly applied, these will be converted
            # to the platform standard newline sequence.
            p.write_file_lines(givenLines, enc)
            p.write_file_lines(givenLines, enc, append=True)
            # Check the result.
            self.assertEqual(p.get_file_bytes(), expectedBytes)

            # Same test, using newline sequences that are different
            # from the platform default.
            def testLinesep(eol):
                p.write_file_lines(givenLines, enc, linesep=eol)
                p.write_file_lines(givenLines, enc, linesep=eol, append=True)
                expected = 2 * cleanNoHanging.replace(u'\n', eol).encode(enc)
                self.assertEqual(p.get_file_bytes(), expected)

            testLinesep(u'\n')
            testLinesep(u'\r')
            testLinesep(u'\r\n')
            testLinesep(u'\x0d\x85')


            # Again, but with linesep=None.
            p.write_file_lines(givenLines, enc, linesep=None)
            p.write_file_lines(givenLines, enc, linesep=None, append=True)
            # Check the result.
            expectedBytes = 2 * given.encode(enc)
            self.assertEqual(p.get_file_bytes(), expectedBytes)
            self.assertEqual(p.get_file_text(enc), 2 * clean)
            expectedResultLines = expectedLines[:]
            expectedResultLines[-1] += expectedLines[0]
            expectedResultLines += expectedLines[1:]
            self.assertEqual(p.get_file_lines(enc), expectedResultLines)

        test('UTF-8')
        test('UTF-16BE')
        test('UTF-16LE')
        test('UTF-16')

if __name__ == '__main__':
    unittest.main()



More information about the Python-checkins mailing list