sf.py
#!/usr/bin/env python3
"""
This script provides various utility functions and is intended to have similar capabilities to the bash scripts ext_*.sh, with additional features.
Functions:
source_env():
Loads environment variables from a env file. If the file does not exist, it prints an error message.
download_file(url, filename):
Downloads a file from the specified URL and saves it to the specified filename. If an error occurs during the download, it prints an error message and exits the script.
main():
Entry point of the script. Prints "Hello World" and calls the source_env() function to load environment variables.
"""
# import sys, os, typer
# sys.path.append(os.path.join(os.path.dirname(__file__), 'sf_functions'))
import os, sys, argparse
import sf_utils
sf = sf_utils.sf()
sf.source_env()
# Set the path to the bin and scripts directory
PROGRAM_NAME = "sf"
SF_VERSION = os.getenv("SF_VERSION")
PROFILE_ENV_FILE = os.getenv("PROFILE_ENV_FILE")
BIN_DIR = os.getenv("BIN_DIR")
SCRIPTS_DIR = os.getenv("SCRIPTS_DIR")
VALID_COMMANDS = {
# "help" : {
# "command": "help",
# "help" : "Display help message",
# "usage" : "sf help <command>",
# "choices": None,
# },
"debug" : {
"command": "debug",
"help" : "Print debug information",
"usage" : PROGRAM_NAME + " debug <command>",
"choices": None,
},
"bin" : {
"command": "bin",
"help" : "Run a binary from the bin directory",
"usage" : PROGRAM_NAME + " bin <command>",
"choices": os.listdir(BIN_DIR),
},
"script": {
"command": "script",
"help" : "Run a script from the scripts directory",
"usage" : PROGRAM_NAME + " script <command>",
"choices": os.listdir(SCRIPTS_DIR),
},
"docker": {
"command": "docker",
"help" : "Docker utility functions",
"usage" : PROGRAM_NAME + " docker <command>",
"choices": None,
},
"show" : {
"command": "show",
"help" : "Show information",
"usage" : PROGRAM_NAME + " show <command>",
"choices": None,
},
}
COMMAND_MAPPINGS = {
"help" : "print_help",
"version": "print_help",
}
program = argparse.ArgumentParser(prog=PROGRAM_NAME, description="Utility script with various functions.", usage=F"{PROGRAM_NAME} <command>")
subparsers = program.add_subparsers(help="Command to execute", dest="command", metavar="command")
program.add_argument("-v", "--version", action="version", version=f"{PROGRAM_NAME} {SF_VERSION}")
for key, command in VALID_COMMANDS.items():
cmd = command["command"]
helptext = command["help"]
usagetext = command["usage"]
# choices = command["choices"]
thisCommand = subparsers.add_parser(cmd, help=helptext, usage=usagetext)
# thisCommand.add_argument("-h", "--help", help=f"Help for {cmd}", nargs="?", default=None)
thisCommand.add_argument("params", help="Parameters to pass to command", nargs="*", default=None)
args = program.parse_args()
# ======================================================================= #
# FUNCTION: print_help #
# ======================================================================= #
def print_help(subcommand : str = None):
global program
sf.print(f"{PROGRAM_NAME} - Utility script with various functions.")
sf.print(f"Version: {SF_VERSION}")
if not subcommand or subcommand not in VALID_COMMANDS:
if subcommand:
sf.print(f"Invalid command: {subcommand}")
program.print_help()
return
if subcommand in VALID_COMMANDS:
helptext = VALID_COMMANDS[subcommand]["help"]
usage = VALID_COMMANDS[subcommand]["usage"]
sf.print(f"Help for {subcommand}:")
sf.print(f" {subcommand}: {helptext}")
sf.print(f" usage: {usage}")
return
# ======================================================================= #
# FUNCTION: print_debug #
# ======================================================================= #
def print_debug():
sf.print(f"Python version: {sys.version}")
sf.print(f"Scripts directory: {SCRIPTS_DIR}")
sf.print(f"Environment variables:")
for key, value in os.environ.items():
sf.print(f"{key}: {value}")
# ======================================================================= #
# FUNCTION: run_script #
# ======================================================================= #
def run_script(script : str = None):
if not script:
sf.print("No script specified")
return
script_path = os.path.join(SCRIPTS_DIR, script)
if not os.path.isfile(script_path):
sf.print(f"Script not found: {script_path}")
return
os.system(f"bash {script_path} {' '.join(args.params[1:])}")
pass
# ======================================================================= #
# FUNCTION: run_bin #
# ======================================================================= #
def run_bin():
pass
# ======================================================================= #
# FUNCTION: command #
# ======================================================================= #
def command(command : str = None, *args, **kwargs):
if not command:
sf.print("No command specified")
return
func_name = f"run_{command}"
if func_name in globals() and callable(globals()[func_name]):
return globals()[func_name](*args, **kwargs)
sf.print(f"Command: {command}")
pass
# ======================================================================= #
# FUNCTION: main #
# ======================================================================= #
def main():
if not args.command or args.command not in VALID_COMMANDS:
program.print_help()
if args.command != None:
sf.print(f"Invalid command: {args.command}")
return
return command(args.command)
if __name__ == "__main__":
main()