jobfile.py revision 1428
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, isfile, join as joinpath
30import sys
31
32def crossproduct(options):
33    number = len(options)
34    indexes = [ 0 ] * number
35    maxes = [ len(opt) for opt in options ]
36    def next():
37        for i in xrange(number - 1, -1, -1):
38            indexes[i] += 1
39            if indexes[i] < maxes[i]:
40                return False
41
42            indexes[i] = 0
43        return True
44
45    done = False
46    while not done:
47        result = []
48        for i in xrange(number):
49            result.append(options[i][indexes[i]])
50        yield result
51        done = next()
52
53class JobFile(object):
54    def __init__(self, jfile):
55        self.data = {}
56        jfile = expanduser(jfile)
57        if not isfile(jfile):
58            for p in sys.path:
59                if isfile(joinpath(p, jfile)):
60                    jfile = joinpath(p, jfile)
61                    break
62
63        execfile(jfile, self.data)
64        self.options = self.data['options']
65        self.environment = self.data['environment']
66        self.jobinfo = {}
67        self.jobs = []
68        for job in crossproduct(self.options):
69            jobname = '.'.join([ id[0] for id in job ])
70            self.jobs.append(jobname)
71            list = []
72            for info in job:
73                for item in info[1:]:
74                    list.append(item)
75            self.jobinfo[jobname] = list
76
77    def env(self, jobname):
78        env = {}
79        for key,val in self.jobinfo[jobname]:
80            env[key] = val
81
82        for key,val in self.environment:
83            env[key] = val
84        return env
85
86    def printinfo(self, jobname):
87        print '%s:' % jobname
88        for key,val in self.jobinfo[jobname]:
89            print '    %s = %s' % (key, val)
90
91        for key,val in self.environment:
92            print '    %s = %s' % (key, val)
93