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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/tools/android_mojo_shell.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/devtools/common/devtoolslib/shell_arguments.py
diff --git a/mojo/devtools/common/devtoolslib/shell_arguments.py b/mojo/devtools/common/devtoolslib/shell_arguments.py
new file mode 100644
index 0000000000000000000000000000000000000000..bf70f89300949e3af3333c156a9a006a3f8a111a
--- /dev/null
+++ b/mojo/devtools/common/devtoolslib/shell_arguments.py
@@ -0,0 +1,65 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Functions that configure the shell before it is run manipulating its argument
+list."""
+
+import urlparse
+
+_MAP_ORIGIN_PREFIX = '--map-origin='
+# When spinning up servers for local origins, we want to use predictable ports
+# so that caching works between subsequent runs with the same command line.
+_MAP_ORIGIN_BASE_PORT = 31338
+
+
+def _IsMapOrigin(arg):
+ """Returns whether |arg| is a --map-origin argument."""
+ return arg.startswith(_MAP_ORIGIN_PREFIX)
+
+
+def _Split(l, pred):
+ positive = []
+ negative = []
+ for v in l:
+ if pred(v):
+ positive.append(v)
+ else:
+ negative.append(v)
+ return (positive, negative)
+
+
+def _RewriteMapOriginParameter(shell, mapping, device_port):
+ parts = mapping[len(_MAP_ORIGIN_PREFIX):].split('=')
+ if len(parts) != 2:
+ return mapping
+ dest = parts[1]
+ # If the destination is a url, don't map it.
+ if urlparse.urlparse(dest)[0]:
+ return mapping
+ # Assume the destination is a local directory and serve it.
+ localUrl = shell.ServeLocalDirectory(dest, device_port)
+ print 'started server at %s for %s' % (dest, localUrl)
+ return _MAP_ORIGIN_PREFIX + parts[0] + '=' + localUrl
+
+
+def RewriteMapOriginParameters(shell, original_arguments):
+ """Spawns a server for each local destination indicated in a map-origin
+ argument in |original_arguments| and rewrites it to point to the server url.
+ The arguments other than "map-origin" and "map-origin" arguments pointing to
+ web urls are left intact.
+
+ Args:
+ shell: The shell that is being configured.
+ original_arguments: List of arguments to be rewritten.
+
+ Returns:
+ The updated argument list.
+ """
+ map_arguments, other_arguments = _Split(original_arguments, _IsMapOrigin)
+ arguments = other_arguments
+ next_port = _MAP_ORIGIN_BASE_PORT
+ for mapping in sorted(map_arguments):
+ arguments.append(_RewriteMapOriginParameter(shell, mapping, next_port))
+ next_port += 1
+ return arguments
« 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