]> git.siccegge.de Git - tools.git/commitdiff
fix locking
authorChristoph Egger <christoph@christoph-egger.org>
Thu, 18 Feb 2016 13:19:52 +0000 (14:19 +0100)
committerChristoph Egger <christoph@christoph-egger.org>
Thu, 18 Feb 2016 13:19:52 +0000 (14:19 +0100)
tls/addrecord

index fa4c840bfd73f0c3762bd8d20a642304e0c3f0a4..8d171c3dc12e17a619f45bc88f109aaa23b7d838 100644 (file)
@@ -73,25 +73,29 @@ class Addrecord:
     def _update_records(self, sort, records):
         to_remove = [ i.split()[0] for i in records ]
 
-        with open('%s/%s.m4' % (sort, self._host), 'r') as oldzone:
-            fcntl.flock(oldzone, fcntl.LOCK_EX)
-            lines = oldzone.readlines()
-            lines = [ line for line in lines if line == '\n' or line.split()[0] not in to_remove ]
-
-            lines.append('\n')
-            lines.append("; Last updated %s by %s\n" % (datetime.datetime.utcnow().isoformat(),
-                                                        self._host))
-            lines = lines + records
-
-            with open('%s/%s.m4.new' % (sort, self._host), 'w') as newzone:
-                newtext = ''.join(lines)
-                newtext = re.sub(r'\n[\n]+', '\n\n', newtext)
-                newtext = re.sub(r'\n;.*\n\n;', '\n;', newtext)
-                newzone.write(newtext)
-
-            os.rename('%s/%s.m4.new' % (sort, self._host),
-                      '%s/%s.m4' % (sort, self._host))
-            fcntl.flock(oldzone, fcntl.LOCK_UN)
+        with open('%s/%s.m4.lock' % (sort, self._host), 'w') as lockfd:
+            fcntl.flock(lockfd, fcntl.LOCK_EX)
+            with open('%s/%s.m4' % (sort, self._host), 'r') as oldzone:
+                lines = oldzone.readlines()
+                lines = [ line for line in lines
+                          if line == '\n' or line.split()[0] not in to_remove ]
+
+                lines.append('\n')
+                lines.append("; Last updated %s by %s\n"
+                             % (datetime.datetime.utcnow().isoformat(),
+                                self._host))
+                lines = lines + records
+
+                with open('%s/%s.m4.new' % (sort, self._host), 'w') as newzone:
+                    newtext = ''.join(lines)
+                    newtext = re.sub(r'\n[\n]+', '\n\n', newtext)
+                    newtext = re.sub(r'\n;.*\n\n;', '\n;', newtext)
+                    newzone.write(newtext)
+
+                os.rename('%s/%s.m4.new' % (sort, self._host),
+                          '%s/%s.m4' % (sort, self._host))
+
+            fcntl.flock(lockfd, fcntl.LOCK_UN)
 
 
 def main():