# -*- coding: utf-8 -*-
# Copyright (C) 2008-2009, Luís Pedro Coelho <lpc@cmu.edu>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
'''
Options

Variables
---------

    * jugdir: main jug directory.
    * jugfile: filesystem name for the Jugfile
    * cmd: command to run.
    * aggressive_unload: --aggressive-unload
    * invalid_name: --invalid
    * argv: Arguments not captured by jug (for script use)
'''
from __future__ import division
import logging
import string

jugdir = 'jugdata'
jugfile = 'jugfile.py'
cmd = None
aggressive_unload = False
invalid_name = None
argv = None
other_args = argv

_Commands = ('execute','status','stats','cleanup','count','invalidate')
_Usage_string = \
'''python %s COMMAND OPTIONS...

Possible commands:
* execute
    Execute tasks
* status:
    Print status
* counts:
    Simply count tasks
* cleanup:
    Cleanup
* stats
    Print statistics [Not implemented]
* invalidate
    Invalidate the results of a task

Options:
--jugfile=JUGFILE
    Name of the jugfile to use (if not given, use jugfile.py)
--aggressive-unload
    Aggressively unload data from memory
--invalid=TASK-NAME
    Task name to invalidate (for invalidate command only)
--jugdir=JUGDIR
    Directory in which to save intermediate files
--verbose=LEVEL
    Verbosity level ('DEBUG', 'INFO', 'QUIET')
'''

def usage():
    '''
    usage()

    Print an usage string and exit.
    '''
    import sys
    print _Usage_string % sys.argv[0]
    sys.exit(1)

def parse():
    '''
    options.parse()

    Parse the command line options and set the option variables.
    '''
    import optparse
    global jugdir, jugfile, cmd, aggressive_unload, invalid_name, argv, other_args
    parser = optparse.OptionParser()
    parser.add_option('--aggressive-unload',action='store_true',dest='aggressive_unload',default=False)
    parser.add_option('--invalid',action='store',dest='invalid_name',default=None)
    parser.add_option('--jugdir',action='store',dest='jugdir',default='jugdata/')
    parser.add_option('--verbose',action='store',dest='verbosity',default='QUIET')
    options,args = parser.parse_args()
    if not args:
        usage()
        return

    cmd = args.pop(0)
    jugfile = args.pop(0)

    if cmd not in _Commands:
        usage()
        return
    if options.invalid_name and cmd != 'invalidate':
        usage()
        return
    if cmd == 'invalidate' and not options.invalid_name:
        usage()
        return
    try:
        nlevel = {
            'DEBUG' : logging.DEBUG,
            'INFO' : logging.INFO,
        }[string.upper(options.verbosity)]
        root = logging.getLogger()
        root.level = nlevel
    except KeyError:
        pass

    aggressive_unload = options.aggressive_unload
    invalid_name = options.invalid_name
    argv = args
    other_args = argv
    jugdir = options.jugdir

# vim: set ts=4 sts=4 sw=4 expandtab smartindent:

@@ -1728,16 +1728,24 @@
 COMMAND 
+JUGFILE 
 OPTIONS.
@@ -1752,18 +1752,9 @@
 ..%0A%0A
-Possible c
+C
 omma
@@ -1767,17 +1767,18 @@
  execute
-%0A
+: 
     Exec
@@ -1796,17 +1796,18 @@
  status:
-%0A
+  
     Prin
@@ -1824,21 +1824,22 @@
  counts:
-%0A
     
+  
 Simply c
@@ -1859,17 +1859,17 @@
 cleanup:
-%0A
+ 
     Clea
@@ -1876,55 +1876,8 @@
 nup%0A
-* stats%0A    Print statistics %5BNot implemented%5D%0A
 * in
@@ -1884,19 +1884,17 @@
 validate
-%0A  
+:
   Invali
@@ -1934,88 +1934,8 @@
 ns:%0A
---jugfile=JUGFILE%0A    Name of the jugfile to use (if not given, use jugfile.py)%0A
 --ag
@@ -3081,16 +3081,60 @@
 ugfile =
+ 'jugfile.py'%0A    if args:%0A        jugfile =
  args.po

Better help output