ret,vals,sys.argv = utils.check_arg(sys.argv,'--type',1)
    if ret:
        dtype = vals[0]

    pretend,j,sys.argv = utils.check_arg(sys.argv,'--pretend')
    
    if ( len(sys.argv) == 3 ):
       
        postgresops.check_evil(sys.argv[0])
        postgresops.check_evil(sys.argv[1])
        
        sname = sys.argv[0]
        sid = sys.argv[1]
        
        pl_meta,dev,pl_index = devicedb.find_plugload(sname,sid)        
        if  pl_index == None:
            print "Cannot find device belonging to this source name/id pair"
            sys.exit(1);
        elif pl_meta == None:
            print "Cannot find metadata for this plugload (possibly undefined?)"
            sys.exit(1);
       
        print "Found device: "+dev.IDstr+" plugload channel %d"%(pl_index)
        
        if (pl_meta.parent == 1):
            print "Error: Plugload is defined as an aggregate sum of plugloads, cannot learn from this (yet!)"
            sys.exit(1);
        
        plugload_row = devicedb.get_devicemetas(where="id=%d"%pl_meta.parent,limit=1);
        
    def run(self, time_from, time_to, source_name=None, source_id=None, pretend=False, use_cache=True, local=False, params=[]):
        if source_id == None and local:
            print "Error: Can only run 'local' test on one stream ( source name & id pair ) at a time"
            sys.exit(1)
        
        
        if source_name == None:
            names = utils.list_sources();
            for name in names:
                ids = utils.list_ids(name)
                for id in ids:
                    self.run(time_from, time_to, name, id, pretend, use_cache, local)
            return
                            
        elif source_id == None:
            ids = utils.list_ids(source_name)
            for id in ids:
                self.run(time_from, time_to, source_name, id, pretend, use_cache, local)
            return
        
        smap = utils.read_source(source_name)
        if not local and use_cache:
            # find any cached files
            import filedb
            filedb.connect()
            
            for file in self.files:
                stepchain = file.stepchain
                cfiles = filedb.get_files(where='time_from=%s and time_to=%s and source_name=%s and source_id=%s and steps=%s',params=(time_from,time_to,source_name,source_id,stepchain))
                for cfile in cfiles:
                    if ( cfile.status == filedb.INVALID or os.path.exists(cfile.file_name) ):
                        file.cached = True
                        file.file_id = cfile.id
                        file.fname = cfile.file_name
                        file.deptask = cfile.task_id
                        file.stepchain = stepchain
                        print "Found cached file for output of "+file.src.name
                        break
        
            # prune any tasks that don't need to be run
            for step in self.steps[:]:
                if ( len(step.outputs) == 0):
                    print "Step %s will be run b/c it has no outputs"%step.name
                    continue
                    
                canbepruned = True
                for f in step.outputs:
                    if not f.cached:
                        canbepruned = False
                        break 
                if canbepruned:
                    print "Pruning step %s because the cache can supply all outputs"%step.name
                    self.steps.remove(step)
                else:
                    for f in step.outputs:
                        if f.cached:
                            f.cached = False
                            print "Cached file %s.O%d will be regenerated b/c not all outputs were cached"%(step.name,f.index)
        
        # create all the files we'll need
        if local:
            dir = ''
        else:
            if ( self.use_tmp ):
                dir = '/tmp/sensezilla/flowdata/'+self.name
            else:
                dir = config.map['global']['data_dir']+'/flowdata/'+self.name
        
        
            if not pretend:
                try:
                    os.makedirs(dir+'/outputs',0755)
                except:pass
            
        
        for file in self.files:
            if not file.cached:
                if local:
                    file.fname = dir+'testing_%s_%s.O%d'%(self.name,file.src.name,file.index)
                    if not pretend:
                        if file.directory:
                            try:
                                os.mkdir(file.fname)
                            except:pass
                        else:
                            fout = open(file.fname,'w')
                            fout.close()
                else:
                    if 'OUTPUT' not in [v[0] for v in file.dests]:
                        if not pretend:
                            if file.directory:
                                file.fname = tempfile.mkdtemp(dir=dir)
                            else:
                                tfile = tempfile.NamedTemporaryFile('w', dir=dir, delete=False)
                                file.fname = tfile.name;
                                tfile.close()
                        else:
                            file.fname = os.tempnam(dir)
                    else:
                        file.fname = dir+'/outputs/%s.O%d_%s_%s_%d_to_%d'%(file.src.name,file.index,source_name,source_id.replace('/','.'),
                                                                           utils.date_to_unix(time_from),utils.date_to_unix(time_to))
                        if file.directory:
                            if not pretend:
                                os.mkdir(file.fname)
                        else:
                            if not pretend: 
                                fout = open(file.fname,'w')
                                fout.close()
                    
                if file.directory:
                    print "Created directory : "+file.fname
                else:
                    print "Created file : "+file.fname


        # generate dictionary of substitutions
        subs = {
                'TIME_FROM':int(utils.date_to_unix(time_from)),
                'TIME_TO':int(utils.date_to_unix(time_to)),
                'SOURCE':source_name,
                'ID':source_id                
        };
        subs.update(params)
        
        try:
            import devicedb
            devicedb.connect()
            plmeta,dev,pl_index = devicedb.find_plugload(source_name,source_id)
            subs['PLUGLOAD'] = plmeta.value;
            subs['DEVID'] = dev.ID
            subs['DEVIDSTR'] = dev.IDstr
            
        except Exception,e:
            print "Cannot contact devicedb "+str(e)