30,31d29
< from os.path import join as joinpath
< from os import environ as env
36,41c34,35
< def searchpath(path, filename):
< for p in path:
< f = joinpath(p, filename)
< if os.path.exists(f):
< return f
< raise IOError, "Can't find file '%s' on path." % filename
---
> class PathSearchFunc(object):
> _sys_paths = None
43,45c37,38
< def disk(filename):
< system()
< return searchpath(disk.path, filename)
---
> def __init__(self, *subdirs):
> self._subdir = os.path.join(*subdirs)
47,49c40,45
< def binary(filename):
< system()
< return searchpath(binary.path, filename)
---
> def __call__(self, filename):
> if self._sys_paths is None:
> try:
> paths = os.environ['M5_PATH'].split(':')
> except KeyError:
> paths = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]
51,53c47,48
< def script(filename):
< system()
< return searchpath(script.path, filename)
---
> # expand '~' and '~user' in paths
> paths = map(os.path.expanduser, paths)
55,60c50,51
< def system():
< if not system.path:
< try:
< path = env['M5_PATH'].split(':')
< except KeyError:
< path = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]
---
> # filter out non-existent directories
> paths = filter(os.path.isdir, paths)
62,63c53,54
< # expand '~' and '~user' in paths
< path = map(os.path.expanduser, path)
---
> if not paths:
> raise IOError, "Can't find a path to system files."
65,66c56
< # filter out non-existent directories
< system.path = filter(os.path.isdir, path)
---
> self._sys_paths = paths
68,69c58,63
< if not system.path:
< raise IOError, "Can't find a path to system files."
---
> filepath = os.path.join(self._subdir, filename)
> paths = (os.path.join(p, filepath) for p in self._sys_paths)
> try:
> return next(p for p in paths if os.path.exists(p))
> except StopIteration:
> raise IOError, "Can't find file '%s' on path." % filename
71,81c65,67
< if not binary.path:
< binary.path = [joinpath(p, 'binaries') for p in system.path]
< if not disk.path:
< disk.path = [joinpath(p, 'disks') for p in system.path]
< if not script.path:
< script.path = [joinpath(config_root, 'boot')]
<
< system.path = None
< binary.path = None
< disk.path = None
< script.path = None
---
> disk = PathSearchFunc('disks')
> binary = PathSearchFunc('binaries')
> script = PathSearchFunc('boot')