Changes to fileutil.
Signed-off-by: David-Sarah Hopwood <david-sarah@jacaranda.org>
This commit is contained in:
parent
97268cc95f
commit
a79d3d69fb
|
@ -287,11 +287,11 @@ class Node(service.MultiService):
|
||||||
fileutil.write(privpath, value, mode="")
|
fileutil.write(privpath, value, mode="")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def write_config(self, name, value, mode="w"):
|
def write_config(self, name, value, mode=""):
|
||||||
"""Write a string to a config file."""
|
"""Write a string to a config file."""
|
||||||
fn = os.path.join(self.basedir, name)
|
fn = os.path.join(self.basedir, name)
|
||||||
try:
|
try:
|
||||||
fileutil.write(fn, value, mode)
|
fileutil.write(fn, value, mode=mode)
|
||||||
except EnvironmentError, e:
|
except EnvironmentError, e:
|
||||||
self.log("Unable to write config file '%s'" % fn)
|
self.log("Unable to write config file '%s'" % fn)
|
||||||
self.log(e)
|
self.log(e)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
Futz with files like a pro.
|
Futz with files like a pro.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, exceptions, os, stat, tempfile, time, binascii
|
import errno, sys, exceptions, os, re, stat, tempfile, time, binascii
|
||||||
|
|
||||||
from twisted.python import log
|
from twisted.python import log
|
||||||
|
|
||||||
|
@ -202,9 +202,11 @@ def rm_dir(dirname):
|
||||||
else:
|
else:
|
||||||
remove(fullname)
|
remove(fullname)
|
||||||
os.rmdir(dirname)
|
os.rmdir(dirname)
|
||||||
|
except EnvironmentError, le:
|
||||||
|
# Ignore "No such file or directory", collect any other exception.
|
||||||
|
if (le.args[0] != 2 and le.args[0] != 3) or (le.args[0] != errno.ENOENT):
|
||||||
|
excs.append(le)
|
||||||
except Exception, le:
|
except Exception, le:
|
||||||
# Ignore "No such file or directory"
|
|
||||||
if (not isinstance(le, OSError)) or le.args[0] != 2:
|
|
||||||
excs.append(le)
|
excs.append(le)
|
||||||
|
|
||||||
# Okay, now we've recursively removed everything, ignoring any "No
|
# Okay, now we've recursively removed everything, ignoring any "No
|
||||||
|
@ -217,13 +219,33 @@ def rm_dir(dirname):
|
||||||
raise OSError, "Failed to remove dir for unknown reason."
|
raise OSError, "Failed to remove dir for unknown reason."
|
||||||
raise OSError, excs
|
raise OSError, excs
|
||||||
|
|
||||||
|
|
||||||
def remove_if_possible(f):
|
def remove_if_possible(f):
|
||||||
try:
|
try:
|
||||||
remove(f)
|
remove(f)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def rmdir_if_empty(path):
|
||||||
|
""" Remove the directory if it is empty. """
|
||||||
|
try:
|
||||||
|
os.rmdir(path)
|
||||||
|
except OSError, e:
|
||||||
|
if e.errno != errno.ENOTEMPTY:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
ASCII = re.compile(r'^[\x00-\x7F]*$')
|
||||||
|
|
||||||
|
def listdir(path, filter=ASCII):
|
||||||
|
try:
|
||||||
|
children = os.listdir(path)
|
||||||
|
except OSError, e:
|
||||||
|
if e.errno != errno.ENOENT:
|
||||||
|
raise
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
return [str(child) for child in children if filter.match(child)]
|
||||||
|
|
||||||
def open_or_create(fname, binarymode=True):
|
def open_or_create(fname, binarymode=True):
|
||||||
try:
|
try:
|
||||||
return open(fname, binarymode and "r+b" or "r+")
|
return open(fname, binarymode and "r+b" or "r+")
|
||||||
|
@ -255,8 +277,8 @@ def write_atomically(target, contents, mode="b"):
|
||||||
f.close()
|
f.close()
|
||||||
move_into_place(target+".tmp", target)
|
move_into_place(target+".tmp", target)
|
||||||
|
|
||||||
def write(path, data, mode="wb"):
|
def write(path, data, mode="b"):
|
||||||
wf = open(path, mode)
|
wf = open(path, "w"+mode)
|
||||||
try:
|
try:
|
||||||
wf.write(data)
|
wf.write(data)
|
||||||
finally:
|
finally:
|
||||||
|
@ -453,8 +475,8 @@ def get_used_space(path):
|
||||||
# [in] 512-byte units." It is also defined that way on MacOS X. Python does
|
# [in] 512-byte units." It is also defined that way on MacOS X. Python does
|
||||||
# not set the attribute on Windows.
|
# not set the attribute on Windows.
|
||||||
#
|
#
|
||||||
# We consider platforms that define st_blocks but give it a wrong value, or
|
# This code relies on the underlying platform to either define st_blocks in
|
||||||
# measure it in a unit other than 512 bytes, to be broken. See also
|
# units of 512 bytes or else leave st_blocks undefined. See also
|
||||||
# <http://bugs.python.org/issue12350>.
|
# <http://bugs.python.org/issue12350>.
|
||||||
|
|
||||||
if hasattr(s, 'st_blocks'):
|
if hasattr(s, 'st_blocks'):
|
||||||
|
|
Loading…
Reference in New Issue