Skip to content

Instantly share code, notes, and snippets.

@sarcilav
Created January 14, 2010 22:46
Show Gist options
  • Save sarcilav/277585 to your computer and use it in GitHub Desktop.
Save sarcilav/277585 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import urllib2
import os
import sys
if len(sys.argv) != 2:
print "Usage: " + sys.argv[0] + " url_of_base_dir"
sys.exit(1)
url = str(sys.argv[1])
htmlfile = "en.html"
urlread = lambda url: urllib2.urlopen(url).read()
s_parameters = "<td class=\"statText\">Parameters:</td>\n"
s_returns = "<td class=\"statText\">Returns:</td>\n"
s_previous = "<td class=\"statText\">"
s_post = "</td>"
s_html_eat_previous = "<tr><td class=\"statText\"><pre>"
s_html_eat_post = "</pre></td></tr>"
s_return = "Returns: "
#Toma el tipo del input y la string que lo representa y retorna la forma apropiada
def eat_that(type, s):
s = s.replace("\n","")
s = s.replace("&lt;", "<")
s = s.replace("&gt;", ">")
s = s.replace("&amp;", "&")
s = s.replace("&quot;", "\"")
s_ans = ""
if(type == "int"):
return s.replace(" ","")
elif(type == "double"):
return s.replace(" ","")
elif(type == "String"):
return s[s.find("\"")+1:s.rfind("\"")]
else :
s = s.replace("{","")
s = s.replace("}","")
list = s.split(",")
if len(list) == 1 and list[0] == "":
list = []
s_ans += str(len(list)) +"\n"
s_type = type.replace("[]","")
for i in list:
s_ans += eat_that(s_type,i)
s_ans += "\n"
return s_ans[0:len(s_ans)-1]
#Toma el html del enunciado del problema , la string que indica los parametros y retorna
#un vector de 2 posiciones la primera la string de la entrada y la segunda la string de la salida
def eat_cases(html,param, returns):
param = param.replace(" ","")
params = param.split(",")
n_params = len(params)
returns = returns.replace(" ","")
n_returns = 1
s_in = ""
s_out = ""
i_index = html.find(s_html_eat_previous)
cnt = 0
p = 0
while(i_index >= 0):
cnt += 1
i_index += len(s_html_eat_previous)
j_index = html.find(s_html_eat_post,i_index)
if( cnt % (n_params + n_returns) == 0):
s_out += eat_that(returns,html[i_index+len(s_return):j_index])
s_out += "\n"
else :
s_in += eat_that(params[p % n_params],html[i_index:j_index] )
s_in +="\n"
p += 1
i_index = html.find(s_html_eat_previous,j_index)
return [str(cnt / (n_params + n_returns)) + "\n" + s_in,s_out]
#Toma el html partido de la pagina inicial y busca los directorios y los crea
def deep_and_parse( val ):
i_first = val.find("\"")
i_second = val.find("\"",i_first + 1)
dir = val[i_first + 1:i_second]
html = urlread(url+dir+htmlfile)
i_params = html.find(s_parameters) + len (s_parameters) + len(s_previous)
j_params = html.find(s_post,i_params)
i_returns = html.find(s_returns) + len(s_returns) + len(s_previous)
j_returns = html.find(s_post,i_returns)
print html[i_params:j_params]
print html[i_returns:j_returns]
list = eat_cases(html, html[i_params:j_params], html[i_returns:j_returns])
dir = dir.lower()
try:
os.makedirs(dir)
except OSError:
pass
file = open(dir+"in","w")
file.writelines(list[0])
file.close()
file = open(dir+"out","w")
file.writelines(list[1])
file.close()
file = open(dir + "run.sh", "w")
s = """#!/bin/sh
./$1 < in > /tmp/out
diff /tmp/out out -y
"""
file.write(s)
file.close()
os.system("chmod 755 " + dir + "run.sh")
list = urlread(url).split("alt=\"[DIR]\"") #importa de 2 en adelante
list = list[2:len(list)]
for i in list:
deep_and_parse(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment