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():