Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 Google Inc. All rights reserved. | 1 # Copyright (c) 2012 Google Inc. 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 from __future__ import with_statement | 5 from __future__ import with_statement |
| 6 | 6 |
| 7 import errno | 7 import errno |
| 8 import filecmp | 8 import filecmp |
| 9 import os.path | 9 import os.path |
| 10 import re | 10 import re |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 fully_qualified = fully_qualified + '#' + toolset | 120 fully_qualified = fully_qualified + '#' + toolset |
| 121 return fully_qualified | 121 return fully_qualified |
| 122 | 122 |
| 123 | 123 |
| 124 @memoize | 124 @memoize |
| 125 def RelativePath(path, relative_to): | 125 def RelativePath(path, relative_to): |
| 126 # Assuming both |path| and |relative_to| are relative to the current | 126 # Assuming both |path| and |relative_to| are relative to the current |
| 127 # directory, returns a relative path that identifies path relative to | 127 # directory, returns a relative path that identifies path relative to |
| 128 # relative_to. | 128 # relative_to. |
| 129 | 129 |
| 130 # Convert to absolute (and therefore normalized paths). | 130 # Convert to absolute (and therefore normalized paths). |
|
Nico
2012/12/21 22:52:08
Is the comment now out of date?
| |
| 131 path = os.path.abspath(path) | 131 path = os.path.realpath(path) |
| 132 relative_to = os.path.abspath(relative_to) | 132 relative_to = os.path.realpath(relative_to) |
| 133 | 133 |
| 134 # Split the paths into components. | 134 # Split the paths into components. |
| 135 path_split = path.split(os.path.sep) | 135 path_split = path.split(os.path.sep) |
| 136 relative_to_split = relative_to.split(os.path.sep) | 136 relative_to_split = relative_to.split(os.path.sep) |
| 137 | 137 |
| 138 # Determine how much of the prefix the two paths share. | 138 # Determine how much of the prefix the two paths share. |
| 139 prefix_len = len(os.path.commonprefix([path_split, relative_to_split])) | 139 prefix_len = len(os.path.commonprefix([path_split, relative_to_split])) |
| 140 | 140 |
| 141 # Put enough ".." components to back up out of relative_to to the common | 141 # Put enough ".." components to back up out of relative_to to the common |
| 142 # prefix, and then append the part of path_split after the common prefix. | 142 # prefix, and then append the part of path_split after the common prefix. |
| 143 relative_split = [os.path.pardir] * (len(relative_to_split) - prefix_len) + \ | 143 relative_split = [os.path.pardir] * (len(relative_to_split) - prefix_len) + \ |
| 144 path_split[prefix_len:] | 144 path_split[prefix_len:] |
| 145 | 145 |
| 146 if len(relative_split) == 0: | 146 if len(relative_split) == 0: |
| 147 # The paths were the same. | 147 # The paths were the same. |
| 148 return '' | 148 return '' |
| 149 | 149 |
| 150 # Turn it back into a string and we're done. | 150 # Turn it back into a string and we're done. |
| 151 return os.path.join(*relative_split) | 151 return os.path.join(*relative_split) |
| 152 | 152 |
| 153 | 153 |
| 154 @memoize | |
| 155 def InvertRelativePath(path, toplevel_dir=None): | |
| 156 """Given a path like foo/bar that is relative to toplevel_dir, return | |
| 157 the inverse relative path back to the toplevel_dir. | |
| 158 | |
| 159 E.g. os.path.normpath(os.path.join(path, InvertRelativePath(path))) | |
| 160 should always produce the empty string, unless the path contains symlinks. | |
| 161 """ | |
| 162 if not path: | |
| 163 return path | |
| 164 toplevel_dir = '.' if toplevel_dir is None else toplevel_dir | |
| 165 return RelativePath(toplevel_dir, os.path.join(toplevel_dir, path)) | |
| 166 | |
| 167 | |
| 154 def FixIfRelativePath(path, relative_to): | 168 def FixIfRelativePath(path, relative_to): |
| 155 # Like RelativePath but returns |path| unchanged if it is absolute. | 169 # Like RelativePath but returns |path| unchanged if it is absolute. |
| 156 if os.path.isabs(path): | 170 if os.path.isabs(path): |
| 157 return path | 171 return path |
| 158 return RelativePath(path, relative_to) | 172 return RelativePath(path, relative_to) |
| 159 | 173 |
| 160 | 174 |
| 161 def UnrelativePath(path, relative_to): | 175 def UnrelativePath(path, relative_to): |
| 162 # Assuming that |relative_to| is relative to the current directory, and |path| | 176 # Assuming that |relative_to| is relative to the current directory, and |path| |
| 163 # is a path relative to the dirname of |relative_to|, returns a path that | 177 # is a path relative to the dirname of |relative_to|, returns a path that |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 return | 478 return |
| 465 visited.add(node) | 479 visited.add(node) |
| 466 visiting.add(node) | 480 visiting.add(node) |
| 467 for neighbor in get_edges(node): | 481 for neighbor in get_edges(node): |
| 468 Visit(neighbor) | 482 Visit(neighbor) |
| 469 visiting.remove(node) | 483 visiting.remove(node) |
| 470 ordered_nodes.insert(0, node) | 484 ordered_nodes.insert(0, node) |
| 471 for node in sorted(graph): | 485 for node in sorted(graph): |
| 472 Visit(node) | 486 Visit(node) |
| 473 return ordered_nodes | 487 return ordered_nodes |
| OLD | NEW |