Debian Codegraph @

lib/export.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 #
5 # Contributors:
6 # Javier Derderian <[email protected]>
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22 import os
23 import sys
24 from PyQt5 import QtCore, QtGui, QtWidgets
25
26 from breezy import (
27 controldir,
28 errors,
29 export,
30 osutils,
31 )
32 from breezy.plugins.qbrz.lib.i18n import gettext
33 from breezy.plugins.qbrz.lib.subprocess import SubProcessDialog
34 from breezy.plugins.qbrz.lib.util import url_for_display
35
36
37 class QBzrExportDialog(SubProcessDialog):
38
39 FORMATS = { # key is archive format, value is tuple of accepted extensions
40 'tar': ('tar',),
41 'tbz2': ('tar.bz2', 'tbz2'),
42 'tgz': ('tar.gz', 'tgz'),
43 'zip': ('zip',),
44 }
45
46 FORMAT_NAMES = { # key is shown name , value is format
47 'tar': 'tar',
48 'tar.bz2': 'tbz2',
49 'tar.gz': 'tgz',
50 'zip': 'zip',
51 }
52
53 def __init__(self, dest=None, branch=None, ui_mode=False, parent=None):
54 """Create export dialog.
55 @param dest: path to export to (either archive name or directory).
56 @param branch: exported branch.
57 @param ui_mode: does dialog should stay on the screen after
58 operation completed successfully.
59 @param parent: parent window.
60 """
61 title = gettext("Export")
62 super(QBzrExportDialog, self).__init__(
63 title,
64 name = "export",
65 default_size = (400, 400),
66 ui_mode = ui_mode,
67 dialog = True,
68 parent = parent,
69 hide_progress=False,
70 )
71 self.branch = branch
72
73 # Create the branch information panel
74 info_hbox = QtWidgets.QHBoxLayout()
75 branch_info = gettext("Branch: %s") % url_for_display(branch.base)
76 info_label = QtWidgets.QLabel(branch_info)
77 info_hbox.addWidget(info_label)
78
79 # Create the export group box
80 gbExportDestination = QtWidgets.QGroupBox(gettext("Export"), self)
81 vboxExportDestination = QtWidgets.QVBoxLayout(gbExportDestination)
82 vboxExportDestination.addStrut(0)
83
84 # Build export as archive section
85 exportarch_radio = QtWidgets.QRadioButton("Export as archive")
86 exportarch_radio.setChecked(True)
87 self.exportarch_radio = exportarch_radio
88 vboxExportDestination.addWidget(exportarch_radio)
89 vboxExportDestination.addLayout(self._build_archive_location_layout())
90 vboxExportDestination.addLayout(self._build_archive_root_layout())
91 vboxExportDestination.addLayout(self._build_archive_type_layout())
92
93 # Build export as directory section
94 exportdir_radio = QtWidgets.QRadioButton("Export as directory")
95 self.exportdir_radio = exportdir_radio
96 vboxExportDestination.addWidget(exportdir_radio)
97 vboxExportDestination.addLayout(
98 self._build_directory_location_layout())
99
100 # Build the options group box
101 gbExportOptions = self._build_options_group_box()
102
103 # Put the form together
104 layout = QtWidgets.QVBoxLayout(self)
105 layout.addLayout(info_hbox)
106 layout.addWidget(gbExportDestination)
107 layout.addWidget(gbExportOptions)
108 layout.addWidget(self.make_default_status_box())
109 layout.addWidget(self.buttonbox)
110
111 # Initialise the locations with sensible defaults
112 if dest is not None:
113 if os.path.isdir(dest) or self.detect_format(dest) is None:
114 self.locationdir_edit.setText(osutils.abspath(dest))
115 self.locationdir_edit.setFocus()
116 exportdir_radio.setChecked(True)
117 self.locationdir_edit.setFocus()
118 else:
119 self.locationdir_edit.setText(osutils.abspath(dest))
120 self.update_root_n_format()
121 exportarch_radio.setChecked(True)
122 self.locationfil_edit.setFocus()
123 else:
124 self.update_export_path(use_parent=True)
125 self.update_root_n_format()
126
127 # Disable the group boxes while doing the real work
128 self.disableUi[bool].connect(gbExportDestination.setDisabled)
129 self.disableUi[bool].connect(gbExportOptions.setDisabled)
130
131 # Setup smart setting of fields as others are edited.
132 # We could do more here (e.g. make the root directory change
133 # when the location changes or vice versa) but opinions vary on
134 # whether that increases or decreases usability so KISS for now.
135 self.format_combo.currentIndexChanged[int].connect(self.format_changed)
136
137 def _build_archive_type_layout(self):
138 format_label = QtWidgets.QLabel(gettext("Archive type:"))
139 format_combo = QtWidgets.QComboBox()
140 self.format_combo = format_combo
141 for ix, k in enumerate(sorted(self.FORMAT_NAMES.keys())):
142 format_combo.insertItem(ix, k)
143
144 # set zip as default if we're on windows, otherwise tar.gz
145 if sys.platform == 'win32':
146 indx = format_combo.findText("zip")
147 else:
148 indx = format_combo.findText("tar.gz")
149 format_combo.setCurrentIndex(indx)
150
151 format_hbox = QtWidgets.QHBoxLayout()
152 format_hbox.addSpacing(25)
153 format_hbox.addWidget(format_label)
154 format_hbox.addWidget(format_combo)
155 format_hbox.setStretchFactor(format_label,0)
156 format_hbox.setStretchFactor(format_combo,1)
157 return format_hbox
158
159 def _build_archive_root_layout(self):
160 folder_label = QtWidgets.QLabel(gettext("Root directory name:"))
161 folder_edit = QtWidgets.QLineEdit()
162 self.folder_edit = folder_edit
163
164 folder_hbox = QtWidgets.QHBoxLayout()
165 folder_hbox.addSpacing(25)
166 folder_hbox.addWidget(folder_label)
167 folder_hbox.addWidget(folder_edit)
168 return folder_hbox
169
170 def _build_archive_location_layout(self):
171 locationfil_label = QtWidgets.QLabel(gettext("Location:"))
172 locationfil_edit = QtWidgets.QLineEdit()
173 self.locationfil_edit = locationfil_edit
174 browsefil_button = QtWidgets.QPushButton(gettext("Browse"))
175 browsefil_button.clicked[bool].connect(self.browsefil_clicked)
176
177 locationfil_hbox = QtWidgets.QHBoxLayout()
178 locationfil_hbox.addSpacing(25)
179 locationfil_hbox.addWidget(locationfil_label)
180 locationfil_hbox.addWidget(locationfil_edit)
181 locationfil_hbox.addWidget(browsefil_button)
182 locationfil_hbox.setStretchFactor(locationfil_label,0)
183 locationfil_hbox.setStretchFactor(locationfil_edit,1)
184 locationfil_hbox.setStretchFactor(browsefil_button,0)
185 return locationfil_hbox
186
187 def _build_directory_location_layout(self):
188 locationdir_edit = QtWidgets.QLineEdit()
189 self.locationdir_edit = locationdir_edit
190 browsedir_button = QtWidgets.QPushButton(gettext("Browse"))
191 browsedir_button.clicked[bool].connect(self.browsedir_clicked)
192
193 locationdir_hbox = QtWidgets.QHBoxLayout()
194 locationdir_hbox.addSpacing(25)
195 locationdir_hbox.addWidget(locationdir_edit)
196 locationdir_hbox.addWidget(browsedir_button)
197 locationdir_hbox.setStretchFactor(locationdir_edit,1)
198 locationdir_hbox.setStretchFactor(browsedir_button,0)
199 return locationdir_hbox
200
201 def _build_options_group_box(self):
202 """Build and return the options group box."""
203 # Build the revision selection fields
204 revisions_box = QtWidgets.QGridLayout()
205 revisions_label = QtWidgets.QLabel(gettext("Revision:"))
206 revisions_tip = QtWidgets.QRadioButton("Branch tip")
207 revisions_tip.setChecked(True)
208 self.revisions_tip = revisions_tip
209 revisions_box.addWidget(revisions_label, 0, 0)
210 revisions_box.addWidget(revisions_tip, 0, 1)
211 revisions_other = QtWidgets.QRadioButton("Other")
212 self.revisions_other = revisions_other
213 revisions_edit = QtWidgets.QLineEdit()
214 self.revisions_edit = revisions_edit
215 revisions_box.addWidget(revisions_other, 1, 1)
216 revisions_box.addWidget(revisions_edit, 1, 2)
217
218 # Build the content filtering field
219 format_box = QtWidgets.QGridLayout()
220 format_canonical = QtWidgets.QCheckBox("Apply content filters to files")
221 self.format_canonical = format_canonical
222 format_box.addWidget(format_canonical, 0, 0)
223
224 # Build the group box and return it
225 gbExportOptions = QtWidgets.QGroupBox(gettext("Options"), self)
226 vbxExportOptions = QtWidgets.QVBoxLayout(gbExportOptions)
227 vbxExportOptions.addLayout(revisions_box)
228 vbxExportOptions.addLayout(format_box)
229 return gbExportOptions
230
231 def update_export_path(self, root_folder=None, use_parent=False):
232 base = url_for_display(self.branch.base)
233 if base[-1] == '/' or base[-1] == '\\':
234 base = base[0:-1]
235 base = os.path.split(base)
236
237 format = str(self.format_combo.currentText())
238 if root_folder == None:
239 export_name = "%s/%s.%s" % (base[0], base[1], format)
240 else:
241 export_name = "%s/%s.%s" % (base[0], root_folder, format)
242 try:
243 basedir = controldir.ControlDir.open(base[0])
244 except errors.NotBranchError: #this is not even a bzr dir
245 pass
246 else:
247 try:
248 basedir.open_branch()
249 except errors.NotBranchError: #this is a shared repo. name "repo-dir"
250 base_sp = os.path.split(base[0])
251 if use_parent:
252 export_name = "%s/%s/%s-%s.%s" % (base_sp[0], base_sp[1],
253 base_sp[1], base[1],
254 format)
255 else:
256 export_name = "%s/%s/%s.%s" % (base_sp[0], base_sp[1],
257 root_folder, format)
258
259 self.locationfil_edit.setText(export_name)
260
261 def get_current_format(self):
262 format_name = str(self.format_combo.currentText())
263 format = self.FORMAT_NAMES[format_name]
264 return format
265
266 def detect_format(self, path):
267 """Return archive type or None."""
268 for k, v in self.FORMATS.items():
269 for i in v:
270 if path.endswith(i):
271 return k
272 return None
273
274 def update_root_n_format(self):
275 path = str(self.locationfil_edit.text())
276 format = self.detect_format(path)
277 if format is not None:
278 ix = sorted(self.FORMATS.keys()).index(format)
279 self.format_combo.setCurrentIndex(ix)
280 self.folder_edit.setText(export.get_root_name(path))
281
282 def browsedir_clicked(self):
283 fileName = QtWidgets.QFileDialog.getExistingDirectory(self,
284 ("Select save location"))
285 if fileName != None and fileName != '':
286 self.locationdir_edit.setText(fileName)
287 self.exportdir_radio.setChecked(True)
288
289 def browsefil_clicked(self):
290 fileName = QtWidgets.QFileDialog.getSaveFileName(self,
291 ("Select save location"))[0]
292 if fileName != None and fileName != '':
293 self.locationfil_edit.setText(fileName)
294 self.update_root_n_format()
295 self.exportarch_radio.setChecked(True)
296
297 def format_changed(self, index):
298 sel_format = str(self.format_combo.currentText())
299 currfil = str(self.locationfil_edit.text())
300 path = os.path.split(currfil)
301 name = path[1]
302 path = path[0]
303 name = name.split(".")[0]
304 self.locationfil_edit.setText("%s/%s.%s" % (path,name,sel_format))
305
306 def validate(self):
307 if self.exportarch_radio.isChecked():
308 location = str(self.locationfil_edit.text())
309 if not location:
310 self.operation_blocked(gettext("Export location is invalid"))
311 return False
312 elif self.exportdir_radio.isChecked():
313 location = str(self.locationdir_edit.text())
314 if not location:
315 self.operation_blocked(gettext("Export location is invalid"))
316 return False
317
318 if self.revisions_other.isChecked():
319 if str(self.revisions_edit.text()) == "":
320 self.operation_blocked(gettext("Export revision is invalid"))
321 return False
322 return True
323
324 def do_start(self):
325 args = ['export']
326
327 # setup export to archive or directory
328 if self.exportarch_radio.isChecked():
329 location = str(self.locationfil_edit.text())
330 format = self.get_current_format()
331 root = str(self.folder_edit.text())
332 if root:
333 args.append("--root=%s" % root)
334 else:
335 args.append("--root=")
336 else:
337 location = str(self.locationdir_edit.text())
338 format = 'dir'
339 args.append(location)
340 args.append("--format=%s" % format)
341
342 # Append options
343 if self.revisions_tip.isChecked():
344 args.append("--revision=-1")
345 else:
346 revision = str(self.revisions_edit.text())
347 args.append("--revision=%s" % revision)
348 if self.format_canonical.isChecked():
349 args.append("--filters")
350
351 # Append the source location
352 branch_location = url_for_display(self.branch.base)
353 args.append(branch_location)
354
355 self.process_widget.do_start(None, *args)