+++ /dev/null
-"""distutils.dir_util\r
-\r
-Utility functions for manipulating directories and directory trees."""\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os\r
-import errno\r
-from distutils.errors import DistutilsFileError, DistutilsInternalError\r
-from distutils import log\r
-\r
-# cache for by mkpath() -- in addition to cheapening redundant calls,\r
-# eliminates redundant "creating /foo/bar/baz" messages in dry-run mode\r
-_path_created = {}\r
-\r
-# I don't use os.makedirs because a) it's new to Python 1.5.2, and\r
-# b) it blows up if the directory already exists (I want to silently\r
-# succeed in that case).\r
-def mkpath(name, mode=0777, verbose=1, dry_run=0):\r
- """Create a directory and any missing ancestor directories.\r
-\r
- If the directory already exists (or if 'name' is the empty string, which\r
- means the current directory, which of course exists), then do nothing.\r
- Raise DistutilsFileError if unable to create some directory along the way\r
- (eg. some sub-path exists, but is a file rather than a directory).\r
- If 'verbose' is true, print a one-line summary of each mkdir to stdout.\r
- Return the list of directories actually created.\r
- """\r
-\r
- global _path_created\r
-\r
- # Detect a common bug -- name is None\r
- if not isinstance(name, basestring):\r
- raise DistutilsInternalError, \\r
- "mkpath: 'name' must be a string (got %r)" % (name,)\r
-\r
- # XXX what's the better way to handle verbosity? print as we create\r
- # each directory in the path (the current behaviour), or only announce\r
- # the creation of the whole path? (quite easy to do the latter since\r
- # we're not using a recursive algorithm)\r
-\r
- name = os.path.normpath(name)\r
- created_dirs = []\r
- if os.path.isdir(name) or name == '':\r
- return created_dirs\r
- if _path_created.get(os.path.abspath(name)):\r
- return created_dirs\r
-\r
- (head, tail) = os.path.split(name)\r
- tails = [tail] # stack of lone dirs to create\r
-\r
- while head and tail and not os.path.isdir(head):\r
- (head, tail) = os.path.split(head)\r
- tails.insert(0, tail) # push next higher dir onto stack\r
-\r
- # now 'head' contains the deepest directory that already exists\r
- # (that is, the child of 'head' in 'name' is the highest directory\r
- # that does *not* exist)\r
- for d in tails:\r
- #print "head = %s, d = %s: " % (head, d),\r
- head = os.path.join(head, d)\r
- abs_head = os.path.abspath(head)\r
-\r
- if _path_created.get(abs_head):\r
- continue\r
-\r
- if verbose >= 1:\r
- log.info("creating %s", head)\r
-\r
- if not dry_run:\r
- try:\r
- os.mkdir(head, mode)\r
- except OSError, exc:\r
- if not (exc.errno == errno.EEXIST and os.path.isdir(head)):\r
- raise DistutilsFileError(\r
- "could not create '%s': %s" % (head, exc.args[-1]))\r
- created_dirs.append(head)\r
-\r
- _path_created[abs_head] = 1\r
- return created_dirs\r
-\r
-def create_tree(base_dir, files, mode=0777, verbose=1, dry_run=0):\r
- """Create all the empty directories under 'base_dir' needed to put 'files'\r
- there.\r
-\r
- 'base_dir' is just the a name of a directory which doesn't necessarily\r
- exist yet; 'files' is a list of filenames to be interpreted relative to\r
- 'base_dir'. 'base_dir' + the directory portion of every file in 'files'\r
- will be created if it doesn't already exist. 'mode', 'verbose' and\r
- 'dry_run' flags are as for 'mkpath()'.\r
- """\r
- # First get the list of directories to create\r
- need_dir = {}\r
- for file in files:\r
- need_dir[os.path.join(base_dir, os.path.dirname(file))] = 1\r
- need_dirs = need_dir.keys()\r
- need_dirs.sort()\r
-\r
- # Now create them\r
- for dir in need_dirs:\r
- mkpath(dir, mode, verbose=verbose, dry_run=dry_run)\r
-\r
-def copy_tree(src, dst, preserve_mode=1, preserve_times=1,\r
- preserve_symlinks=0, update=0, verbose=1, dry_run=0):\r
- """Copy an entire directory tree 'src' to a new location 'dst'.\r
-\r
- Both 'src' and 'dst' must be directory names. If 'src' is not a\r
- directory, raise DistutilsFileError. If 'dst' does not exist, it is\r
- created with 'mkpath()'. The end result of the copy is that every\r
- file in 'src' is copied to 'dst', and directories under 'src' are\r
- recursively copied to 'dst'. Return the list of files that were\r
- copied or might have been copied, using their output name. The\r
- return value is unaffected by 'update' or 'dry_run': it is simply\r
- the list of all files under 'src', with the names changed to be\r
- under 'dst'.\r
-\r
- 'preserve_mode' and 'preserve_times' are the same as for\r
- 'copy_file'; note that they only apply to regular files, not to\r
- directories. If 'preserve_symlinks' is true, symlinks will be\r
- copied as symlinks (on platforms that support them!); otherwise\r
- (the default), the destination of the symlink will be copied.\r
- 'update' and 'verbose' are the same as for 'copy_file'.\r
- """\r
- from distutils.file_util import copy_file\r
-\r
- if not dry_run and not os.path.isdir(src):\r
- raise DistutilsFileError, \\r
- "cannot copy tree '%s': not a directory" % src\r
- try:\r
- names = os.listdir(src)\r
- except os.error, (errno, errstr):\r
- if dry_run:\r
- names = []\r
- else:\r
- raise DistutilsFileError, \\r
- "error listing files in '%s': %s" % (src, errstr)\r
-\r
- if not dry_run:\r
- mkpath(dst, verbose=verbose)\r
-\r
- outputs = []\r
-\r
- for n in names:\r
- src_name = os.path.join(src, n)\r
- dst_name = os.path.join(dst, n)\r
-\r
- if preserve_symlinks and os.path.islink(src_name):\r
- link_dest = os.readlink(src_name)\r
- if verbose >= 1:\r
- log.info("linking %s -> %s", dst_name, link_dest)\r
- if not dry_run:\r
- os.symlink(link_dest, dst_name)\r
- outputs.append(dst_name)\r
-\r
- elif os.path.isdir(src_name):\r
- outputs.extend(\r
- copy_tree(src_name, dst_name, preserve_mode,\r
- preserve_times, preserve_symlinks, update,\r
- verbose=verbose, dry_run=dry_run))\r
- else:\r
- copy_file(src_name, dst_name, preserve_mode,\r
- preserve_times, update, verbose=verbose,\r
- dry_run=dry_run)\r
- outputs.append(dst_name)\r
-\r
- return outputs\r
-\r
-def _build_cmdtuple(path, cmdtuples):\r
- """Helper for remove_tree()."""\r
- for f in os.listdir(path):\r
- real_f = os.path.join(path,f)\r
- if os.path.isdir(real_f) and not os.path.islink(real_f):\r
- _build_cmdtuple(real_f, cmdtuples)\r
- else:\r
- cmdtuples.append((os.remove, real_f))\r
- cmdtuples.append((os.rmdir, path))\r
-\r
-def remove_tree(directory, verbose=1, dry_run=0):\r
- """Recursively remove an entire directory tree.\r
-\r
- Any errors are ignored (apart from being reported to stdout if 'verbose'\r
- is true).\r
- """\r
- from distutils.util import grok_environment_error\r
- global _path_created\r
-\r
- if verbose >= 1:\r
- log.info("removing '%s' (and everything under it)", directory)\r
- if dry_run:\r
- return\r
- cmdtuples = []\r
- _build_cmdtuple(directory, cmdtuples)\r
- for cmd in cmdtuples:\r
- try:\r
- cmd[0](cmd[1])\r
- # remove dir from cache if it's already there\r
- abspath = os.path.abspath(cmd[1])\r
- if abspath in _path_created:\r
- del _path_created[abspath]\r
- except (IOError, OSError), exc:\r
- log.warn(grok_environment_error(\r
- exc, "error removing %s: " % directory))\r
-\r
-def ensure_relative(path):\r
- """Take the full path 'path', and make it a relative path.\r
-\r
- This is useful to make 'path' the second argument to os.path.join().\r
- """\r
- drive, path = os.path.splitdrive(path)\r
- if path[0:1] == os.sep:\r
- path = drive + path[1:]\r
- return path\r