wscript (3851B)
1 #! /usr/bin/env python 2 3 """ 4 You will need either bzip2 or gzip, and a local waf copy 5 (unset the variable WAFDIR if set) 6 7 Using more than 100000 tasks may eat your memory 8 """ 9 10 top = '.' 11 out = 'build' 12 13 TEMPLATE = """ 14 #! /usr/bin/gnuplot -persist 15 # output file, compression type, input file 16 set terminal png 17 set output "%s" 18 set ylabel "Amount of files created" 19 set xlabel "File size in kB" 20 set title "Compressed tar file distribution (%s)" 21 plot '%s' using 1:2 with lines lt 3 title "" 22 """ 23 24 import random, bz2, os, threading 25 lock = threading.Lock() 26 27 def options(opt): 28 opt.add_option('--num', action='store', type='int', default=200, help='amount of compressed files to create') 29 30 # values for storing the min and max 31 gzip = [10000000, 0] 32 bzip2 = [10000000, 0] 33 xz = [10000000, 0] 34 35 def try_compress(self): 36 global mi, ma 37 38 frompath = self.generator.frompath 39 40 uid = id(threading.current_thread()) 41 filename = frompath.abspath() + os.sep + 'test%d.bin' % uid 42 43 self.files = self.generator.files[:] 44 random.shuffle(self.files) 45 46 if self.generator.kind == 'bzip2': 47 store = bzip2 48 cmd = 'cjf' 49 ext = 'bz2' 50 elif self.generator.kind == 'xz': 51 store = xz 52 cmd = 'cJf' 53 ext = 'xz' 54 else: 55 store = gzip 56 cmd = 'czf' 57 ext = 'gz' 58 59 self.generator.bld.exec_command('tar %s %s %s' % (cmd, filename, ' '.join(self.files)), cwd=frompath.abspath()) 60 61 siz = os.stat(filename).st_size 62 if siz == 0: 63 return -1 64 65 try: 66 lock.acquire() 67 self.outputs[0].write('%d\n' % siz, 'a') 68 69 if siz < store[0]: 70 store[0] = siz 71 os.rename(filename, self.generator.bld.bldnode.abspath() + os.sep + 'min%d.tar.%s' % (siz, ext)) 72 elif siz > store[1]: 73 store[1] = siz 74 os.rename(filename, self.generator.bld.bldnode.abspath() + os.sep + 'max%d.tar.%s' % (siz, ext)) 75 else: 76 os.remove(filename) 77 finally: 78 lock.release() 79 80 def count_result(self): 81 txt = self.inputs[0].read().strip() 82 lst = txt.split() 83 lst = [int(x) for x in lst if x] 84 mi = min(lst) 85 ma = max(lst) 86 87 dc = {} 88 for x in lst: 89 try: 90 dc[x] += 1 91 except KeyError: 92 dc[x] = 1 93 94 nlst = ['%d %d' % (x, dc.get(x, 0)) for x in range(mi, ma+1)] 95 self.outputs[0].write('\n'.join(nlst)) 96 97 def write_template(self): 98 t = self.generator.triplet 99 self.outputs[0].write(TEMPLATE % (t[0].abspath(), t[1], t[2].abspath())) 100 101 def configure(conf): 102 conf.find_program('gzip', mandatory=False) 103 conf.find_program('bzip2', mandatory=False) 104 if not conf.env.GZIP and not conf.env.BZIP2: 105 conf.fatal('Either gzip or bzip2 is necessary for this') 106 107 # xz is a gzip-like, lzma-based compression tool 108 conf.find_program('xz', mandatory=False) 109 110 conf.find_program('gnuplot', var='GNUPLOT') 111 112 def build(bld): 113 wafdir_lst = bld.srcnode.ant_glob('.waf*', dir=True) 114 if not wafdir_lst: 115 bld.fatal('Missing local Waf directory') 116 node = wafdir_lst[0] 117 rels = [x.path_from(node) for x in node.ant_glob('**/*.py')] 118 119 KINDS = [] 120 if bld.env.BZIP2: 121 KINDS.append('bzip2') 122 if bld.env.GZIP: 123 KINDS.append('gzip') 124 if bld.env.XZ: 125 KINDS.append('xz') 126 127 for kind in KINDS: 128 p = bld.bldnode 129 130 ini = p.make_node('size_%s_1.txt' % kind) # list of file sizes 131 dist = p.make_node('size_%s_2.txt' % kind) # distribution file (count the results) 132 plot = p.make_node('size_%s_3.plot' % kind) # script file created for gnuplot 133 png = p.make_node('size_%s_4.png' % kind) # picture created 134 135 for x in range(bld.options.num): 136 # the same target cannot have the signature of all the tasks that update it 137 # so the tasks will be executed each time 138 bld(rule=try_compress, target=ini, always=True, kind=kind, frompath=node, files=rels) 139 140 # for the same reason, count_result will be executed each time 141 bld(rule=count_result, target=dist, source=[ini], always=True) 142 bld(rule=write_template, target=plot, triplet=[png, kind, dist], always=True) 143 bld(rule='${GNUPLOT} < ${SRC[1].abspath()}', target=png, source=[dist, plot]) 144