]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Added 'write_file()' function.
authorGreg Ward <gward@python.net>
Tue, 21 Sep 1999 18:37:51 +0000 (18:37 +0000)
committerGreg Ward <gward@python.net>
Tue, 21 Sep 1999 18:37:51 +0000 (18:37 +0000)
Added global cache PATH_CREATED used by 'mkpath()' to ensure it doesn't
  try to create the same path more than once in a session (and, more
  importantly, to ensure that it doesn't print "creating X" more than
  once for each X per session!).

Lib/distutils/util.py

index bb790af43815eca6387889f978b4c2b4d33eea25..85b04e7a809ec3a6bcfe530c4a86c3f707a1b91f 100644 (file)
@@ -15,6 +15,10 @@ import os
 from distutils.errors import *
 
 
+# cache for by mkpath() -- in addition to cheapening redundant calls,
+# eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
+PATH_CREATED = {}
+
 # I don't use os.makedirs because a) it's new to Python 1.5.2, and
 # b) it blows up if the directory already exists (I want to silently
 # succeed in that case).
@@ -26,12 +30,17 @@ def mkpath (name, mode=0777, verbose=0, dry_run=0):
        directory).  If 'verbose' is true, print a one-line summary of
        each mkdir to stdout."""
 
+    global PATH_CREATED
+
     # XXX what's the better way to handle verbosity? print as we create
     # each directory in the path (the current behaviour), or only announce
-    # the creation of the whole path, and force verbose=0 on all sub-calls?
+    # the creation of the whole path? (quite easy to do the latter since
+    # we're not using a recursive algorithm)
 
     if os.path.isdir (name):
         return
+    if PATH_CREATED.get (name):
+        return
 
     (head, tail) = os.path.split (name)
     tails = [tail]                      # stack of lone dirs to create
@@ -59,6 +68,8 @@ def mkpath (name, mode=0777, verbose=0, dry_run=0):
             except os.error, (errno, errstr):
                 raise DistutilsFileError, "%s: %s" % (head, errstr)
 
+        PATH_CREATED[head] = 1
+
 # mkpath ()
 
 
@@ -394,3 +405,13 @@ def move_file (src, dst,
     return dst
 
 # move_file ()
+
+
+def write_file (filename, contents):
+    """Create a file with the specified naem and write 'contents' (a
+       sequence of strings without line terminators) to it."""
+
+    f = open (filename, "w")
+    for line in contents:
+        f.write (line + "\n")
+    f.close ()