from docopt import docopt
from invoke import run

import json

from os import path

from bootstrap import quickstart


def main():
    """sparse: manage streamparse clusters.

    sparse provides a front-end to streamparse, a framework for creating Python
    projects for running, debugging, and submitting computation topologies against
    real-time streams, using Apache Storm.

    It requires the lein (Clojure build tool) to be on your $PATH, and uses
    lein and Clojure under the hood for JVM interop.

    Usage:
        sparse quickstart <proj_name>
        sparse setup [-e ENV]
        sparse run [-e ENV] [-t TIME]
        sparse debug [-e ENV]
        sparse kill [-e ENV]
        sparse restart [-e ENV]
        sparse attach [-e ENV]
        sparse list [-e ENV]
        sparse submit [-e ENV]
        sparse logs [-e ENV]
        sparse (-h | --help)
        sparse --version

    Options:
        -h --help         Show this screen.
        --version         Show version.
        -e ENV            Set environment; as described in config.json [default: local].
        -t TIME           Time (in milliseconds) to keep cluster running [default: 5000].
        --verbose         Verbose output.
        --debug           Debug output.
    """
    args = docopt(main.__doc__, version="sparse 0.1")
    if args["run"]:
        print "Running wordcount topology..."
        cfg = json.load(open("config.json"))
        topo_dir = cfg["topology_specs"]
        topo_first = cfg["topologies"][0] + ".clj"
        print path.join(topo_dir, topo_first)
        run("invoke stormlocal --topology={topology} --time={time}".format(
            topology=path.join(topo_dir, topo_first), time=args["-t"]))
    elif args["debug"]:
        print "Debugging wordcount topology..."
        run("lein run -s topologies/wordcount.clj")
    elif args["list"]:
        print "invoke tasks:"
        run("invoke -l")
        print
        print "fabric tasks:"
        run("fab -l")
    elif args["setup"]:
        print "Setting up virtualenv on remote cluster..."
        run("fab workers setup_virtualenv")
    elif args["quickstart"]:
        quickstart(args['<proj_name>'])


if __name__ == "__main__":
    main()

@@ -112,16 +112,298 @@
 start%0A%0A%0A
+# XXX: these are commands we're working on still%0ATODO_CMDS = %22%22%22%0A        sparse setup %5B-e ENV%5D%0A        sparse debug %5B-e ENV%5D%0A        sparse kill %5B-e ENV%5D%0A        sparse restart %5B-e ENV%5D%0A        sparse attach %5B-e ENV%5D%0A        sparse list %5B-e ENV%5D%0A        sparse logs %5B-e ENV%5D%0A%0A%22%22%22%0A%0A%0A
 def main
@@ -841,38 +841,8 @@
 me%3E%0A
-        sparse setup %5B-e ENV%5D%0A
     
@@ -896,192 +896,12 @@
 e de
-bug %5B-e ENV%5D%0A        sparse kill %5B-e ENV%5D%0A        sparse restart %5B-e ENV%5D%0A        sparse attach %5B-e ENV%5D%0A        sparse list %5B-e ENV%5D%0A        sparse submit %5B-e ENV%5D%0A        sparse logs
+ploy
  %5B-e
@@ -1782,132 +1782,8 @@
 %5D))%0A
-    elif args%5B%22debug%22%5D:%0A        print %22Debugging wordcount topology...%22%0A        run(%22lein run -s topologies/wordcount.clj%22)%0A
     
@@ -1822,16 +1822,24 @@
  %22invoke
+ (local)
  tasks:%22
@@ -1900,16 +1900,25 @@
 %22fabric 
+(remote) 
 tasks:%22%0A
@@ -1940,135 +1940,8 @@
 b -l
-%22)%0A    elif args%5B%22setup%22%5D:%0A        print %22Setting up virtualenv on remote cluster...%22%0A        run(%22fab workers setup_virtualenv
 %22)%0A 

simplify sparse cmdln for deploy