~bzr/qbzr/daily-ppa

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -*- coding: utf-8 -*-
#
# QBzr - Qt frontend to Bazaar commands
#
# Contributors:
#  Javier Derderian <javierder@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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import sys
from PyQt4 import QtCore, QtGui

from bzrlib import (
    bzrdir,
    errors,
    export,
    osutils,
    )
from bzrlib.plugins.qbzr.lib.i18n import gettext
from bzrlib.plugins.qbzr.lib.subprocess import SubProcessDialog
from bzrlib.plugins.qbzr.lib.util import url_for_display


class QBzrExportDialog(SubProcessDialog):

    FORMATS = { # key is archive format, value is tuple of accepted extensions
        'tar': ('tar',),
        'tbz2': ('tar.bz2', 'tbz2'),
        'tgz': ('tar.gz', 'tgz'),
        'zip': ('zip',),
        }
    
    FORMAT_NAMES = { # key is shown name , value is format  
        'tar': 'tar',
        'tar.bz2': 'tbz2',
        'tar.gz': 'tgz',
        'zip': 'zip',
        }
    
    def __init__(self, dest=None, branch=None, ui_mode=False, parent=None):
        """Create export dialog.
        @param  dest:   path to export to (either archive name or directory).
        @param  branch: exported branch.
        @param  ui_mode:    does dialog should stay on the screen after
            operation completed successfully.
        @param  parent: parent window.
        """
        title = gettext("Export")
        super(QBzrExportDialog, self).__init__(
                                  title,
                                  name = "export",
                                  default_size = (400, 400),
                                  ui_mode = ui_mode,
                                  dialog = True,
                                  parent = parent,
                                  hide_progress=False,
                                  )
        self.branch = branch
 
        # Create the branch information panel
        info_hbox = QtGui.QHBoxLayout()
        branch_info = gettext("Branch: %s") % url_for_display(branch.base)
        info_label = QtGui.QLabel(branch_info)
        info_hbox.addWidget(info_label)
 
        # Create the export group box
        gbExportDestination = QtGui.QGroupBox(gettext("Export"), self)
        vboxExportDestination = QtGui.QVBoxLayout(gbExportDestination)
        vboxExportDestination.addStrut(0)

        # Build export as archive section
        exportarch_radio = QtGui.QRadioButton("Export as archive")
        exportarch_radio.setChecked(True)
        self.exportarch_radio = exportarch_radio 
        vboxExportDestination.addWidget(exportarch_radio)
        vboxExportDestination.addLayout(self._build_archive_location_layout())
        vboxExportDestination.addLayout(self._build_archive_root_layout())
        vboxExportDestination.addLayout(self._build_archive_type_layout())
 
        # Build export as directory section
        exportdir_radio = QtGui.QRadioButton("Export as directory")
        self.exportdir_radio = exportdir_radio
        vboxExportDestination.addWidget(exportdir_radio)
        vboxExportDestination.addLayout(
            self._build_directory_location_layout())
 
        # Build the options group box
        gbExportOptions = self._build_options_group_box()

        # Put the form together
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(info_hbox)
        layout.addWidget(gbExportDestination)
        layout.addWidget(gbExportOptions)
        layout.addWidget(self.make_default_status_box())
        layout.addWidget(self.buttonbox)

        # Initialise the locations with sensible defaults
        if dest is not None:
            if os.path.isdir(dest) or self.detect_format(dest) is None:
                self.locationdir_edit.setText(osutils.abspath(dest))
                self.locationdir_edit.setFocus()
                exportdir_radio.setChecked(True)   
                self.locationdir_edit.setFocus()
            else:
                self.locationdir_edit.setText(osutils.abspath(dest))
                self.update_root_n_format()
                exportarch_radio.setChecked(True)
                self.locationfil_edit.setFocus()
        else:
            self.update_export_path(use_parent=True)
            self.update_root_n_format()

        # Disable the group boxes while doing the real work
        QtCore.QObject.connect(self,
                               QtCore.SIGNAL("disableUi(bool)"),
                               gbExportDestination,
                               QtCore.SLOT("setDisabled(bool)"))
        QtCore.QObject.connect(self,
                               QtCore.SIGNAL("disableUi(bool)"),
                               gbExportOptions,
                               QtCore.SLOT("setDisabled(bool)"))

        # Setup smart setting of fields as others are edited.
        # We could do more here (e.g. make the root directory change
        # when the location changes or vice versa) but opinions vary on
        # whether that increases or decreases usability so KISS for now.
        QtCore.QObject.connect(self.format_combo,
                               QtCore.SIGNAL("currentIndexChanged(int)"),
                               self.format_changed)
 
    def _build_archive_type_layout(self):
        format_label = QtGui.QLabel(gettext("Archive type:"))
        format_combo = QtGui.QComboBox()
        self.format_combo = format_combo
        for ix, k in enumerate(sorted(self.FORMAT_NAMES.keys())):
            format_combo.insertItem(ix, k)
    
        # set zip as default if we're on windows, otherwise tar.gz
        if sys.platform == 'win32':
            indx = format_combo.findText("zip")
        else:
            indx = format_combo.findText("tar.gz")
        format_combo.setCurrentIndex(indx)
        
        format_hbox = QtGui.QHBoxLayout()
        format_hbox.addSpacing(25)
        format_hbox.addWidget(format_label)
        format_hbox.addWidget(format_combo)
        format_hbox.setStretchFactor(format_label,0)
        format_hbox.setStretchFactor(format_combo,1)
        return format_hbox

    def _build_archive_root_layout(self):
        folder_label = QtGui.QLabel(gettext("Root directory name:"))
        folder_edit = QtGui.QLineEdit()
        self.folder_edit = folder_edit

        folder_hbox = QtGui.QHBoxLayout()
        folder_hbox.addSpacing(25)
        folder_hbox.addWidget(folder_label)
        folder_hbox.addWidget(folder_edit)
        return folder_hbox
        
    def _build_archive_location_layout(self):
        locationfil_label = QtGui.QLabel(gettext("Location:"))
        locationfil_edit = QtGui.QLineEdit()
        self.locationfil_edit = locationfil_edit
        browsefil_button = QtGui.QPushButton(gettext("Browse"))
        QtCore.QObject.connect(browsefil_button,
                               QtCore.SIGNAL("clicked(bool)"),
                               self.browsefil_clicked)

        locationfil_hbox = QtGui.QHBoxLayout()        
        locationfil_hbox.addSpacing(25)
        locationfil_hbox.addWidget(locationfil_label)
        locationfil_hbox.addWidget(locationfil_edit)
        locationfil_hbox.addWidget(browsefil_button)
        locationfil_hbox.setStretchFactor(locationfil_label,0)
        locationfil_hbox.setStretchFactor(locationfil_edit,1)
        locationfil_hbox.setStretchFactor(browsefil_button,0)
        return locationfil_hbox
 
    def _build_directory_location_layout(self):
        locationdir_edit = QtGui.QLineEdit()
        self.locationdir_edit = locationdir_edit
        browsedir_button = QtGui.QPushButton(gettext("Browse"))
        QtCore.QObject.connect(browsedir_button,
            QtCore.SIGNAL("clicked(bool)"), self.browsedir_clicked)
 
        locationdir_hbox = QtGui.QHBoxLayout()        
        locationdir_hbox.addSpacing(25)
        locationdir_hbox.addWidget(locationdir_edit)
        locationdir_hbox.addWidget(browsedir_button)
        locationdir_hbox.setStretchFactor(locationdir_edit,1)
        locationdir_hbox.setStretchFactor(browsedir_button,0)
        return locationdir_hbox
        
    def _build_options_group_box(self):
        """Build and return the options group box."""
        # Build the revision selection fields
        revisions_box = QtGui.QGridLayout()
        revisions_label = QtGui.QLabel(gettext("Revision:"))
        revisions_tip = QtGui.QRadioButton("Branch tip")
        revisions_tip.setChecked(True)
        self.revisions_tip = revisions_tip
        revisions_box.addWidget(revisions_label, 0, 0)
        revisions_box.addWidget(revisions_tip, 0, 1)
        revisions_other = QtGui.QRadioButton("Other")
        self.revisions_other = revisions_other
        revisions_edit = QtGui.QLineEdit()
        self.revisions_edit = revisions_edit
        revisions_box.addWidget(revisions_other, 1, 1)
        revisions_box.addWidget(revisions_edit, 1, 2)

        # Build the content filtering field
        format_box = QtGui.QGridLayout()
        format_canonical = QtGui.QCheckBox("Apply content filters to files")
        self.format_canonical = format_canonical
        format_box.addWidget(format_canonical, 0, 0)

        # Build the group box and return it
        gbExportOptions = QtGui.QGroupBox(gettext("Options"), self)
        vbxExportOptions = QtGui.QVBoxLayout(gbExportOptions)
        vbxExportOptions.addLayout(revisions_box)
        vbxExportOptions.addLayout(format_box)
        return gbExportOptions

    def update_export_path(self, root_folder=None, use_parent=False):
        base = url_for_display(self.branch.base)
        if base[-1] == '/' or base[-1] == '\\':
            base = base[0:-1]
        base = os.path.split(base)
        
        format = str(self.format_combo.currentText())
        if root_folder == None:
            export_name = "%s/%s.%s" % (base[0], base[1], format)
        else:
            export_name = "%s/%s.%s" % (base[0], root_folder, format)
        try:
            basedir = bzrdir.BzrDir.open(base[0])
        except errors.NotBranchError: #this is not even a bzr dir
            pass
        else:
            try:
                basedir.open_branch()
            except errors.NotBranchError: #this is a shared repo. name "repo-dir"
                base_sp = os.path.split(base[0])
                if use_parent:
                    export_name = "%s/%s/%s-%s.%s" % (base_sp[0], base_sp[1],
                                                      base_sp[1], base[1],
                                                      format)
                else:
                    export_name = "%s/%s/%s.%s" % (base_sp[0], base_sp[1],
                                                  root_folder, format)
            
        self.locationfil_edit.setText(export_name)
        
    def get_current_format(self):
        format_name = str(self.format_combo.currentText())
        format = self.FORMAT_NAMES[format_name]
        return format
            
    def detect_format(self, path):
        """Return archive type or None."""
        for k, v in self.FORMATS.iteritems():
            for i in v:
                if path.endswith(i):
                    return k
        return None

    def update_root_n_format(self):
        path = unicode(self.locationfil_edit.text())
        format = self.detect_format(path)
        if format is not None:
            ix = sorted(self.FORMATS.keys()).index(format)
            self.format_combo.setCurrentIndex(ix)
        self.folder_edit.setText(export.get_root_name(path))

    def browsedir_clicked(self):
        fileName = QtGui.QFileDialog.getExistingDirectory(self,
            ("Select save location"))
        if fileName != None and fileName != '':
            self.locationdir_edit.setText(fileName)
            self.exportdir_radio.setChecked(True)
                
    def browsefil_clicked(self):
        fileName = QtGui.QFileDialog.getSaveFileName(self,
            ("Select save location"))
        if fileName != None and fileName != '':
            self.locationfil_edit.setText(fileName)   
            self.update_root_n_format()
            self.exportarch_radio.setChecked(True)
            
    def format_changed(self, index):
        sel_format = str(self.format_combo.currentText())
        currfil = str(self.locationfil_edit.text())
        path = os.path.split(currfil)
        name = path[1]
        path = path[0]
        name = name.split(".")[0]
        self.locationfil_edit.setText("%s/%s.%s" % (path,name,sel_format))
        
    def validate(self):
        if self.exportarch_radio.isChecked():
            location = unicode(self.locationfil_edit.text())
            if location == "":
                raise errors.BzrCommandError("Export location is invalid")
        elif self.exportdir_radio.isChecked():
            location = unicode(self.locationdir_edit.text())
            if location == "":
                raise errors.BzrCommandError("Export location is invalid")        
        
        if self.revisions_other.isChecked():
            if str(self.revisions_edit.text()) == "":
                raise errors.BzrCommandError("Export revision is invalid")
        return True

    def do_start(self):
        args = ['export']

        # setup export to archive or directory
        if self.exportarch_radio.isChecked():
            location = unicode(self.locationfil_edit.text())
            format = self.get_current_format()
            root = unicode(self.folder_edit.text())
            if root:
                args.append("--root=%s" % root)
            else:
                args.append("--root=")
        else:
            location = unicode(self.locationdir_edit.text())
            format = 'dir'
        args.append(location)
        args.append("--format=%s" % format)

        # Append options
        if self.revisions_tip.isChecked():
            args.append("--revision=-1")
        else:
            revision = unicode(self.revisions_edit.text())
            args.append("--revision=%s" % revision)
        if self.format_canonical.isChecked():
            args.append("--filters")

        # Append the source location
        branch_location = url_for_display(self.branch.base)
        args.append(branch_location)

        self.process_widget.do_start(None, *args)