Skip to content

Commit

Permalink
Move more commands to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Erwan Loisant committed Mar 29, 2010
1 parent 7a1b7f5 commit 6d33362
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 230 deletions.
20 changes: 19 additions & 1 deletion framework/pym/commands/base.py
Expand Up @@ -13,14 +13,16 @@

from framework.pym.utils import *

NAMES = ['run', 'new', 'new,run', 'clean', 'clean,run', 'test', 'auto-test']
NAMES = ['run', 'new', 'new,run', 'clean', 'clean,run', 'test', 'auto-test', 'id']

def execute(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")

if command == 'id':
id(env)
if command == 'new' or command == 'new,run':
new(app, args, env)
if command == 'clean' or command == 'clean,run':
Expand Down Expand Up @@ -223,3 +225,19 @@ def autotest(app, args):
print "~"
kill(play_process.pid)
break

def id(play_env):
if not play_env["id"]:
print "~ framework ID is not set"
new_id = raw_input("~ What is the new framework ID (or blank to unset)? ")
if new_id:
print "~"
print "~ OK, the framework ID is now %s" % new_id
print "~"
open(play_env["id_file"], 'w').write(new_id)
else:
print "~"
print "~ OK, the framework ID is unset"
print "~"
if os.path.exists(play_env["id_file"]):
os.remove(play_env["id_file"])
24 changes: 23 additions & 1 deletion framework/pym/commands/daemon.py
Expand Up @@ -2,7 +2,7 @@
import subprocess
from framework.pym.utils import *

NAMES = ['start', 'stop', 'restart', 'pid']
NAMES = ['start', 'stop', 'restart', 'pid', 'out']

def execute(**kargs):
command = kargs.get("command")
Expand All @@ -18,6 +18,8 @@ def execute(**kargs):
restart(app)
if command == 'pid':
pid(app)
if command == 'out':
out(app)

def start(app, args):
app.check()
Expand Down Expand Up @@ -100,6 +102,26 @@ def pid(app):
print "~ PID of the running applications is %s" % pid
print "~ "

def out(app):
app.check()
if not os.path.exists(os.path.join(app.log_path(), 'system.out')):
print "~ Oops! %s not found" % os.path.normpath(os.path.join(app.log_path(), 'system.out'))
print "~"
sys.exit(-1)
sout = open(os.path.join(app.log_path(), 'system.out'), 'r')
try:
sout.seek(-5000, os.SEEK_END)
except IOError:
sout.seek(0)
while True:
where = sout.tell()
line = sout.readline().strip()
if not line:
time.sleep(1)
sout.seek(where)
else:
print line

def kill(pid):
if os.name == 'nt':
import ctypes
Expand Down
57 changes: 47 additions & 10 deletions framework/pym/commands/modulesrepo.py
@@ -1,13 +1,19 @@
# Display the modules list
import os
import sys
import zipfile
import urllib2
import shutil
import string

import sys, zipfile, urllib2
from framework.pym.utils import *
import framework.pym.simplejson as json

NM = ['new-module']
LM = ['list-modules', 'lm']
BM = ['build-modules', 'bm']
IM = ['install']

NAMES = LM + BM + IM
NAMES = NM + LM + BM + IM

# TODO: Make that configurable
modules_server = 'http://www.playframework.org'
Expand All @@ -16,14 +22,45 @@ def execute(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")

if command in LM:
if command in NM:
new(app, args, env)
elif command in LM:
list(app, args)
elif command in BM:
build(app, args)
elif command in IM:
install(app, args)

def new(app, args, play_env):
if os.path.exists(app.path):
print "~ Oops. %s already exists" % app.path
print "~"
sys.exit(-1)

print "~ The new module will be created in %s" % os.path.normpath(app.path)
print "~"
application_name = os.path.basename(app.path)
shutil.copytree(os.path.join(play_env["basedir"], 'resources/module-skel'), app.path)
# check_application()
replaceAll(os.path.join(app.path, 'build.xml'), r'%MODULE%', application_name)
replaceAll(os.path.join(app.path, 'conf/messages'), r'%MODULE%', application_name)
replaceAll(os.path.join(app.path, 'conf/routes'), r'%MODULE%', application_name)
replaceAll(os.path.join(app.path, 'conf/routes'), r'%MODULE_LOWERCASE%', string.lower(application_name))
os.mkdir(os.path.join(app.path, 'app/controllers/%s' % application_name))
os.mkdir(os.path.join(app.path, 'app/models/%s' % application_name))
os.mkdir(os.path.join(app.path, 'app/views/%s' % application_name))
os.mkdir(os.path.join(app.path, 'app/views/tags/%s' % application_name))
os.mkdir(os.path.join(app.path, 'src/play/modules/%s' % application_name))

print "~ OK, the module is created."
print "~ Start using it by adding this line in the application.conf modules list: "
print "~ module.%s=%s" % (application_name, os.path.normpath(app.path))
print "~"
print "~ Have fun!"
print "~"

def list(app, args):
print "~ You can also browse this list online at %s/modules" % modules_server
print "~"
Expand Down Expand Up @@ -72,37 +109,37 @@ def build(app, args):
version = raw_input("~ What is the module version number? ")
fwkMatch = raw_input("~ What are the playframework versions required? ")

build_file = os.path.join(application_path, 'build.xml')
build_file = os.path.join(app.path, 'build.xml')
if os.path.exists(build_file):
print "~"
print "~ Building..."
print "~"
os.system('ant -f %s -Dplay.path=%s' % (build_file, ftb) )
print "~"

mv = '%s-%s' % (os.path.basename(application_path), version)
mv = '%s-%s' % (os.path.basename(app.path), version)
print("~ Packaging %s ... " % mv)

dist_dir = os.path.join(application_path, 'dist')
dist_dir = os.path.join(app.path, 'dist')
if os.path.exists(dist_dir):
shutil.rmtree(dist_dir)
os.mkdir(dist_dir)

manifest = os.path.join(application_path, 'manifest')
manifest = os.path.join(app.path, 'manifest')
manifestF = open(manifest, 'w')
manifestF.write('version=%s\nframeworkVersions=%s\n' % (version, fwkMatch))
manifestF.close()

zip = zipfile.ZipFile(os.path.join(dist_dir, '%s.zip' % mv), 'w', zipfile.ZIP_STORED)
for (dirpath, dirnames, filenames) in os.walk(application_path):
for (dirpath, dirnames, filenames) in os.walk(app.path):
if dirpath == dist_dir:
continue
if dirpath.find('/.') > -1 or dirpath.find('/tmp/') > -1 or dirpath.find('/test-result/') > -1 or dirpath.find('/logs/') > -1 or dirpath.find('/eclipse/') > -1 or dirpath.endswith('/test-result') or dirpath.endswith('/logs') or dirpath.endswith('/eclipse') or dirpath.endswith('/nbproject'):
continue
for file in filenames:
if file.find('~') > -1 or file.endswith('.iml') or file.startswith('.'):
continue
zip.write(os.path.join(dirpath, file), os.path.join(dirpath[len(application_path):], file))
zip.write(os.path.join(dirpath, file), os.path.join(dirpath[len(app.path):], file))
zip.close()

os.remove(manifest)
Expand Down
13 changes: 13 additions & 0 deletions framework/pym/commands/secret.py
@@ -0,0 +1,13 @@
from framework.pym.utils import *

NAMES = ['secret']

def execute(**kargs):
app = kargs.get("app")

app.check()
print "~ Generating the secret key..."
sk = secretKey()
replaceAll(os.path.join(app.path, 'conf', 'application.conf'), r'application.secret=.*', 'application.secret=%s' % sk)
print "~ Keep the secret : %s" % sk
print "~"
64 changes: 64 additions & 0 deletions framework/pym/commands/status.py
@@ -0,0 +1,64 @@
import os, os.path
import shutil
import getopt
import hmac
import urllib2
from hashlib import sha1 as sha

from framework.pym.utils import *

NAMES = ['status', 'st']

def execute(**kargs):
app = kargs.get("app")
args = kargs.get("args")
play_env = kargs.get("env")

url = ''
secret_key = ''

try:
optlist, args = getopt.getopt(remaining_args, '', ['url=', 'secret='])
for o, a in optlist:
if o in ('--url'):
if a.endswith('/'):
url = a + '@status'
else:
url = a + '/@status'
if o in ('--secret'):
secret_key = a
except getopt.GetoptError, err:
print "~ %s" % str(err)
print "~ "
sys.exit(-1)

if not url or not secret_key:
app.check()
if not url:
http_port = int(play_app.readConf('http.port'))
url = 'http://localhost:%s/@status' % http_port
if not secret_key:
secret_key = play_app.readConf('application.secret')

hm = hmac.new(secret_key, '@status', sha)
authorization = hm.hexdigest()

try:
proxy_handler = urllib2.ProxyHandler({})
req = urllib2.Request(url)
req.add_header('Authorization', authorization)
opener = urllib2.build_opener(proxy_handler)
status = opener.open(req);
print '~ Status from %s,' % url
print '~'
print status.read()
print '~'
except urllib2.HTTPError, e:
print "~ Cannot retrieve the application status... (%s)" % (e.code)
print "~"
sys.exit(-1)
except urllib2.URLError, e:
print "~ Cannot contact the application..."
print "~"
sys.exit(-1)
print

0 comments on commit 6d33362

Please sign in to comment.