#! /usr/bin/env python
# encoding: utf-8
# WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file

import os,shutil,re
from waflib import Options,Build,Logs
re_num=re.compile('[a-zA-Z_-]+(\d+)')
CACHESIZE=10*1024*1024*1024
CLEANRATIO=0.8
DIRSIZE=4096
def compile(self):
	if Options.cache_global and not Options.options.nocache:
		try:
			os.makedirs(Options.cache_global)
		except:
			pass
	try:
		self.raw_compile()
	finally:
		if Options.cache_global and not Options.options.nocache:
			self.sweep()
def sweep(self):
	global CACHESIZE
	CACHEDIR=Options.cache_global
	re_num=re.compile('[a-zA-Z_]+(\d+)')
	val=re_num.sub('\\1',os.path.basename(Options.cache_global))
	try:
		CACHESIZE=int(val)
	except:
		pass
	flist={}
	for x in os.listdir(CACHEDIR):
		j=os.path.join(CACHEDIR,x)
		if os.path.isdir(j)and len(x)==64:
			flist[x]=[os.stat(j).st_mtime,0]
	for(x,v)in flist.items():
		cnt=DIRSIZE
		d=os.path.join(CACHEDIR,x)
		for k in os.listdir(d):
			cnt+=os.stat(os.path.join(d,k)).st_size
		flist[x][1]=cnt
	total=sum([x[1]for x in flist.values()])
	Logs.debug('lru: Cache size is %r'%total)
	if total>=CACHESIZE:
		Logs.debug('lru: Trimming the cache since %r > %r'%(total,CACHESIZE))
		lst=[(p,v[0],v[1])for(p,v)in flist.items()]
		lst.sort(key=lambda x:x[1])
		lst.reverse()
		while total>=CACHESIZE*CLEANRATIO:
			(k,t,s)=lst.pop()
			p=os.path.join(CACHEDIR,k)
			v=p+'.del'
			try:
				os.rename(p,v)
			except:
				pass
			else:
				try:
					shutil.rmtree(v)
				except:
					Logs.warn('If you ever see this message, report it (%r)'%v)
			total-=s
			del flist[k]
	Logs.debug('lru: Total at the end %r'%total)
Build.BuildContext.raw_compile=Build.BuildContext.compile
Build.BuildContext.compile=compile
Build.BuildContext.sweep=sweep
