SysPaths.py revision 10681
113481Sgiacomo.travaglini@arm.com# Copyright (c) 2006 The Regents of The University of Michigan
213481Sgiacomo.travaglini@arm.com# All rights reserved.
313481Sgiacomo.travaglini@arm.com#
413481Sgiacomo.travaglini@arm.com# Redistribution and use in source and binary forms, with or without
513481Sgiacomo.travaglini@arm.com# modification, are permitted provided that the following conditions are
613481Sgiacomo.travaglini@arm.com# met: redistributions of source code must retain the above copyright
713481Sgiacomo.travaglini@arm.com# notice, this list of conditions and the following disclaimer;
813481Sgiacomo.travaglini@arm.com# redistributions in binary form must reproduce the above copyright
913481Sgiacomo.travaglini@arm.com# notice, this list of conditions and the following disclaimer in the
1013481Sgiacomo.travaglini@arm.com# documentation and/or other materials provided with the distribution;
1113481Sgiacomo.travaglini@arm.com# neither the name of the copyright holders nor the names of its
1213481Sgiacomo.travaglini@arm.com# contributors may be used to endorse or promote products derived from
1313481Sgiacomo.travaglini@arm.com# this software without specific prior written permission.
1413481Sgiacomo.travaglini@arm.com#
1513481Sgiacomo.travaglini@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613481Sgiacomo.travaglini@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713481Sgiacomo.travaglini@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813481Sgiacomo.travaglini@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913481Sgiacomo.travaglini@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013481Sgiacomo.travaglini@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113481Sgiacomo.travaglini@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213481Sgiacomo.travaglini@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313481Sgiacomo.travaglini@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413481Sgiacomo.travaglini@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513481Sgiacomo.travaglini@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613481Sgiacomo.travaglini@arm.com#
2713481Sgiacomo.travaglini@arm.com# Authors: Ali Saidi
2813481Sgiacomo.travaglini@arm.com
2913481Sgiacomo.travaglini@arm.comimport os, sys
3013481Sgiacomo.travaglini@arm.comfrom os.path import isdir, join as joinpath
3113481Sgiacomo.travaglini@arm.comfrom os import environ as env
32
33config_path = os.path.dirname(os.path.abspath(__file__))
34config_root = os.path.dirname(config_path)
35
36def searchpath(path, file):
37    for p in path:
38        f = joinpath(p, file)
39        if os.path.exists(f):
40            return f
41    raise IOError, "Can't find file '%s' on path." % file
42
43def disk(file):
44    system()
45    return searchpath(disk.path, file)
46
47def binary(file):
48    system()
49    return searchpath(binary.path, file)
50
51def script(file):
52    system()
53    return searchpath(script.path, file)
54
55def system():
56    if not system.path:
57        try:
58            path = env['M5_PATH'].split(':')
59        except KeyError:
60            path = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]
61
62        # filter out non-existent directories
63        system.path = filter(os.path.isdir, path)
64
65        if not system.path:
66            raise IOError, "Can't find a path to system files."
67
68    if not binary.path:
69        binary.path = [joinpath(p, 'binaries') for p in system.path]
70    if not disk.path:
71        disk.path = [joinpath(p, 'disks') for p in system.path]
72    if not script.path:
73        script.path = [joinpath(config_root, 'boot')]
74
75system.path = None
76binary.path = None
77disk.path = None
78script.path = None
79