
import os
ospath = os.path
import string

class FileSystem:
    def __init__ (self, base = '.'):
        self.base = base

    def absolute (self, name):
        result = ospath.join (self.base, name)
        return ospath.abspath (result)

    def exists (self, name):
        name = ospath.join (self.base, name)
        return ospath.exists (name)

    def getmtime (self, name):
        name = ospath.join (self.base, name)
        return ospath.getmtime (name)

    def join (self, part1, part2 = None):
        result = ospath.join (self.base, part1)
        if part2:
            result = ospath.join (result, part2)
        return result

    def listdir (self, dir, options = None):
        dir = ospath.join (self.base, dir)
        return os.listdir (dir)

    def open (self, fname, attributes = 'r'):
        fname = ospath.join (self.base, fname)
        if ('w' in attributes) or ('a' in attributes):
            dir, base = ospath.split (fname)
            if not ospath.exists (dir):
                os.makedirs (dir)
        return open (fname, attributes)

    def require (self, fname):
        fullname = ospath.join (self.base, fname)
        if not ospath.exists (fullname):
            self.open (fname, 'w')
        return fullname

    def stat (self, name):
        name = ospath.join (self.base, name)
        result = os.stat (name)
        return result

    def subdir (self, name):
        path = ospath.join (self.base, name)
        return FileSystem (path)

    def search (self, *files):
        for file in files:
            if self.exists (file):
                return file
        return None

class VmakeFS:
    def __init__ (self, path = None):
        if path == None:
            path = os.environ['VMAKE_PATH']
        if type (path) == type (''):
            path = string.split (path, ',')
        self.path = path

    def findFile (self, name, defaultToOwn = None):
        for dir in self.path:
            fullname = ospath.join (dir, name)
            if ospath.exists (fullname):
                return fullname
        if defaultToOwn:
            return ospath.join (self.path [0], name)
        else:
            return None

    def absolute (self, name):
        if ospath.isabs (name):
            return name
        fullname = self.findFile (name, 1)
        return fullname

    def exists (self, name):
        return self.findFile (name) != None

    def getmtime (self, name):
        name = self.findFile (name, 1)
        return ospath.getmtime (name)

    def join (self, part1, part2 = None):
        result = ospath.join (self.path [0], part1)
        if part2:
            result = ospath.join (result, part2)
        return result

    def listdir (self, dir, options = None):
        dir = self.findFile (dir)
        return os.listdir (dir)

    def open (self, fname, attributes = 'r'):
        fname = self.findFile (fname, 1)
        if ('w' in attributes) or ('a' in attributes):
            dir, base = ospath.split (fname)
            if not ospath.exists (dir):
                os.makedirs (dir)
        return open (fname, attributes)

    def require (self, fname):
        fullname = self.findFile (fname, 1)
        if not ospath.exists (fullname):
            self.open (fname, 'w')
        return fullname

    def stat (self, name):
        name = self.findFile (name, 1)
        result = os.stat (name)
        return result

    def subdir (self, name):
        newpath = []
        for dir in self.path:
            newpath.append (ospath.join (dir, name))
        return VmakeFS (newpath)

    def search (self, *files):
        for file in files:
            if self.exists (file):
                return file
        return None

