Profile if UD_PROFILE is in environment
[mirror/userdir-ldap.git] / ud-generate
index 0826215..b329719 100755 (executable)
@@ -28,8 +28,7 @@
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp
-import lockfile
+import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp, fcntl
 from userdir_ldap import *
 from userdir_exceptions import *
 import UDLdap
@@ -89,25 +88,23 @@ def safe_rmtree(dir):
       else:
          raise e
 
-def get_lock(fn, wait=5*60, max_age=3600*6):
-   try:
-      stat = os.stat(fn + '.lock')
-      if stat.st_mtime < time.time() - max_age:
-         sys.stderr.write("Removing stale lock %s"%(fn + '.lock'))
-         os.unlink(fn + '.lock')
-   except OSError, error:
-      if error.errno == errno.ENOENT:
-         pass
-      else:
-         raise
+def get_lock(fn, wait=5*60):
+   f = open(fn, "w")
+   sl = 0.1
+   ends = time.time() + wait
 
-   lock = lockfile.FileLock(fn)
-   try:
-      lock.acquire(timeout=wait)
-   except lockfile.LockTimeout:
-      return None
-
-   return lock
+   while True:
+      success = False
+      try:
+         fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+         return f
+      except IOError:
+         pass
+      if time.time() >= ends:
+         return None
+      sl = min(sl*2, 10, ends - time.time())
+      time.sleep(sl)
+   return None
 
 
 def Sanitize(Str):
@@ -361,9 +358,6 @@ def GenSSHShadow(global_dir, accounts):
    # Fetch all the users
    userkeys = {}
 
-   safe_rmtree(os.path.join(global_dir, 'userkeys'))
-   safe_makedirs(os.path.join(global_dir, 'userkeys'))
-
    for a in accounts:
       if not 'sshRSAAuthKey' in a: continue
 
@@ -573,7 +567,9 @@ def GenCDB(accounts, File, key):
    Fdb = None
    try:
       OldMask = os.umask(0022)
-      Fdb = os.popen("cdbmake %s %s.tmp"%(File, File), "w")
+      # 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
@@ -1318,11 +1314,19 @@ def ud_generate():
 
    finally:
       if lock is not None:
-         lock.release()
+         lock.close()
 
-if __name__ == "__main__":
-   ud_generate()
 
+if __name__ == "__main__":
+   if 'UD_PROFILE' in os.environ:
+      import cProfile
+      import pstats
+      cProfile.run('ud_generate()', "udg_prof")
+      p = pstats.Stats('udg_prof')
+      ##p.sort_stats('time').print_stats()
+      p.sort_stats('cumulative').print_stats()
+   else:
+      ud_generate()
 
 # vim:set et:
 # vim:set ts=3: