607b426
From 9b9d6728ef96f605641ade056a3d00b3c1f715d7 Mon Sep 17 00:00:00 2001
607b426
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
607b426
Date: Thu, 30 Sep 2021 08:43:15 +0200
607b426
Subject: [PATCH] conversions: use makedirs(exist_ok=True)
607b426
607b426
https://bugzilla.redhat.com/show_bug.cgi?id=1922761 reports the following
607b426
failure:
607b426
607b426
Traceback (most recent call last):
607b426
  File "/bin/ebook-edit", line 20, in <module>
607b426
    sys.exit(ebook_edit())
607b426
  File "/usr/lib64/calibre/calibre/gui_launch.py", line 119, in ebook_edit
607b426
    main(args)
607b426
  File "/usr/lib64/calibre/calibre/gui2/tweak_book/main.py", line 98, in main
607b426
    _run(args)
607b426
  File "/usr/lib64/calibre/calibre/gui2/tweak_book/main.py", line 66, in _run
607b426
    from calibre.gui2.tweak_book.ui import Main
607b426
  File "/usr/lib64/calibre/calibre/gui2/tweak_book/ui.py", line 29, in <module>
607b426
    from calibre.gui2.tweak_book.boss import Boss
607b426
  File "/usr/lib64/calibre/calibre/gui2/tweak_book/boss.py", line 28, in <module>
607b426
    from calibre.ebooks.oeb.polish.main import SUPPORTED, tweak_polish
607b426
  File "/usr/lib64/calibre/calibre/ebooks/oeb/polish/main.py", line 20, in <module>
607b426
    from calibre.ebooks.oeb.polish.jacket import (
607b426
  File "/usr/lib64/calibre/calibre/ebooks/oeb/polish/jacket.py", line 9, in <module>
607b426
    from calibre.ebooks.conversion.config import load_defaults
607b426
  File "/usr/lib64/calibre/calibre/ebooks/conversion/config.py", line 20, in <module>
607b426
    os.makedirs(config_dir)
607b426
  File "/usr/lib64/python3.9/os.py", line 225, in makedirs
607b426
    mkdir(name, mode)
607b426
FileExistsError: [Errno 17] Arquivo existe: '/home/vinicius/.config/calibre/conversion'
607b426
607b426
I assume that the user had two calibre instances started, and they raced on
607b426
the check and creation of the directory. Let's use os.makedirs(exist_ok=True).
607b426
(added in Python 3.2).
607b426
607b426
Also, only create the directories when saving/reading the file, not when the
607b426
module is imported.
607b426
---
607b426
 src/calibre/ebooks/conversion/config.py | 8 ++++++--
607b426
 1 file changed, 6 insertions(+), 2 deletions(-)
607b426
607b426
diff --git a/src/calibre/ebooks/conversion/config.py b/src/calibre/ebooks/conversion/config.py
607b426
index 459caab0d34..ec2da772730 100644
607b426
--- a/src/calibre/ebooks/conversion/config.py
607b426
+++ b/src/calibre/ebooks/conversion/config.py
607b426
@@ -17,8 +17,6 @@
607b426
 
607b426
 
607b426
 config_dir = os.path.join(config_dir, 'conversion')
607b426
-if not os.path.exists(config_dir):
607b426
-    os.makedirs(config_dir)
607b426
 
607b426
 
607b426
 def name_to_path(name):
607b426
@@ -28,6 +26,9 @@ def name_to_path(name):
607b426
 def save_defaults(name, recs):
607b426
     path = name_to_path(name)
607b426
     raw = recs.serialize()
607b426
+
607b426
+    os.makedirs(config_dir, exist_ok=True)
607b426
+
607b426
     with lopen(path, 'wb'):
607b426
         pass
607b426
     with ExclusiveFile(path) as f:
607b426
@@ -36,6 +37,9 @@ def save_defaults(name, recs):
607b426
 
607b426
 def load_defaults(name):
607b426
     path = name_to_path(name)
607b426
+
607b426
+    os.makedirs(config_dir, exist_ok=True)
607b426
+
607b426
     if not os.path.exists(path):
607b426
         open(path, 'wb').close()
607b426
     with ExclusiveFile(path) as f: