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-1.0.0.dev0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

configure_with_sudo-1.0.0.dev0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file configure_with_sudo-1.0.0.dev0.tar.gz.

File metadata

File hashes

Hashes for configure_with_sudo-1.0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 49cbd4d43766876da14f3eaf987b51702fa574d71d2d087dff73ed587ca00439
MD5 049b597f61c6d8efa00e68b741e9e62c
BLAKE2b-256 3d6e4ac7173d573a452b29a7caaffe57d6cf48a88413f9495222e804cdfadc1f

See more details on using hashes here.

File details

Details for the file configure_with_sudo-1.0.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for configure_with_sudo-1.0.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 86d5b85a511785dff5f0123397e514d10182dd8dc9693034ed53683b7d4f5318
MD5 51f92834fa775c6000b19b53afd9a8ef
BLAKE2b-256 2fc6b803bce10063d3b1df11973e94300118498b1b48ce0cfd6c1425f2c739d6

See more details on using hashes here.

Supported by

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