Created
March 25, 2011 13:11
-
-
Save gilsondev/886813 to your computer and use it in GitHub Desktop.
This script prepares the development environment web2py. It downloads, decompresses to start their projects.
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 | |
#-*- coding:utf-8 -*- | |
# | |
# date: 20/11/2009 | |
# author: italo moreira campelo maia (italo maia) | |
# website: http://italomaia.com | |
# blog: http://eusouolobomau.blogspot.com/ | |
# | |
import os | |
import sys | |
import re | |
import getopt | |
import urllib | |
import logging | |
from StringIO import StringIO | |
from zipfile import ZipFile | |
from os import path | |
SOURCE_FILE = "http://www.web2py.com/examples/static/web2py_src.zip" | |
BASE_DIR = path.abspath(path.dirname(__file__)) | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hvp:", ["help", "verbose", "startproject="]) | |
except getopt.GetoptError, err: | |
print str(err) | |
sys.exit(1) | |
for opt, arg in opts: | |
if opt in ("-v", "--verbose"): | |
logging.basicConfig(level=logging.DEBUG) | |
elif opt in ("-h", "--help"): | |
help() | |
elif opt in ("-p", "--startproject"): | |
start_project(arg) | |
def start_project(prj_name): | |
prj_name = slugify(prj_name) | |
web2py_path = path.join(BASE_DIR, "web2py") | |
prj_path = path.join(BASE_DIR, prj_name) | |
if path.exists(prj_path): | |
print "Não é possíve criar o projeto pois já existe um diretório chamado %(prj)s na pasta atual." % {"prj":prj_name} | |
sys.exit(1) | |
elif path.exists(web2py_path): | |
print "Já existe um diretório web2py no diretório atual. Apenas renomeando..." | |
os.rename(web2py_path, prj_path) | |
print "Projeto web2py criado com sucesso" | |
else: | |
logging.info("Nome do projeto: %(prj)s" % {"prj":prj_name} ) | |
# lendo do repositório | |
logging.debug("Lendo o repositório (pode demorar um pouco)") | |
source = urllib.urlopen(SOURCE_FILE) | |
data = source.read() | |
buff = StringIO(data) | |
# fanzendo o unzip | |
logging.debug("Descompactando...") | |
zipfile = ZipFile(buff) | |
zipfile.extractall() | |
# nomeando o projeto | |
os.rename(web2py_path, prj_path) | |
print "Projeto web2py criado com sucesso" | |
def missing_argument(action): | |
print """A ação %(action)s exige um argumento.""" % {"action":action} | |
sys.exit(1) | |
def too_many_arguments(action, n): | |
print "%(action)s exige apenas %(n)d argumento(s)." % {"action":action, "n":n} | |
sys.exit(1) | |
def help(): | |
print """web2py-admin.py é um script que facilita a criação de um | |
novo projeto web2py. Ele baixa e descompacta, fácilmente, a última | |
versão do web2py para você. | |
Opções: | |
-p/--startproject - cria um novo projeto. Arg: nome do projeto | |
-v/--verbose - ativa o modo verborrágico | |
-h/--help - mensagem de ajuda do script. Arg: sem argumentos | |
Sintaxe: | |
"web2py-admin <action> <?argument>" | |
Exemplos: | |
> web2py-admin -p nome-do-projeto # ou | |
> web2py-admin --startproject nome-do-projeto | |
> web2py-admin -h # ou | |
> web2py-admin --help | |
""" | |
sys.exit(0) | |
def slugify(value): | |
"Passa uma string para o formato slug" | |
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) | |
return re.sub('[-\s]+', '-', value) | |
if __name__=="__main__": | |
if len(sys.argv[1:])==0: | |
help() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment