| OLD | NEW |
| (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 |
| OLD | NEW |