Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: mojo/devtools/common/devtoolslib/android_shell.py

Issue 1139053005: Add ServeLocalDirectory to the shell abstraction. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Address Ben's comments. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/devtools/common/devtoolslib/http_server.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import atexit 5 import atexit
6 import itertools 6 import itertools
7 import json 7 import json
8 import logging 8 import logging
9 import os 9 import os
10 import os.path 10 import os.path
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 "tcp:%d" % host_port])) 135 "tcp:%d" % host_port]))
136 136
137 unmap_command = self._CreateADBCommand(["reverse", "--remove", 137 unmap_command = self._CreateADBCommand(["reverse", "--remove",
138 "tcp:%d" % device_port]) 138 "tcp:%d" % device_port])
139 139
140 def _UnmapPort(): 140 def _UnmapPort():
141 subprocess.Popen(unmap_command) 141 subprocess.Popen(unmap_command)
142 atexit.register(_UnmapPort) 142 atexit.register(_UnmapPort)
143 return device_port 143 return device_port
144 144
145 def _StartHttpServerForDirectory(self, path, port=0):
146 """Starts an http server serving files from |path|. Returns the local
147 url.
148 """
149 assert path
150 print 'starting http for', path
151 server_address = StartHttpServer(path)
152
153 print 'local port=%d' % server_address[1]
154 return 'http://127.0.0.1:%d/' % self._MapPort(port, server_address[1])
155
156 def _StartHttpServerForOriginMapping(self, mapping, port): 145 def _StartHttpServerForOriginMapping(self, mapping, port):
157 """If |mapping| points at a local file starts an http server to serve files 146 """If |mapping| points at a local file starts an http server to serve files
158 from the directory and returns the new mapping. 147 from the directory and returns the new mapping.
159 148
160 This is intended to be called for every --map-origin value. 149 This is intended to be called for every --map-origin value.
161 """ 150 """
162 parts = mapping.split('=') 151 parts = mapping.split('=')
163 if len(parts) != 2: 152 if len(parts) != 2:
164 return mapping 153 return mapping
165 dest = parts[1] 154 dest = parts[1]
166 # If the destination is a url, don't map it. 155 # If the destination is a url, don't map it.
167 if urlparse.urlparse(dest)[0]: 156 if urlparse.urlparse(dest)[0]:
168 return mapping 157 return mapping
169 # Assume the destination is a local file. Start a local server that 158 # Assume the destination is a local file. Start a local server that
170 # redirects to it. 159 # redirects to it.
171 localUrl = self._StartHttpServerForDirectory(dest, port) 160 localUrl = self.ServeLocalDirectory(dest, port)
172 print 'started server at %s for %s' % (dest, localUrl) 161 print 'started server at %s for %s' % (dest, localUrl)
173 return parts[0] + '=' + localUrl 162 return parts[0] + '=' + localUrl
174 163
175 def _StartHttpServerForOriginMappings(self, map_parameters, fixed_port): 164 def _StartHttpServerForOriginMappings(self, map_parameters, fixed_port):
176 """Calls _StartHttpServerForOriginMapping for every --map-origin 165 """Calls _StartHttpServerForOriginMapping for every --map-origin
177 argument. 166 argument.
178 """ 167 """
179 if not map_parameters: 168 if not map_parameters:
180 return [] 169 return []
181 170
(...skipping 30 matching lines...) Expand all
212 self._CreateADBCommand(['install', '-r', shell_apk_path, '-i', 201 self._CreateADBCommand(['install', '-r', shell_apk_path, '-i',
213 MOJO_SHELL_PACKAGE_NAME]), 202 MOJO_SHELL_PACKAGE_NAME]),
214 stdout=self.verbose_pipe) 203 stdout=self.verbose_pipe)
215 204
216 def SetUpLocalOrigin(self, local_dir, fixed_port=True): 205 def SetUpLocalOrigin(self, local_dir, fixed_port=True):
217 """Sets up a local http server to serve files in |local_dir| along with 206 """Sets up a local http server to serve files in |local_dir| along with
218 device port forwarding. Returns the origin flag to be set when running the 207 device port forwarding. Returns the origin flag to be set when running the
219 shell. 208 shell.
220 """ 209 """
221 210
222 origin_url = self._StartHttpServerForDirectory( 211 origin_url = self.ServeLocalDirectory(
223 local_dir, DEFAULT_BASE_PORT if fixed_port else 0) 212 local_dir, DEFAULT_BASE_PORT if fixed_port else 0)
224 return "--origin=" + origin_url 213 return "--origin=" + origin_url
225 214
215 def ServeLocalDirectory(self, local_dir_path, port=0):
216 """Serves the content of the local (host) directory, making it available to
217 the shell under the url returned by the function.
218
219 The server will run on a separate thread until the program terminates. The
220 call returns immediately.
221
222 Args:
223 local_dir_path: path to the directory to be served
224 port: port at which the server will be available to the shell
225
226 Returns:
227 The url that the shell can use to access the content of |local_dir_path|.
228 """
229 assert local_dir_path
230 print 'starting http for', local_dir_path
231 server_address = StartHttpServer(local_dir_path)
232
233 print 'local port=%d' % server_address[1]
234 return 'http://127.0.0.1:%d/' % self._MapPort(port,
235 server_address[1])
236
226 def StartShell(self, 237 def StartShell(self,
227 arguments, 238 arguments,
228 stdout=None, 239 stdout=None,
229 on_application_stop=None, 240 on_application_stop=None,
230 fixed_port=True): 241 fixed_port=True):
231 """Starts the mojo shell, passing it the given arguments. 242 """Starts the mojo shell, passing it the given arguments.
232 243
233 The |arguments| list must contain the "--origin=" arg. SetUpLocalOrigin() 244 The |arguments| list must contain the "--origin=" arg. SetUpLocalOrigin()
234 can be used to set up a local directory on the host machine as origin. 245 can be used to set up a local directory on the host machine as origin.
235 If |stdout| is not None, it should be a valid argument for subprocess.Popen. 246 If |stdout| is not None, it should be a valid argument for subprocess.Popen.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 Returns: 335 Returns:
325 The process responsible for reading the logs. 336 The process responsible for reading the logs.
326 """ 337 """
327 logcat = subprocess.Popen(self._CreateADBCommand([ 338 logcat = subprocess.Popen(self._CreateADBCommand([
328 'logcat', 339 'logcat',
329 '-s', 340 '-s',
330 ' '.join(LOGCAT_TAGS)]), 341 ' '.join(LOGCAT_TAGS)]),
331 stdout=sys.stdout) 342 stdout=sys.stdout)
332 atexit.register(_ExitIfNeeded, logcat) 343 atexit.register(_ExitIfNeeded, logcat)
333 return logcat 344 return logcat
OLDNEW
« no previous file with comments | « no previous file | mojo/devtools/common/devtoolslib/http_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698