srt_path_util.py
Go to the documentation of this file.
1 #
2 # path_util.py
3 #
4 # Some nice path utilities... really, these should have been part of the posixpath module in python.
5 #
6 # Created November 1997 Gordon Watts (Brown)
7 #
8 
9 import os
10 import sys
11 import string
12 import regex
13 import regsub
14 
15 import srt_ntbashutil
16 
17 #
18 # get_relative_path
19 #
20 # Simple dude to extract a relative path from two paths
21 #
22 def get_relative_path (the_path, base_path):
23 
24  #
25  # Dumb special case
26  #
27 
28  if the_path == base_path:
29  return "."
30 
31  #
32  # There is a bug in commonprefix... it doesn't do it by directory, but, rather
33  # by string... so it can cut off in the middle of a directory (!!!!!). Actually,
34  # the docs say that that is what it is meant to do, but geez!
35  #
36 
37  common_path = os.path.commonprefix ([the_path, base_path])
38  index = len(common_path)
39  index = string.rfind (the_path[:index+1], "\\")
40  if index == -1:
41  index = 0
42 
43  common_path = the_path[:index]
44  l_path = the_path[index+1:]
45  l_base = base_path[index+1:]
46 
47  #
48  # Make sure they had something in common, or this isn't going to work!
49  #
50 
51  if common_path == "":
52  raise "Path '" + the_path + "' and base path '" + base_path + "' have nothing in common, so cannot be made relative!"
53 
54  #
55  # Add a set of ".."s to the start of l_path for each directory there is in
56  # l_base. That should do it!
57  #
58 
59  while (l_base != "\\") and (l_base != ""):
60  (l_base, fragment) = os.path.split (l_base)
61  l_path = "..\\" + l_path
62 
63  return l_path
64 
65 #
66 # find_file_in_PATH
67 #
68 # Search the PATH environ var for a certian file. Report back the filename if we find it, a null
69 # string if we can't.
70 #
71 def find_file_in_PATH (filename):
72  return find_file_in_search_string (filename, os.environ["PATH"])
73 
74 #
75 # find_file_in_search_string
76 #
77 # Find a file in a given search string. The seperator is the default for this platform (os.pathsep).
78 #
79 def find_file_in_search_string (filename, search_dir_string):
80  search_list = string.split (search_dir_string, os.pathsep)
81  search_list.insert (0, "./")
82 
83  for search_path in search_list:
84  file_to_look_for = search_path + os.sep + filename
85  if os.path.exists (file_to_look_for):
86  return file_to_look_for
87 
88 #
89 # change_to_type
90 #
91 # Change the file extension of a given path
92 #
93 def change_to_type (old_path, new_type):
94  (base_dir, fname) = os.path.split (old_path)
95  new_fname = regsub.gsub("\.[^\.]*$", "." + new_type, fname)
96  return os.path.join (base_dir, new_fname)
97 
98 #
99 # file_type
100 #
101 # Return the filetype of the handed path.
102 #
103 def file_type (the_path):
104  (base_dir, fname) = os.path.split (the_path)
105  temp = regex.compile (".*\.\([^\.]*\)$")
106  if temp.match(the_path) != -1:
107  return temp.group(1)
108  return ""
109 
110 #
111 # path_to_unix
112 #
113 # Convert from NT to unix path -- just change the \ to /s
114 #
115 def path_to_unix (the_path):
116  return regsub.gsub("\\\\", "/", the_path)
def find_file_in_search_string(filename, search_dir_string)
def file_type(the_path)
def path_to_unix(the_path)
def get_relative_path(the_path, base_path)
def find_file_in_PATH(filename)
def change_to_type(old_path, new_type)