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 11 matching lines...) Expand all Loading... |
22 Paths may not start with /, must be posix paths, and for sanity shouldn't | 22 Paths may not start with /, must be posix paths, and for sanity shouldn't |
23 repeat the path separator //. | 23 repeat the path separator //. |
24 ''' | 24 ''' |
25 return not path.startswith('/') and not '\\' in path and not '//' in path | 25 return not path.startswith('/') and not '\\' in path and not '//' in path |
26 | 26 |
27 | 27 |
28 def AssertIsValid(path): | 28 def AssertIsValid(path): |
29 assert IsValid(path), 'Path "%s" is invalid' % path | 29 assert IsValid(path), 'Path "%s" is invalid' % path |
30 | 30 |
31 | 31 |
| 32 def Join(*paths): |
| 33 assert all(IsValid(path) for path in paths), paths |
| 34 return posixpath.join(*paths) |
| 35 |
| 36 |
32 def SplitParent(path): | 37 def SplitParent(path): |
33 '''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. |
34 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 |
35 '/hello/world/' is '/hello' and the base is 'world/'. | 40 '/hello/world/' is '/hello' and the base is 'world/'. |
36 ''' | 41 ''' |
37 parent, base = posixpath.split(path.rstrip('/')) | 42 parent, base = posixpath.split(path.rstrip('/')) |
38 if path.endswith('/'): | 43 if path.endswith('/'): |
39 base += '/' | 44 base += '/' |
40 return parent, base | 45 return parent, base |
41 | 46 |
42 | 47 |
43 def ToDirectory(path): | 48 def ToDirectory(path): |
44 '''Returns a string representing |path| as a directory, that is, | 49 '''Returns a string representing |path| as a directory, that is, |
45 IsDirectory(result) is True (and does not fail assertions). If |path| is | 50 IsDirectory(result) is True (and does not fail assertions). If |path| is |
46 already a directory then this is a no-op. | 51 already a directory then this is a no-op. |
47 ''' | 52 ''' |
48 return path if IsDirectory(path) else (path + '/') | 53 return path if IsDirectory(path) else (path + '/') |
49 | 54 |
50 | 55 |
51 def AssertIsDirectory(path): | 56 def AssertIsDirectory(path): |
52 assert IsDirectory(path), '"%s" is not a directory' % path | 57 assert IsDirectory(path), '"%s" is not a directory' % path |
53 | 58 |
54 | 59 |
55 def AssertIsFile(path): | 60 def AssertIsFile(path): |
56 assert not IsDirectory(path), '"%s" is not a file' % path | 61 assert not IsDirectory(path), '"%s" is not a file' % path |
OLD | NEW |