X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tools.py;h=e37c0d5e327a79130e1ef940e480cc1c381266da;hb=6aeeb0a49f3f2fbaad74adbbe9dc52be74eb204d;hp=c5d3e400495ca22f4920bbfa6ef102edf6704745;hpb=3b859412179e76c2d7aa586abfc38716a6c2ea78;p=nodemanager.git diff --git a/tools.py b/tools.py index c5d3e40..e37c0d5 100644 --- a/tools.py +++ b/tools.py @@ -125,18 +125,23 @@ def write_temp_file(do_write, mode=None, uidgid=None): # replace a target file with a new contents - checks for changes # return True if a change occurred, in which case # chown/chmod settings should be taken care of -def replace_file_with_string (target, new_contents): +def replace_file_with_string (target, new_contents, chmod=None, remove_if_empty=False): try: current=file(target).read() except: current="" - # xxx if verbose, report diffs... if current==new_contents: + # if turns out to be an empty string, and remove_if_empty is set, + # then make sure to trash the file if it exists + if remove_if_empty and os.path.isfile(target): + try: os.unlink(target) + finally: return True return False # overwrite target file f=file(target,'w') f.write(new_contents) f.close() + if chmod: os.chmod(target,chmod) return True # not needed yet - should that unlink the new file ?