Commit 02abac66 authored by Jason R. Coombs's avatar Jason R. Coombs

Use context managers to reliably close files

parent 95ca7733
...@@ -183,14 +183,11 @@ def has_powershell(): ...@@ -183,14 +183,11 @@ def has_powershell():
if platform.system() != 'Windows': if platform.system() != 'Windows':
return False return False
cmd = ['powershell', '-Command', 'echo test'] cmd = ['powershell', '-Command', 'echo test']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_powershell.viable = has_powershell download_file_powershell.viable = has_powershell
...@@ -201,14 +198,11 @@ def download_file_curl(url, target): ...@@ -201,14 +198,11 @@ def download_file_curl(url, target):
def has_curl(): def has_curl():
cmd = ['curl', '--version'] cmd = ['curl', '--version']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_curl.viable = has_curl download_file_curl.viable = has_curl
...@@ -219,14 +213,11 @@ def download_file_wget(url, target): ...@@ -219,14 +213,11 @@ def download_file_wget(url, target):
def has_wget(): def has_wget():
cmd = ['wget', '--version'] cmd = ['wget', '--version']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_wget.viable = has_wget download_file_wget.viable = has_wget
...@@ -240,19 +231,16 @@ def download_file_insecure(url, target): ...@@ -240,19 +231,16 @@ def download_file_insecure(url, target):
from urllib.request import urlopen from urllib.request import urlopen
except ImportError: except ImportError:
from urllib2 import urlopen from urllib2 import urlopen
src = dst = None src = urlopen(url)
try: try:
src = urlopen(url) # Read all the data in one block.
# Read/write all in one block, so we don't create a corrupt file
# if the download is interrupted.
data = src.read() data = src.read()
dst = open(target, "wb")
dst.write(data)
finally: finally:
if src: src.close()
src.close()
if dst: # Write all the data in one block to avoid creating a partial file.
dst.close() with open(target, "wb") as dst:
dst.write(data)
download_file_insecure.viable = lambda: True download_file_insecure.viable = lambda: True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment