[Android WebViewShell] Add inclusion test for webview exposed stable interfaces.
[chromium-blink-merge.git] / testing / legion / rpc_methods.py
blobb846269bb2cd41dbb20f2779e5e5c000e09f2678
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."""
7 import logging
8 import os
9 import socket
10 import sys
11 import threading
13 #pylint: disable=relative-import
14 import common_lib
15 import process
18 class RPCMethods(object):
19 """Class exposing RPC methods."""
21 _dotted_whitelist = ['subprocess']
23 def __init__(self, server):
24 self._server = server
25 self.subprocess = process.Process
27 def _dispatch(self, method, params):
28 obj = self
29 if '.' in method:
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)
45 def Quit(self):
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.
51 """
52 t = threading.Thread(target=self._server.shutdown)
53 t.start()
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:
62 fh.write(text)
64 def ReadFile(self, path, mode='rb'):
65 """Reads a file from the local task machine."""
66 with open(path, mode) as fh:
67 return fh.read()
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.
75 """
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()