1 # Copyright (c) 2011 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.
5 """Some utility methods for getting and manipulating paths."""
7 # TODO(pamg): Have the buildbot use these, too.
14 class PathNotFound(Exception): pass
17 """Get the full path to the directory containing the current script."""
18 script_filename
= os
.path
.abspath(sys
.argv
[0])
19 return os
.path
.dirname(script_filename
)
21 def FindAncestor(start_dir
, ancestor
):
22 """Finds an ancestor dir in a path.
24 For example, FindAncestor('c:\foo\bar\baz', 'bar') would return
25 'c:\foo\bar'. Unlike FindUpward*, this only looks at direct path ancestors.
27 start_dir
= os
.path
.abspath(start_dir
)
30 (parent
, tail
) = os
.path
.split(path
)
36 raise PathNotFound("Unable to find ancestor %s in %s" % (ancestor
, start_dir
))
38 def FindUpwardParent(start_dir
, *desired_list
):
39 """Finds the desired object's parent, searching upward from the start_dir.
41 Searches start_dir and all its parents looking for the desired directory
42 or file, which may be given in one or more path components. Returns the
43 first directory in which the top desired path component was found, or raises
44 PathNotFound if it wasn't.
46 desired_path
= os
.path
.join(*desired_list
)
49 found_path
= os
.path
.join(cur_dir
, desired_path
)
50 while not os
.path
.exists(found_path
):
52 cur_dir
= os
.path
.dirname(cur_dir
)
53 if last_dir
== cur_dir
:
54 raise PathNotFound('Unable to find %s above %s' %
55 (desired_path
, start_dir
))
56 found_path
= os
.path
.join(cur_dir
, desired_path
)
57 # Strip the entire original desired path from the end of the one found
58 # and remove a trailing path separator, if present.
59 found_path
= found_path
[:len(found_path
) - len(desired_path
)]
60 if found_path
.endswith(os
.sep
):
61 found_path
= found_path
[:len(found_path
) - 1]
65 def FindUpward(start_dir
, *desired_list
):
66 """Returns a path to the desired directory or file, searching upward.
68 Searches start_dir and all its parents looking for the desired directory
69 or file, which may be given in one or more path components. Returns the full
70 path to the desired object, or raises PathNotFound if it wasn't found.
72 parent
= FindUpwardParent(start_dir
, *desired_list
)
73 return os
.path
.join(parent
, *desired_list
)
76 def MaybeMakeDirectory(*path
):
77 """Creates an entire path, if it doesn't already exist."""
78 file_path
= os
.path
.join(*path
)
80 os
.makedirs(file_path
)
82 # errno.EEXIST is "File exists". If we see another error, re-raise.
83 if e
.errno
!= errno
.EEXIST
: