waf

FORK: waf with some random patches
git clone https://git.neptards.moe/neptards/waf.git
Log | Files | Refs | README

optim.py (2557B)


      1 #! /usr/bin/env python
      2 
      3 import os, subprocess, shutil, random, optparse
      4 
      5 comp = {
      6 	'bz2': 'cjf',
      7 	'xz' : 'cJf',
      8 	'gz' : 'czf',
      9 }
     10 
     11 def read_wafdir():
     12 	try:
     13 		os.listdir('waflib')
     14 	except:
     15 		raise ImportError('please provide a waflib directory in the current folder')
     16 
     17 	d = 'waflib'
     18 	lst = [d + os.sep + x for x in os.listdir(d) if x.endswith('.py')]
     19 	e = d + os.sep + 'Tools'
     20 	lst.extend([e + os.sep + x for x in os.listdir(e) if x.endswith('.py')])
     21 	f = d + os.sep + 'extras'
     22 	lst.extend([f + os.sep + x for x in os.listdir(f) if x.endswith('.py')])
     23 
     24 	random.shuffle(lst)
     25 	#lst.sort()
     26 	return lst
     27 
     28 def gen(lst, options):
     29 
     30 	if options.maxi:
     31 		opti_ref = 0
     32 		filename = 'max.tar.%s' % options.kind
     33 		def compare(a, b):
     34 			return a > b
     35 	else:
     36 		opti_ref = 1000000000
     37 		filename = 'min.tar.%s' % options.kind
     38 		def compare(a, b):
     39 			return a < b
     40 	cmd = 'tar %s %s ' % (comp[options.kind], filename)
     41 	opti = [opti_ref]
     42 
     43 	LEN = len(lst)
     44 
     45 	POP = 3*LEN + 1
     46 	popul = [range(LEN) for x in range(POP)]
     47 	fitn = [0 for x in range(POP)]
     48 
     49 	def rnd():
     50 		return random.randint(0, LEN -1)
     51 
     52 	def mutate():
     53 		for x in range(LEN):
     54 			# rotate the previous element by one
     55 			v = popul[x+LEN] = popul[x+LEN - 1]
     56 			a = v.pop(0)
     57 			v.append(a)
     58 
     59 		for x in range(LEN):
     60 			# swap elements
     61 			a = rnd()
     62 			b = rnd()
     63 
     64 			v = popul[x]
     65 			c = v[a]
     66 			v[a] = v[b]
     67 			v[b] = c
     68 
     69 		for x in range(LEN):
     70 			# get one element out, add at the end
     71 			v = popul[x+2*LEN]
     72 
     73 			a = rnd()
     74 			c = v[a]
     75 			del v[a]
     76 			v.append(c)
     77 
     78 	def evil():
     79 
     80 		best = opti_ref
     81 		pos = -1
     82 		for x in range(len(popul)):
     83 			v = popul[x]
     84 			arr = [lst[a] for a in v]
     85 			tmp = '%s %s' % (cmd, ' '.join(arr))
     86 			subprocess.Popen(tmp, shell=True).wait()
     87 			siz = os.stat(filename).st_size
     88 
     89 			fitn[x] = siz
     90 			if compare(siz, best):
     91 				best = siz
     92 				pos = x
     93 
     94 				if compare(siz, opti[0]):
     95 					opti[0] = siz
     96 					shutil.copy2(filename, 'best_' + filename)
     97 
     98 			#print popul[x], sum(popul[x]), sum(range(LEN))
     99 			assert (sum(popul[x]) == sum(range(LEN)))
    100 
    101 		#print pos
    102 		for x in range(len(popul)):
    103 			if x == pos:
    104 				continue
    105 			popul[x] = popul[pos][:]
    106 			assert(len(popul[x]) == LEN)
    107 		return best
    108 
    109 	for i in range(10000):
    110 		mutate()
    111 		print(evil())
    112 
    113 if __name__ == '__main__':
    114 
    115 	parser = optparse.OptionParser()
    116 	parser.add_option('--max', dest='maxi', default=False, action='store_true', help='maximize the file size (default is minimize)')
    117 	parser.add_option('--kind', dest='kind', default='bz2', action='store', help='bz2, xz or gz')
    118 	(options, args) = parser.parse_args()
    119 
    120 	lst = read_wafdir()
    121 	gen(lst, options)
    122