Package gmisclib :: Module find_home
[frames] | no frames]

Source Code for Module gmisclib.find_home

 1  """This lets you find files with just a path name relative to where 
 2  the program's executable code script sits.""" 
 3   
 4  import sys 
 5   
 6  _scriptname = sys.argv[0]       # Possibly, this is wrong, if we are importing 
 7                                  # late in execution, after some other module 
 8                                  # has fiddled with sys.argv. 
 9                                  # If so, sorry! 
10   
11  import os 
12   
13  __version__ = "$Revision: 1.6 $" 
14   
15   
16 -def _searchdir(pathlist, s, access):
17 for t in pathlist: 18 if t == '': 19 t = '.' 20 tmp = '%s/%s' % (t, s) 21 if os.access(tmp, access) and os.path.isdir(tmp): 22 return tmp 23 raise RuntimeError, "Can't find %s" % s
24 25
26 -def _search(pathlist, s, access):
27 for t in pathlist: 28 if t == '': 29 t = '.' 30 tmp = '%s/%s' % (t, s) 31 if os.access(tmp, access): 32 return tmp 33 raise RuntimeError, "Can't find %s" % s
34 35
36 -def executable(s, general=True):
37 """Find an executable program. 38 It searches first in the directory of the currently executing 39 python script. Optionally, it then looks at PATH. 40 If general=False, the function should fail unless 41 the executable is in the same directory as the currently 42 executing python script.""" 43 p = [ os.path.dirname( _scriptname ) ] 44 45 if general: 46 p.extend( os.environ['PATH'].split(':') ) 47 48 return _search(p, s, os.X_OK)
49 50 51
52 -def module(s, general=True):
53 """Find a module. Returns pathname.""" 54 p = [ os.path.dirname( _scriptname ) ] 55 56 if general: 57 p.extend( sys.path ) 58 59 return _search(p, "%s.py" % s, os.R_OK)
60 61
62 -def os_prgm(nm, general=1):
63 """Find an operating-system dependent executable program.""" 64 return executable('bin__' + os.uname()[0] + '/' + nm, general)
65 66 67
68 -def data(s):
69 """Find a data file.""" 70 p = [ '.', os.path.dirname( _scriptname ) ] 71 return _search(p, s, os.R_OK)
72 73 74
75 -def directory(s):
76 """Find a directory.""" 77 p = [ os.path.dirname( _scriptname ) ] 78 return _searchdir(p, s, os.X_OK)
79 80 81 82 83 if __name__ == '__main__': 84 assert data("find_home.py") 85