OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 posixpath | 5 import posixpath |
6 | 6 |
7 | 7 |
8 # TODO(kalman): Write a Path class and use that everywhere rather than a | 8 # TODO(kalman): Write a Path class and use that everywhere rather than a |
9 # utility class. | 9 # utility class. |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 '''Returns the parent directory and base name of |path| in a tuple. | 38 '''Returns the parent directory and base name of |path| in a tuple. |
39 Any trailing slash of |path| is preserved, such that the parent of | 39 Any trailing slash of |path| is preserved, such that the parent of |
40 '/hello/world/' is '/hello' and the base is 'world/'. | 40 '/hello/world/' is '/hello' and the base is 'world/'. |
41 ''' | 41 ''' |
42 parent, base = posixpath.split(path.rstrip('/')) | 42 parent, base = posixpath.split(path.rstrip('/')) |
43 if path.endswith('/'): | 43 if path.endswith('/'): |
44 base += '/' | 44 base += '/' |
45 return parent, base | 45 return parent, base |
46 | 46 |
47 | 47 |
| 48 def Split(path): |
| 49 '''Returns a list of the directories and filename in a path. 'p1/p2/p3' |
| 50 will return ['p1/', 'p2/', 'p3']. |
| 51 ''' |
| 52 AssertIsValid(path) |
| 53 names = [name + '/' for name in path.rstrip('/').split('/')] |
| 54 if names and not path.endswith('/'): |
| 55 names[-1] = names[-1][:-1] |
| 56 return names |
| 57 |
| 58 |
48 def ToDirectory(path): | 59 def ToDirectory(path): |
49 '''Returns a string representing |path| as a directory, that is, | 60 '''Returns a string representing |path| as a directory, that is, |
50 IsDirectory(result) is True (and does not fail assertions). If |path| is | 61 IsDirectory(result) is True (and does not fail assertions). If |path| is |
51 already a directory then this is a no-op. | 62 already a directory then this is a no-op. |
52 ''' | 63 ''' |
53 return path if IsDirectory(path) else (path + '/') | 64 return path if IsDirectory(path) else (path + '/') |
54 | 65 |
55 | 66 |
56 def AssertIsDirectory(path): | 67 def AssertIsDirectory(path): |
57 assert IsDirectory(path), '"%s" is not a directory' % path | 68 assert IsDirectory(path), '"%s" is not a directory' % path |
(...skipping 17 matching lines...) Expand all Loading... |
75 last_path = posixpath.join(last_path, segment) | 86 last_path = posixpath.join(last_path, segment) |
76 rel_path = posixpath.relpath(path, last_path) | 87 rel_path = posixpath.relpath(path, last_path) |
77 | 88 |
78 # Don't let relpath say the filename is '.' | 89 # Don't let relpath say the filename is '.' |
79 if rel_path == '.': | 90 if rel_path == '.': |
80 rel_path = '' | 91 rel_path = '' |
81 else: | 92 else: |
82 last_path = ToDirectory(last_path) | 93 last_path = ToDirectory(last_path) |
83 | 94 |
84 yield (last_path, rel_path) | 95 yield (last_path, rel_path) |
OLD | NEW |