~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/utils/locking.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/utils/locking.py
 
2
#
 
3
#
 
4
 
 
5
""" generic functions """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from log import rlog, loglevel
 
10
from trace import whichmodule
 
11
from lockmanager import lockmanager, rlockmanager
 
12
 
 
13
import logging, sys
 
14
locks = []
 
15
 
 
16
def lockdec(lock):
 
17
 
 
18
    """ locking decorator. """
 
19
 
 
20
    def locked(func):
 
21
 
 
22
        """ locking function for %s """ % str(func)
 
23
 
 
24
        def lockedfunc(*args, **kwargs):
 
25
            """ the locked function. """
 
26
 
 
27
            if loglevel <= 1:
 
28
                where = whichmodule(1)
 
29
                rlog(1, 'locking', 'locking on %s (%s)' % (where, str(func)))
 
30
            lock.acquire()
 
31
            locks.append(str(func))
 
32
            res = None
 
33
 
 
34
            try:
 
35
                res = func(*args, **kwargs)
 
36
            finally:
 
37
                lock.release()
 
38
                locks.remove(str(func))
 
39
 
 
40
            return res
 
41
 
 
42
        return lockedfunc
 
43
 
 
44
    return locked
 
45
 
 
46
def funclocked(func):
 
47
 
 
48
    """ locking function for %s """ % str(func)
 
49
 
 
50
    def lockedfunc(*args, **kwargs):
 
51
 
 
52
        """ the locked function. """
 
53
 
 
54
        if loglevel <= 1:
 
55
            where = whichmodule(1)
 
56
            rlog(1, 'locking', 'locking on %s' % where)
 
57
        rlockmanager.acquire(func)
 
58
        locks.append(str(func))
 
59
        res = None
 
60
 
 
61
        try:
 
62
            res = func(*args, **kwargs)
 
63
        finally:
 
64
            rlockmanager.release(func)
 
65
            locks.remove(str(func))
 
66
 
 
67
        return res
 
68
 
 
69
 
 
70
    return lockedfunc
 
71
 
 
72
class Locked(object):
 
73
 
 
74
    """ class used to lock an entire object. """
 
75
 
 
76
    def __getattribute__(self, attr):
 
77
        if loglevel <= 1:
 
78
            where = whichmodule(1)
 
79
            rlog(1, 'locking', 'locking on %s' % where)
 
80
        rlockmanager.acquire(object)
 
81
        res = None
 
82
 
 
83
        try:
 
84
            res = super(Locked, self).__getattribute__(attr)
 
85
        finally:
 
86
            rlockmanager.release(object)
 
87
 
 
88
        return res