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.
4
5"""Some utility methods for getting and manipulating paths."""
6
7# TODO(pamg): Have the buildbot use these, too.
8
9
10import errno
11import os
12import sys
13
14class PathNotFound(Exception): pass
15
16def ScriptDir():
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)
20
21def FindAncestor(start_dir, ancestor):
22  """Finds an ancestor dir in a path.
23
24  For example, FindAncestor('c:\foo\bar\baz', 'bar') would return
25  'c:\foo\bar'.  Unlike FindUpward*, this only looks at direct path ancestors.
26  """
27  start_dir = os.path.abspath(start_dir)
28  path = start_dir
29  while True:
30    (parent, tail) = os.path.split(path)
31    if tail == ancestor:
32      return path
33    if not tail:
34      break
35    path = parent
36  raise PathNotFound("Unable to find ancestor %s in %s" % (ancestor, start_dir))
37
38def FindUpwardParent(start_dir, *desired_list):
39  """Finds the desired object's parent, searching upward from the start_dir.
40
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.
45  """
46  desired_path = os.path.join(*desired_list)
47  last_dir = ''
48  cur_dir = start_dir
49  found_path = os.path.join(cur_dir, desired_path)
50  while not os.path.exists(found_path):
51    last_dir = cur_dir
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]
62  return found_path
63
64
65def FindUpward(start_dir, *desired_list):
66  """Returns a path to the desired directory or file, searching upward.
67
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.
71  """
72  parent = FindUpwardParent(start_dir, *desired_list)
73  return os.path.join(parent, *desired_list)
74
75
76def MaybeMakeDirectory(*path):
77  """Creates an entire path, if it doesn't already exist."""
78  file_path = os.path.join(*path)
79  try:
80    os.makedirs(file_path)
81  except OSError, e:
82    # errno.EEXIST is "File exists".  If we see another error, re-raise.
83    if e.errno != errno.EEXIST:
84      raise
85