Fix up the importing in server/hosts to not be so fragile. Importing
utils from these modules was just a bad idea (it depends on the server
dir being in sys.path) and the circular references between some of the
modules are making things unnecessarily fragile.

The main changes are:
    - converted all of the imports from the server directory and from
      the common lib into absolute imports (i.e. from autotest_lib.*)
    - moved the RemoteHost definition into a remote.py file, so that
      SiteHost can subclass Host without introducing a circular
      dependency between base_classes and site_host

Signed-off-by: John Admanski <jadmanski@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@1417 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/server/autoserv b/server/autoserv
index 4f32495..a0915c4 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -10,10 +10,10 @@
 mbligh@google.com (Martin J. Bligh)
 """
 
-from common.check_version import check_python_version
-check_python_version()
+import sys, os, re, traceback, signal
 
-import sys, os, re, server_job, hosts.site_host, utils, traceback, signal
+import common
+from autotest_lib.server import server_job, utils
 
 
 # send stdin to /dev/null
diff --git a/server/hosts/__init__.py b/server/hosts/__init__.py
index f3c250b..de0e541 100644
--- a/server/hosts/__init__.py
+++ b/server/hosts/__init__.py
@@ -15,8 +15,7 @@
 
 # host abstract classes
 from base_classes import Host
-from base_classes import SiteHost
-from base_classes import RemoteHost
+from remote import SiteHost, RemoteHost
 
 # host implementation classes
 from ssh_host import SSHHost
diff --git a/server/hosts/base_classes.py b/server/hosts/base_classes.py
index 826a2ef..6054612 100644
--- a/server/hosts/base_classes.py
+++ b/server/hosts/base_classes.py
@@ -19,7 +19,10 @@
 """
 
 import time
-import bootloader, utils
+
+from autotest_lib.server import utils
+import bootloader
+
 
 class Host(object):
 	"""
@@ -93,36 +96,3 @@
 
 	def get_autodir(self):
 		return None
-
-
-# site_host.py may be non-existant or empty, make sure that an appropriate 
-# SiteHost class is created nevertheless
-try:
-	from site_host import SiteHost
-except ImportError:
-	pass
-
-if not "SiteHost" in dir():
-	class SiteHost(Host):
-		def __init__(self):
-			super(SiteHost, self).__init__()
-
-
-class RemoteHost(SiteHost):
-	"""This class represents a remote machine on which you can run 
-	programs.
-
-	It may be accessed through a network, a serial line, ...
-	It is not the machine autoserv is running on.
-
-	Implementation details:
-	This is an abstract class, leaf subclasses must implement the methods
-	listed here and in parent classes which have no implementation. They 
-	may reimplement methods which already have an implementation. You 
-	must not instantiate this class but should instantiate one of those 
-	leaf subclasses."""
-
-	hostname= None
-
-	def __init__(self):
-		super(RemoteHost, self).__init__()
diff --git a/server/hosts/bootloader.py b/server/hosts/bootloader.py
index 3bc4dc3..0b9ebeb 100644
--- a/server/hosts/bootloader.py
+++ b/server/hosts/bootloader.py
@@ -18,9 +18,8 @@
 import sys
 import weakref
 
-import utils
-
-from common.error import *
+from autotest_lib.client.common_lib import error
+from autotest_lib.server import utils
 
 
 BOOTTOOL_SRC = '../client/tools/boottool'  # Get it from autotest client
@@ -172,7 +171,8 @@
 
 	def install_boottool(self):
 		if self.__host() is None:
-			raise AutoservError("Host does not exist anymore")
+			raise error.AutoservError(
+			    "Host does not exist anymore")
 		tmpdir = self.__host().get_tmp_dir()
 		self.__host().send_file(os.path.abspath(os.path.join(
 			utils.get_server_dir(), BOOTTOOL_SRC)), tmpdir)
diff --git a/server/hosts/ssh_host.py b/server/hosts/ssh_host.py
index 45fedb6..8277a36 100644
--- a/server/hosts/ssh_host.py
+++ b/server/hosts/ssh_host.py
@@ -18,13 +18,15 @@
 """
 
 
-import types, os, sys, signal, subprocess, time, re, socket
-import base_classes, utils, bootloader
-from common.error import *
+import types, os, sys, signal, subprocess, time, re, socket, pdb
+
+from autotest_lib.client.common_lib import error
+from autotest_lib.server import utils
+import remote, bootloader
 
 
 
-class SSHHost(base_classes.RemoteHost):
+class SSHHost(remote.RemoteHost):
 	"""
 	This class represents a remote machine controlled through an ssh 
 	session on which you can run programs.
@@ -109,7 +111,7 @@
 		for dir in self.tmp_dirs:
 			try:
 				self.run('rm -rf "%s"' % (utils.sh_escape(dir)))
-			except AutoservRunError:
+			except error.AutoservRunError:
 				pass
 		# kill the console logger
 		if getattr(self, 'logger_popen', None):
@@ -134,7 +136,7 @@
 		# Get the gateway of the remote machine
 		try:
 			traceroute = self.run('traceroute -n %s' % local_ip)
-		except AutoservRunError:
+		except error.AutoservRunError:
 			return
 		first_node = traceroute.stdout.split("\n")[0]
 		match = re.search(r'\s+((\d+\.){3}\d+)\s+', first_node)
@@ -146,7 +148,7 @@
 		try:
 			self.run('ping -c 1 %s' % router_ip)
 			arp = self.run('arp -n -a %s' % router_ip)
-		except AutoservRunError:
+		except error.AutoservRunError:
 			return
 		match = re.search(r'\s+(([0-9A-F]{2}:){5}[0-9A-F]{2})\s+', arp.stdout)
 		if match:
@@ -182,7 +184,7 @@
 			return
 		try:
 			self.run('modprobe netconsole %s' % self.__netconsole_param)
-		except AutoservRunError:
+		except error.AutoservRunError:
 			# if it fails there isn't much we can do, just keep going
 			pass
 
@@ -190,21 +192,23 @@
 	def __unload_netconsole_module(self):
 		try:
 			self.run('modprobe -r netconsole')
-		except AutoservRunError:
+		except error.AutoservRunError:
 			pass
 
 
 	def wait_for_restart(self, timeout=DEFAULT_REBOOT_TIMEOUT):
 		if not self.wait_down(300):	# Make sure he's dead, Jim
 			self.__record("ABORT", None, "reboot.verify", "shutdown failed")
-			raise AutoservRebootError("Host did not shut down")
+			raise error.AutoservRebootError(
+			    "Host did not shut down")
 		self.wait_up(timeout)
 		time.sleep(2) # this is needed for complete reliability
 		if self.wait_up(timeout):
 			self.__record("GOOD", None, "reboot.verify")
 		else:
 			self.__record("ABORT", None, "reboot.verify", "Host did not return from reboot")
-			raise AutoservRebootError("Host did not return from reboot")
+			raise error.AutoservRebootError(
+			    "Host did not return from reboot")
 		print "Reboot complete"
 
 
@@ -214,7 +218,8 @@
 		"""
 		if not self.__console_run(r"'~$hardreset'"):
 			self.__record("ABORT", None, "reboot.start", "hard reset unavailable")
-			raise AutoservUnsupportedError('Hard reset unavailable')
+			raise error.AutoservUnsupportedError(
+			    'Hard reset unavailable')
 
 		if wait:
 			self.wait_for_restart(timeout)
@@ -324,11 +329,11 @@
 		if result.exit_status == 255:  # ssh's exit status for timeout
 			if re.match(r'^ssh: connect to host .* port .*: ' +
 			            r'Connection timed out\r$', result.stderr):
-				raise AutoservSSHTimeout("ssh timed out",
-				                         result)
+				raise error.AutoservSSHTimeout("ssh timed out",
+							       result)
 		if not ignore_status and result.exit_status > 0:
-			raise AutoservRunError("command execution error",
-			                       result)
+			raise error.AutoservRunError("command execution error",
+						     result)
 		return result
 
 
@@ -394,7 +399,7 @@
 			if regexp and stream:
 				err_re = re.compile (regexp)
 				if err_re.search(stream):
-					raise AutoservRunError(
+					raise error.AutoservRunError(
 					    '%s failed, found error pattern: '
 					    '"%s"' % (command, regexp), result)
 
@@ -407,8 +412,8 @@
 						return
 
 		if not ignore_status and result.exit_status > 0:
-			raise AutoservRunError("command execution error",
-					       result)
+			raise error.AutoservRunError("command execution error",
+						     result)
 
 
 	def reboot(self, timeout=DEFAULT_REBOOT_TIMEOUT, label=None,
@@ -442,7 +447,7 @@
 		self.__record("GOOD", None, "reboot.start")
 		try:
 			self.run('(sleep 5; reboot) </dev/null >/dev/null 2>&1 &')
-		except AutoservRunError:
+		except error.AutoservRunError:
 			self.__record("ABORT", None, "reboot.start",
 				      "reboot command failed")
 			raise
@@ -664,11 +669,11 @@
 			print "Performing a hardreset on %s" % self.hostname
 			try:
 				self.hardreset()
-			except AutoservUnsupportedError:
+			except error.AutoservUnsupportedError:
 				print "Hardreset is unsupported on %s" % self.hostname
 		if not self.wait_up(60 * 30):
 			# 30 minutes should be more than enough
-			raise AutoservHostError
+			raise error.AutoservHostError
 		print 'Host up, continuing'
 
 
@@ -694,7 +699,7 @@
 		Check that uptime is available and monotonically increasing.
 		"""
 		if not self.ping():
-			raise AutoservHostError('Client is not pingable')
+			raise error.AutoservHostError('Client is not pingable')
 		result = self.run("/bin/cat /proc/uptime", 30)
 		return result.stdout.strip().split()[0]
 
diff --git a/server/server_job.py b/server/server_job.py
index bc39916..14b1ec6 100755
--- a/server/server_job.py
+++ b/server/server_job.py
@@ -140,7 +140,7 @@
 			client
 				True if a client-side control file
 		"""
-		path = os.path.dirname(sys.modules['server_job'].__file__)
+		path = os.path.dirname(__file__)
 		self.autodir = os.path.abspath(os.path.join(path, '..'))
 		self.serverdir = os.path.join(self.autodir, 'server')
 		self.testdir   = os.path.join(self.serverdir, 'tests')
diff --git a/server/utils.py b/server/utils.py
index b271678..480e8ec 100644
--- a/server/utils.py
+++ b/server/utils.py
@@ -13,7 +13,6 @@
 """
 
 import atexit, os, re, shutil, textwrap, sys, tempfile, types, urllib
-import hosts
 from common.utils import *
 
 
@@ -263,6 +262,3 @@
 				return ret
 		else:
 			return default
-
-
-