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

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

Issue 1153213005: Extract argument-rewriting map-origin magic to devtoolslib/shell_arguments. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Better names are better. Created 5 years, 6 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/tools/android_mojo_shell.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 """Functions that configure the shell before it is run manipulating its argument
6 list."""
7
8 import urlparse
9
10 _MAP_ORIGIN_PREFIX = '--map-origin='
11 # When spinning up servers for local origins, we want to use predictable ports
12 # so that caching works between subsequent runs with the same command line.
13 _MAP_ORIGIN_BASE_PORT = 31338
14
15
16 def _IsMapOrigin(arg):
17 """Returns whether |arg| is a --map-origin argument."""
18 return arg.startswith(_MAP_ORIGIN_PREFIX)
19
20
21 def _Split(l, pred):
22 positive = []
23 negative = []
24 for v in l:
25 if pred(v):
26 positive.append(v)
27 else:
28 negative.append(v)
29 return (positive, negative)
30
31
32 def _RewriteMapOriginParameter(shell, mapping, device_port):
33 parts = mapping[len(_MAP_ORIGIN_PREFIX):].split('=')
34 if len(parts) != 2:
35 return mapping
36 dest = parts[1]
37 # If the destination is a url, don't map it.
38 if urlparse.urlparse(dest)[0]:
39 return mapping
40 # Assume the destination is a local directory and serve it.
41 localUrl = shell.ServeLocalDirectory(dest, device_port)
42 print 'started server at %s for %s' % (dest, localUrl)
43 return _MAP_ORIGIN_PREFIX + parts[0] + '=' + localUrl
44
45
46 def RewriteMapOriginParameters(shell, original_arguments):
47 """Spawns a server for each local destination indicated in a map-origin
48 argument in |original_arguments| and rewrites it to point to the server url.
49 The arguments other than "map-origin" and "map-origin" arguments pointing to
50 web urls are left intact.
51
52 Args:
53 shell: The shell that is being configured.
54 original_arguments: List of arguments to be rewritten.
55
56 Returns:
57 The updated argument list.
58 """
59 map_arguments, other_arguments = _Split(original_arguments, _IsMapOrigin)
60 arguments = other_arguments
61 next_port = _MAP_ORIGIN_BASE_PORT
62 for mapping in sorted(map_arguments):
63 arguments.append(_RewriteMapOriginParameter(shell, mapping, next_port))
64 next_port += 1
65 return arguments
OLDNEW
« no previous file with comments | « no previous file | mojo/tools/android_mojo_shell.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698