diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 6e54d8d2bf..0424b44682 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -437,27 +437,19 @@ def fit_image(width, height, pwidth, pheight): class CurrentDir(object): - def __init__(self, path, workaround_temp_folder_permissions=False): + def __init__(self, path): self.path = path self.cwd = None - self.workaround_temp_folder_permissions = workaround_temp_folder_permissions def __enter__(self, *args): self.cwd = os.getcwdu() - try: - os.chdir(self.path) - except OSError: - if not self.workaround_temp_folder_permissions: - raise - from calibre.ptempfile import reset_temp_folder_permissions - reset_temp_folder_permissions() - os.chdir(self.path) + os.chdir(self.path) return self.cwd def __exit__(self, *args): try: os.chdir(self.cwd) - except: + except EnvironmentError: # The previous CWD no longer exists pass diff --git a/src/calibre/customize/conversion.py b/src/calibre/customize/conversion.py index c760d3c8dc..88e989b933 100644 --- a/src/calibre/customize/conversion.py +++ b/src/calibre/customize/conversion.py @@ -237,7 +237,7 @@ class InputFormatPlugin(Plugin): # In case stdout is broken pass - with CurrentDir(output_dir, workaround_temp_folder_permissions=True): + with CurrentDir(output_dir): for x in os.listdir('.'): shutil.rmtree(x) if os.path.isdir(x) else os.remove(x) diff --git a/src/calibre/ptempfile.py b/src/calibre/ptempfile.py index c2821e8343..849a2a5351 100644 --- a/src/calibre/ptempfile.py +++ b/src/calibre/ptempfile.py @@ -1,4 +1,4 @@ -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ @@ -9,7 +9,7 @@ import tempfile, os, atexit from polyglot.builtins import map from calibre.constants import (__version__, __appname__, filesystem_encoding, - get_unicode_windows_env_var, iswindows, get_windows_temp_path, isosx) + get_unicode_windows_env_var, iswindows, get_windows_temp_path, isosx, ispy3) def cleanup(path): @@ -59,20 +59,6 @@ def app_prefix(prefix): return '%s_%s_%s'%(__appname__, __version__, prefix) -def reset_temp_folder_permissions(): - # There are some broken windows installs where the permissions for the temp - # folder are set to not be executable, which means chdir() into temp - # folders fails. Try to fix that by resetting the permissions on the temp - # folder. - global _base_dir - if iswindows and _base_dir: - import subprocess - from calibre import prints - parent = os.path.dirname(_base_dir) - retcode = subprocess.Popen(['icacls.exe', parent, '/reset', '/Q', '/T']).wait() - prints('Trying to reset permissions of temp folder', parent, 'return code:', retcode) - - _osx_cache_dir = None @@ -279,9 +265,13 @@ class SpooledTemporaryFile(tempfile.SpooledTemporaryFile): suffix = '' if dir is None: dir = base_dir() - tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size, - suffix=suffix, prefix=prefix, dir=dir, mode=mode, - bufsize=bufsize) + if ispy3: + tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size, + suffix=suffix, prefix=prefix, dir=dir, mode=mode) + else: + tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size, + suffix=suffix, prefix=prefix, dir=dir, mode=mode, + bufsize=bufsize) def truncate(self, *args): # The stdlib SpooledTemporaryFile implementation of truncate() doesn't