[ditrack commit] r1628 - src/trunk/DITrack/Util

Vlad Skvortsov vss at 73rus.com
Thu May 24 15:03:43 PDT 2007


Hey Ivan,

can you please announce when you are done with the lower level locking 
stuff? I'd like to review it looking at the whole picture.


gli at ditrack.org wrote:
> Author: gli
> Date: 2007-05-22 07:33:11 -0700 (Tue, 22 May 2007)
> New Revision: 1628
>
> Modified:
>    src/trunk/DITrack/Util/Locking.py
> Log:
> Use current lock()/unlock() functions only to "posix" systems. Do nothing for "nt".
>
> Modified: src/trunk/DITrack/Util/Locking.py
> ===================================================================
> --- src/trunk/DITrack/Util/Locking.py	2007-05-22 14:08:10 UTC (rev 1627)
> +++ src/trunk/DITrack/Util/Locking.py	2007-05-22 14:33:11 UTC (rev 1628)
> @@ -29,7 +29,8 @@
>  #
>  
>  import os
> -import fcntl
> +if os.name == "posix":
> +    import fcntl
>  
>  import DITrack.DB.Common
>  
> @@ -58,37 +59,41 @@
>  
>      lock_fname = os.path.join(datadir, DITrack.DB.Common.LOCK_FILE)
>  
> -    if not os.path.exists(lock_fname):
> -        # if there's no lock file - create it and lock it
> +    if os.name == "posix":
> +        if not os.path.exists(lock_fname):
> +            # if there's no lock file - create it and lock it
> +            try:
> +                fd = open (lock_fname, "w")
> +                fcntl.flock(fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
> +            except IOError, (errid, strerror):
> +                DITrack.Util.common.err("Can't lock file %s: %s" % 
> +                    (lock_fname, strerror))
> +            fd.write(repr(os.getpid()))
> +            fd.flush()
> +            return fd
> +        # else if file exists
>          try:
> -            fd = open (lock_fname, "w")
> +            fd = open (lock_fname, "r")
>              fcntl.flock(fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
> -        except IOError, (errid, strerror):
> -            DITrack.Util.common.err("Can't lock file %s: %s" % 
> -                (lock_fname, strerror))
> -        fd.write(repr(os.getpid()))
> -        fd.flush()
> -        return fd
> -    # else if file exists
> -    try:
> -        fd = open (lock_fname, "r")
> -        fcntl.flock(fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
> -    except IOError, (err, strerror):
> -        if ((err == errno.EACCES) or (err == errno.EAGAIN)):
> -            # Lock on file conent exists
> -            pid = open(lock_fname, "r").readline().strip()
> -            if not pid.isdigit():
> -                raise DITrack.DB.Exceptions.DBLockFileCorruptedError
> -            # lockfile exists, but is corrupted
> -            raise DITrack.DB.Exceptions.DBIsLockedError(pid)
> +        except IOError, (err, strerror):
> +            if ((err == errno.EACCES) or (err == errno.EAGAIN)):
> +                # Lock on file conent exists
> +                pid = open(lock_fname, "r").readline().strip()
> +                if not pid.isdigit():
> +                    raise DITrack.DB.Exceptions.DBLockFileCorruptedError
> +                # lockfile exists, but is corrupted
> +                raise DITrack.DB.Exceptions.DBIsLockedError(pid)
> +            else:
> +                raise
>          else:
> -            raise
> +            raise DITrack.DB.Exceptions.DBStaleLockError
>      else:
> -        raise DITrack.DB.Exceptions.DBStaleLockError
> +        return None
>  
>  def unlock(lockfd):
> -    # if database path is wrong, object doesn't have lockfd attribute
> -    if lockfd:
> -        fcntl.flock(lockfd.fileno(), fcntl.LOCK_UN)
> -        os.unlink(lockfd.name)
> +    if os.name == "posix":
> +        # if database path is wrong, object doesn't have lockfd attribute
> +        if lockfd:
> +            fcntl.flock(lockfd.fileno(), fcntl.LOCK_UN)
> +            os.unlink(lockfd.name)
>  
>
> _______________________________________________
> Commit mailing list
> Commit at lists.ditrack.org
> http://lists.ditrack.org/mailman/listinfo/commit
>   



-- 
Vlad Skvortsov, vss at 73rus.com, http://vss.73rus.com



More information about the Dev mailing list