1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Defines the task RPC methods."""
13 #pylint: disable=relative-import
18 class RPCMethods(object):
19 """Class exposing RPC methods."""
21 _dotted_whitelist
= ['subprocess']
23 def __init__(self
, server
):
25 self
.subprocess
= process
.Process
27 def _dispatch(self
, method
, params
):
30 # Allow only white listed dotted names
31 name
, method
= method
.split('.')
32 assert name
in self
._dotted
_whitelist
33 obj
= getattr(self
, name
)
34 return getattr(obj
, method
)(*params
)
36 def Echo(self
, message
):
37 """Simple RPC method to print and return a message."""
38 logging
.info('Echoing %s', message
)
39 return 'echo %s' % str(message
)
41 def AbsPath(self
, path
):
42 """Returns the absolute path."""
43 return os
.path
.abspath(path
)
46 """Call _server.shutdown in another thread.
48 This is needed because server.shutdown waits for the server to actually
49 quit. However the server cannot shutdown until it completes handling this
50 call. Calling this in the same thread results in a deadlock.
52 t
= threading
.Thread(target
=self
._server
.shutdown
)
55 def GetOutputDir(self
):
56 """Returns the isolated output directory on the task machine."""
57 return common_lib
.GetOutputDir()
59 def WriteFile(self
, path
, text
, mode
='wb+'):
60 """Writes a file on the task machine."""
61 with
open(path
, mode
) as fh
:
64 def ReadFile(self
, path
, mode
='rb'):
65 """Reads a file from the local task machine."""
66 with
open(path
, mode
) as fh
:
69 def PathJoin(self
, *parts
):
70 """Performs an os.path.join on the task machine.
72 This is needed due to the fact that there is no guarantee that os.sep will
73 be the same across all machines in a particular test. This method will
74 join the path parts locally to ensure the correct separator is used.
76 return os
.path
.join(*parts
)
78 def ListDir(self
, path
):
79 """Returns the results of os.listdir."""
80 return os
.listdir(path
)
82 def GetIpAddress(self
):
83 """Returns the local IPv4 address."""
84 return socket
.gethostbyname(socket
.gethostname())
86 def GetHostname(self
):
87 """Returns the hostname."""
88 return socket
.gethostname()