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."""
14 #pylint: disable=relative-import
19 class RPCMethods(object):
20 """Class exposing RPC methods."""
22 _dotted_whitelist
= ['subprocess']
24 def __init__(self
, server
):
26 self
.subprocess
= process
.Process
28 def _dispatch(self
, method
, params
):
31 # Allow only white listed dotted names
32 name
, method
= method
.split('.')
33 assert name
in self
._dotted
_whitelist
34 obj
= getattr(self
, name
)
35 return getattr(obj
, method
)(*params
)
37 def Echo(self
, message
):
38 """Simple RPC method to print and return a message."""
39 logging
.info('Echoing %s', message
)
40 return 'echo %s' % str(message
)
42 def AbsPath(self
, path
):
43 """Returns the absolute path."""
44 return os
.path
.abspath(path
)
47 """Call _server.shutdown in another thread.
49 This is needed because server.shutdown waits for the server to actually
50 quit. However the server cannot shutdown until it completes handling this
51 call. Calling this in the same thread results in a deadlock.
53 t
= threading
.Thread(target
=self
._server
.shutdown
)
56 def GetOutputDir(self
):
57 """Returns the isolated output directory on the task machine."""
58 return common_lib
.GetOutputDir()
60 def WriteFile(self
, path
, text
, mode
='wb+'):
61 """Writes a file on the task machine."""
62 with
open(path
, mode
) as fh
:
65 def ReadFile(self
, path
, mode
='rb'):
66 """Reads a file from the local task machine."""
67 with
open(path
, mode
) as fh
:
70 def PathJoin(self
, *parts
):
71 """Performs an os.path.join on the task machine.
73 This is needed due to the fact that there is no guarantee that os.sep will
74 be the same across all machines in a particular test. This method will
75 join the path parts locally to ensure the correct separator is used.
77 return os
.path
.join(*parts
)
79 def ListDir(self
, path
):
80 """Returns the results of os.listdir."""
81 return os
.listdir(path
)
83 def GetIpAddress(self
):
84 """Returns the local IPv4 address."""
85 return socket
.gethostbyname(socket
.gethostname())
87 def GetHostname(self
):
88 """Returns the hostname."""
89 return socket
.gethostname()
91 def GetPlatform(self
):
92 """Returns the value of platform.platform()."""
93 return platform
.platform()
95 def GetExecutable(self
):
96 """Returns the value of sys.executable."""
100 """Returns the value of os.getcwd()."""