#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from flask import Flask, Response, request
import subprocess
import sys
import shutil
import os
import json
import pam
import getpass
app = Flask(__name__)

@app.route('/')
def index():
    return 'index'

@app.route('/foo')
def test():
    return 'test'

@app.route('/user')
def user():
    return getpass.getuser()

@app.route('/git-root')
def git_root():
    return os.path.expanduser(os.path.join("~", "git"))

@app.route('/bitquant-root')
def bitquant_root():
    return os.path.expanduser(os.path.join("~", "git", "bitquant"))

@app.route('/cgi-root')
def cgi_root():
    return "/var/www/cgi-bin/bittrader"

@app.route("/refresh-scripts")
def refresh_scripts():
    retval = "Refreshing scripts\n"
    local_cgi_path = os.path.join(bitquant_root(),
                                  "web", "cgi-bin", "bittrader")
    for file in os.listdir(cgi_root()):
        retval = retval + "removing old " + file + "\n"
        os.remove(os.path.join(cgi_root(), file))
    for file in os.listdir(local_cgi_path):
        if file.endswith(".sh") or file.endswith(".py"):
            shutil.copy2(os.path.join(local_cgi_path, file), cgi_root())
            retval = retval + "copying " + file + "\n"
    return retval + "(done)"

@app.route("/version")
def version():
    os.chdir(bitquant_root())
    retval = {
        "user" : user(),
        "version" : subprocess.check_output(["git", "rev-parse",
                                             "--short", "HEAD"]).strip(),
        "bootstrapped" : os.path.exists(os.path.join(bitquant_root(),
                                                     "web", "bootstrap.done"))
        }
    return Response(json.dumps(retval), mimetype='application/json')
        
if __name__ == '__main__' and len(sys.argv) == 1:
    from wsgiref.handlers import CGIHandler
    CGIHandler().run(app)
elif __name__ == '__main__' and sys.argv[1] == "refresh-scripts":
    print refresh_scripts()

@@ -163,19 +163,8 @@
 son%0A
-import pam%0A
 impo
@@ -174,16 +174,18 @@
 getpass%0A
+%0A%0A
 app = Fl
@@ -197,16 +197,47 @@
 _name__)
+%0Adefault_password = %22cubswin:)%22
 %0A%0A@app.r

remove pam import