From ac1424a9ceb07cbd249c67e40bb1bf4ac38e8df1 Mon Sep 17 00:00:00 2001 From: Greg Ward Date: Tue, 21 Sep 1999 18:37:51 +0000 Subject: [PATCH] Added 'write_file()' function. 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 | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py index bb790af4381..85b04e7a809 100644 --- a/Lib/distutils/util.py +++ b/Lib/distutils/util.py @@ -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 ()