OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import posixpath |
| 6 |
| 7 |
| 8 # TODO(kalman): Write a Path class and use that everywhere rather than a |
| 9 # utility class. |
| 10 |
| 11 |
| 12 def IsDirectory(path): |
| 13 '''Returns whether |path| should be considered a directory. |
| 14 ''' |
| 15 # This assertion is sprinkled throughout the code base. |
| 16 assert not path.startswith('/'), path |
| 17 return path == '' or path.endswith('/') |
| 18 |
| 19 |
| 20 def SplitParent(path): |
| 21 '''Returns the parent directory and base name of |path| in a tuple. |
| 22 Any trailing slash of |path| is preserved, such that the parent of |
| 23 '/hello/world/' is '/hello' and the base is 'world/'. |
| 24 ''' |
| 25 parent, base = posixpath.split(path.rstrip('/')) |
| 26 if path.endswith('/'): |
| 27 base += '/' |
| 28 return parent, base |
| 29 |
| 30 |
| 31 def ToDirectory(path): |
| 32 '''Returns a string representing |path| as a directory, that is, |
| 33 IsDirectory(result) is True (and does not fail assertions). If |path| is |
| 34 already a directory then this is a no-op. |
| 35 ''' |
| 36 return path if IsDirectory(path) else (path + '/') |
OLD | NEW |