🐛 Backport TemporaryDirectory to Py2

This commit is contained in:
Gina Häußge committed 2020-06-16 11:42:39 +02:00
1 parent 4689e87a1f
commit 1903a7a319
3 files changed
+31 -4

No files matched your search

@@ -14,7 +14,7 @@ from octoprint.settings import valid_boolean_trues
from octoprint.server.util.flask import no_firstrun_access, with_revalidation_checking, check_etag
from octoprint.access import ADMIN_GROUP
from octoprint.access.permissions import Permissions
from octoprint.util import to_bytes
from octoprint.util import to_bytes, TemporaryDirectory
from octoprint.util.pip import LocalPipCaller
from octoprint.util.version import get_octoprint_version_string, get_octoprint_version, is_octoprint_compatible, is_python_compatible
from octoprint.util.platform import get_os, is_os_compatible
@@ -456,7 +456,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
if url is not None:
# fetch URL
folder = tempfile.TemporaryDirectory()
folder = TemporaryDirectory()
path = download_file(url, folder.name)
source = url
source_type = "url"
@@ -4,13 +4,13 @@ from __future__ import absolute_import, division, print_function, unicode_litera
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
import tempfile
import logging
import shutil
import os
import ast
import io
from octoprint.util import TemporaryDirectory
from octoprint.util.net import download_file
from octoprint.settings import settings
from .. import exceptions
@@ -43,7 +43,7 @@ def perform_update(target, check, target_version, log_cb=None, online=True, forc
try:
try:
_log_message("Download file from {}".format(url))
folder = tempfile.TemporaryDirectory()
folder = TemporaryDirectory()
path = download_file(url, folder.name)
except Exception as exc:
raise exceptions.NetworkError(cause=exc)
+27
View File
@@ -1100,6 +1100,33 @@ def temppath(prefix=None, suffix=""):
os.remove(temp.name)
if hasattr(tempfile, "TemporaryDirectory"):
# Python 3
TemporaryDirectory = tempfile.TemporaryDirectory
else:
# Python 2
class TemporaryDirectory(object):
def __init__(self, suffix='', prefix="tmp", dir=None):
self._path = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
@property
def name(self):
return self._path
def cleanup(self):
try:
os.remove(self._path)
except Exception as exc:
logging.getLogger(__name__).warning("Could not delete temporary directory {}: {}".format(self.name,
exc))
def __enter__(self):
return self.name
def __exit__(self):
self.cleanup()
def bom_aware_open(filename, encoding="ascii", mode="r", **kwargs):
import codecs