jobfile.py revision 2141
16657Snate@binkert.org# Copyright (c) 2005-2006 The Regents of The University of Michigan
26657Snate@binkert.org# All rights reserved.
36657Snate@binkert.org#
46657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
56657Snate@binkert.org# modification, are permitted provided that the following conditions are
66657Snate@binkert.org# met: redistributions of source code must retain the above copyright
76657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
86657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
96657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
106657Snate@binkert.org# documentation and/or other materials provided with the distribution;
116657Snate@binkert.org# neither the name of the copyright holders nor the names of its
126657Snate@binkert.org# contributors may be used to endorse or promote products derived from
136657Snate@binkert.org# this software without specific prior written permission.
146657Snate@binkert.org#
156657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266657Snate@binkert.org#
276657Snate@binkert.org# Authors: Nathan Binkert
286999Snate@binkert.org
296657Snate@binkert.orgimport sys
306657Snate@binkert.org
316657Snate@binkert.orgclass ternary(object):
326657Snate@binkert.org    def __new__(cls, *args):
336657Snate@binkert.org        if len(args) > 1:
346657Snate@binkert.org            raise TypeError, \
356657Snate@binkert.org                  '%s() takes at most 1 argument (%d given)' % \
366657Snate@binkert.org                  (cls.__name__, len(args))
376657Snate@binkert.org
386657Snate@binkert.org        if args:
396657Snate@binkert.org            if not isinstance(args[0], (bool, ternary)):
406657Snate@binkert.org                raise TypeError, \
416657Snate@binkert.org                      '%s() argument must be True, False, or Any' % \
426657Snate@binkert.org                      cls.__name__
436657Snate@binkert.org            return args[0]
446657Snate@binkert.org        return super(ternary, cls).__new__(cls)
456657Snate@binkert.org
466657Snate@binkert.org    def __bool__(self):
476657Snate@binkert.org        return True
486657Snate@binkert.org
496657Snate@binkert.org    def __neg__(self):
506657Snate@binkert.org        return self
516657Snate@binkert.org
526657Snate@binkert.org    def __eq__(self, other):
536657Snate@binkert.org        return True
546882SBrad.Beckmann@amd.com
556657Snate@binkert.org    def __ne__(self, other):
566657Snate@binkert.org        return False
576657Snate@binkert.org
586657Snate@binkert.org    def __str__(self):
596657Snate@binkert.org        return 'Any'
606657Snate@binkert.org
616657Snate@binkert.org    def __repr__(self):
626657Snate@binkert.org        return 'Any'
636657Snate@binkert.org
646657Snate@binkert.orgAny = ternary()
656657Snate@binkert.org
666657Snate@binkert.orgclass Flags(dict):
676657Snate@binkert.org    def __init__(self, *args, **kwargs):
686657Snate@binkert.org        super(Flags, self).__init__()
696657Snate@binkert.org        self.update(*args, **kwargs)
706657Snate@binkert.org
716657Snate@binkert.org    def __getattr__(self, attr):
726657Snate@binkert.org        return self[attr]
736657Snate@binkert.org
746657Snate@binkert.org    def __setattr__(self, attr, value):
756657Snate@binkert.org        self[attr] = value
766657Snate@binkert.org
776657Snate@binkert.org    def __setitem__(self, item, value):
786657Snate@binkert.org        return super(Flags, self).__setitem__(item, ternary(value))
796657Snate@binkert.org
806657Snate@binkert.org    def __getitem__(self, item):
816657Snate@binkert.org        if item not in self:
826657Snate@binkert.org            return False
836657Snate@binkert.org        return super(Flags, self).__getitem__(item)
846657Snate@binkert.org
856657Snate@binkert.org    def update(self, *args, **kwargs):
866657Snate@binkert.org        for arg in args:
876657Snate@binkert.org            if isinstance(arg, Flags):
886657Snate@binkert.org                super(Flags, self).update(arg)
896657Snate@binkert.org            elif isinstance(arg, dict):
906657Snate@binkert.org                for key,val in kwargs.iteritems():
916657Snate@binkert.org                    self[key] = val
926657Snate@binkert.org            else:
936657Snate@binkert.org                raise AttributeError, \
946657Snate@binkert.org                      'flags not of type %s or %s, but %s' % \
956657Snate@binkert.org                      (Flags, dict, type(arg))
966657Snate@binkert.org
976657Snate@binkert.org        for key,val in kwargs.iteritems():
986657Snate@binkert.org            self[key] = val
996657Snate@binkert.org
1006657Snate@binkert.org    def match(self, *args, **kwargs):
1016657Snate@binkert.org        match = Flags(*args, **kwargs)
1026657Snate@binkert.org
1036657Snate@binkert.org        for key,value in match.iteritems():
1046657Snate@binkert.org            if self[key] != value:
1056657Snate@binkert.org                return False
1066657Snate@binkert.org
1076657Snate@binkert.org        return True
1086657Snate@binkert.org
1096657Snate@binkert.orgdef crossproduct(items):
1106657Snate@binkert.org    if not isinstance(items, (list, tuple)):
1116657Snate@binkert.org        raise AttributeError, 'crossproduct works only on sequences'
1126657Snate@binkert.org
1136657Snate@binkert.org    if not items:
1146657Snate@binkert.org        yield None
1156657Snate@binkert.org        return
1166657Snate@binkert.org
1176657Snate@binkert.org    current = items[0]
1186657Snate@binkert.org    remainder = items[1:]
1196657Snate@binkert.org
1206657Snate@binkert.org    if not hasattr(current, '__iter__'):
1216657Snate@binkert.org        current = [ current ]
1226657Snate@binkert.org
1236657Snate@binkert.org    for item in current:
1246657Snate@binkert.org        for rem in crossproduct(remainder):
1256657Snate@binkert.org            data = [ item ]
1266657Snate@binkert.org            if rem:
1276657Snate@binkert.org                data += rem
1286657Snate@binkert.org            yield data
1296657Snate@binkert.org
1306657Snate@binkert.orgdef flatten(items):
1316657Snate@binkert.org    if not isinstance(items, (list, tuple)):
1326657Snate@binkert.org        yield items
1336657Snate@binkert.org        return
1346657Snate@binkert.org
1356657Snate@binkert.org    for item in items:
1366657Snate@binkert.org        for flat in flatten(item):
1376657Snate@binkert.org            yield flat
1386657Snate@binkert.org
1396657Snate@binkert.orgclass Data(object):
1406657Snate@binkert.org    def __init__(self, name, desc, **kwargs):
1416657Snate@binkert.org        self.name = name
1426657Snate@binkert.org        self.desc = desc
1436657Snate@binkert.org        self.system = None
1446657Snate@binkert.org        self.flags = Flags()
1456657Snate@binkert.org        self.env = {}
1466657Snate@binkert.org        for k,v in kwargs.iteritems():
1476657Snate@binkert.org            setattr(self, k, v)
1486657Snate@binkert.org
1496657Snate@binkert.org    def update(self, obj):
1506657Snate@binkert.org        if not isinstance(obj, Data):
1516657Snate@binkert.org            raise AttributeError, "can only update from Data object"
1526657Snate@binkert.org
1536657Snate@binkert.org        self.env.update(obj.env)
1546657Snate@binkert.org        self.flags.update(obj.flags)
1556657Snate@binkert.org        if obj.system:
1566657Snate@binkert.org            if self.system and self.system != obj.system:
1576657Snate@binkert.org                raise AttributeError, \
1586882SBrad.Beckmann@amd.com                      "conflicting values for system: '%s'/'%s'" % \
1596882SBrad.Beckmann@amd.com                      (self.system, obj.system)
1606882SBrad.Beckmann@amd.com            self.system = obj.system
1616657Snate@binkert.org
1626657Snate@binkert.org    def printinfo(self):
1636657Snate@binkert.org        if self.name:
1646657Snate@binkert.org            print 'name: %s' % self.name
1656657Snate@binkert.org        if self.desc:
1666657Snate@binkert.org            print 'desc: %s' % self.desc
1676657Snate@binkert.org        if self.system:
1686657Snate@binkert.org            print 'system: %s' % self.system
1696657Snate@binkert.org
1706657Snate@binkert.org    def printverbose(self):
1716657Snate@binkert.org        print 'flags:'
1726657Snate@binkert.org        keys = self.flags.keys()
1736657Snate@binkert.org        keys.sort()
1746657Snate@binkert.org        for key in keys:
1756657Snate@binkert.org            print '    %s = %s' % (key, self.flags[key])
1766657Snate@binkert.org        print 'env:'
1776657Snate@binkert.org        keys = self.env.keys()
1786657Snate@binkert.org        keys.sort()
1796657Snate@binkert.org        for key in keys:
1806657Snate@binkert.org            print '    %s = %s' % (key, self.env[key])
1816657Snate@binkert.org        print
1826657Snate@binkert.org
1836657Snate@binkert.org    def __str__(self):
1846657Snate@binkert.org        return self.name
1856657Snate@binkert.org
1866657Snate@binkert.orgclass Job(Data):
1876657Snate@binkert.org    def __init__(self, options):
1886657Snate@binkert.org        super(Job, self).__init__('', '')
1896657Snate@binkert.org        self.setoptions(options)
1906657Snate@binkert.org
1916657Snate@binkert.org        self.checkpoint = False
1926657Snate@binkert.org        opts = []
1936657Snate@binkert.org        for opt in options:
1946999Snate@binkert.org            cpt = opt.group.checkpoint
1956657Snate@binkert.org            if not cpt:
1966657Snate@binkert.org                self.checkpoint = True
1976657Snate@binkert.org                continue
1986657Snate@binkert.org            if isinstance(cpt, Option):
1996657Snate@binkert.org                opt = cpt.clone(suboptions=False)
2006657Snate@binkert.org            else:
2016657Snate@binkert.org                opt = opt.clone(suboptions=False)
2027007Snate@binkert.org
2037007Snate@binkert.org            opts.append(opt)
2046657Snate@binkert.org
2057002Snate@binkert.org        if not opts:
2067002Snate@binkert.org            self.checkpoint = False
2076657Snate@binkert.org
2086657Snate@binkert.org        if self.checkpoint:
2096657Snate@binkert.org            self.checkpoint = Job(opts)
2106657Snate@binkert.org
2116657Snate@binkert.org    def clone(self):
2126657Snate@binkert.org        return Job(self.options)
2136657Snate@binkert.org
2146657Snate@binkert.org    def __getattribute__(self, attr):
2156657Snate@binkert.org        if attr == 'name':
2166657Snate@binkert.org            names = [ ]
2176657Snate@binkert.org            for opt in self.options:
2186657Snate@binkert.org                if opt.name:
2196657Snate@binkert.org                    names.append(opt.name)
2206657Snate@binkert.org            return ':'.join(names)
2217007Snate@binkert.org
2227007Snate@binkert.org        if attr == 'desc':
2236657Snate@binkert.org            descs = [ ]
2246657Snate@binkert.org            for opt in self.options:
2257007Snate@binkert.org                if opt.desc:
2266657Snate@binkert.org                    descs.append(opt.desc)
2276657Snate@binkert.org            return ', '.join(descs)
2286657Snate@binkert.org
2296657Snate@binkert.org        return super(Job, self).__getattribute__(attr)
2306657Snate@binkert.org
2316657Snate@binkert.org    def setoptions(self, options):
2326657Snate@binkert.org        config = options[0].config
2336657Snate@binkert.org        for opt in options:
2346657Snate@binkert.org            if opt.config != config:
2356657Snate@binkert.org                raise AttributeError, \
2366657Snate@binkert.org                      "All options are not from the same Configuration"
2376657Snate@binkert.org
2386657Snate@binkert.org        self.config = config
2396657Snate@binkert.org        self.groups = [ opt.group for opt in options ]
2406657Snate@binkert.org        self.options = options
2416657Snate@binkert.org
2426657Snate@binkert.org        self.update(self.config)
2436657Snate@binkert.org        for group in self.groups:
2446657Snate@binkert.org            self.update(group)
2456657Snate@binkert.org
2466657Snate@binkert.org        for option in self.options:
2476657Snate@binkert.org            self.update(option)
2486657Snate@binkert.org            if option._suboption:
2496657Snate@binkert.org                self.update(option._suboption)
2506657Snate@binkert.org
2516657Snate@binkert.org    def printinfo(self):
2526657Snate@binkert.org        super(Job, self).printinfo()
2536657Snate@binkert.org        if self.checkpoint:
2546657Snate@binkert.org            print 'checkpoint: %s' % self.checkpoint.name
2556657Snate@binkert.org        print 'config: %s' % self.config.name
2566657Snate@binkert.org        print 'groups: %s' % [ g.name for g in self.groups ]
2576657Snate@binkert.org        print 'options: %s' % [ o.name for o in self.options ]
2586657Snate@binkert.org        super(Job, self).printverbose()
2596657Snate@binkert.org
2606657Snate@binkert.orgclass SubOption(Data):
2616657Snate@binkert.org    def __init__(self, name, desc, **kwargs):
2626657Snate@binkert.org        super(SubOption, self).__init__(name, desc, **kwargs)
2636657Snate@binkert.org        self.number = None
2646657Snate@binkert.org
2656657Snate@binkert.orgclass Option(Data):
2666657Snate@binkert.org    def __init__(self, name, desc, **kwargs):
2676657Snate@binkert.org        super(Option, self).__init__(name, desc, **kwargs)
2686657Snate@binkert.org        self._suboptions = []
2696657Snate@binkert.org        self._suboption = None
2706657Snate@binkert.org        self.number = None
2716657Snate@binkert.org
2726657Snate@binkert.org    def __getattribute__(self, attr):
2736657Snate@binkert.org        if attr == 'name':
2746657Snate@binkert.org            name = self.__dict__[attr]
2756657Snate@binkert.org            if self._suboption is not None:
2767007Snate@binkert.org                name = '%s:%s' % (name, self._suboption.name)
2777007Snate@binkert.org            return name
2787007Snate@binkert.org
2796657Snate@binkert.org        if attr == 'desc':
2806657Snate@binkert.org            desc = self.__dict__[attr]
2816657Snate@binkert.org            if self._suboption is not None:
2826657Snate@binkert.org                desc = '%s, %s' % (desc, self._suboption.desc)
2836657Snate@binkert.org            return desc
2846657Snate@binkert.org
2856657Snate@binkert.org        return super(Option, self).__getattribute__(attr)
2866657Snate@binkert.org
2876657Snate@binkert.org    def suboption(self, name, desc, **kwargs):
2886657Snate@binkert.org        subo = SubOption(name, desc, **kwargs)
2897007Snate@binkert.org        subo.config = self.config
2907007Snate@binkert.org        subo.group = self.group
2917007Snate@binkert.org        subo.option = self
2927007Snate@binkert.org        subo.number = len(self._suboptions)
2937007Snate@binkert.org        self._suboptions.append(subo)
2947007Snate@binkert.org        return subo
2957007Snate@binkert.org
2967007Snate@binkert.org    def clone(self, suboptions=True):
2977007Snate@binkert.org        option = Option(self.__dict__['name'], self.__dict__['desc'])
2987007Snate@binkert.org        option.update(self)
2997007Snate@binkert.org        option.group = self.group
3007007Snate@binkert.org        option.config = self.config
3017007Snate@binkert.org        option.number = self.number
3027007Snate@binkert.org        if suboptions:
3036657Snate@binkert.org            option._suboptions.extend(self._suboptions)
3047007Snate@binkert.org            option._suboption = self._suboption
3057007Snate@binkert.org        return option
3067007Snate@binkert.org
3077007Snate@binkert.org    def subopts(self):
3087007Snate@binkert.org        if not self._suboptions:
3097007Snate@binkert.org            return [ self ]
3107007Snate@binkert.org
3117007Snate@binkert.org        subopts = []
3126657Snate@binkert.org        for subo in self._suboptions:
3136657Snate@binkert.org            option = self.clone()
3146657Snate@binkert.org            option._suboption = subo
3156657Snate@binkert.org            subopts.append(option)
3166657Snate@binkert.org
3176657Snate@binkert.org        return subopts
3186657Snate@binkert.org
3196657Snate@binkert.org    def printinfo(self):
3206657Snate@binkert.org        super(Option, self).printinfo()
3216657Snate@binkert.org        print 'config: %s' % self.config.name
3227007Snate@binkert.org        super(Option, self).printverbose()
3237007Snate@binkert.org
3247007Snate@binkert.orgclass Group(Data):
3257007Snate@binkert.org    def __init__(self, name, desc, **kwargs):
3267007Snate@binkert.org        super(Group, self).__init__(name, desc, **kwargs)
3276657Snate@binkert.org        self._options = []
3286657Snate@binkert.org        self.checkpoint = False
3296657Snate@binkert.org        self.number = None
3306657Snate@binkert.org
3316657Snate@binkert.org    def option(self, name, desc, **kwargs):
3326657Snate@binkert.org        opt = Option(name, desc, **kwargs)
3336657Snate@binkert.org        opt.config = self.config
3346657Snate@binkert.org        opt.group = self
3356657Snate@binkert.org        opt.number = len(self._options)
3367007Snate@binkert.org        self._options.append(opt)
3377007Snate@binkert.org        return opt
3387007Snate@binkert.org
3397007Snate@binkert.org    def options(self):
3407007Snate@binkert.org        return self._options
3416657Snate@binkert.org
3426657Snate@binkert.org    def subopts(self):
3436657Snate@binkert.org        subopts = []
3446657Snate@binkert.org        for opt in self._options:
3456657Snate@binkert.org            for subo in opt.subopts():
3466657Snate@binkert.org                subopts.append(subo)
3476657Snate@binkert.org        return subopts
3487007Snate@binkert.org
3497007Snate@binkert.org    def printinfo(self):
3507007Snate@binkert.org        super(Group, self).printinfo()
3517007Snate@binkert.org        print 'config: %s' % self.config.name
3527007Snate@binkert.org        print 'options: %s' % [ o.name for o in self._options ]
3536657Snate@binkert.org        super(Group, self).printverbose()
3546657Snate@binkert.org
3557002Snate@binkert.orgclass Configuration(Data):
3566657Snate@binkert.org    def __init__(self, name, desc, **kwargs):
3576657Snate@binkert.org        super(Configuration, self).__init__(name, desc, **kwargs)
3586657Snate@binkert.org        self._groups = []
3596657Snate@binkert.org        self._posfilters = []
3606657Snate@binkert.org        self._negfilters = []
3616657Snate@binkert.org
3626657Snate@binkert.org    def group(self, name, desc, **kwargs):
3636657Snate@binkert.org        grp = Group(name, desc, **kwargs)
3646657Snate@binkert.org        grp.config = self
3656657Snate@binkert.org        grp.number = len(self._groups)
3666657Snate@binkert.org        self._groups.append(grp)
3676657Snate@binkert.org        return grp
3686657Snate@binkert.org
3696657Snate@binkert.org    def groups(self, flags=Flags(), sign=True):
3706657Snate@binkert.org        if not flags:
3716657Snate@binkert.org            return self._groups
3726657Snate@binkert.org
3736657Snate@binkert.org        return [ grp for grp in self._groups if sign ^ grp.flags.match(flags) ]
3746657Snate@binkert.org
3756657Snate@binkert.org    def checkchildren(self, kids):
3766657Snate@binkert.org        for kid in kids:
3777007Snate@binkert.org            if kid.config != self:
3786657Snate@binkert.org                raise AttributeError, "child from the wrong configuration"
3797007Snate@binkert.org
3806657Snate@binkert.org    def sortgroups(self, groups):
3816657Snate@binkert.org        groups = [ (grp.number, grp) for grp in groups ]
3826657Snate@binkert.org        groups.sort()
3836657Snate@binkert.org        return [ grp[1] for grp in groups ]
3846657Snate@binkert.org
3856657Snate@binkert.org    def options(self, groups = None, checkpoint = False):
3866657Snate@binkert.org        if groups is None:
3876657Snate@binkert.org            groups = self._groups
3886657Snate@binkert.org        self.checkchildren(groups)
3897007Snate@binkert.org        groups = self.sortgroups(groups)
3907007Snate@binkert.org        if checkpoint:
3916657Snate@binkert.org            groups = [ grp for grp in groups if grp.checkpoint ]
3926657Snate@binkert.org            optgroups = [ g.options() for g in groups ]
3937007Snate@binkert.org        else:
3947007Snate@binkert.org            optgroups = [ g.subopts() for g in groups ]
3956657Snate@binkert.org        for options in crossproduct(optgroups):
3966657Snate@binkert.org            for opt in options:
3977002Snate@binkert.org                cpt = opt.group.checkpoint
3986657Snate@binkert.org                if not isinstance(cpt, bool) and cpt != opt:
3996657Snate@binkert.org                    if checkpoint:
4006657Snate@binkert.org                        break
4017007Snate@binkert.org                    else:
4026657Snate@binkert.org                        yield options
4036657Snate@binkert.org            else:
4046657Snate@binkert.org                if checkpoint:
4056657Snate@binkert.org                    yield options
4066657Snate@binkert.org
4076999Snate@binkert.org    def addfilter(self, filt, pos=True):
4086657Snate@binkert.org        import re
4096657Snate@binkert.org        filt = re.compile(filt)
4106657Snate@binkert.org        if pos:
4116657Snate@binkert.org            self._posfilters.append(filt)
4126657Snate@binkert.org        else:
4136657Snate@binkert.org            self._negfilters.append(filt)
4146657Snate@binkert.org
4157002Snate@binkert.org    def jobfilter(self, job):
4167002Snate@binkert.org        for filt in self._negfilters:
4176657Snate@binkert.org            if filt.match(job.name):
4187002Snate@binkert.org                return False
4197002Snate@binkert.org
4206657Snate@binkert.org        if not self._posfilters:
4216657Snate@binkert.org            return True
4226657Snate@binkert.org
4236657Snate@binkert.org        for filt in self._posfilters:
4246657Snate@binkert.org            if filt.match(job.name):
4256657Snate@binkert.org                return True
4267007Snate@binkert.org
4277007Snate@binkert.org        return False
4286657Snate@binkert.org
4296657Snate@binkert.org    def checkpoints(self, groups = None):
4306657Snate@binkert.org        for options in self.options(groups, True):
4316657Snate@binkert.org            job = Job(options)
4326657Snate@binkert.org            if self.jobfilter(job):
4336657Snate@binkert.org                yield job
4346657Snate@binkert.org
4356657Snate@binkert.org    def jobs(self, groups = None):
4366657Snate@binkert.org        for options in self.options(groups, False):
4376657Snate@binkert.org            job = Job(options)
4386657Snate@binkert.org            if self.jobfilter(job):
4396657Snate@binkert.org                yield job
4406657Snate@binkert.org
4416657Snate@binkert.org    def alljobs(self, groups = None):
4426657Snate@binkert.org        for options in self.options(groups, True):
4436657Snate@binkert.org            yield Job(options)
4446657Snate@binkert.org        for options in self.options(groups, False):
4456657Snate@binkert.org            yield Job(options)
4466657Snate@binkert.org
4476657Snate@binkert.org    def find(self, jobname):
4486657Snate@binkert.org        for job in self.alljobs():
4496999Snate@binkert.org            if job.name == jobname:
4506657Snate@binkert.org                return job
4516657Snate@binkert.org        else:
4526657Snate@binkert.org            raise AttributeError, "job '%s' not found" % jobname
4536657Snate@binkert.org
4546657Snate@binkert.org    def job(self, options):
4557007Snate@binkert.org        self.checkchildren(options)
4567007Snate@binkert.org        options = [ (opt.group.number, opt) for opt in options ]
4577007Snate@binkert.org        options.sort()
4586657Snate@binkert.org        options = [ opt[1] for opt in options ]
4597002Snate@binkert.org        job = Job(options)
4607002Snate@binkert.org        return job
4617002Snate@binkert.org
4626657Snate@binkert.org    def printinfo(self):
4636657Snate@binkert.org        super(Configuration, self).printinfo()
4647007Snate@binkert.org        print 'groups: %s' % [ g.name for g in self._grouips ]
4656657Snate@binkert.org        super(Configuration, self).printverbose()
4666657Snate@binkert.org
4676657Snate@binkert.orgdef JobFile(jobfile):
4686657Snate@binkert.org    from os.path import expanduser, isfile, join as joinpath
4696657Snate@binkert.org    filename = expanduser(jobfile)
4706657Snate@binkert.org
4716657Snate@binkert.org    # Can't find filename in the current path, search sys.path
4726657Snate@binkert.org    if not isfile(filename):
4736657Snate@binkert.org        for path in sys.path:
4746657Snate@binkert.org            testname = joinpath(path, filename)
4756657Snate@binkert.org            if isfile(testname):
4766862Sdrh5@cs.wisc.edu                filename = testname
4776862Sdrh5@cs.wisc.edu                break
4786862Sdrh5@cs.wisc.edu        else:
4796862Sdrh5@cs.wisc.edu            raise AttributeError, \
4806657Snate@binkert.org                  "Could not find file '%s'" % jobfile
4816657Snate@binkert.org
4826657Snate@binkert.org    data = {}
4836657Snate@binkert.org    execfile(filename, data)
4846657Snate@binkert.org    if 'conf' not in data:
4857007Snate@binkert.org        raise ImportError, 'cannot import name conf from %s' % jobfile
4867007Snate@binkert.org    conf = data['conf']
4877002Snate@binkert.org    import jobfile
4887007Snate@binkert.org    if not isinstance(conf, Configuration):
4897007Snate@binkert.org        raise AttributeError, \
4907002Snate@binkert.org              'conf in jobfile: %s (%s) is not type %s' % \
4917007Snate@binkert.org              (jobfile, type(conf), Configuration)
4927007Snate@binkert.org    return conf
4936657Snate@binkert.org
4946657Snate@binkert.orgif __name__ == '__main__':
4956657Snate@binkert.org    from jobfile import *
4966657Snate@binkert.org    import sys
4976657Snate@binkert.org
4986657Snate@binkert.org    usage = 'Usage: %s [-b] [-c] [-v] <jobfile>' % sys.argv[0]
4996657Snate@binkert.org
5006657Snate@binkert.org    try:
5016657Snate@binkert.org        import getopt
5026657Snate@binkert.org        opts, args = getopt.getopt(sys.argv[1:], '-bcv')
5036657Snate@binkert.org    except getopt.GetoptError:
5046657Snate@binkert.org        sys.exit(usage)
5056657Snate@binkert.org
5066657Snate@binkert.org    if len(args) != 1:
5076657Snate@binkert.org        raise AttributeError, usage
5086657Snate@binkert.org
5096657Snate@binkert.org    both = False
5107002Snate@binkert.org    checkpoint = False
5116657Snate@binkert.org    verbose = False
5127007Snate@binkert.org    for opt,arg in opts:
5136657Snate@binkert.org        if opt == '-b':
5146657Snate@binkert.org            both = True
5156657Snate@binkert.org            checkpoint = True
5166657Snate@binkert.org        if opt == '-c':
5176657Snate@binkert.org            checkpoint = True
5186999Snate@binkert.org        if opt == '-v':
5196657Snate@binkert.org            verbose = True
5206657Snate@binkert.org
5216657Snate@binkert.org    jobfile = args[0]
5226657Snate@binkert.org    conf = JobFile(jobfile)
5236657Snate@binkert.org
5246657Snate@binkert.org    if both:
5257002Snate@binkert.org        gen = conf.alljobs()
5267002Snate@binkert.org    elif checkpoint:
5277002Snate@binkert.org        gen = conf.checkpoints()
5286657Snate@binkert.org    else:
5296657Snate@binkert.org        gen = conf.jobs()
5307002Snate@binkert.org
5317002Snate@binkert.org    for job in gen:
5326657Snate@binkert.org        if not verbose:
5336657Snate@binkert.org            cpt = ''
5346657Snate@binkert.org            if job.checkpoint:
5356657Snate@binkert.org                cpt = job.checkpoint.name
5366657Snate@binkert.org            print job.name, cpt
5376657Snate@binkert.org        else:
5386657Snate@binkert.org            job.printinfo()
5397007Snate@binkert.org