Risk: Medium - Hits quite a bit of different areas
Visibility: Low
utils.py
Create a wrapper for urlopen and urlretrieve to enforce a timeout
using the socket module and setsockettimeout. 

- All additional files outside of utils.py are using urllib/urllib2 in
one way or another and are being changed to use our wrapper

Signed-off-by: Scott Zawalski <scottz@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@1590 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
index c9e8370..c5f9933 100644
--- a/client/common_lib/utils.py
+++ b/client/common_lib/utils.py
@@ -3,7 +3,7 @@
 # Copyright 2008 Google Inc. Released under the GPL v2
 
 import os, pickle, random, re, select, shutil, signal, StringIO, subprocess
-import sys, time, textwrap, urllib, urlparse
+import socket, sys, time, textwrap, urllib, urlparse
 import error, barrier
 
 
@@ -75,6 +75,29 @@
 	return (url_parts[0] in ('http', 'ftp'))
 
 
+def urlopen(url, data=None, proxies=None, timeout=300):
+	"""Wrapper to urllib.urlopen with timeout addition."""
+
+	# Save old timeout
+	old_timeout = socket.getdefaulttimeout()
+	socket.setdefaulttimeout(timeout)
+	try:
+		return urllib.urlopen(url, data=data, proxies=proxies)
+	finally:
+		socket.setdefaulttimeout(old_timeout)
+
+
+def urlretrieve(url, filename=None, reporthook=None, data=None, timeout=300):
+	"""Wrapper to urllib.urlretrieve with timeout addition."""
+	old_timeout = socket.getdefaulttimeout()
+	socket.setdefaulttimeout(timeout)
+	try:
+		return urllib.urlretrieve(url, filename=filename,
+		                          reporthook=reporthook, data=data)
+	finally:
+		socket.setdefaulttimeout(old_timeout)
+	
+
 def get_file(src, dest, permissions=None):
 	"""Get a file from src, which can be local or a remote URL"""
 	if (src == dest):