From 3b5016c01049f462dadd2ec258f6a60aef019eca Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 11 Nov 2020 17:27:01 +0530 Subject: [PATCH] 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) --- src/calibre/gui2/wizard/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/wizard/__init__.py b/src/calibre/gui2/wizard/__init__.py index 1801212eda..ed01642d59 100644 --- a/src/calibre/gui2/wizard/__init__.py +++ b/src/calibre/gui2/wizard/__init__.py @@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en' import os import re import traceback -from contextlib import closing +from contextlib import closing, suppress from PyQt5.Qt import ( QAbstractListModel, QDir, QIcon, QItemSelection, QItemSelectionModel, Qt, QWizard, QWizardPage, pyqtSignal @@ -655,6 +655,7 @@ class LibraryPage(QWizardPage, LibraryUI): def __init__(self): QWizardPage.__init__(self) + self.made_dirs = [] self.initial_library_location = None self.setupUi(self) self.registerField('library_location', self.location) @@ -664,6 +665,10 @@ class LibraryPage(QWizardPage, LibraryUI): self.location.textChanged.connect(self.location_text_changed) self.set_move_lib_label_text() + def makedirs(self, x): + self.made_dirs.append(x) + os.makedirs(x) + def location_text_changed(self, newtext): self.completeChanged.emit() @@ -759,7 +764,7 @@ class LibraryPage(QWizardPage, LibraryUI): show=True) if not os.path.exists(x): try: - os.makedirs(x) + self.makedirs(x) except: return error_dialog(self, _('Bad location'), _('Failed to create a folder at %s')%x, @@ -794,7 +799,7 @@ class LibraryPage(QWizardPage, LibraryUI): self.default_library_name = lp if not os.path.exists(lp): try: - os.makedirs(lp) + self.makedirs(lp) except: traceback.print_exc() try: @@ -828,6 +833,10 @@ class LibraryPage(QWizardPage, LibraryUI): os.rmdir(dln) except Exception: 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): os.makedirs(newloc) prefs['library_path'] = newloc