Welcome wizard: Fix changing the language causing a empty folder to be created. Fixes #1903825 [Calibre installer makes an extra library folder](https://bugs.launchpad.net/calibre/+bug/1903825)

This commit is contained in:
Kovid Goyal 2020-11-11 17:27:01 +05:30
parent 587b113c84
commit 3b5016c010
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import os import os
import re import re
import traceback import traceback
from contextlib import closing from contextlib import closing, suppress
from PyQt5.Qt import ( from PyQt5.Qt import (
QAbstractListModel, QDir, QIcon, QItemSelection, QItemSelectionModel, Qt, QAbstractListModel, QDir, QIcon, QItemSelection, QItemSelectionModel, Qt,
QWizard, QWizardPage, pyqtSignal QWizard, QWizardPage, pyqtSignal
@ -655,6 +655,7 @@ class LibraryPage(QWizardPage, LibraryUI):
def __init__(self): def __init__(self):
QWizardPage.__init__(self) QWizardPage.__init__(self)
self.made_dirs = []
self.initial_library_location = None self.initial_library_location = None
self.setupUi(self) self.setupUi(self)
self.registerField('library_location', self.location) self.registerField('library_location', self.location)
@ -664,6 +665,10 @@ class LibraryPage(QWizardPage, LibraryUI):
self.location.textChanged.connect(self.location_text_changed) self.location.textChanged.connect(self.location_text_changed)
self.set_move_lib_label_text() self.set_move_lib_label_text()
def makedirs(self, x):
self.made_dirs.append(x)
os.makedirs(x)
def location_text_changed(self, newtext): def location_text_changed(self, newtext):
self.completeChanged.emit() self.completeChanged.emit()
@ -759,7 +764,7 @@ class LibraryPage(QWizardPage, LibraryUI):
show=True) show=True)
if not os.path.exists(x): if not os.path.exists(x):
try: try:
os.makedirs(x) self.makedirs(x)
except: except:
return error_dialog(self, _('Bad location'), return error_dialog(self, _('Bad location'),
_('Failed to create a folder at %s')%x, _('Failed to create a folder at %s')%x,
@ -794,7 +799,7 @@ class LibraryPage(QWizardPage, LibraryUI):
self.default_library_name = lp self.default_library_name = lp
if not os.path.exists(lp): if not os.path.exists(lp):
try: try:
os.makedirs(lp) self.makedirs(lp)
except: except:
traceback.print_exc() traceback.print_exc()
try: try:
@ -828,6 +833,10 @@ class LibraryPage(QWizardPage, LibraryUI):
os.rmdir(dln) os.rmdir(dln)
except Exception: except Exception:
pass pass
# dont leave behind any empty dirs
for x in self.made_dirs:
with suppress(OSError):
os.rmdir(x)
if not os.path.exists(newloc): if not os.path.exists(newloc):
os.makedirs(newloc) os.makedirs(newloc)
prefs['library_path'] = newloc prefs['library_path'] = newloc