SysPaths.py revision 12233
12931Sktlim@umich.edu# Copyright (c) 2006 The Regents of The University of Michigan 22931Sktlim@umich.edu# All rights reserved. 32931Sktlim@umich.edu# 42931Sktlim@umich.edu# Redistribution and use in source and binary forms, with or without 52931Sktlim@umich.edu# modification, are permitted provided that the following conditions are 62931Sktlim@umich.edu# met: redistributions of source code must retain the above copyright 72931Sktlim@umich.edu# notice, this list of conditions and the following disclaimer; 82931Sktlim@umich.edu# redistributions in binary form must reproduce the above copyright 92931Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the 102931Sktlim@umich.edu# documentation and/or other materials provided with the distribution; 112931Sktlim@umich.edu# neither the name of the copyright holders nor the names of its 122931Sktlim@umich.edu# contributors may be used to endorse or promote products derived from 132931Sktlim@umich.edu# this software without specific prior written permission. 142931Sktlim@umich.edu# 152931Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 162931Sktlim@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 172931Sktlim@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 182931Sktlim@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 192931Sktlim@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 202931Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 212931Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222931Sktlim@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232931Sktlim@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242931Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 252931Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262931Sktlim@umich.edu# 272931Sktlim@umich.edu# Authors: Ali Saidi 282931Sktlim@umich.edu 292774SN/Aimport os, sys 302566SN/A 313510Shsul@eecs.umich.educonfig_path = os.path.dirname(os.path.abspath(__file__)) 323510Shsul@eecs.umich.educonfig_root = os.path.dirname(config_path) 333510Shsul@eecs.umich.edu 3412233Sgabeblack@google.comclass PathSearchFunc(object): 3512233Sgabeblack@google.com _sys_paths = None 3610681Ssteve.reinhardt@amd.com 3712233Sgabeblack@google.com def __init__(self, *subdirs): 3812233Sgabeblack@google.com self._subdir = os.path.join(*subdirs) 392566SN/A 4012233Sgabeblack@google.com def __call__(self, filename): 4112233Sgabeblack@google.com if self._sys_paths is None: 4212233Sgabeblack@google.com try: 4312233Sgabeblack@google.com paths = os.environ['M5_PATH'].split(':') 4412233Sgabeblack@google.com except KeyError: 4512233Sgabeblack@google.com paths = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ] 462902SN/A 4712233Sgabeblack@google.com # expand '~' and '~user' in paths 4812233Sgabeblack@google.com paths = map(os.path.expanduser, paths) 492902SN/A 5012233Sgabeblack@google.com # filter out non-existent directories 5112233Sgabeblack@google.com paths = filter(os.path.isdir, paths) 5212233Sgabeblack@google.com 5312233Sgabeblack@google.com if not paths: 5412233Sgabeblack@google.com raise IOError, "Can't find a path to system files." 5512233Sgabeblack@google.com 5612233Sgabeblack@google.com self._sys_paths = paths 5712233Sgabeblack@google.com 5812233Sgabeblack@google.com filepath = os.path.join(self._subdir, filename) 5912233Sgabeblack@google.com paths = (os.path.join(p, filepath) for p in self._sys_paths) 602774SN/A try: 6112233Sgabeblack@google.com return next(p for p in paths if os.path.exists(p)) 6212233Sgabeblack@google.com except StopIteration: 6312233Sgabeblack@google.com raise IOError, "Can't find file '%s' on path." % filename 642566SN/A 6512233Sgabeblack@google.comdisk = PathSearchFunc('disks') 6612233Sgabeblack@google.combinary = PathSearchFunc('binaries') 6712233Sgabeblack@google.comscript = PathSearchFunc('boot') 68