X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tools.py;h=36754fc9fc45547c766555d72fcce55107d1270e;hb=cabdd946793b35b2fcaff5ee2ffec4a1644d8391;hp=e37c0d5e327a79130e1ef940e480cc1c381266da;hpb=6aeeb0a49f3f2fbaad74adbbe9dc52be74eb204d;p=nodemanager.git diff --git a/tools.py b/tools.py index e37c0d5..36754fc 100644 --- a/tools.py +++ b/tools.py @@ -1,15 +1,13 @@ -# $Id$ -# $URL$ - """A few things that didn't seem to fit anywhere else.""" -import os +import os, os.path import pwd import tempfile import fcntl import errno import threading import subprocess +import shutil import logger @@ -60,7 +58,7 @@ def daemon(): os.setsid() if os.fork() != 0: os._exit(0) os.chdir('/') - os.umask(0) + os.umask(0022) devnull = os.open(os.devnull, os.O_RDWR) os.dup2(devnull, 0) # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull @@ -111,7 +109,7 @@ The return value is the pid of the other running process, or None otherwise.""" def write_file(filename, do_write, **kw_args): """Write file atomically by opening a temporary file, using to write that file, and then renaming the temporary file.""" - os.rename(write_temp_file(do_write, **kw_args), filename) + shutil.move(write_temp_file(do_write, **kw_args), filename) def write_temp_file(do_write, mode=None, uidgid=None): fd, temporary_filename = tempfile.mkstemp() @@ -123,8 +121,11 @@ def write_temp_file(do_write, mode=None, uidgid=None): return temporary_filename # 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 +# can handle chmod if requested +# can also remove resulting file if contents are void, if requested +# performs atomically: +# writes in a tmp file, which is then renamed (from sliverauth originally) +# returns True if a change occurred, or the file is deleted def replace_file_with_string (target, new_contents, chmod=None, remove_if_empty=False): try: current=file(target).read() @@ -133,20 +134,22 @@ def replace_file_with_string (target, new_contents, chmod=None, remove_if_empty= 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): + if remove_if_empty and not new_contents and os.path.isfile(target): + logger.verbose("tools.replace_file_with_string: removing file %s"%target) try: os.unlink(target) finally: return True return False - # overwrite target file - f=file(target,'w') - f.write(new_contents) - f.close() + # overwrite target file: create a temp in the same directory + path=os.path.dirname(target) or '.' + fd, name = tempfile.mkstemp('','repl',path) + os.write(fd,new_contents) + os.close(fd) + if os.path.exists(target): + os.unlink(target) + shutil.move(name,target) if chmod: os.chmod(target,chmod) return True -# not needed yet - should that unlink the new file ? -#def replace_file_with_file (target, new): -# return replace_file_with_string (target, file(new).read()) #################### # utilities functions to get (cached) information from the node