Skip to content
Snippets Groups Projects
wgcli.py 3.34 KiB
Newer Older
Stephane Thiell's avatar
Stephane Thiell committed
#!/usr/bin/env python
# CLI tool over Stanford Workgroup
# Demo script for workgroup.py
# Aug 12, 2015
# Written by S. Thiell <sthiell@stanford.edu>

import atexit
import os
import sys

from srcc.utils.workgroup import SUWorkgroupManager, SUWorkgroupAPIError


WG_CERT = 'certs/xstream.stanford.edu.cert'
WG_CERT_KEY = 'certs/xstream.key'

HISTFILE = os.path.join(os.path.expanduser("~"), ".wgcli_history")


def readline_setup():
    """Configure readline to automatically load and save a history file."""
    import readline
    readline.parse_and_bind("tab: complete")
    readline.set_completer_delims("")
    try:
        readline.read_history_file(HISTFILE)
    except IOError:
        pass
    atexit.register(readline.write_history_file, HISTFILE)


def print_workgroup(workgroupt, workgroup, maxdepth=1000, depth=1):
    print "%s%s: %s" % ("\t" * (depth - 1), workgroupt, workgroup.name)

    if depth > maxdepth:
        return

    print "%sMEMBER: %s" % ("\t" * depth, ', '.join(workgroup.members))
    for app in workgroup.applications:
        print "%sAPP: %s" % ("\t" * depth, app)
    for admin in workgroup.applications:
        print "%sADMIN: %s" % ("\t" * depth, admin)
    for admin_app in workgroup.admin_apps:
        print "%sADMIN_APP: %s" % ("\t" * depth, admin_app)

    if depth > maxdepth:
        return

    for child in workgroup.workgroups.values():
        print_workgroup('WORKGROUP', child, maxdepth, depth+1)

    for child in workgroup.admin_workgroups.values():
        print_workgroup('ADMIN_WORKGROUP', child, maxdepth, depth+1)

def cmd_tree(manager, workgroup):
    print_workgroup('WORKGROUP', manager.workgroup(workgroup))

def cmd_ls(manager, workgroup):
    print_workgroup('WORKGROUP', manager.workgroup(workgroup), maxdepth=1)

def main():
    """wgcli script main entry point"""
    try:
        readline_setup()
    except ImportError:
        print >>sys.stderr, "Warning: failed to initialize readline"

    manager = SUWorkgroupManager(cert=WG_CERT, cert_key=WG_CERT_KEY)

    print "######################################"
    print "Welcome to Stanford Workgroup CLI tool"
    print "######################################"
    print
    print "Commands are:"
    print "  workgroup <workgroup>"
    print "     to select a working workgroup"
    print "  ls"
    print "     to list members"
    print "  tree"
    print "     to list members recursively"
    print

    workgroup = None

    cmd = ''
    while cmd.lower() != 'quit':
        if workgroup:
            prompt = "%s> " % workgroup.name
        else:
            prompt = "#> "
        cmd = raw_input(prompt)
        ##print "Got cmd %s" % cmd
        args = cmd.split()
        if len(args) == 0:
            continue
        cmd = args[0]
        try:
            if cmd == 'workgroup':
                workgroup = manager.workgroup(args[1])
            elif cmd == 'add':
                workgroup.add_member(args[1])
            elif cmd == 'remove':
                workgroup.remove_member(args[1])
            elif cmd == 'reload':
                manager.clear()
                workgroup = manager.workgroup(workgroup.name)
            elif cmd == 'ls':
                cmd_ls(manager, workgroup.name)
            elif cmd == 'tree':
                cmd_tree(manager, workgroup.name)
        except SUWorkgroupAPIError, exc:
            print exc


if __name__ == '__main__':
    main()