File: lock.py

package info (click to toggle)
bzr-gtk 0.103.0+bzr792-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,524 kB
  • sloc: python: 9,616; makefile: 16; sh: 10
file content (63 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from gi.repository import Gtk


RESPONSE_BREAK = 0
RESPONSE_CANCEL = Gtk.ResponseType.CANCEL

READ = "read"
WRITE = "write"


def acquire(branch, lock_type):
    if branch.get_physical_lock_status():
        dialog = LockDialog(branch)
        response = dialog.run()
        dialog.destroy()

        if response == RESPONSE_BREAK:
            branch.break_lock()
        else:
            return False

    if lock_type == READ:
        branch.lock_read()
    elif lock_type == WRITE:
        branch.lock_write()

    return True


def release(branch):
    if branch.get_physical_lock_status():
        dialog = LockDialog(branch)
        response = dialog.run()
        dialog.destroy()

        if response == RESPONSE_BREAK:
            branch.break_lock()
        elif response == RESPONSE_CANCEL:
            return False

    branch.unlock()

    return True


class LockDialog(Gtk.Dialog):

    def __init__(self, branch):
        super(LockDialog, self).__init__()

        self.branch = branch

        self.set_title('Lock Not Held')

        self.get_content_area().add(
            Gtk.Label(label=(
                'This operation cannot be completed as '
                'another application has locked the branch.')))

        self.add_button('Break Lock', RESPONSE_BREAK)
        self.add_button(Gtk.STOCK_CANCEL, RESPONSE_CANCEL)

        self.get_content_area().show_all()