Created
February 21, 2010 02:57
-
-
Save qingfeng/310091 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# encoding: utf-8 | |
# Author: qingfeng@douban.com | |
from __future__ import with_statement | |
from optparse import OptionParser | |
import os | |
parser = OptionParser(version="new9 command v1.0 -- by qingfeng@douban.com") | |
parser.add_option("-p", "--install", | |
dest="pkg_name", | |
help="Auto install Mako and web.py") | |
parser.add_option("--start", | |
action="store_true", dest="start", | |
help="Start Dev Project") | |
parser.add_option("--rm", | |
action="store_true", dest="delete", | |
help="Delete Dev Project") | |
parser.add_option("--addurl", | |
dest="url", | |
help="Add url to project.") | |
(options, args) = parser.parse_args() | |
def install(pkg_name): | |
cmd = os.environ.get('EASY_INSTALL','easy_install') | |
print os.popen("%s %s" % (cmd,pkg_name)).read() | |
def startproject(): | |
"""Start Dev Project""" | |
if os.path.isdir("hello"): | |
print "hello project exists." | |
return 0 | |
os.mkdir("hello") | |
os.mkdir("hello/apps") | |
with open("hello/__init__.py","w") as f: | |
f.write('') | |
with open("hello/apps/__init__.py","w") as f: | |
f.write("__all__ = ['hello']") | |
with open("hello/apps/utils.py","w") as f: | |
f.write('''from web.contrib.template import render_mako | |
# input_encoding and output_encoding is important for unicode | |
# template file. Reference: | |
# http://www.makotemplates.org/docs/documentation.html#unicode | |
render = render_mako( | |
directories=['templates'], | |
input_encoding='utf-8', | |
output_encoding='utf-8', | |
) | |
def remove_self(params): | |
params.pop('self') | |
return params | |
''') | |
with open("hello/apps/hello.py","w") as f: | |
f.write('''# encoding: utf-8 | |
import web | |
from utils import render,remove_self | |
urls = ( | |
"", "hello" | |
) | |
class hello: | |
def GET(self): | |
#Please write your code here# | |
name = 'qingfeng' | |
#Please write your code here# | |
return render.hello(**remove_self(locals())) | |
runapp = web.application(urls, locals()) | |
''') | |
with open("hello/urls.py","w") as f: | |
f.write('''from apps import * | |
urls = ( | |
'/hello', hello.runapp, | |
) | |
''') | |
with open("hello/main.py","w") as f: | |
f.write('''#!/usr/bin/env python | |
# encoding: utf-8 | |
import web | |
from urls import urls | |
app = web.application(urls, globals(), autoreload=True) | |
if __name__ == "__main__": | |
app.run() | |
''') | |
os.chmod("hello/main.py",0755) | |
os.mkdir("hello/templates") | |
with open("hello/templates/hello.html","w") as f: | |
f.write(''' | |
Hello, ${name}. | |
''') | |
def deleteproject(): | |
import shutil | |
shutil.rmtree("hello") | |
def addurl(url): | |
"""Add url to project.""" | |
try: | |
from hello.apps import __all__ as allapps | |
except: | |
print "The project does not exist, please use the ./new9 - start building the project." | |
return 0 | |
allapps.append(url) | |
with open("hello/apps/__init__.py","w") as f: | |
f.write("__all__ = %s" % allapps) | |
with open("hello/urls.py","r") as f: | |
s = f.read() | |
s = s.replace(")",''' | |
'/%s',%s.runapp, | |
) | |
''' % (url,url)) | |
with open("hello/urls.py","w") as f: | |
f.write(s) | |
with open("hello/apps/%s.py" % url,"w") as f: | |
f.write('''# encoding: utf-8 | |
import web | |
from utils import render,remove_self | |
urls = ( | |
"", "%s" | |
) | |
class %s: | |
def GET(self): | |
#Please write your code here# | |
name = 'qingfeng' | |
#Please write your code here# | |
return render.hello(**remove_self(locals())) | |
runapp = web.application(urls, locals()) | |
''' % (url,url)) | |
with open("hello/templates/%s.html" % url,"w") as f: | |
f.write('Hello, ${name}.') | |
if __name__ == '__main__': | |
if options.pkg_name: | |
install(options.pkg_name) | |
if options.start: | |
startproject() | |
if options.delete: | |
deleteproject() | |
if options.url: | |
addurl(options.url) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment