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
2913774Sandreas.sandberg@arm.comfrom __future__ import print_function
3013774Sandreas.sandberg@arm.comfrom __future__ import absolute_import
3113731Sandreas.sandberg@arm.com
3213731Sandreas.sandberg@arm.comfrom six import string_types
332774SN/Aimport os, sys
342566SN/A
353510Shsul@eecs.umich.educonfig_path = os.path.dirname(os.path.abspath(__file__))
363510Shsul@eecs.umich.educonfig_root = os.path.dirname(config_path)
373510Shsul@eecs.umich.edu
3812233Sgabeblack@google.comclass PathSearchFunc(object):
3912233Sgabeblack@google.com    _sys_paths = None
4010681Ssteve.reinhardt@amd.com
4112254Sgabeblack@google.com    def __init__(self, subdirs, sys_paths=None):
4213731Sandreas.sandberg@arm.com        if isinstance(subdirs, string_types):
4312254Sgabeblack@google.com            subdirs = [subdirs]
4412233Sgabeblack@google.com        self._subdir = os.path.join(*subdirs)
4512254Sgabeblack@google.com        if sys_paths:
4612254Sgabeblack@google.com            self._sys_paths = sys_paths
472566SN/A
4812233Sgabeblack@google.com    def __call__(self, filename):
4912233Sgabeblack@google.com        if self._sys_paths is None:
5012233Sgabeblack@google.com            try:
5112233Sgabeblack@google.com                paths = os.environ['M5_PATH'].split(':')
5212233Sgabeblack@google.com            except KeyError:
5312233Sgabeblack@google.com                paths = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]
542902SN/A
5512233Sgabeblack@google.com            # expand '~' and '~user' in paths
5612233Sgabeblack@google.com            paths = map(os.path.expanduser, paths)
572902SN/A
5812233Sgabeblack@google.com            # filter out non-existent directories
5912233Sgabeblack@google.com            paths = filter(os.path.isdir, paths)
6012233Sgabeblack@google.com
6112233Sgabeblack@google.com            if not paths:
6213731Sandreas.sandberg@arm.com                raise IOError("Can't find a path to system files.")
6312233Sgabeblack@google.com
6413731Sandreas.sandberg@arm.com            self._sys_paths = list(paths)
6512233Sgabeblack@google.com
6612233Sgabeblack@google.com        filepath = os.path.join(self._subdir, filename)
6712233Sgabeblack@google.com        paths = (os.path.join(p, filepath) for p in self._sys_paths)
682774SN/A        try:
6912233Sgabeblack@google.com            return next(p for p in paths if os.path.exists(p))
7012233Sgabeblack@google.com        except StopIteration:
7113731Sandreas.sandberg@arm.com            raise IOError("Can't find file '%s' on path." % filename)
722566SN/A
7312233Sgabeblack@google.comdisk = PathSearchFunc('disks')
7412233Sgabeblack@google.combinary = PathSearchFunc('binaries')
7512254Sgabeblack@google.comscript = PathSearchFunc('boot', sys_paths=[config_root])
76