lt.py 2.06 KB
Newer Older
Alan Marchiori's avatar
initial  
Alan Marchiori committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python3
"""lab tool [lt]
Bucknell University Computer Science (c) 2020

global configuration (dirs, etc) stored in config/__init__.py
commands are defiend in commands/

user config (after init) stored in config.local_conf.

you may need to chmod +x lt.py
"""

# Command Line Interface Creation Kit
# https://click.palletsprojects.com
import click

# Local class config info
import config
Alan Marchiori's avatar
Alan Marchiori committed
19
from config.echo import error
Alan Marchiori's avatar
initial  
Alan Marchiori committed
20
21
22
23

# CLI commands
import commands

Alan Marchiori's avatar
Alan Marchiori committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# logging
import traceback
import logging
import logging.handlers
import os
import platform

smtp_handler = logging.handlers.SMTPHandler(
    mailhost=("smtp.bucknell.edu", 25),
    fromaddr="labtool@eg.bucknell.edu",
    toaddrs=config.crash_reports,
    subject=u"labtool crash report!")

__version__ = '1.0.3'
Alan Marchiori's avatar
Alan Marchiori committed
38
__date__ = '2020-01-23T15:22:20.641522'
Alan Marchiori's avatar
Alan Marchiori committed
39
__user__ = 'cs206'
Alan Marchiori's avatar
push    
Alan Marchiori committed
40
__host__ = 'linuxremote3.bucknell.edu'
Alan Marchiori's avatar
initial  
Alan Marchiori committed
41
42
@click.group()
def main():
Alan Marchiori's avatar
Alan Marchiori committed
43
44
45
46
47
    """Bucknell University Computer Science lab tool [lt].

    Below is a short list of commands. Each command has an additional help
    page available (run "lt [COMMAND] --help").
    """
Alan Marchiori's avatar
initial  
Alan Marchiori committed
48
49
50
51
    # this is the main cli entry point, commands
    # are attached to this group through python magic (next lines)
    pass

Alan Marchiori's avatar
Alan Marchiori committed
52
53
54
@main.command()
def version():
    "Display the version of labtool"
Alan Marchiori's avatar
Alan Marchiori committed
55
56
57
58
59
    click.echo("labtool version {} built on {} by {}@{}.".format(
        __version__,
        __date__,
        __user__,
        __host__))
Alan Marchiori's avatar
Alan Marchiori committed
60

Alan Marchiori's avatar
initial  
Alan Marchiori committed
61
62
63
64
65
66
# attach all commands to the main cli entry group
for cmd in commands.__all__:
    main.add_command(getattr(commands,cmd))

# run the main cli
if __name__ =="__main__":
Alan Marchiori's avatar
Alan Marchiori committed
67
68
69
    logger = logging.getLogger()
    logger.addHandler(smtp_handler)

Alan Marchiori's avatar
initial  
Alan Marchiori committed
70
    # open primary userconfig context
Alan Marchiori's avatar
Alan Marchiori committed
71
72
73
74
75
76
77
78
79
80
81
82
83
    try:
        with config.UserConfig() as uc:
            main()
    except Exception as x:
        error("Oops--Something went wrong in labtool ({}).".format(str(x)))

        if 'show_exc' in uc.cfg:
            traceback.print_exc()
        else:
            logger.exception('{}@{}: Unhanded exception.'.format(
                os.environ['USER'],
                platform.node()
            ))