SysPaths.py revision 12254
14202Sbinkertn@umich.edu# Copyright (c) 2006 The Regents of The University of Michigan 24202Sbinkertn@umich.edu# All rights reserved. 34202Sbinkertn@umich.edu# 44202Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without 54202Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are 64202Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright 74202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer; 84202Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright 94202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the 104202Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution; 114202Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its 124202Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from 134202Sbinkertn@umich.edu# this software without specific prior written permission. 144202Sbinkertn@umich.edu# 154202Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 164202Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 174202Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 184202Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 194202Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 204202Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 214202Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 224202Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 234202Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 244202Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 254202Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 264202Sbinkertn@umich.edu# 274202Sbinkertn@umich.edu# Authors: Ali Saidi 284202Sbinkertn@umich.edu 294202Sbinkertn@umich.eduimport os, sys 304202Sbinkertn@umich.edu 314202Sbinkertn@umich.educonfig_path = os.path.dirname(os.path.abspath(__file__)) 324202Sbinkertn@umich.educonfig_root = os.path.dirname(config_path) 334486Sbinkertn@umich.edu 344486Sbinkertn@umich.educlass PathSearchFunc(object): 354486Sbinkertn@umich.edu _sys_paths = None 364486Sbinkertn@umich.edu 374486Sbinkertn@umich.edu def __init__(self, subdirs, sys_paths=None): 384202Sbinkertn@umich.edu if isinstance(subdirs, basestring): 394202Sbinkertn@umich.edu subdirs = [subdirs] 404202Sbinkertn@umich.edu self._subdir = os.path.join(*subdirs) 414202Sbinkertn@umich.edu if sys_paths: 424202Sbinkertn@umich.edu self._sys_paths = sys_paths 434202Sbinkertn@umich.edu 444202Sbinkertn@umich.edu def __call__(self, filename): 454202Sbinkertn@umich.edu if self._sys_paths is None: 464202Sbinkertn@umich.edu try: 474202Sbinkertn@umich.edu paths = os.environ['M5_PATH'].split(':') 484202Sbinkertn@umich.edu except KeyError: 494202Sbinkertn@umich.edu paths = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ] 504202Sbinkertn@umich.edu 514202Sbinkertn@umich.edu # expand '~' and '~user' in paths 52 paths = map(os.path.expanduser, paths) 53 54 # filter out non-existent directories 55 paths = filter(os.path.isdir, paths) 56 57 if not paths: 58 raise IOError, "Can't find a path to system files." 59 60 self._sys_paths = paths 61 62 filepath = os.path.join(self._subdir, filename) 63 paths = (os.path.join(p, filepath) for p in self._sys_paths) 64 try: 65 return next(p for p in paths if os.path.exists(p)) 66 except StopIteration: 67 raise IOError, "Can't find file '%s' on path." % filename 68 69disk = PathSearchFunc('disks') 70binary = PathSearchFunc('binaries') 71script = PathSearchFunc('boot', sys_paths=[config_root]) 72