ud-generate: use subprocess.Popen instead of os.popen in GenCDB.
[mirror/userdir-ldap.git] / ud-generate
index 97d003e..adc1436 100755 (executable)
@@ -645,28 +645,24 @@ def GenForward(accounts, File):
    Done(File, F, None)
 
 def GenCDB(accounts, File, key):
-   Fdb = None
+   prefix = ["/usr/bin/eatmydata"] if os.path.exists('/usr/bin/eatmydata') else []
+   # nothing else does the fsync stuff, so why do it here?
+   Fdb = subprocess.Popen(prefix + ["cdbmake", File, "%s.tmp" % File],
+                          preexec_fn=lambda: os.umask(0022),
+                          stdin=subprocess.PIPE)
    try:
-      OldMask = os.umask(0022)
-      # nothing else does the fsync stuff, so why do it here?
-      prefix = "/usr/bin/eatmydata " if os.path.exists('/usr/bin/eatmydata') else ''
-      Fdb = os.popen("%scdbmake %s %s.tmp"%(prefix, File, File), "w")
-      os.umask(OldMask)
-
       # Write out the email address for each user
       for a in accounts:
          if not key in a: continue
          value = a[key]
          user = a['uid']
-         Fdb.write("+%d,%d:%s->%s\n" % (len(user), len(value), user, value))
+         Fdb.stdin.write("+%d,%d:%s->%s\n" % (len(user), len(value), user, value))
 
-      Fdb.write("\n")
-   # Oops, something unspeakable happened.
-   except:
-      Fdb.close()
-      raise
-   if Fdb.close() != None:
-      raise "cdbmake gave an error"
+      Fdb.stdin.write("\n")
+   finally:
+      Fdb.stdin.close()
+      if Fdb.wait() != 0:
+         raise Exception("cdbmake gave an error")
 
 def GenDBM(accounts, File, key):
    Fdb = None