File: initialize.py

package info (click to toggle)
bzr-gtk 0.94.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,528 kB
  • ctags: 902
  • sloc: python: 9,783; sh: 32; makefile: 8
file content (118 lines) | stat: -rw-r--r-- 4,554 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (C) 2007 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

try:
    import pygtk
    pygtk.require("2.0")
except:
    pass

import gtk

import os

from dialog import error_dialog
from errors import show_bzr_error

from bzrlib import bzrdir
from bzrlib import transport
import bzrlib.errors as errors

class InitDialog(gtk.Dialog):
    """ Initialize dialog. """
    def __init__(self, path, parent=None):
        """ Initialize the Initialize dialog. """
        gtk.Dialog.__init__(self, title="Initialize - Olive",
                                  parent=parent,
                                  flags=0,
                                  buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        
        # Get arguments
        self.path = path
        
        # Create the widgets
        self._button_init = gtk.Button(_("_Initialize"), use_underline=True)
        self._label_question = gtk.Label(_("Which directory do you want to initialize?"))
        self._radio_current = gtk.RadioButton(None, _("Current directory"))
        self._radio_custom = gtk.RadioButton(self._radio_current, _("Create a new directory with the name:"))
        self._entry_custom = gtk.Entry()
        self._hbox_custom = gtk.HBox()
        
        # Set callbacks
        self._button_init.connect('clicked', self._on_init_clicked)
        self._radio_custom.connect('toggled', self._on_custom_toggled)
        
        # Set properties
        self._entry_custom.set_sensitive(False)
        
        # Construct the dialog
        self.action_area.pack_end(self._button_init)
        
        self._hbox_custom.pack_start(self._radio_custom, False, False)
        self._hbox_custom.pack_start(self._entry_custom, True, True)
        
        self.vbox.pack_start(self._label_question)
        self.vbox.pack_start(self._radio_current)
        self.vbox.pack_start(self._hbox_custom)
        
        # Display the dialog
        self.vbox.show_all()
    
    def _on_custom_toggled(self, widget):
        """ Occurs if the Custom radiobutton is toggled. """
        if self._radio_custom.get_active():
            self._entry_custom.set_sensitive(True)
            self._entry_custom.grab_focus()
        else:
            self._entry_custom.set_sensitive(False)
    
    @show_bzr_error
    def _on_init_clicked(self, widget):
        if self._radio_custom.get_active() and len(self._entry_custom.get_text()) == 0:
            error_dialog(_("Directory name not specified"),
                         _("You should specify a new directory name."))
            return
        
        if self._radio_current.get_active():
            location = self.path
        else:
            location = self.path + os.sep + self._entry_custom.get_text()
        
        format = bzrdir.format_registry.make_bzrdir('default')        
        to_transport = transport.get_transport(location)
        
        try:
            to_transport.mkdir('.')
        except errors.FileExists:
            pass
                    
        try:
            existing_bzrdir = bzrdir.BzrDir.open(location)
        except errors.NotBranchError:
            branch = bzrdir.BzrDir.create_branch_convenience(to_transport.base,
                                                             format=format)
        else:
            from bzrlib.transport.local import LocalTransport
            if existing_bzrdir.has_branch():
                if (isinstance(to_transport, LocalTransport)
                    and not existing_bzrdir.has_workingtree()):
                        raise errors.BranchExistsWithoutWorkingTree(location)
                raise errors.AlreadyBranchError(location)
            else:
                branch = existing_bzrdir.create_branch()
                existing_bzrdir.create_workingtree()
        
        self.response(gtk.RESPONSE_OK)