Created
April 10, 2015 20:02
-
-
Save marcosbitetti/f9485398cc800c11bd87 to your computer and use it in GitHub Desktop.
Jade, Stylus and Coffeescript environment develompent server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import BaseHTTPServer | |
import SimpleHTTPServer | |
import time,os,sys | |
import SocketServer | |
import io,os,sys,re,fnmatch | |
import subprocess | |
def get_myme(file_name): | |
ext = file_name[file_name.rfind("."):len(file_name)] | |
tp = "application/octet-stream" | |
if ext==".txt": | |
tp="text/plain" | |
if ext==".asc": | |
tp="text/plain" | |
if ext==".dat": | |
tp="text/plain" | |
if ext==".ini": | |
tp="text/plain" | |
if ext==".ascii": | |
tp="text/plain" | |
if ext==".json": | |
tp="application/json" | |
if ext==".js": | |
tp="text/javascript" | |
if ext==".coffee": | |
tp="text/coffeescript" | |
if ext==".css": | |
tp="text/css" | |
if ext==".jpg": | |
tp="image/jpeg" | |
if ext==".jpeg": | |
tp="image/jpeg" | |
if ext==".jpe": | |
tp="image/jpeg" | |
if ext==".png": | |
tp="image/png" | |
if ext==".gif": | |
tp="image/gif" | |
if ext==".ico": | |
tp="image/ico" | |
if ext==".svg": | |
tp="image/svg+xml" | |
if ext==".swf": | |
tp="application/x-shockwave-flash" | |
if ext==".htm": | |
tp="text/html" | |
if ext==".html": | |
tp="text/html" | |
if ext==".rhtm": | |
tp="text/html" | |
if ext==".xml": | |
tp="text/xml" | |
if ext==".flv": | |
tp="video/x-flv" | |
if ext==".mp4": | |
tp="video/mp4" | |
if ext==".ogg": | |
tp="video/ogg" | |
if ext==".webml": | |
tp="video/webml" | |
if ext==".apk": | |
tp="application/vnd.android.package-archive" | |
return tp | |
# definicoes | |
PORT = 81 | |
HOST_NAME = '0.0.0.0' | |
MY_DIR = os.path.dirname(os.path.realpath(__file__)) | |
WEB_DIR = "%s%sweb" % (MY_DIR, os.sep) | |
SRC_DIR = "%s%ssrc" % (MY_DIR, os.sep) | |
if len( sys.argv )>1: | |
PORT = int(sys.argv[1]) | |
if len( sys.argv )>2: | |
WEB_DIR = "%s%s%s" % (MY_DIR, os.sep,sys.argv[2]) | |
if len( sys.argv )>3: | |
SRC_DIR = "%s%s%s" % (MY_DIR, os.sep,sys.argv[2]) | |
class WebHandler (BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_HEAD(s): | |
s.send_response(200) | |
s.send_header("Content-Type","text/html") | |
s.end_headers() | |
def do_GET(s): | |
str_file = s.path | |
if str_file=="/": | |
str_file="/index.html" | |
path = WEB_DIR + str_file | |
verifica_files() | |
if os.path.exists(path): | |
f = open(path, "rb") | |
content = f.read() | |
s.send_response(200) | |
s.send_header("Content-Type",get_myme(str_file)) | |
s.send_header("Content-Size",len(content)) | |
s.end_headers() | |
s.wfile.write(content) | |
f.close() | |
else: | |
if os.path.exists(WEB_DIR+"/404.html"): | |
f = open(WEB_DIR+"/404.html", "rb") | |
content = f.read() | |
s.send_response(404) | |
s.send_header("Content-Type","text/html") | |
s.send_header("Content-Size",len(content)) | |
s.end_headers() | |
s.wfile.write(content) | |
f.close() | |
else: | |
s.send_response(404) | |
s.send_header("Content-Type","text/html") | |
s.send_header("Content-Size","21") | |
s.end_headers() | |
s.wfile.write("404 - File not found.") | |
def run_command(command): | |
args = command.split(" ") | |
#com = args[0] | |
#del args[0] | |
#p = subprocess.Popen(args,0, None, | |
# None, | |
# subprocess.PIPE, | |
# subprocess.PIPE, | |
# None, | |
# False, | |
# True | |
# ) | |
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
msg, err = p.communicate() | |
return msg + "\n" + err | |
#out, err = p.communicate() | |
#pl = command.split(" ",2) | |
#out = '' | |
#try: | |
# out = subprocess.check_output(command,shell=True) #out | |
# return "%s" % out | |
#except subprocess.CalledProcessError as err: | |
# return out #err.output | |
def call_compiler(): | |
global commands | |
print "#" * 63 | |
print '# #' | |
print '# Files need be recompiled #' | |
print '# #' | |
print "#" * 63 | |
print "" | |
print "\n##### Call coffee ####\n" | |
print run_command(commands[0]) | |
print "\n##### Call jade ####\n" | |
print run_command(commands[1]) | |
print "\n##### Call stylus ####\n" | |
print run_command(commands[2]) | |
monta_cache() | |
def verifica_files(): | |
global file_cache | |
tot = 0 | |
for root,dir,files in os.walk(SRC_DIR): | |
for items in fnmatch.filter(files,"*"): | |
path = root + os.sep + items | |
info = os.stat(path) | |
tot = tot + 1 | |
for f in file_cache: | |
if f[0] == path: | |
if not f[1] == info.st_mtime: | |
call_compiler() | |
return | |
# verify if total of arquives is the same | |
if tot != len(file_cache): | |
call_compiler() | |
def monta_cache(): | |
global file_cache | |
del file_cache[:] | |
for root,dir,files in os.walk(SRC_DIR): | |
for items in fnmatch.filter(files,"*"): | |
path = root + os.sep + items | |
info = os.stat(path) | |
file_cache.append( [path,info.st_mtime] ) | |
#### | |
# Commandos para compilar | |
### | |
commands = ['','',''] | |
p = MY_DIR + os.sep + "compiler_command" | |
comp_content = """ | |
#compile coffee | |
coffee --bare --output ./web/lib --join main.js --compile ./src/ | |
#compile jade | |
jade src --out ./web | |
#compile stylus | |
stylus -c ./src --out ./web | |
""" | |
if os.path.exists(p+".sh"): | |
f = open(p+".sh","r") | |
comp_content = f.read() | |
f.close() | |
if os.path.exists(p+".bat"): | |
f = open(p+".bat","r") | |
comp_content = f.read() | |
f.close() | |
s = 0 | |
for lin in comp_content.replace("\r","").split("\n"): | |
if lin.find("compile")==1: | |
if lin.find("coffee")>1: | |
s = 1 # coffee | |
if lin.find("jade")>1: | |
s = 2 # jade | |
if lin.find("stylus")>1: | |
s = 3 # stylus | |
else: | |
if s>0: | |
if len(lin)>1: | |
commands[s-1] = lin | |
s = 0 | |
#### | |
# Start cache | |
#### | |
file_cache = [] | |
monta_cache() | |
#### | |
# Start server | |
#### | |
print "Server starts at %s" % time.asctime() | |
server_class = BaseHTTPServer.HTTPServer | |
httpd = server_class((HOST_NAME, PORT), WebHandler) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print "Server ends at %s" % time.asctime() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment