import sys,os,re,datetime,psem min_date=datetime.datetime(1,1,1,0,0,0); max_date=datetime.datetime(9999,12,31,23,59,59); n_logs=0; uid=os.getuid(); gid=os.getgid(); pid=os.getpid(); shell="/bin/ksh"; def compress_log(server, year, month, day, hour, type, log): global min_date global n_logs p=os.path.join(os.getcwd(),year,month,day) log_date=datetime.datetime(int(year),int(month),int(day)) if log_date < min_date: min_date = log_date n_logs += 1 if not os.path.isdir(p): try: os.makedirs(p,0750) os.chown(p,uid,gid) except OSError,e: if not os.path.isdir(p): raise(e) return(-1) cmd = 'bzip2 '+log+' && chown `id -un`:`id -gn` ' + log + '.bz2 && chmod 0640 '+ log + '.bz2 && if [ "$p" != "$CWD" ]; then mv ' + log + '.bz2' + ' ' + p + '; fi;' pid = psem.fork() if pid == 0: os.execl(shell,shell,'-c',cmd) elif pid == -1: s = os.system(shell + " -c '" + cmd + "'") if os.WEXITSTATUS(s) != 0: return( os.WEXITSTATUS(s) ) return(0); if __name__ == "__main__": os.write(1, "Do you really want to run test using 16 1MB log files ? Y/N") ans=os.read(0,1) are=re.compile("^[Yy]") if are.search(ans): os.write(1,'generating files 0..15'+"\n") for i in range( 0 , 16 ) : os.write(1,'generating file '+str(i)+"\n") pid = psem.fork() if pid == 0 : os.execl( shell, shell, '-c', 'dd if=/dev/urandom bs=1000000 count=1 of=app'+str(i)+'_20090407.0'+str(i)+'.log') elif pid == -1: os.system( shell +' -c "dd if=/dev/urandom bs=1000000 count=1 of=app'+str(i)+'_20090407.0'+str(i)+'.log"') if pid == 0 : sys.exit(0) psem.wait() psem.wait(psem.ALL,psem.BLOCK) os.write(1,'compressing files 0..15\n') for i in range( 0 , 16 ) : os.write(1,'compressing file '+str(i)+"\n") compress_log('app'+str(i),'2009','07', '04', '0'+str(i), 'bz2', 'app'+str(i)+"_20090407.0"+str(i)+".log" ) psem.wait(psem.ALL,psem.BLOCK) else: os.write(1,"OK, test NOT done.");