/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/push.py

  • Committer: Jelmer Vernooij
  • Date: 2007-02-01 15:50:40 UTC
  • Revision ID: jelmer@samba.org-20070201155040-3hq4mfbxs99kzazy
add framework for tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic)
2
 
#                       <szilveszter.farkas@gmail.com>
3
 
# Copyright (C) 2007 by Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
16
14
# along with this program; if not, write to the Free Software
17
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
 
from gi.repository import Gtk
20
 
 
21
 
from errors import show_bzr_error
22
 
 
23
 
from bzrlib import (
24
 
    errors,
25
 
    ui,
26
 
    )
27
 
from bzrlib.controldir import ControlDir
28
 
from bzrlib.push import PushResult
29
 
from bzrlib.transport import get_transport
30
 
 
31
 
from bzrlib.plugins.gtk.dialog import (
32
 
    question_dialog,
33
 
    )
34
 
from bzrlib.plugins.gtk.history import UrlHistory
35
 
from bzrlib.plugins.gtk.i18n import _i18n
36
 
from bzrlib.plugins.gtk.ui import ProgressPanel
37
 
 
38
 
 
39
 
class PushDialog(Gtk.Dialog):
40
 
    """New implementation of the Push dialog."""
41
 
 
42
 
    def __init__(self, repository=None, revid=None, branch=None, parent=None):
43
 
        """Initialize the Push dialog. """
44
 
        super(PushDialog, self).__init__(
45
 
            title="Push", parent=parent, flags=0, border_width=6,
46
 
            buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE))
47
 
        if repository is None:
48
 
            repository = branch.repository
 
17
try:
 
18
    import pygtk
 
19
    pygtk.require("2.0")
 
20
except:
 
21
    pass
 
22
    
 
23
import gtk
 
24
import gtk.gdk
 
25
import gtk.glade
 
26
 
 
27
import bzrlib.errors as errors
 
28
 
 
29
from dialog import error_dialog, info_dialog
 
30
from guifiles import GLADEFILENAME
 
31
 
 
32
 
 
33
class OlivePush:
 
34
    """ Display Push dialog and perform the needed actions. """
 
35
    def __init__(self, branch):
 
36
        """ Initialize the Push dialog. """
 
37
        self.glade = gtk.glade.XML(GLADEFILENAME, 'window_push')
 
38
        
 
39
        self.window = self.glade.get_widget('window_push')
 
40
 
49
41
        self.branch = branch
50
 
 
51
 
        # Unused arguments
52
 
        self.repository = repository
53
 
        if revid is None:
54
 
            revid = branch.last_revision()
55
 
        self.revid = revid
56
 
 
57
 
        # Create the widgets
58
 
        self._label_location = Gtk.Label(label=_i18n("Location:"))
59
 
        self._combo = Gtk.ComboBox.new_with_entry()
60
 
        self._button_push = Gtk.Button(_i18n("_Push"), use_underline=True)
61
 
        self._hbox_location = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
62
 
        self._push_message = Gtk.Label(xalign=0)
63
 
        self._progress_widget = ProgressPanel()
64
 
 
65
 
        # Set callbacks
66
 
        ui.ui_factory.set_progress_bar_widget(self._progress_widget)
67
 
        self.connect('close', self._on_close_clicked)
68
 
        self._button_push.connect('clicked', self._on_push_clicked)
69
 
 
70
 
        # Set properties
71
 
        content_area = self.get_content_area()
72
 
        content_area.set_spacing(6)
73
 
 
74
 
        # Pack widgets
75
 
        self._hbox_location.pack_start(self._label_location, False, False, 0)
76
 
        self._hbox_location.pack_start(self._combo, False, False, 0)
77
 
        content_area.pack_start(self._hbox_location, True, True, 0)
78
 
        content_area.pack_start(self._progress_widget, True, True, 0)
79
 
        content_area.pack_start(self._push_message, True, True, 0)
80
 
        self.get_action_area().pack_end(self._button_push, True, True, 0)
81
 
 
82
 
        # Show the dialog
83
 
        content_area.show_all()
84
 
        self._progress_widget.hide()
85
 
        self._push_message.hide()
86
 
 
87
 
        # Build location history
88
 
        self._history = UrlHistory(self.branch.get_config(), 'push_history')
89
 
        self._build_history()
90
 
 
91
 
    def _build_history(self):
92
 
        """Build up the location history. """
93
 
        self._combo_model = Gtk.ListStore(str)
94
 
        for item in self._history.get_entries():
95
 
            self._combo_model.append([item])
96
 
        self._combo.set_model(self._combo_model)
97
 
        self._combo.set_entry_text_column(0)
98
 
 
99
 
        if self.branch is not None:
100
 
            location = self.branch.get_push_location()
101
 
            if location is not None:
102
 
                self._combo.get_child().set_text(location)
103
 
 
104
 
    def _on_close_clicked(self, widget):
105
 
        """Close dialog handler."""
106
 
        ui.ui_factory.set_progress_bar_widget(None)
107
 
 
108
 
    @show_bzr_error
109
 
    def _on_push_clicked(self, widget):
110
 
        """Push button clicked handler. """
111
 
        self._push_message.hide()
112
 
        self._progress_widget.tick()
113
 
        location = self._combo.get_child().get_text()
114
 
 
115
 
        try:
116
 
            message = do_push(self.branch, location, overwrite=False)
117
 
        except errors.DivergedBranches:
118
 
            response = question_dialog(
119
 
                _i18n('Branches have been diverged'),
120
 
                _i18n('You cannot push if branches have diverged.\n'
121
 
                      'Overwrite?'))
122
 
            if response == Gtk.ResponseType.YES:
123
 
                message = do_push(self.branch, location, overwrite=True)
 
42
        
 
43
        # Dictionary for signal_autoconnect
 
44
        dic = { "on_button_push_push_clicked": self.push,
 
45
                "on_button_push_cancel_clicked": self.close,
 
46
                "on_button_push_test_clicked": self.test,
 
47
                "on_radiobutton_push_stored_toggled": self.stored_toggled,
 
48
                "on_radiobutton_push_specific_toggled": self.specific_toggled, }
 
49
        
 
50
        # Connect the signals to the handlers
 
51
        self.glade.signal_autoconnect(dic)
 
52
        
 
53
        # Get some useful widgets
 
54
        self.radio_stored = self.glade.get_widget('radiobutton_push_stored')
 
55
        self.radio_specific = self.glade.get_widget('radiobutton_push_specific')
 
56
        self.entry_stored = self.glade.get_widget('entry_push_stored')
 
57
        self.entry_location = self.glade.get_widget('entry_push_location')
 
58
        self.check_remember = self.glade.get_widget('checkbutton_push_remember')
 
59
        self.check_overwrite = self.glade.get_widget('checkbutton_push_overwrite')
 
60
        self.check_create = self.glade.get_widget('checkbutton_push_create')
 
61
        self.label_test = self.glade.get_widget('label_push_test')
 
62
        self.image_test = self.glade.get_widget('image_push_test')
 
63
        
 
64
        # Set initial state
 
65
        self.entry_location.set_sensitive(0)
 
66
        self.check_remember.set_sensitive(0)
 
67
        self.check_create.set_sensitive(0)
 
68
        
 
69
        self.entry_stored.set_text(branch.get_push_location() or '')
 
70
    
 
71
    def display(self):
 
72
        """ Display the Push dialog. """
 
73
        self.window.show()
 
74
        self.width, self.height = self.window.get_size()
 
75
    
 
76
    def stored_toggled(self, widget):
 
77
        if widget.get_active():
 
78
            self.entry_stored.set_sensitive(1)
 
79
            self.entry_location.set_sensitive(0)
 
80
            self.check_remember.set_sensitive(0)
 
81
            self.check_create.set_sensitive(0)
 
82
        else:
 
83
            self.entry_stored.set_sensitive(0)
 
84
            self.entry_location.set_sensitive(1)
 
85
            self.check_remember.set_sensitive(1)
 
86
            self.check_create.set_sensitive(1)
 
87
    
 
88
    def specific_toggled(self, widget):
 
89
        if widget.get_active():
 
90
            self.entry_stored.set_sensitive(0)
 
91
            self.entry_location.set_sensitive(1)
 
92
            self.check_remember.set_sensitive(1)
 
93
            self.check_create.set_sensitive(1)
 
94
        else:
 
95
            self.entry_stored.set_sensitive(1)
 
96
            self.entry_location.set_sensitive(0)
 
97
            self.check_remember.set_sensitive(0)
 
98
            self.check_create.set_sensitive(0)
 
99
    
 
100
    def push(self, widget):
 
101
        revs = 0
 
102
        if self.radio_stored.get_active():
 
103
            try:
 
104
                revs = do_push(self.branch,
 
105
                               overwrite=self.check_overwrite.get_active())
 
106
            except errors.DivergedBranches:
 
107
                response = question_dialog(_('Branches have been diverged'),
 
108
                             _('You cannot push if branches have diverged. \nOverwrite?'))
 
109
                if response == gtk.RESPONSE_OK:
 
110
                    revs = do_push(self.branch, overwrite=True)
 
111
                return
 
112
        elif self.radio_specific.get_active():
 
113
            location = self.entry_location.get_text()
 
114
            if location == '':
 
115
                error_dialog(_('No location specified'),
 
116
                             _('Please specify a location or use the default.'))
 
117
                return
 
118
            
 
119
            try:
 
120
                revs = do_push(self.branch, location,
 
121
                               self.check_remember.get_active(),
 
122
                               False,
 
123
                               self.check_create.get_active())
 
124
            except errors.DivergedBranches:
 
125
                response = question_dialog(_('Branches have been diverged'),
 
126
                             _('You cannot push if branches have diverged. \nOverwrite?'))
 
127
                if response == gtk.RESPONSE_OK:
 
128
                    revs = do_push(self.branch, location,
 
129
                        self.check_remember.get_active(),
 
130
                            True,
 
131
                            self.check_create.get_active())
 
132
                else:
 
133
                    return
 
134
        
 
135
        self.close()
 
136
        info_dialog(_('Push successful'),
 
137
                    _('%d revision(s) pushed.') % revs)
 
138
    
 
139
    def test(self, widget):
 
140
        """ Test if write access possible. """
 
141
        import re
 
142
        _urlRE = re.compile(r'^(?P<proto>[^:/\\]+)://(?P<path>.*)$')
 
143
        
 
144
        if self.radio_stored.get_active():
 
145
            url = self.entry_stored.get_text()
 
146
        elif self.radio_specific.get_active():
 
147
            url = self.entry_location.get_text()
 
148
        
 
149
        m = _urlRE.match(url)
 
150
        if m:
 
151
            proto = m.groupdict()['proto']
 
152
            if (proto == 'sftp') or (proto == 'file') or (proto == 'ftp'):
 
153
                # have write acces (most probably)
 
154
                self.image_test.set_from_stock(gtk.STOCK_YES, 4)
 
155
                self.label_test.set_markup(_('<b>Write access is probably available</b>'))
124
156
            else:
125
 
                return
126
 
        self._history.add_entry(location)
127
 
        if (self.branch is not None
128
 
            and self.branch.get_push_location() is None):
129
 
            self.branch.set_push_location(location)
130
 
        if message:
131
 
            self._progress_widget.finished()
132
 
            self._push_message.props.label = message
133
 
            self._push_message.show()
134
 
 
135
 
 
136
 
def do_push(br_from, location, overwrite):
137
 
    """Update a mirror of a branch.
138
 
 
139
 
    :param br_from: the source branch
 
157
                # no write access
 
158
                self.image_test.set_from_stock(gtk.STOCK_NO, 4)
 
159
                self.label_test.set_markup(_('<b>No write access</b>'))
 
160
        else:
 
161
            # couldn't determine
 
162
            self.image_test.set_from_stock(gtk.STOCK_DIALOG_QUESTION, 4)
 
163
            self.label_test.set_markup(_('<b>Could not determine</b>'))
 
164
    
 
165
    def close(self, widget=None):
 
166
        self.window.destroy()
 
167
 
 
168
def do_push(branch, location=None, remember=False, overwrite=False,
 
169
         create_prefix=False):
 
170
    """ Update a mirror of a branch.
 
171
    
 
172
    :param branch: the source branch
 
173
    
140
174
    :param location: the location of the branch that you'd like to update
 
175
    
 
176
    :param remember: if set, the location will be stored
 
177
    
141
178
    :param overwrite: overwrite target location if it diverged
 
179
    
 
180
    :param create_prefix: create the path leading up to the branch if it doesn't exist
 
181
    
142
182
    :return: number of revisions pushed
143
183
    """
144
 
    revision_id = None
145
 
    to_transport = get_transport(location)
 
184
    from bzrlib.branch import Branch
 
185
    from bzrlib.bzrdir import BzrDir
 
186
    from bzrlib.transport import get_transport
 
187
        
 
188
    br_from = branch
 
189
    
 
190
    stored_loc = br_from.get_push_location()
 
191
    if location is None:
 
192
        if stored_loc is None:
 
193
            error_dialog(_('Push location is unknown'),
 
194
                         _('Please specify a location manually.'))
 
195
            return
 
196
        else:
 
197
            location = stored_loc
 
198
 
 
199
    transport = get_transport(location)
 
200
    location_url = transport.base
 
201
 
 
202
    if br_from.get_push_location() is None or remember:
 
203
        br_from.set_push_location(location_url)
 
204
 
 
205
    old_rh = []
 
206
 
146
207
    try:
147
 
        dir_to = ControlDir.open_from_transport(to_transport)
 
208
        dir_to = BzrDir.open(location_url)
 
209
        br_to = dir_to.open_branch()
148
210
    except errors.NotBranchError:
149
 
        dir_to = None
150
 
 
151
 
    if dir_to is None:
152
 
        try:
153
 
            br_to = br_from.create_clone_on_transport(
154
 
                to_transport, revision_id=revision_id)
155
 
        except errors.NoSuchFile:
156
 
            response = question_dialog(
157
 
                _i18n('Non existing parent directory'),
158
 
                _i18n("The parent directory (%s)\ndoesn't exist. Create?") %
159
 
                    location)
160
 
            if response == Gtk.ResponseType.OK:
161
 
                br_to = br_from.create_clone_on_transport(
162
 
                    to_transport, revision_id=revision_id, create_prefix=True)
163
 
            else:
164
 
                return _i18n("Push aborted.")
165
 
        push_result = create_push_result(br_from, br_to)
 
211
        # create a branch.
 
212
        transport = transport.clone('..')
 
213
        if not create_prefix:
 
214
            try:
 
215
                relurl = transport.relpath(location_url)
 
216
                transport.mkdir(relurl)
 
217
            except errors.NoSuchFile:
 
218
                error_dialog(_('Non existing parent directory'),
 
219
                             _("The parent directory (%s)\ndoesn't exist.") % location)
 
220
                return
 
221
        else:
 
222
            current = transport.base
 
223
            needed = [(transport, transport.relpath(location_url))]
 
224
            while needed:
 
225
                try:
 
226
                    transport, relpath = needed[-1]
 
227
                    transport.mkdir(relpath)
 
228
                    needed.pop()
 
229
                except errors.NoSuchFile:
 
230
                    new_transport = transport.clone('..')
 
231
                    needed.append((new_transport,
 
232
                                   new_transport.relpath(transport.base)))
 
233
                    if new_transport.base == transport.base:
 
234
                        error_dialog(_('Path prefix not created'),
 
235
                                     _("The path leading up to the specified location couldn't\nbe created."))
 
236
                        return
 
237
        dir_to = br_from.bzrdir.clone(location_url,
 
238
            revision_id=br_from.last_revision())
 
239
        br_to = dir_to.open_branch()
 
240
        count = len(br_to.revision_history())
166
241
    else:
167
 
        push_result = dir_to.push_branch(br_from, revision_id, overwrite)
168
 
    message = create_push_message(br_from, push_result)
169
 
    return message
170
 
 
171
 
 
172
 
def create_push_message(br_from, push_result):
173
 
    """Return a mesage explaining what happened during the push."""
174
 
    messages = []
175
 
    rev_count = br_from.revno() - push_result.old_revno
176
 
    messages.append(_i18n("%d revision(s) pushed.") % rev_count)
177
 
    if push_result.stacked_on is not None:
178
 
        messages.append(_i18n("Stacked on %s.") % push_result.stacked_on)
179
 
    if push_result.workingtree_updated is False:
180
 
        messages.append(_i18n(
181
 
            "\nThe working tree was not updated:"
182
 
            "\nSee 'bzr help working-trees' for more information."))
183
 
    return '\n'.join(messages)
184
 
 
185
 
 
186
 
def create_push_result(br_from, br_to):
187
 
    """Return a PushResult like one created by ControlDir.push_branch()."""
188
 
    push_result = PushResult()
189
 
    push_result.source_branch = br_from
190
 
    push_result.target_branch = br_to
191
 
    push_result.branch_push_result = None
192
 
    push_result.master_branch = None
193
 
    push_result.old_revno = 0
194
 
    push_result.old_revid = br_to.last_revision()
195
 
    push_result.workingtree_updated = None  # Not applicable to this case.
196
 
    try:
197
 
        push_result.stacked_on = br_to.get_stacked_on_url()
198
 
    except (errors.UnstackableBranchFormat,
199
 
            errors.UnstackableRepositoryFormat,
200
 
            errors.NotStacked):
201
 
        push_result.stacked_on = None
202
 
    return push_result
 
242
        old_rh = br_to.revision_history()
 
243
        try:
 
244
            tree_to = dir_to.open_workingtree()
 
245
        except errors.NotLocalUrl:
 
246
            # FIXME - what to do here? how should we warn the user?
 
247
            #warning('This transport does not update the working '
 
248
            #        'tree of: %s' % (br_to.base,))
 
249
            count = br_to.pull(br_from, overwrite)
 
250
        except errors.NoWorkingTree:
 
251
            count = br_to.pull(br_from, overwrite)
 
252
        else:
 
253
            count = tree_to.pull(br_from, overwrite)
 
254
 
 
255
    return count