#! /usr/bin/env python
import re
import subprocess
import time
import datetime
import os

startTime = time.time()

MAX_l=3
MAX_m=11

MAX_cores=4 #number of processor cores on the machine

notSat='In all files "Property is satisfied."'

#make a pool of 4 processes
popen = range(0,MAX_cores)
for i in range(0,MAX_cores):
  popen[i] = subprocess.Popen(["echo","start"])

for l in range(MAX_l,0,-1):
  for m in range(MAX_m,0,-1):
    f = open('ZCfeasible34.xml')
    fileStr = f.read()
    f.close()

    [head, tail] = re.split('const int *l.*', fileStr, 1)
    fileStr = head + 'const int l = ' + str(l) + ';' + tail

    [head, tail] = re.split('const int *m.*', fileStr, 1)
    fileStr = head + 'const int m = ' + str(m) + ';' + tail

    f = open('ZCfeasible34_l=' + str(l) + 'm=' + str(m) + '.xml', 'w')
    f.write(fileStr)
    f.close()

    if os.access('ZCfeasible34_l=' + str(l) + 'm=' + str(m) + '.log', os.F_OK) == False :
      f = open('ZCfeasible34_l=' + str(l) + 'm=' + str(m) + '.log', 'w')
    else :
      print 'ZCfeasible34_l=' + str(l) + 'm=' + str(m) + '.log already completed'
      continue

    loop = True
    while loop:
      for i in range(0,MAX_cores):
        if popen[i].poll() == 0 :
          popen[i] = subprocess.Popen(["./memtime", "./verifyta", "-u", "ZCfeasible34_l=" + str(l) + "m=" + str(m) + ".xml", "mutex.q"], stdout=f, stderr=subprocess.STDOUT)
          loop = False
          break

        time.sleep(1)

#elapsedTime = timedelta(seconds = time.time()-startTime)
f = open('processingTime', 'w')
f.write(str(datetime.timedelta(seconds = time.time()-startTime)))
f.close()
