Skip to main content

A Python class to handle executing system commands using sudo

Project description

configure_with_sudo

Description

A Python module to handle executing system commands using sudo.

Usage

Extend GenericConfigure, setting at a minimum an appropriate argv command & argument list.

Example

The following class will check if pip is installed and, if not, install it system-wide using sudo easy_install pip:

from configure_with_sudo import GenericConfigure

class InstallPip(GenericConfigure):

    def __init__(self, kill_sudo_cred=True):
        # Initial argv is simply to check if 'pip' is installed
        argv = ["/usr/local/bin/pip", "-V"]
        super(InstallPip, self).__init__(
            argv, use_sudo=True, kill_sudo_cred=kill_sudo_cred)

        try:
            # Execute to check if pip is already installed
            # if it is, self.configured will be set to True
            # and subsequent calls to execute() will do nothing
            self.execute(use_sudo=False)

        except Exception:
            # If pip -V failed, assume pip is not installed.
            # Replace self.argv with the installation command.
            self.argv = ["/usr/bin/easy_install", "pip"]

pip_installer = InstallPip()
pip_installer.execute()

The following example will capture the output of sudo ls -l /private/var/root, and print it line-by-line:

class DirectoryLister(GenericConfigure):

    def __init__(self, path):
        argv = ["ls", "-l", path]
        super().__init__(argv, use_sudo=True)

path = "/private/var/root"
lister = DirectoryLister(path)
output = lister.execute(return_output=True)

for line in output:
    print(line)

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

configure_with_sudo-0.1.0.dev4.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

configure_with_sudo-0.1.0.dev4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file configure_with_sudo-0.1.0.dev4.tar.gz.

File metadata

  • Download URL: configure_with_sudo-0.1.0.dev4.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.5

File hashes

Hashes for configure_with_sudo-0.1.0.dev4.tar.gz
Algorithm Hash digest
SHA256 0d9d01d0ca6991e945d052caf06be520f54a5af8c32cc453020064087d26eb6e
MD5 3ffde6a8d44cc52d58ae06bbf03be602
BLAKE2b-256 dfb6c38f8b7704680d684e6424ecf2d8f858ef1bbd5e0e8dc7b5a7965ca678ce

See more details on using hashes here.

File details

Details for the file configure_with_sudo-0.1.0.dev4-py3-none-any.whl.

File metadata

  • Download URL: configure_with_sudo-0.1.0.dev4-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.5

File hashes

Hashes for configure_with_sudo-0.1.0.dev4-py3-none-any.whl
Algorithm Hash digest
SHA256 48821b3ebc2b3be8debb48a615aa6958ce9c927215bd2a63268bcc1569bf8cd3
MD5 f7e65e43bac87bc2509edbcec2aa4106
BLAKE2b-256 762fcead6015b45f7eb0030b432b47f4496922f6ba05ab56ccc310901dbf1e2e

See more details on using hashes here.

Supported by

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