summaryrefslogtreecommitdiffstats
path: root/deluge
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2016-10-11 11:26:52 +0100
committerCalum Lind <calumlind+deluge@gmail.com>2016-10-18 18:40:25 +0100
commit9dd3b1617d44d717fc8d2844d40bd61969c1a157 (patch)
treead4d29f211bf4cc5d4642ba50128c48efa6459f3 /deluge
parent58835eeb2ea974962527d9848b18c6560b7ff881 (diff)
downloaddeluge-9dd3b1617d44d717fc8d2844d40bd61969c1a157.tar.gz
deluge-9dd3b1617d44d717fc8d2844d40bd61969c1a157.tar.bz2
deluge-9dd3b1617d44d717fc8d2844d40bd61969c1a157.zip
[#2889] Fixes for 'Too many files open' error
* Ensure all file descriptors are closed. Using the with statement ensures closure. * The main problem was with thousands of unclosed file desciptors from tracker_icons mkstemp. * Use a prefix 'deluge_ticon.' to identify created tracker_icon tmp files.
Diffstat (limited to 'deluge')
-rw-r--r--deluge/common.py24
-rw-r--r--deluge/config.py17
-rw-r--r--deluge/core/core.py19
-rw-r--r--deluge/core/rpcserver.py10
-rw-r--r--deluge/log.py19
-rw-r--r--deluge/maketorrent.py44
-rw-r--r--deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py8
-rw-r--r--deluge/plugins/Blocklist/deluge/plugins/blocklist/detect.py5
-rw-r--r--deluge/plugins/Blocklist/deluge/plugins/blocklist/peerguardian.py3
-rw-r--r--deluge/plugins/Stats/deluge/plugins/stats/tests/test_stats.py5
-rw-r--r--deluge/scripts/create_plugin.py9
-rw-r--r--deluge/tests/test_config.py24
-rw-r--r--deluge/tests/test_core.py37
-rw-r--r--deluge/tests/test_httpdownloader.py24
-rw-r--r--deluge/tests/test_maketorrent.py30
-rw-r--r--deluge/tests/test_torrent.py15
-rw-r--r--deluge/tests/test_torrentmanager.py4
-rw-r--r--deluge/ui/common.py3
-rw-r--r--deluge/ui/console/commands/add.py3
-rw-r--r--deluge/ui/console/commands/plugin.py3
-rw-r--r--deluge/ui/console/modes/add_util.py3
-rw-r--r--deluge/ui/console/modes/addtorrents.py3
-rw-r--r--deluge/ui/console/modes/legacy.py22
-rw-r--r--deluge/ui/gtkui/addtorrentdialog.py3
-rw-r--r--deluge/ui/gtkui/createtorrentdialog.py8
-rw-r--r--deluge/ui/gtkui/ipcinterface.py15
-rw-r--r--deluge/ui/gtkui/preferences.py3
-rw-r--r--deluge/ui/tracker_icons.py43
-rw-r--r--deluge/ui/web/json_api.py7
-rw-r--r--deluge/ui/web/server.py13
30 files changed, 234 insertions, 192 deletions
diff --git a/deluge/common.py b/deluge/common.py
index 4d257a817..7411ae995 100644
--- a/deluge/common.py
+++ b/deluge/common.py
@@ -874,10 +874,9 @@ def create_auth_file():
auth_file = deluge.configmanager.get_config_dir("auth")
# Check for auth file and create if necessary
if not os.path.exists(auth_file):
- fd = open(auth_file, "w")
- fd.flush()
- os.fsync(fd.fileno())
- fd.close()
+ with open(auth_file, "w") as _file:
+ _file.flush()
+ os.fsync(_file.fileno())
# Change the permissions on the file so only this user can read/write it
os.chmod(auth_file, stat.S_IREAD | stat.S_IWRITE)
@@ -891,15 +890,14 @@ def create_localclient_account(append=False):
if not os.path.exists(auth_file):
create_auth_file()
- fd = open(auth_file, "a" if append else "w")
- fd.write(":".join([
- "localclient",
- sha(str(random.random())).hexdigest(),
- str(AUTH_LEVEL_ADMIN)
- ]) + '\n')
- fd.flush()
- os.fsync(fd.fileno())
- fd.close()
+ with open(auth_file, "a" if append else "w") as _file:
+ _file.write(":".join([
+ "localclient",
+ sha(str(random.random())).hexdigest(),
+ str(AUTH_LEVEL_ADMIN)
+ ]) + '\n')
+ _file.flush()
+ os.fsync(_file.fileno())
def set_env_variable(name, value):
diff --git a/deluge/config.py b/deluge/config.py
index abd74a159..9fe700159 100644
--- a/deluge/config.py
+++ b/deluge/config.py
@@ -391,7 +391,8 @@ what is currently in the config and it could not convert the value
filename = self.__config_file
try:
- data = open(filename, "rb").read()
+ with open(filename, "rb") as _file:
+ data = _file.read()
except IOError as ex:
log.warning("Unable to open config file %s: %s", filename, ex)
return
@@ -439,7 +440,8 @@ what is currently in the config and it could not convert the value
# Check to see if the current config differs from the one on disk
# We will only write a new config file if there is a difference
try:
- data = open(filename, "rb").read()
+ with open(filename, "rb") as _file:
+ data = _file.read()
objects = find_json_objects(data)
start, end = objects[0]
version = json.loads(data[start:end])
@@ -456,12 +458,11 @@ what is currently in the config and it could not convert the value
# Save the new config and make sure it's written to disk
try:
log.debug("Saving new config file %s", filename + ".new")
- f = open(filename + ".new", "wb")
- json.dump(self.__version, f, indent=2)
- json.dump(self.__config, f, indent=2)
- f.flush()
- os.fsync(f.fileno())
- f.close()
+ with open(filename + ".new", "wb") as _file:
+ json.dump(self.__version, _file, indent=2)
+ json.dump(self.__config, _file, indent=2)
+ _file.flush()
+ os.fsync(_file.fileno())
except IOError as ex:
log.error("Error writing new config file: %s", ex)
return False
diff --git a/deluge/core/core.py b/deluge/core/core.py
index f558f3f8d..3c0a2a896 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -284,9 +284,8 @@ class Core(component.Component):
def on_download_success(filename):
# We got the file, so add it to the session
- f = open(filename, "rb")
- data = f.read()
- f.close()
+ with open(filename, "rb") as _file:
+ data = _file.read()
try:
os.remove(filename)
except OSError as ex:
@@ -298,9 +297,9 @@ class Core(component.Component):
log.error("Failed to add torrent from url %s", url)
return failure
- d = download_file(
- url, tempfile.mkstemp()[1], headers=headers, force_filename=True
- )
+ tmp_fd, tmp_file = tempfile.mkstemp(prefix='deluge_url.', suffix='.torrent')
+ os.close(tmp_fd)
+ d = download_file(url, tmp_file, headers=headers, force_filename=True)
d.addCallbacks(on_download_success, on_download_fail)
return d
@@ -744,7 +743,8 @@ class Core(component.Component):
if add_to_session:
options = {}
options["download_location"] = os.path.split(path)[0]
- self.add_torrent_file(os.path.split(target)[1], open(target, "rb").read(), options)
+ with open(target, "rb") as _file:
+ self.add_torrent_file(os.path.split(target)[1], _file.read(), options)
@export
def upload_plugin(self, filename, filedump):
@@ -760,9 +760,8 @@ class Core(component.Component):
log.exception(ex)
return
- f = open(os.path.join(get_config_dir(), "plugins", filename), "wb")
- f.write(filedump)
- f.close()
+ with open(os.path.join(get_config_dir(), "plugins", filename), "wb") as _file:
+ _file.write(filedump)
component.get("CorePluginManager").scan_for_plugins()
@export
diff --git a/deluge/core/rpcserver.py b/deluge/core/rpcserver.py
index 3981fc957..8fd6df54c 100644
--- a/deluge/core/rpcserver.py
+++ b/deluge/core/rpcserver.py
@@ -579,12 +579,10 @@ def generate_ssl_keys():
# Write out files
ssl_dir = deluge.configmanager.get_config_dir("ssl")
- open(os.path.join(ssl_dir, "daemon.pkey"), "w").write(
- crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)
- )
- open(os.path.join(ssl_dir, "daemon.cert"), "w").write(
- crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
- )
+ with open(os.path.join(ssl_dir, "daemon.pkey"), "w") as _file:
+ _file.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
+ with open(os.path.join(ssl_dir, "daemon.cert"), "w") as _file:
+ _file.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
# Make the files only readable by this user
for f in ("daemon.pkey", "daemon.cert"):
os.chmod(os.path.join(ssl_dir, f), stat.S_IREAD | stat.S_IWRITE)
diff --git a/deluge/log.py b/deluge/log.py
index 0732f84a0..faf2a011f 100644
--- a/deluge/log.py
+++ b/deluge/log.py
@@ -203,15 +203,16 @@ def tweak_logging_levels():
log = logging.getLogger(__name__)
log.warn("logging.conf found! tweaking logging levels from %s",
logging_config_file)
- for line in open(logging_config_file, "r").readlines():
- if line.strip().startswith("#"):
- continue
- name, level = line.strip().split(":")
- if level not in levels:
- continue
-
- log.warn("Setting logger \"%s\" to logging level \"%s\"", name, level)
- set_logger_level(level, name)
+ with open(logging_config_file, "r") as _file:
+ for line in _file:
+ if line.strip().startswith("#"):
+ continue
+ name, level = line.strip().split(":")
+ if level not in levels:
+ continue
+
+ log.warn("Setting logger \"%s\" to logging level \"%s\"", name, level)
+ set_logger_level(level, name)
def set_logger_level(level, logger_name=None):
diff --git a/deluge/maketorrent.py b/deluge/maketorrent.py
index 15bd763d2..87d5a4e42 100644
--- a/deluge/maketorrent.py
+++ b/deluge/maketorrent.py
@@ -155,20 +155,19 @@ class TorrentMetadata(object):
buf = ""
fs[-1]["attr"] = "p"
else:
- fd = open(os.path.join(self.data_path, *path), "rb")
- r = fd.read(piece_size - len(buf))
- while r:
- buf += r
- if len(buf) == piece_size:
- pieces.append(sha(buf).digest())
- # Run the progress function if necessary
- if progress:
- progress(len(pieces), num_pieces)
- buf = ""
- else:
- break
- r = fd.read(piece_size - len(buf))
- fd.close()
+ with open(os.path.join(self.data_path, *path), "rb") as _file:
+ r = _file.read(piece_size - len(buf))
+ while r:
+ buf += r
+ if len(buf) == piece_size:
+ pieces.append(sha(buf).digest())
+ # Run the progress function if necessary
+ if progress:
+ progress(len(pieces), num_pieces)
+ buf = ""
+ else:
+ break
+ r = _file.read(piece_size - len(buf))
if buf:
pieces.append(sha(buf).digest())
@@ -184,19 +183,20 @@ class TorrentMetadata(object):
torrent["info"]["length"] = get_path_size(self.data_path)
pieces = []
- fd = open(self.data_path, "rb")
- r = fd.read(piece_size)
- while r:
- pieces.append(sha(r).digest())
- if progress:
- progress(len(pieces), num_pieces)
+ with open(self.data_path, "rb") as _file:
+ r = _file.read(piece_size)
+ while r:
+ pieces.append(sha(r).digest())
+ if progress:
+ progress(len(pieces), num_pieces)
- r = fd.read(piece_size)
+ r = _file.read(piece_size)
torrent["info"]["pieces"] = "".join(pieces)
# Write out the torrent file
- open(torrent_path, "wb").write(bencode(torrent))
+ with open(torrent_path, "wb") as _file:
+ _file.write(bencode(torrent))
def get_data_path(self):
"""Get the path to the files that the torrent will contain.
diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
index ae74b02f6..f94588b7c 100644
--- a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
+++ b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
@@ -150,14 +150,14 @@ class Core(CorePluginBase):
try:
log.debug("Attempting to open %s for add.", filename)
if magnet:
- _file = open(filename, "r")
+ with open(filename, "r") as _file:
+ filedump = _file.read()
else:
- _file = open(filename, "rb")
+ with open(filename, "rb") as _file:
+ filedump = _file.read()
- filedump = _file.read()
if not filedump:
raise RuntimeError("Torrent is 0 bytes!")
- _file.close()
except IOError as ex:
log.warning("Unable to open %s: %s", filename, ex)
raise ex
diff --git a/deluge/plugins/Blocklist/deluge/plugins/blocklist/detect.py b/deluge/plugins/Blocklist/deluge/plugins/blocklist/detect.py
index 79fcf2bfb..f39ac7b06 100644
--- a/deluge/plugins/Blocklist/deluge/plugins/blocklist/detect.py
+++ b/deluge/plugins/Blocklist/deluge/plugins/blocklist/detect.py
@@ -34,9 +34,8 @@ class UnknownFormatError(Exception):
def detect_compression(filename):
- f = open(filename, "rb")
- magic_number = f.read(2)
- f.close()
+ with open(filename, "rb") as _file:
+ magic_number = _file.read(2)
return COMPRESSION_TYPES.get(magic_number, "")
diff --git a/deluge/plugins/Blocklist/deluge/plugins/blocklist/peerguardian.py b/deluge/plugins/Blocklist/deluge/plugins/blocklist/peerguardian.py
index 421c5480a..62fe61682 100644
--- a/deluge/plugins/Blocklist/deluge/plugins/blocklist/peerguardian.py
+++ b/deluge/plugins/Blocklist/deluge/plugins/blocklist/peerguardian.py
@@ -27,7 +27,8 @@ class PGReader(object):
log.debug("PGReader loading: %s", filename)
try:
- self.fd = gzip.open(filename, "rb")
+ with gzip.open(filename, "rb") as _file:
+ self.fd = _file
except IOError:
log.debug("Blocklist: PGReader: Incorrect file type or list is corrupt")
diff --git a/deluge/plugins/Stats/deluge/plugins/stats/tests/test_stats.py b/deluge/plugins/Stats/deluge/plugins/stats/tests/test_stats.py
index c9a980368..a9ca8dc6a 100644
--- a/deluge/plugins/Stats/deluge/plugins/stats/tests/test_stats.py
+++ b/deluge/plugins/Stats/deluge/plugins/stats/tests/test_stats.py
@@ -110,6 +110,5 @@ class StatsTestCase(BaseTestCase):
file_like = FakeFile()
surface.write_to_png(file_like)
data = "".join(file_like.data)
- f = open("file_like.png", "wb")
- f.write(data)
- f.close()
+ with open("file_like.png", "wb") as _file:
+ _file.write(data)
diff --git a/deluge/scripts/create_plugin.py b/deluge/scripts/create_plugin.py
index 16033531c..718ed59f9 100644
--- a/deluge/scripts/create_plugin.py
+++ b/deluge/scripts/create_plugin.py
@@ -74,11 +74,10 @@ def create_plugin():
}
filename = os.path.join(path, filename)
- f = open(filename, "w")
- if filename.endswith(".py") and include_gpl:
- f.write(GPL % plugin_args)
- f.write(template % plugin_args)
- f.close()
+ with open(filename, "w") as _file:
+ if filename.endswith(".py") and include_gpl:
+ _file.write(GPL % plugin_args)
+ _file.write(template % plugin_args)
print("creating folders..")
os.mkdir(plugin_base)
diff --git a/deluge/tests/test_config.py b/deluge/tests/test_config.py
index 4f5588445..3be4d0b55 100644
--- a/deluge/tests/test_config.py
+++ b/deluge/tests/test_config.py
@@ -83,32 +83,32 @@ class ConfigTestCase(unittest.TestCase):
# Test loading an old config from 1.1.x
import pickle
- pickle.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb"))
+ with open(os.path.join(self.config_dir, "test.conf"), "wb") as _file:
+ pickle.dump(DEFAULTS, _file)
check_config()
# Test opening a previous 1.2 config file of just a json object
import json
- json.dump(DEFAULTS, open(os.path.join(self.config_dir, "test.conf"), "wb"), indent=2)
+ with open(os.path.join(self.config_dir, "test.conf"), "wb") as _file:
+ json.dump(DEFAULTS, _file, indent=2)
check_config()
# Test opening a previous 1.2 config file of having the format versions
# as ints
- f = open(os.path.join(self.config_dir, "test.conf"), "wb")
- f.write(str(1) + "\n")
- f.write(str(1) + "\n")
- json.dump(DEFAULTS, f, indent=2)
- f.close()
+ with open(os.path.join(self.config_dir, "test.conf"), "wb") as _file:
+ _file.write(str(1) + "\n")
+ _file.write(str(1) + "\n")
+ json.dump(DEFAULTS, _file, indent=2)
check_config()
# Test the 1.2 config format
- v = {"format": 1, "file": 1}
- f = open(os.path.join(self.config_dir, "test.conf"), "wb")
- json.dump(v, f, indent=2)
- json.dump(DEFAULTS, f, indent=2)
- f.close()
+ version = {"format": 1, "file": 1}
+ with open(os.path.join(self.config_dir, "test.conf"), "wb") as _file:
+ json.dump(version, _file, indent=2)
+ json.dump(DEFAULTS, _file, indent=2)
check_config()
diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py
index ac97de180..f23bf06b6 100644
--- a/deluge/tests/test_core.py
+++ b/deluge/tests/test_core.py
@@ -35,13 +35,16 @@ class CookieResource(Resource):
return
request.setHeader("Content-Type", "application/x-bittorrent")
- return open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")).read()
+ with open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")) as _file:
+ data = _file.read()
+ return data
class PartialDownload(Resource):
def render(self, request):
- data = open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")).read()
+ with open(rpath("ubuntu-9.04-desktop-i386.iso.torrent")) as _file:
+ data = _file.read()
request.setHeader("Content-Type", len(data))
request.setHeader("Content-Type", "application/x-bittorrent")
if request.requestHeaders.hasHeader("accept-encoding"):
@@ -109,7 +112,8 @@ class CoreTestCase(BaseTestCase):
files_to_add = []
for f in filenames:
filename = os.path.join(os.path.dirname(__file__), f)
- filedump = base64.encodestring(open(filename).read())
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
files_to_add.append((filename, filedump, options))
errors = yield self.core.add_torrent_files(files_to_add)
self.assertEquals(len(errors), 0)
@@ -121,7 +125,8 @@ class CoreTestCase(BaseTestCase):
files_to_add = []
for f in filenames:
filename = os.path.join(os.path.dirname(__file__), f)
- filedump = base64.encodestring(open(filename).read())
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
files_to_add.append((filename, filedump, options))
errors = yield self.core.add_torrent_files(files_to_add)
self.assertEquals(len(errors), 1)
@@ -131,11 +136,14 @@ class CoreTestCase(BaseTestCase):
def test_add_torrent_file(self):
options = {}
filename = os.path.join(os.path.dirname(__file__), "test.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
# Get the info hash from the test.torrent
from deluge.bencode import bdecode, bencode
- info_hash = sha(bencode(bdecode(open(filename).read())["info"])).hexdigest()
+ with open(filename) as _file:
+ info_hash = sha(bencode(bdecode(_file.read())["info"])).hexdigest()
self.assertEquals(torrent_id, info_hash)
def test_add_torrent_file_invalid_filedump(self):
@@ -196,7 +204,9 @@ class CoreTestCase(BaseTestCase):
def test_remove_torrent(self):
options = {}
filename = os.path.join(os.path.dirname(__file__), "test.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
removed = self.core.remove_torrent(torrent_id, True)
self.assertTrue(removed)
self.assertEquals(len(self.core.get_session_state()), 0)
@@ -208,9 +218,14 @@ class CoreTestCase(BaseTestCase):
def test_remove_torrents(self):
options = {}
filename = os.path.join(os.path.dirname(__file__), "test.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
+
filename2 = os.path.join(os.path.dirname(__file__), "unicode_filenames.torrent")
- torrent_id2 = yield self.core.add_torrent_file(filename2, base64.encodestring(open(filename2).read()), options)
+ with open(filename2) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id2 = yield self.core.add_torrent_file(filename2, filedump, options)
d = self.core.remove_torrents([torrent_id, torrent_id2], True)
def test_ret(val):
@@ -226,7 +241,9 @@ class CoreTestCase(BaseTestCase):
def test_remove_torrents_invalid(self):
options = {}
filename = os.path.join(os.path.dirname(__file__), "test.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
val = yield self.core.remove_torrents(["invalidid1", "invalidid2", torrent_id], False)
self.assertEqual(len(val), 2)
self.assertEqual(val[0], ('invalidid1', "torrent_id 'invalidid1' not in session."))
diff --git a/deluge/tests/test_httpdownloader.py b/deluge/tests/test_httpdownloader.py
index 686b9581a..6b500146a 100644
--- a/deluge/tests/test_httpdownloader.py
+++ b/deluge/tests/test_httpdownloader.py
@@ -137,23 +137,19 @@ class DownloadFileTestCase(unittest.TestCase):
return self.webserver.stopListening()
def assertContains(self, filename, contents): # NOQA
- f = open(filename)
- try:
- self.assertEqual(f.read(), contents)
- except Exception as ex:
- self.fail(ex)
- finally:
- f.close()
+ with open(filename) as _file:
+ try:
+ self.assertEqual(_file.read(), contents)
+ except Exception as ex:
+ self.fail(ex)
return filename
def failIfContains(self, filename, contents): # NOQA
- f = open(filename)
- try:
- self.failIfEqual(f.read(), contents)
- except Exception as ex:
- self.fail(ex)
- finally:
- f.close()
+ with open(filename) as _file:
+ try:
+ self.failIfEqual(_file.read(), contents)
+ except Exception as ex:
+ self.fail(ex)
return filename
def test_download(self):
diff --git a/deluge/tests/test_maketorrent.py b/deluge/tests/test_maketorrent.py
index 8c13a4984..24f8b6f43 100644
--- a/deluge/tests/test_maketorrent.py
+++ b/deluge/tests/test_maketorrent.py
@@ -20,13 +20,16 @@ class MakeTorrentTestCase(unittest.TestCase):
def test_save_multifile(self):
# Create a temporary folder for torrent creation
tmp_path = tempfile.mkdtemp()
- open(os.path.join(tmp_path, "file_A"), "wb").write("a" * (312 * 1024))
- open(os.path.join(tmp_path, "file_B"), "wb").write("b" * (2354 * 1024))
- open(os.path.join(tmp_path, "file_C"), "wb").write("c" * (11 * 1024))
+ with open(os.path.join(tmp_path, "file_A"), "wb") as _file:
+ _file.write("a" * (312 * 1024))
+ with open(os.path.join(tmp_path, "file_B"), "wb") as _file:
+ _file.write("b" * (2354 * 1024))
+ with open(os.path.join(tmp_path, "file_C"), "wb") as _file:
+ _file.write("c" * (11 * 1024))
t = maketorrent.TorrentMetadata()
t.data_path = tmp_path
- tmp_file = tempfile.mkstemp(".torrent")[1]
+ tmp_fd, tmp_file = tempfile.mkstemp(".torrent")
t.save(tmp_file)
check_torrent(tmp_file)
@@ -35,32 +38,38 @@ class MakeTorrentTestCase(unittest.TestCase):
os.remove(os.path.join(tmp_path, "file_B"))
os.remove(os.path.join(tmp_path, "file_C"))
os.rmdir(tmp_path)
+ os.close(tmp_fd)
os.remove(tmp_file)
def test_save_singlefile(self):
tmp_data = tempfile.mkstemp("testdata")[1]
- open(tmp_data, "wb").write("a" * (2314 * 1024))
+ with open(tmp_data, "wb") as _file:
+ _file.write("a" * (2314 * 1024))
t = maketorrent.TorrentMetadata()
t.data_path = tmp_data
- tmp_file = tempfile.mkstemp(".torrent")[1]
+ tmp_fd, tmp_file = tempfile.mkstemp(".torrent")
t.save(tmp_file)
check_torrent(tmp_file)
os.remove(tmp_data)
+ os.close(tmp_fd)
os.remove(tmp_file)
def test_save_multifile_padded(self):
# Create a temporary folder for torrent creation
tmp_path = tempfile.mkdtemp()
- open(os.path.join(tmp_path, "file_A"), "wb").write("a" * (312 * 1024))
- open(os.path.join(tmp_path, "file_B"), "wb").write("b" * (2354 * 1024))
- open(os.path.join(tmp_path, "file_C"), "wb").write("c" * (11 * 1024))
+ with open(os.path.join(tmp_path, "file_A"), "wb") as _file:
+ _file.write("a" * (312 * 1024))
+ with open(os.path.join(tmp_path, "file_B"), "wb") as _file:
+ _file.write("b" * (2354 * 1024))
+ with open(os.path.join(tmp_path, "file_C"), "wb") as _file:
+ _file.write("c" * (11 * 1024))
t = maketorrent.TorrentMetadata()
t.data_path = tmp_path
t.pad_files = True
- tmp_file = tempfile.mkstemp(".torrent")[1]
+ tmp_fd, tmp_file = tempfile.mkstemp(".torrent")
t.save(tmp_file)
check_torrent(tmp_file)
@@ -69,4 +78,5 @@ class MakeTorrentTestCase(unittest.TestCase):
os.remove(os.path.join(tmp_path, "file_B"))
os.remove(os.path.join(tmp_path, "file_C"))
os.rmdir(tmp_path)
+ os.close(tmp_fd)
os.remove(tmp_file)
diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py
index e059ae251..f477c2251 100644
--- a/deluge/tests/test_torrent.py
+++ b/deluge/tests/test_torrent.py
@@ -53,8 +53,8 @@ class TorrentTestCase(BaseTestCase):
def get_torrent_atp(self, filename):
filename = os.path.join(os.path.dirname(__file__), filename)
- e = lt.bdecode(open(filename, 'rb').read())
- info = lt.torrent_info(e)
+ with open(filename, 'rb') as _file:
+ info = lt.torrent_info(lt.bdecode(_file.read()))
atp = {"ti": info}
atp["save_path"] = os.getcwd()
atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse
@@ -118,7 +118,9 @@ class TorrentTestCase(BaseTestCase):
def test_torrent_error_data_missing(self):
options = {"seed_mode": True}
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
torrent = self.core.torrentmanager.torrents[torrent_id]
self.assert_state(torrent, "Seeding")
@@ -132,7 +134,9 @@ class TorrentTestCase(BaseTestCase):
def test_torrent_error_resume_original_state(self):
options = {"seed_mode": True, "add_paused": True}
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, options)
torrent = self.core.torrentmanager.torrents[torrent_id]
orig_state = "Paused"
@@ -175,7 +179,8 @@ class TorrentTestCase(BaseTestCase):
)
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
- filedump = open(filename).read()
+ with open(filename) as _file:
+ filedump = _file.read()
torrent_id = yield self.core.torrentmanager.add(state=torrent_state, filedump=filedump,
resume_data=lt.bencode(resume_data))
torrent = self.core.torrentmanager.torrents[torrent_id]
diff --git a/deluge/tests/test_torrentmanager.py b/deluge/tests/test_torrentmanager.py
index e8a8452e9..ee620b679 100644
--- a/deluge/tests/test_torrentmanager.py
+++ b/deluge/tests/test_torrentmanager.py
@@ -31,7 +31,9 @@ class TorrentmanagerTestCase(BaseTestCase):
@defer.inlineCallbacks
def test_remove_torrent(self):
filename = os.path.join(os.path.dirname(__file__), "test.torrent")
- torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), {})
+ with open(filename) as _file:
+ filedump = base64.encodestring(_file.read())
+ torrent_id = yield self.core.add_torrent_file(filename, filedump, {})
self.assertTrue(self.core.torrentmanager.remove(torrent_id, False))
@pytest.mark.todo
diff --git a/deluge/ui/common.py b/deluge/ui/common.py
index 202785525..0ed96b80c 100644
--- a/deluge/ui/common.py
+++ b/deluge/ui/common.py
@@ -71,7 +71,8 @@ class TorrentInfo(object):
# Get the torrent data from the torrent file
try:
log.debug("Attempting to open %s.", filename)
- self.__m_filedata = open(filename, "rb").read()
+ with open(filename, "rb") as _file:
+ self.__m_filedata = _file.read()
self.__m_metadata = bencode.bdecode(self.__m_filedata)
except Exception as ex:
log.warning("Unable to open %s: %s", filename, ex)
diff --git a/deluge/ui/console/commands/add.py b/deluge/ui/console/commands/add.py
index 1fa157758..6f34aaec6 100644
--- a/deluge/ui/console/commands/add.py
+++ b/deluge/ui/console/commands/add.py
@@ -71,7 +71,8 @@ class Command(BaseCommand):
continue
self.console.write("{!info!}Attempting to add torrent: %s" % path)
filename = os.path.split(path)[-1]
- filedump = base64.encodestring(open(path, "rb").read())
+ with open(path, "rb") as _file:
+ filedump = base64.encodestring(_file.read())
deferreds.append(client.core.add_torrent_file(filename, filedump, t_options).addCallback(
on_success).addErrback(on_fail))
diff --git a/deluge/ui/console/commands/plugin.py b/deluge/ui/console/commands/plugin.py
index 58d9feb6e..56da89d8c 100644
--- a/deluge/ui/console/commands/plugin.py
+++ b/deluge/ui/console/commands/plugin.py
@@ -92,7 +92,8 @@ class Command(BaseCommand):
if not client.is_localhost():
# We need to send this plugin to the daemon
- filedump = base64.encodestring(open(filepath, "rb").read())
+ with open(filepath, "rb") as _file:
+ filedump = base64.encodestring(_file.read())
try:
client.core.upload_plugin(filename, filedump)
client.core.rescan_plugins()
diff --git a/deluge/ui/console/modes/add_util.py b/deluge/ui/console/modes/add_util.py
index e203ed287..88756f495 100644
--- a/deluge/ui/console/modes/add_util.py
+++ b/deluge/ui/console/modes/add_util.py
@@ -75,7 +75,8 @@ def add_torrent(t_file, options, success_cb, fail_cb, ress):
continue
filename = os.path.split(f)[-1]
- filedump = base64.encodestring(open(f).read())
+ with open(f) as _file:
+ filedump = base64.encodestring(_file.read())
client.core.add_torrent_file(
filename, filedump, t_options).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress)
diff --git a/deluge/ui/console/modes/addtorrents.py b/deluge/ui/console/modes/addtorrents.py
index e5abe2825..26a045519 100644
--- a/deluge/ui/console/modes/addtorrents.py
+++ b/deluge/ui/console/modes/addtorrents.py
@@ -410,7 +410,8 @@ class AddTorrents(BaseMode, component.Component):
filename = m
directory = os.path.join(*self.path_stack[:self.path_stack_pos])
path = os.path.join(directory, filename)
- filedump = base64.encodestring(open(path).read())
+ with open(path) as _file:
+ filedump = base64.encodestring(_file.read())
t_options = {}
if result["location"]:
t_options["download_location"] = result["location"]
diff --git a/deluge/ui/console/modes/legacy.py b/deluge/ui/console/modes/legacy.py
index 46b71cae8..00e4c217d 100644
--- a/deluge/ui/console/modes/legacy.py
+++ b/deluge/ui/console/modes/legacy.py
@@ -142,14 +142,16 @@ class Legacy(BaseMode, Commander, component.Component):
if self.console_config["save_legacy_history"]:
try:
- lines1 = open(self.history_file[0], "r").read().splitlines()
+ with open(self.history_file[0], "r") as _file:
+ lines1 = _file.read().splitlines()
self._hf_lines[0] = len(lines1)
except IOError:
lines1 = []
self._hf_lines[0] = 0
try:
- lines2 = open(self.history_file[1], "r").read().splitlines()
+ with open(self.history_file[1], "r") as _file:
+ lines2 = _file.read().splitlines()
self._hf_lines[1] = len(lines2)
except IOError:
lines2 = []
@@ -476,13 +478,11 @@ class Legacy(BaseMode, Commander, component.Component):
active_file = 0
# Write the line
- f = open(self.history_file[active_file], "a")
-
- if isinstance(text, unicode):
- text = text.encode(self.encoding)
- f.write(text)
-
- f.write(os.linesep)
+ with open(self.history_file[active_file], "a") as _file:
+ if isinstance(text, unicode):
+ text = text.encode(self.encoding)
+ _file.write(text)
+ _file.write(os.linesep)
# And increment line counter
self._hf_lines[active_file] += 1
@@ -491,8 +491,8 @@ class Legacy(BaseMode, Commander, component.Component):
# therefore swapping the currently active file
if self._hf_lines[active_file] == MAX_HISTFILE_SIZE:
self._hf_lines[1 - active_file] = 0
- f = open(self.history_file[1 - active_file], "w")
- f.truncate(0)
+ with open(self.history_file[1 - active_file], "w") as _file:
+ _file.truncate(0)
def get_line_chunks(line):
"""
diff --git a/deluge/ui/gtkui/addtorrentdialog.py b/deluge/ui/gtkui/addtorrentdialog.py
index 5dc1fbf89..4b9b4e140 100644
--- a/deluge/ui/gtkui/addtorrentdialog.py
+++ b/deluge/ui/gtkui/addtorrentdialog.py
@@ -623,7 +623,7 @@ class AddTorrentDialog(component.Component):
# Create a tmp file path
import tempfile
- (tmp_handle, tmp_file) = tempfile.mkstemp()
+ tmp_fd, tmp_file = tempfile.mkstemp(prefix='deluge_url.', suffix='.torrent')
def on_part(data, current_length, total_length):
if total_length:
@@ -651,6 +651,7 @@ class AddTorrentDialog(component.Component):
return result
d = download_file(url, tmp_file, on_part)
+ os.close(tmp_fd)
d.addCallbacks(on_download_success, on_download_fail)
def _on_button_hash_clicked(self, widget):
diff --git a/deluge/ui/gtkui/createtorrentdialog.py b/deluge/ui/gtkui/createtorrentdialog.py
index 6cf97c191..5c6fd21ad 100644
--- a/deluge/ui/gtkui/createtorrentdialog.py
+++ b/deluge/ui/gtkui/createtorrentdialog.py
@@ -370,10 +370,10 @@ class CreateTorrentDialog(object):
trackers=trackers)
if add_to_session:
- client.core.add_torrent_file(
- os.path.split(target)[-1],
- base64.encodestring(open(target, "rb").read()),
- {"download_location": os.path.split(path)[0]})
+ with open(target, "rb") as _file:
+ filedump = base64.encodestring(_file.read())
+ client.core.add_torrent_file(os.path.split(target)[-1], filedump,
+ {"download_location": os.path.split(path)[0]})
def _on_create_torrent_progress(self, value, num_pieces):
percent = float(value) / float(num_pieces)
diff --git a/deluge/ui/gtkui/ipcinterface.py b/deluge/ui/gtkui/ipcinterface.py
index 8cd764b9b..0d7a446f6 100644
--- a/deluge/ui/gtkui/ipcinterface.py
+++ b/deluge/ui/gtkui/ipcinterface.py
@@ -94,12 +94,14 @@ class IPCInterface(component.Component):
port = random.randrange(20000, 65535)
self.listener = reactor.listenTCP(port, self.factory)
# Store the port number in the socket file
- open(socket, "w").write(str(port))
+ with open(socket, "w") as _file:
+ _file.write(str(port))
# We need to process any args when starting this process
process_args(args)
else:
# Send to existing deluge process
- port = int(open(socket, "r").readline())
+ with open(socket) as _file:
+ port = int(_file.readline())
self.factory = ClientFactory()
self.factory.args = args
self.factory.protocol = IPCProtocolClient
@@ -108,7 +110,7 @@ class IPCInterface(component.Component):
sys.exit(0)
else:
# Find and remove any restart tempfiles
- restart_tempfile = glob(os.path.join(ipc_dir, 'tmp*deluge'))
+ restart_tempfile = glob(os.path.join(ipc_dir, 'restart.*'))
for f in restart_tempfile:
os.remove(f)
lockfile = socket + ".lock"
@@ -152,7 +154,7 @@ class IPCInterface(component.Component):
else:
log.warning('Restarting Deluge... (%s)', ex)
# Create a tempfile to keep track of restart
- mkstemp('deluge', dir=ipc_dir)
+ mkstemp(prefix='restart.', dir=ipc_dir)
os.execv(sys.argv[0], sys.argv)
else:
process_args(args)
@@ -212,5 +214,6 @@ def process_args(args):
component.get("AddTorrentDialog").add_from_files([path])
component.get("AddTorrentDialog").show(config["focus_add_dialog"])
else:
- client.core.add_torrent_file(os.path.split(path)[-1],
- base64.encodestring(open(path, "rb").read()), None)
+ with open(path, "rb") as _file:
+ filedump = base64.encodestring(_file.read())
+ client.core.add_torrent_file(os.path.split(path)[-1], filedump, None)
diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py
index 4fe1b703e..41390d08e 100644
--- a/deluge/ui/gtkui/preferences.py
+++ b/deluge/ui/gtkui/preferences.py
@@ -996,7 +996,8 @@ class Preferences(component.Component):
if not client.is_localhost():
# We need to send this plugin to the daemon
- filedump = base64.encodestring(open(filepath, "rb").read())
+ with open(filepath, "rb") as _file:
+ filedump = base64.encodestring(_file.read())
client.core.upload_plugin(filename, filedump)
client.core.rescan_plugins()
diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py
index 138426e83..e59a257bf 100644
--- a/deluge/ui/tracker_icons.py
+++ b/deluge/ui/tracker_icons.py
@@ -85,9 +85,8 @@ class TrackerIcon(object):
:rtype: string
"""
if not self.data:
- f = open(self.filename, "rb")
- self.data = f.read()
- f.close()
+ with open(self.filename, "rb") as _file:
+ self.data = _file.read()
return self.data
def get_filename(self, full=True):
@@ -235,7 +234,9 @@ class TrackerIcons(Component):
if not url:
url = self.host_to_url(host)
log.debug("Downloading %s %s", host, url)
- return download_file(url, mkstemp()[1], force_filename=True, handle_redirects=False)
+ tmp_fd, tmp_file = mkstemp(prefix='deluge_ticon.')
+ os.close(tmp_fd)
+ return download_file(url, tmp_file, force_filename=True, handle_redirects=False)
def on_download_page_complete(self, page):
"""
@@ -284,14 +285,13 @@ class TrackerIcons(Component):
:returns: a Deferred which callbacks a list of available favicons (url, type)
:rtype: Deferred
"""
- f = open(page, "r")
- parser = FaviconParser()
- for line in f:
- parser.feed(line)
- if parser.left_head:
- break
- parser.close()
- f.close()
+ with open(page, "r") as _file:
+ parser = FaviconParser()
+ for line in _file:
+ parser.feed(line)
+ if parser.left_head:
+ break
+ parser.close()
try:
os.remove(page)
except OSError as ex:
@@ -364,7 +364,8 @@ class TrackerIcons(Component):
if PIL_INSTALLED:
try:
- Image.open(icon_name)
+ with Image.open(icon_name):
+ pass
except IOError as ex:
raise InvalidIconError(ex)
else:
@@ -441,14 +442,14 @@ class TrackerIcons(Component):
"""
if icon:
filename = icon.get_filename()
- img = Image.open(filename)
- if img.size > (16, 16):
- new_filename = filename.rpartition(".")[0] + ".png"
- img = img.resize((16, 16), Image.ANTIALIAS)
- img.save(new_filename)
- if new_filename != filename:
- os.remove(filename)
- icon = TrackerIcon(new_filename)
+ with Image.open(filename) as img:
+ if img.size > (16, 16):
+ new_filename = filename.rpartition(".")[0] + ".png"
+ img = img.resize((16, 16), Image.ANTIALIAS)
+ img.save(new_filename)
+ if new_filename != filename:
+ os.remove(filename)
+ icon = TrackerIcon(new_filename)
return icon
def store_icon(self, icon, host):
diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py
index 205f03fd3..34cd14efe 100644
--- a/deluge/ui/web/json_api.py
+++ b/deluge/ui/web/json_api.py
@@ -714,7 +714,8 @@ class WebApi(JSONComponent):
deferreds.append(d)
else:
filename = os.path.basename(torrent["path"])
- fdump = base64.encodestring(open(torrent["path"], "rb").read())
+ with open(torrent["path"], "rb") as _file:
+ fdump = base64.encodestring(_file.read())
log.info("Adding torrent from file `%s` with options `%r`",
filename, torrent["options"])
d = client.core.add_torrent_file(filename, fdump, torrent["options"])
@@ -933,8 +934,8 @@ class WebApi(JSONComponent):
if client.is_localhost():
client.core.rescan_plugins()
return True
-
- plugin_data = base64.encodestring(open(path, "rb").read())
+ with open(path, "rb") as _file:
+ plugin_data = base64.encodestring(_file.read())
def on_upload_complete(*args):
client.core.rescan_plugins()
diff --git a/deluge/ui/web/server.py b/deluge/ui/web/server.py
index f0b6131d3..fa4aa2438 100644
--- a/deluge/ui/web/server.py
+++ b/deluge/ui/web/server.py
@@ -183,9 +183,10 @@ class Flag(resource.Resource):
request.setHeader("cache-control",
"public, must-revalidate, max-age=86400")
request.setHeader("content-type", "image/png")
- data = open(filename, "rb")
+ with open(filename, "rb") as _file:
+ data = _file.read()
request.setResponseCode(http.OK)
- return data.read()
+ return data
else:
request.setResponseCode(http.NOT_FOUND)
return ""
@@ -232,7 +233,9 @@ class LookupResource(resource.Resource, component.Component):
log.debug("Serving path: '%s'", path)
mime_type = mimetypes.guess_type(path)
request.setHeader("content-type", mime_type[0])
- return compress(open(path, "rb").read(), request)
+ with open(path, "rb") as _file:
+ data = _file.read()
+ return compress(data, request)
request.setResponseCode(http.NOT_FOUND)
return "<h1>404 - Not Found</h1>"
@@ -390,7 +393,9 @@ class ScriptResource(resource.Resource, component.Component):
log.debug("Serving path: '%s'", path)
mime_type = mimetypes.guess_type(path)
request.setHeader("content-type", mime_type[0])
- return compress(open(path, "rb").read(), request)
+ with open(path, "rb") as _file:
+ data = _file.read()
+ return compress(data, request)
request.setResponseCode(http.NOT_FOUND)
return "<h1>404 - Not Found</h1>"