From 2ba2adae6525f9cf9169e7ef3d50eaabba57feb4 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 19:37:51 -0400 Subject: add lm-cli --- src/usr/bin/logout-manager-cli.py | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/usr/bin/logout-manager-cli.py b/src/usr/bin/logout-manager-cli.py index e69de29..db754b6 100755 --- a/src/usr/bin/logout-manager-cli.py +++ b/src/usr/bin/logout-manager-cli.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# File: logout-manager-cli.py +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2020-03-10 18:40 +# Title: cli logout manager +# Purpose: Feature completeness in this package +# History: +# Usage: +# logout-manager-cli.py +# Reference: +# https://stackoverflow.com/questions/39092149/argparse-how-to-make-mutually-exclusive-arguments-optional/39092229#39092229 +# https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 +# Improve: +# Dependencies: +# Devuan: python3-dotenv +# Documentation: + +import os, platform, sys, argparse +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib + +def eprint(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) + +logout_manager_cli_version="2020-03-10" + +parser = argparse.ArgumentParser(description="run logout-manager commands using cli") +parser.add_argument('action', help='which action to take',nargs='?', choices=('lock','logout','hibernate','shutdown','reboot')) +parser.add_argument("-d","--debug", nargs='?', default=0, type=int, choices=range(0,11), help="Set debug level.") +parser.add_argument("-n","--dryrun", action='store_true', help="only report. Useful for checking if hibernate is allowed.") +parser.add_argument("-V","--version", action="version", version="%(prog)s " + logout_manager_cli_version) + +args = parser.parse_args() + +# load configs +# in cli, must happen after arparse to benefit from debug value +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions + +# MAIN LOOP +allowed_actions=['lock','logout','shutdown','reboot'] +if config.can_hibernate: + allowed_actions.append('hibernate') + +if args.action in allowed_actions: + func = getattr(globals()['actions'],args.action) + func(config) +elif args.action: + eprint("Unable to take action: %s" % str(args.action)) + sys.exit(1) + +# if we get here, no action was used +parser.print_help() +sys.exit(2) -- cgit