diff --git a/configs/common/SysPaths.py b/configs/common/SysPaths.py --- a/configs/common/SysPaths.py +++ b/configs/common/SysPaths.py @@ -26,46 +26,25 @@ # # Authors: Ali Saidi -import os, sys -from os.path import isdir, join as joinpath -from os import environ as env +import os +from path import Path, SubPath + +args = [] +try: + args.append(os.environ['M5_PATH']) +except KeyError: + args = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ] config_path = os.path.dirname(os.path.abspath(__file__)) config_root = os.path.dirname(config_path) +args.append(config_root) -def disk(file): - system() - return joinpath(disk.dir, file) +system = Path(args) -def binary(file): - system() - return joinpath(binary.dir, file) +# nesting SubPath in Path so, the subdir only applies to system and +# does not apply to any new path added +disk = Path(SubPath(system, 'disks')) +binary = Path(SubPath(system, 'binaries')) +script = Path(SubPath(system, 'boot')) -def script(file): - system() - return joinpath(script.dir, file) - -def system(): - if not system.dir: - try: - path = env['M5_PATH'].split(':') - except KeyError: - path = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ] - - for system.dir in path: - if os.path.isdir(system.dir): - break - else: - raise ImportError, "Can't find a path to system files." - - if not binary.dir: - binary.dir = joinpath(system.dir, 'binaries') - if not disk.dir: - disk.dir = joinpath(system.dir, 'disks') - if not script.dir: - script.dir = joinpath(config_root, 'boot') - -system.dir = None -binary.dir = None -disk.dir = None -script.dir = None +__all__ = [ 'system', 'disk', 'binary', 'script' ] diff --git a/configs/common/path.py b/configs/common/path.py --- /dev/null +++ b/configs/common/path.py @@ -0,0 +1,86 @@ +import os, sys +from os.path import dirname, isdir, isfile, join as joinpath +from os import environ as env + +class Base(object): + def find(self, file, test=isfile): + for dir in self: + f = joinpath(dir, file) + if test(f): + return f + else: + return None + + def __call__(self, file, exception=AttributeError): + result = self.find(file) + if result is None and exception is not None: + raise exception, "Could not find file %s in path %s" % (file, self) + + return result + +class Path(Base): + def __init__(self, *args): + self.path = [] + self.extend(args) + + def subpath(self, subdir): + return SubPath(self, subdir) + + def clear(self): + self.path.clear() + + def append(self, path): + if isinstance(path, Base): + self.path.append(path) + elif isinstance(path, str): + self.path.extend(path.split(':')) + elif isinstance(path, (list, tuple)): + self.extend(path) + else: + raise AttributeError, "Invalid argument to path '%s'" % path + + def extend(self, items): + for item in items: + self.append(item) + + def __iter__(self): + for item in self.path: + if isinstance(item, Base): + for i in item: + yield i + else: + yield item + + def __len__(self): + count = 0 + for item in self.path: + if isinstance(item, Path): + count += len(item) + else: + count += 1 + + def __str__(self): + return ':'.join(list(self)) + + def __repr__(self): + return 'Path(%s)' % self.path + +class SubPath(Base): + def __init__(self, parent, subdir): + self.parent = parent + self.subdir = subdir + + def __iter__(self): + for item in self.parent: + yield joinpath(item, self.subdir) + + def __len__(self): + return len(self.parent) + + def __str__(self): + return ':'.join(list(self)) + + def __repr__(self): + return "SubPath(%s, '%s')" % (`self.parent`, self.subdir) + +__all__ = [ 'Base', 'Path', 'SubPath']