first draft for getting all the remote, local and others commands into the same class
[tests.git] / system / TestSsh.py
1 #this class is  used for any ssh command and
2 #also for any remote or a local command independently
3 #on which box this must be done.
4 #new TestSsh object take like an argument an instance
5 #of the class where it was created 
6
7 import os.path
8 import utils
9
10 class TestSsh:
11
12     def __init__(self,caller):
13         self.caller=caller
14
15
16     def hostanme(self):
17         return self.caller.hostname()
18     def is_local(self):
19         return self.caller.is_local()
20     def buildname(self):
21         return self.caller.buildname()
22
23     # command gets run on the right box
24     def to_host(self,command):
25         if self.caller.is_local():
26             return command
27         else:
28             return "ssh %s %s"%(self.hostname(),utils.backslash_shell_specials(command))
29
30     def full_command(self,command):
31         return self.to_host(self.caller.host_to_guest(command))
32
33     def run_in_guest (self,command):
34         return utils.system(self.full_command(command))
35     
36     def run_in_host (self,command):
37         return utils.system(self.to_host(command))
38
39     # xxx quick n dirty
40     def run_in_guest_piped (self,local,remote):
41         return utils.system(local+" | "+self.full_command(remote))
42     
43     def run_in_buildname (self,command):
44         if self.is_local():
45             return utils.system(command)
46         ssh_comand="ssh "
47         if self.caller.key:
48             ssh_comand += "-i %s.rsa "%(self.caller.key)
49         ssh_command += "%s/%s"%(self.buildname,utils.backslash_shell_specials(command))
50         return utils.system(ssh_command)
51
52     def copy (self,local_file,recursive=False):
53         if self.is_local():
54             return 0
55         command="scp "
56         if recursive: command += "-r "
57         if self.caller.key:
58             command += "-i %s.rsa "
59         command +="%s %s:%s/%s"%(local_file,self.hostname(),self.buildname,
60                                  os.path.basename(local_file) or ".")
61         return utils.system(command)
62