Skip to main content

Platform agnostic command execution and privilege elevation

Project description

command_runner

A toolkit for rapid platform agnostic development

License Percentage of issues still open Maintainability codecov Build Status GitHub Release

command_runner is solution for rapid development that tries to solve various problems a developper may face among:

  • Handling of all possible subprocess.popen / subprocess.check_output scenarios / python versions in one handy function
  • System agnostic functionalty, the developper shouldn't carry the burden of Windows & Linux differences
  • Optional Windows UAC elevation module compatible with CPython, PyInstaller & Nuitka
  • Optional Linux sudo elevation compatible with CPython, PyInstaller & Nuitka

command_runner

It works as wrapper for subprocess.popen and subprocess.check_output that solves:

  • Platform differences
  • Python language version differences
    • Handle timeouts even on earlier Python implementations
    • Handle encoding even on earlier Python implementations
  • Promises to always return an exit_code regardless of the execution state (even with timeouts, keboard interruptions)
  • Catch all possible exceptions and log them
  • Allows live stdout output of current execution

command_runner in a nutshell

Install with pip install command_runner

The following example will work regardless of the host OS and the Python version.

from command_runner import command_runner

exit_code, output = command_runner('ping 127.0.0.1', timeout=30, encoding='utf-8')

UAC Elevation / sudo elevation

command_runner package allowing privilege elevation. Becomming an admin is fairly easy with command_runner.elevate You only have to import the elevate module, and then launch your main function with the elevate function.

elevation In a nutshell

from command_runner.elevate import elevate

def main():
    """My main function that should be elevated"""
    print("Who's the administrator, now ?")

if __name__ == '__main__':
    elevate(main)

elevate function handles arguments (positional and keyword arguments). elevate(main, arg, arg2, kw=somearg) will call main(arg, arg2, kw=somearg)

Guide

Setup

pip install command_runner or download the latest git release

Advanced command_runner usage

Special exit codes

In order to keep the promise to always provide an exit_code, some arbitrary exit codes have been added for the case where none is given. Those exit codes are:

  • -252 : KeyboardInterrupt
  • -253 : FileNotFoundError, OSError, IOError
  • -254 : Timeout
  • -255 : Any other uncatched exceptions

Default encoding

command_runner has an encoding argument which defaults to utf-8 for Unixes and cp437 for Windows platforms. Using cp437 ensures that most cmd.exe output is encoded properly, including accents and special characters, on most locale systems. Still you can specify your own encoding for other usages, like Powershell where unicode_escape is preferred.

exit_code, output = command_runner(r'C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe --help', encoding='unicode_escape')

Earlier subprocess.popen implementations didn't have an encoding setting so command_runner will deal with encoding for those.

Timeouts

command_runner as a timeout argument which defaults to 1800 seconds. This default setting ensures commands will not block the main script execution. Feel free to lower / higher that setting with

exit_code, command_runner('ping 127.0.0.1', timeout=30)

Disabling logs

Whenever you want another loglevel for command_runner, you might do with the following statement in your codeclimate

import logging
import command_runner

logging.getLogger('command_runner').setLevel(logging.ERROR)

Advanced elevate usage

is_admin() function

The elevate module has a nifty is_admin() function that returns a boolean according to your current root/administrator privileges. Usage:

from command_runner.elevate import is_admin

print('Am I an admin ? %s' % is_admin())

sudo elevation

Initially designed for Windows UAC, command_runner.elevate can also elevate privileges on Linux, using the sudo command. This is mainly designed for PyInstaller / Nuitka executables, as it's really not safe to allow automatic privilege elevation of a Python interpreter.

Example for a binary in /usr/local/bin/my_compiled_python_binary

You'll have to allow this file to be run with sudo without a password prompt. This can be achieved in /etc/sudoers file.

Example for Redhat / Rocky Linux, where adding the following line will allow the elevation process to succeed without password:

someuser ALL= NOPASSWD:/usr/local/bin/my_compiled_python_binary

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

command_runner-0.6.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

command_runner-0.6.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file command_runner-0.6.0.tar.gz.

File metadata

  • Download URL: command_runner-0.6.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for command_runner-0.6.0.tar.gz
Algorithm Hash digest
SHA256 85b88aeb5556946753a2fc902e600595de76d59249d23570cc004347c9f1c4c8
MD5 b69ddab0aac789531d274d4c198c31cd
BLAKE2b-256 2c18dfa37ba1fe2eb9166cfd21620e45f6ed8d23c4e2f08cac481e61f5219730

See more details on using hashes here.

File details

Details for the file command_runner-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: command_runner-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for command_runner-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d624b9936694bd9d44ec792fac81a8fb9e013743a3e62d2ec94973bcc9be6ef
MD5 f7eee8523796c93aad4e6c21a6d4244d
BLAKE2b-256 57e91b4e571710f7df5e01afd0d522f79646f51e74d2cc1a74923d98b6c870ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page