SysPaths.py revision 13731
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
2913731Sandreas.sandberg@arm.com
3013731Sandreas.sandberg@arm.comfrom six import string_types
312774SN/Aimport os, sys
322566SN/A
333510Shsul@eecs.umich.educonfig_path = os.path.dirname(os.path.abspath(__file__))
343510Shsul@eecs.umich.educonfig_root = os.path.dirname(config_path)
353510Shsul@eecs.umich.edu
3612233Sgabeblack@google.comclass PathSearchFunc(object):
3712233Sgabeblack@google.com    _sys_paths = None
3810681Ssteve.reinhardt@amd.com
3912254Sgabeblack@google.com    def __init__(self, subdirs, sys_paths=None):
4013731Sandreas.sandberg@arm.com        if isinstance(subdirs, string_types):
4112254Sgabeblack@google.com            subdirs = [subdirs]
4212233Sgabeblack@google.com        self._subdir = os.path.join(*subdirs)
4312254Sgabeblack@google.com        if sys_paths:
4412254Sgabeblack@google.com            self._sys_paths = sys_paths
452566SN/A
4612233Sgabeblack@google.com    def __call__(self, filename):
4712233Sgabeblack@google.com        if self._sys_paths is None:
4812233Sgabeblack@google.com            try:
4912233Sgabeblack@google.com                paths = os.environ['M5_PATH'].split(':')
5012233Sgabeblack@google.com            except KeyError:
5112233Sgabeblack@google.com                paths = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]
522902SN/A
5312233Sgabeblack@google.com            # expand '~' and '~user' in paths
5412233Sgabeblack@google.com            paths = map(os.path.expanduser, paths)
552902SN/A
5612233Sgabeblack@google.com            # filter out non-existent directories
5712233Sgabeblack@google.com            paths = filter(os.path.isdir, paths)
5812233Sgabeblack@google.com
5912233Sgabeblack@google.com            if not paths:
6013731Sandreas.sandberg@arm.com                raise IOError("Can't find a path to system files.")
6112233Sgabeblack@google.com
6213731Sandreas.sandberg@arm.com            self._sys_paths = list(paths)
6312233Sgabeblack@google.com
6412233Sgabeblack@google.com        filepath = os.path.join(self._subdir, filename)
6512233Sgabeblack@google.com        paths = (os.path.join(p, filepath) for p in self._sys_paths)
662774SN/A        try:
6712233Sgabeblack@google.com            return next(p for p in paths if os.path.exists(p))
6812233Sgabeblack@google.com        except StopIteration:
6913731Sandreas.sandberg@arm.com            raise IOError("Can't find file '%s' on path." % filename)
702566SN/A
7112233Sgabeblack@google.comdisk = PathSearchFunc('disks')
7212233Sgabeblack@google.combinary = PathSearchFunc('binaries')
7312254Sgabeblack@google.comscript = PathSearchFunc('boot', sys_paths=[config_root])
74