예제 #1
0
def main():
    if sys.argv[1:]:
        sync = sys.argv[1] + ' ' + TESTCOMMENT
    else:
        print 'Usage: ./testsync.py path/sync.py'
        exit(1)

    for cnt in xrange(1000):

        assert 0==os.system(sync)

        assert save({'cnt':cnt,'hname':gethostname()},time.time(),PATH)

        
        c2 = read_c2(PATH)

        #debug
        print cnt
        print c2
        print
        print [x for x in c2 if thishost(x)]

        if len([x for x in c2 if thishost(x)])!=cnt+1:
            print 'Bug! Bug! Bug!'
        
        randomsleep(60,120)
예제 #2
0
def diffc2(f, g):
    '''
    return True if 2 c2 files are equal
    '''
    if type(f) is str:
        f = read_c2(f)

    if type(g) is str:
        g = read_c2(g)

    assert type(f) is type(g) is dict, type(f)

    if set(f.keys()) != set(g.keys()):
        return False

    for k in f:
        if f[k]['ts'] != g[k]['ts']:
            return False

    return True
예제 #3
0
def diffc2(f,g):
    '''
    return True if 2 c2 files are equal
    '''
    if type(f) is str:
        f = read_c2(f)

    if type(g) is str:
        g = read_c2(g)

    assert type(f) is type(g) is dict,type(f)

    if set(f.keys()) != set(g.keys()):
        return False

    for k in f:
        if f[k]['ts'] != g[k]['ts']:
            return False

    return True
예제 #4
0
def main():
    if sys.argv[1:]:
        sync = sys.argv[1] + ' ' + TESTCOMMENT
    else:
        print 'Usage: ./testsync.py path/sync.py'
        exit(1)

    for cnt in xrange(1000):

        assert 0==os.system(sync)

        assert save({'cnt':cnt,'hname':gethostname()},time.time(),PATH)

        
        c2 = read_c2(PATH)

        #debug
        print cnt
        print c2
        print
        print [x for x in c2 if thishost(x)]

        if len([x for x in c2 if thishost(x)])!=cnt+1:
            print 'Bug! Bug! Bug!'
        
        randomsleep(60,120)

def thishost(x):
    '''
    return True if x obj war inserted by this host
    '''
    for k,v in x:
        if k=='hname' and v==gethostname():
            return 1
    return 0

if __name__=="__main__":
    main()
예제 #5
0
def main():
    
    o = optparse.OptionParser(usage='usage: %prog [-r] [file1 [file2] [...]]')
    o.add_option('-r','--recursive',default=False,action='store_true')
    o.add_option('-s','--summary',help='don\'t print items, only general info',default=False,action='store_true')
    o.add_option('-l','--short-line',help='trunc each line to 80 chars',default=False,action='store_true')
    o.add_option('-k','--keys',help='show keys',default=False,action='store_true')
    o.add_option('-v','--values',help='show values',default=False,action='store_true')
    o.add_option('-w','--raw',help='show raw values',default=False,action='store_true')
    o.add_option('-i','--info',help='show timestamps, hostnames, ...',default=False,action='store_true')
    o.add_option('-g','--graph',help='plot graph with the timestamps of stored values',default=False,action='store_true')
    o.add_option('-m','--meta',help='show metadata',default=False,action='store_true')
    o.add_option('-a','--all',help='enable all info',default=False,action='store_true')

    o.add_option('-H',False,help='filter on hostname',dest='hname')
    o.add_option('-D',False,help='show only last # days',dest='ndays')
    o.add_option('-S',False,help='show only if string is in key or value field (might slow)',dest='search')
    o.add_option('-K',False,help='show only if string is in key field',dest='key')

    opts, files = o.parse_args()

    # enable all
    if opts.all:
        opts.keys = opts.values = opts.info = opts.meta = True

    # if no options is setted, enable keys
    if not any(opts.__dict__.values()):
        opts.keys = True

    if not files:
	o.print_help()
	return

    #format line
    line = opts.short_line and (lambda l: l[:80]) or (lambda l: l)

    if opts.recursive:
        allfiles = []
        for file in files:
            if path.isdir(file):
                for dir,ds,fs in os.walk(file):
                    allfiles += [path.join(dir,x) for x in fs]
        files = allfiles
    #print files
    for file in files:
        if not file.endswith('.c2'):
            continue
        if len(files)>1:
            print '_'*len(file)
            print file
            print
        
        c2 = read_c2(file)
        #info
        print 'Number of items',len(c2)
        if opts.summary:
            continue

        plot = {} #plot graph

        for i,(k,v) in enumerate(c2.items()):

            # filter

            if type(v) is dict:
                if opts.hname and 'hn' in v and v['hn']!=opts.hname:
                    continue

                if opts.ndays and 'ts' in v and time.time()-v['ts']>int(opts.ndays)*24*60*60:
                    continue

                if opts.key and opts.key not in str(k):
                    continue

                if opts.search and opts.search not in str(k) and opts.search not in str(v):
                    continue

            #

            print '~ Item %d ~'%i
            if opts.keys:
                print line('Key: '+str(k))
            if opts.values:
                if type(v) is dict and 'dt' in v:
                    print line('Value: '+str(v['dt']))
                else:
                    print line(str(v))
            if opts.raw:
                print line('Raw Value: '+str(v))
            if opts.info and type(v) is dict:
                if 'ts' in v:
                    print 'Timestamp:',time.ctime(v['ts'])
                else:
                    print 'No timestamp'
                if 'hn' in v:
                    print 'Hostname:',v['hn']
                else:
                    print 'No hostname'
            if opts.graph and 'ts' in v:
                date = '%.4d-%.2d-%.2d'%time.gmtime(v['ts'])[:3]
                plot.setdefault(date,0)
                plot[date] += 1

            if opts.meta:
                if type(v) is dict and 'mt' in v:
                    print line(' '+str(v['mt']))
                else:
                    print 'No debug info'

        if opts.graph:
            fname = file
            if fname.endswith('.c2'):
                fname = fname[:-3]
            prettyplot(list(plot.iteritems()),fname,showlines=True,title='Age of cache info')
예제 #6
0
def main():
    
    o = optparse.OptionParser(usage='usage: %prog [-r] [file1 [file2] [...]]')
    o.add_option('-v','--verbose',help='print different values',default=False,action='store_true')

    opts, files = o.parse_args()

    if len(files) != 2:
        print __doc__
        return

    one = read_c2(files[0])
    two = read_c2(files[1])

    k1 = set(one.keys())
    k2 = set(two.keys())

    onlyone = len(k1-k2)
    onlytwo = len(k2-k1)

    eq =  0
    diff1 = 0 # one.c2 is newer
    diff2 = 0 # two.c2 is newer

    for k in k1 & k2:
        if one[k]['dt'] == two[k]['dt']:
            eq += 1
        else:
            if opts.verbose:
                print '1>',one[k]
                print '2<',two[k]

            if one[k]['ts'] > two[k]['ts']:
                diff1 += 1
            elif one[k]['ts'] < two[k]['ts']:
                diff2 += 1
            else:
                assert 0,'Same timestamp, different data. Improbably'

    diff = diff1 + diff2

    if not diff:
        if onlyone and not onlytwo:
            f = (0,1)
        elif not onlyone and onlytwo:
            f = (1,0)
        elif not onlyone and not onlytwo:
            print 'Files are equals'
            return
        
        print '%s > %s' % (files[f[0]],files[f[1]])
    else:

        # All different data is newer in `newer` c2 file
        if not diff2:
            newer = 0
        elif not diff1:
            newer = 1
        else:
            newer = 0

        if newer:
            print 'The newest c2 is',files[newer]
        print 'Same values:',eq
        print 'Different values:',diff
        print 'Values only in %s:'%files[0],onlyone
        print 'Values only in %s:'%files[1],onlytwo
예제 #7
0
def main():

    o = optparse.OptionParser(usage='usage: %prog [-r] [file1 [file2] [...]]')
    o.add_option('-v',
                 '--verbose',
                 help='print different values',
                 default=False,
                 action='store_true')

    opts, files = o.parse_args()

    if len(files) != 2:
        print __doc__
        return

    one = read_c2(files[0])
    two = read_c2(files[1])

    k1 = set(one.keys())
    k2 = set(two.keys())

    onlyone = len(k1 - k2)
    onlytwo = len(k2 - k1)

    eq = 0
    diff1 = 0  # one.c2 is newer
    diff2 = 0  # two.c2 is newer

    for k in k1 & k2:
        if one[k]['dt'] == two[k]['dt']:
            eq += 1
        else:
            if opts.verbose:
                print '1>', one[k]
                print '2<', two[k]

            if one[k]['ts'] > two[k]['ts']:
                diff1 += 1
            elif one[k]['ts'] < two[k]['ts']:
                diff2 += 1
            else:
                assert 0, 'Same timestamp, different data. Improbably'

    diff = diff1 + diff2

    if not diff:
        if onlyone and not onlytwo:
            f = (0, 1)
        elif not onlyone and onlytwo:
            f = (1, 0)
        elif not onlyone and not onlytwo:
            print 'Files are equals'
            return

        print '%s > %s' % (files[f[0]], files[f[1]])
    else:

        # All different data is newer in `newer` c2 file
        if not diff2:
            newer = 0
        elif not diff1:
            newer = 1
        else:
            newer = 0

        if newer:
            print 'The newest c2 is', files[newer]
        print 'Same values:', eq
        print 'Different values:', diff
        print 'Values only in %s:' % files[0], onlyone
        print 'Values only in %s:' % files[1], onlytwo