0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 17:47:13 +01:00

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!).
This commit is contained in:
Greg Ward 1999-09-21 18:37:51 +00:00
parent b116e45a29
commit ac1424a9ce

View 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 ()