from subprocess import Popen
import sys
import urllib2
import time
# Find hostname to use for passing to webdriver
resp=urllib2.urlopen('http://169.254.169.254/latest/meta-data/public-hostname')
PHOST=resp.read()

# Poll Hub interface to determine free/busy status of resources
def freeCheck():
	try:
        	response=urllib2.urlopen('http://localhost:4200/grid/console')
        	html=response.read()
        	exit
	except urllib2.URLError:
        	pass

	count = html.count("platform=")
	return count

# If resources available (first condition) add more requests
#   - rename whatever test you want to run to the 'sdtest.py' value
# Otherwise, exit and wait a while
#if 'type=WebDriver' in html:
freeCount=freeCheck()
if freeCount > 0:
	idx=freeCount
	while (idx > 0):
		#Popen('python gridExecute.py ' + str(freeCount) + ' 0 sdtest.py 4000000 '+PHOST+'&',shell=True)
        	Popen('python gridExecute.py 4 0 sdtest.py 4000000 '+PHOST+'&',shell=True)
		time.sleep(10)
		freeCount=freeCheck()
		if freeCount>0:
			idx = freeCount-4
			time.sleep(30)
		else:
			exit		

elif 'requests waiting for a slot to be free' in html:
        exit
else:
        exit

@@ -288,17 +288,24 @@
 heck():%0A
-%09
+        
 try:%0A   
@@ -309,17 +309,24 @@
         
-%09
+        
 response
@@ -388,17 +388,24 @@
         
-%09
+        
 html=res
@@ -429,15 +429,29 @@
     
-%09exit%0A%09
+        exit%0A        
 exce
@@ -483,16 +483,30 @@
     
-%09pass%0A%0A%09
+        pass%0A%0A        
 coun
@@ -533,17 +533,24 @@
 form=%22)%0A
-%09
+        
 return c
@@ -771,18 +771,21 @@
 Check()%0A
-if
+while
  freeCou
@@ -790,50 +790,54 @@
 ount
- %3E 0:%0A%09idx=freeCount%0A%09while (idx %3E 0):%0A%09%09#
+%3E0:%0A        if freeCount %3C 4:%0A                
 Pope
@@ -866,28 +866,18 @@
 py '
- + 
++
 str(
-freeCount) + 
+idx)+
 ' 0 
@@ -926,17 +926,89 @@
         
-%09
+        freeCount = 0%0A                exit%0A        else:%0A                
 Popen('p
@@ -1074,18 +1074,32 @@
 l=True)%0A
-%09%09
+                
 time.sle
@@ -1105,186 +1105,171 @@
 eep(
-10)%0A%09%09freeCount=freeCheck()%0A%09%09if freeCount%3E0:%0A%09%09%09idx = freeCount-4%0A%09%09%09time.sleep(30)%0A%09%09else:%0A%09%09%09exit%09%09%0A%0Aelif 'requests waiting for a slot to be free' in html:%0A        exit%0Aelse:%0A
+60)%0A                try:%0A                        freeCount=freeCheck()%0A                        print('fc:'+str(freeCount))%0A                except:%0A                
     

Update SD-SeGrid-Execute.py