jobfile.py revision 1376
1# Copyright (c) 2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29from os.path import expanduser
30def crossproduct(options):
31    number = len(options)
32    indexes = [ 0 ] * number
33    maxes = [ len(opt) for opt in options ]
34    def next():
35        for i in xrange(number - 1, -1, -1):
36            indexes[i] += 1
37            if indexes[i] < maxes[i]:
38                return False
39
40            indexes[i] = 0
41        return True
42
43    done = False
44    while not done:
45        result = []
46        for i in xrange(number):
47            result.append(options[i][indexes[i]])
48        yield result
49        done = next()
50
51class JobFile(object):
52    def __init__(self, file):
53        self.data = {}
54        execfile(expanduser(file), self.data)
55        self.options = self.data['options']
56        self.environment = self.data['environment']
57        self.jobinfo = {}
58        self.jobs = []
59        for job in crossproduct(self.options):
60            jobname = '.'.join([ id[0] for id in job ])
61            self.jobs.append(jobname)
62            list = []
63            for info in job:
64                for item in info[1:]:
65                    list.append(item)
66            self.jobinfo[jobname] = list
67
68    def env(self, jobname):
69        env = {}
70        for key,val in self.jobinfo[jobname]:
71            env[key] = val
72
73        for key,val in self.environment:
74            env[key] = val
75        return env
76
77    def printinfo(self, jobname):
78        print '%s:' % jobname
79        for key,val in self.jobinfo[jobname]:
80            print '    %s = %s' % (key, val)
81
82        for key,val in self.environment:
83            print '    %s = %s' % (key, val)
84