Skip to content

Instantly share code, notes, and snippets.

@pklaus
Last active April 15, 2019 09:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/698c7c99b0ef11a5a1b9a03e5892b2de to your computer and use it in GitHub Desktop.
Save pklaus/698c7c99b0ef11a5a1b9a03e5892b2de to your computer and use it in GitHub Desktop.
A utility to run different instructions found in a configurable folder (env var ANYRUN_INSTRUCTIONS_FOLDER).
#!/bin/env python
"""
Run a script given as argument.
./anyrun your_script_file
This would check the search folder (--search-folder or env var ANYRUN_SEARCH_FOLDER)
for a file named your_script_file and execute it in a shell environment.
To use this tool in multiple contexts on your machine, you can create a wrapper for it.
For example, save this wrapper as `displays` and you can quickly creating a tool to
setup your monitors depending on where you are:
#!/bin/bash
export ANYRUN_CMD='displays'
export ANYRUN_SEARCH_FOLDER='~/.displays/'
./anyrun "$@"
In `~/.displays/` you could then put some scripts calling xrandr and setting up your
desired environment when you call `displays laptop` or `displays workspace`.
Relies on click. Get it with: `pip install click`
"""
import click
import os, subprocess, enum, sys
class ExitCode(enum.IntEnum):
MISSING_ARGUMENT = 2
SCRIPT_FILE_DOESNT_EXIST = 64
SCRIPT_EXECUTION_FAILED = 65
ENCODING_ERROR = 66
UNKNOWN_ERROR = 128
@click.command()
@click.argument('script_file')
@click.option('--search-folder', default='~/.anyrun', envvar='ANYRUN_SEARCH_FOLDER')
@click.option('--encoding', default='utf-8', envvar='ANYRUN_ENCODING')
def anyrun(script_file, search_folder, encoding):
script_file = os.path.join(search_folder, script_file)
try:
with open(os.path.expanduser(script_file), 'r') as f:
commands = f.read()
except FileNotFoundError:
click.echo(f'Script file {script_file} not found', err=True)
exit(ExitCode.SCRIPT_FILE_DOESNT_EXIST)
# OK, we're fine, let's go...
try:
result = subprocess.run(commands, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
click.echo(f'Running the script found in {script_file} '
f'resulted in a return code of {e.returncode}', err=True)
if e.stderr:
click.echo(f'stderr output:', err=True)
click.echo(e.stderr, err=True)
exit(ExitCode.SCRIPT_EXECUTION_FAILED)
try:
output = result.stdout.decode(encoding)
except ValueError as e:
click.echo(f'Couln\'t decode the output of the script with the '
f'desired encoding {encoding}.', err=True)
exit(ExitCode.ENCODING_ERROR)
click.echo(output, nl=False)
if __name__ == "__main__":
try:
anyrun(prog_name=os.environ.get('ANYRUN_CMD', 'anyrun'))
except SystemExit as e:
# If we quit due to an ExitCode we want to inform the user about it:
if e.code != 0:
try:
e.code = ExitCode(e.code)
except:
e.code = ExitCode.UNKNOWN_ERROR
click.echo(f'Exiting with error return code {e.code.value} ({e.code.name})', err=True)
exit(e.code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment