diff options
author | Greg Ward <gward@python.net> | 1999-09-21 18:37:51 (GMT) |
---|---|---|
committer | Greg Ward <gward@python.net> | 1999-09-21 18:37:51 (GMT) |
commit | ac1424a9ceb07cbd249c67e40bb1bf4ac38e8df1 (patch) | |
tree | 729a879a34704833bf4e330baa4af7f7010afd59 | |
parent | b116e45a29bfb6cecae92f58e75e10838bac765f (diff) | |
download | cpython-ac1424a9ceb07cbd249c67e40bb1bf4ac38e8df1.zip cpython-ac1424a9ceb07cbd249c67e40bb1bf4ac38e8df1.tar.gz cpython-ac1424a9ceb07cbd249c67e40bb1bf4ac38e8df1.tar.bz2 |
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!).
-rw-r--r-- | Lib/distutils/util.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py index bb790af..85b04e7 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 () |