bugfix in child processes management - review initscript install
[nodemanager.git] / tools.py
index 98a5b7e..e37c0d5 100644 (file)
--- a/tools.py
+++ b/tools.py
@@ -91,8 +91,8 @@ def fork_as(su, function, *args):
 ####################
 # manage files
 def pid_file():
-    """We use a pid file to ensure that only one copy of NM is running at a given time.  
-If successful, this function will write a pid file containing the pid of the current process.  
+    """We use a pid file to ensure that only one copy of NM is running at a given time.
+If successful, this function will write a pid file containing the pid of the current process.
 The return value is the pid of the other running process, or None otherwise."""
     other_pid = None
     if os.access(PID_FILE, os.F_OK):  # check for a pid file
@@ -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 ?