jobfile.py revision 1376
113206Sgabeblack@google.com# Copyright (c) 2005 The Regents of The University of Michigan
213206Sgabeblack@google.com# All rights reserved.
313206Sgabeblack@google.com#
413206Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
513206Sgabeblack@google.com# modification, are permitted provided that the following conditions are
613206Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
713206Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
813206Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
913206Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1013206Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1113206Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1213206Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1313206Sgabeblack@google.com# this software without specific prior written permission.
1413206Sgabeblack@google.com#
1513206Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613206Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713206Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813206Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913206Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013206Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113206Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213206Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313206Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413206Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513206Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613206Sgabeblack@google.com#
2713206Sgabeblack@google.com# Authors: Nathan Binkert
2813206Sgabeblack@google.com
2913206Sgabeblack@google.comfrom os.path import expanduser
3013206Sgabeblack@google.comdef crossproduct(options):
3113206Sgabeblack@google.com    number = len(options)
3213206Sgabeblack@google.com    indexes = [ 0 ] * number
3313206Sgabeblack@google.com    maxes = [ len(opt) for opt in options ]
3413206Sgabeblack@google.com    def next():
3513206Sgabeblack@google.com        for i in xrange(number - 1, -1, -1):
3613206Sgabeblack@google.com            indexes[i] += 1
3713206Sgabeblack@google.com            if indexes[i] < maxes[i]:
3813206Sgabeblack@google.com                return False
3913206Sgabeblack@google.com
4013206Sgabeblack@google.com            indexes[i] = 0
4113206Sgabeblack@google.com        return True
4213206Sgabeblack@google.com
4313206Sgabeblack@google.com    done = False
4413206Sgabeblack@google.com    while not done:
4513206Sgabeblack@google.com        result = []
4613206Sgabeblack@google.com        for i in xrange(number):
4713206Sgabeblack@google.com            result.append(options[i][indexes[i]])
4813206Sgabeblack@google.com        yield result
4913206Sgabeblack@google.com        done = next()
5013206Sgabeblack@google.com
5113206Sgabeblack@google.comclass JobFile(object):
5213206Sgabeblack@google.com    def __init__(self, file):
5313206Sgabeblack@google.com        self.data = {}
5413206Sgabeblack@google.com        execfile(expanduser(file), self.data)
5513206Sgabeblack@google.com        self.options = self.data['options']
5613206Sgabeblack@google.com        self.environment = self.data['environment']
5713206Sgabeblack@google.com        self.jobinfo = {}
5813206Sgabeblack@google.com        self.jobs = []
5913206Sgabeblack@google.com        for job in crossproduct(self.options):
6013206Sgabeblack@google.com            jobname = '.'.join([ id[0] for id in job ])
6113206Sgabeblack@google.com            self.jobs.append(jobname)
6213206Sgabeblack@google.com            list = []
6313206Sgabeblack@google.com            for info in job:
6413206Sgabeblack@google.com                for item in info[1:]:
6513206Sgabeblack@google.com                    list.append(item)
6613206Sgabeblack@google.com            self.jobinfo[jobname] = list
6713206Sgabeblack@google.com
6813206Sgabeblack@google.com    def env(self, jobname):
6913206Sgabeblack@google.com        env = {}
7013206Sgabeblack@google.com        for key,val in self.jobinfo[jobname]:
7113206Sgabeblack@google.com            env[key] = val
7213206Sgabeblack@google.com
7313206Sgabeblack@google.com        for key,val in self.environment:
7413206Sgabeblack@google.com            env[key] = val
7513206Sgabeblack@google.com        return env
7613206Sgabeblack@google.com
7713206Sgabeblack@google.com    def printinfo(self, jobname):
7813206Sgabeblack@google.com        print '%s:' % jobname
7913206Sgabeblack@google.com        for key,val in self.jobinfo[jobname]:
8013206Sgabeblack@google.com            print '    %s = %s' % (key, val)
8113206Sgabeblack@google.com
8213206Sgabeblack@google.com        for key,val in self.environment:
8313206Sgabeblack@google.com            print '    %s = %s' % (key, val)
8413206Sgabeblack@google.com