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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for configure_with_sudo-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55148e2bcc18a11f661b5a0fbafd59586fb6ce2eb3325fbfff62d31983bd3b0b |
|
MD5 | 73bdaf841367767148df0a7428e625ac |
|
BLAKE2b-256 | bd8247688450819b6c843b28464933045f7666f79cb69e5f6527b75dae8a9760 |
Close
Hashes for configure_with_sudo-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98912c03d0185444c0e3084d1e712207d25243c577a3b1be9001b1a86b58559e |
|
MD5 | 13a1b81dba302c766b816a7c60d7a6cf |
|
BLAKE2b-256 | 60e03f5ba01d2929f170b0be4150e5733a1419b542ac3eb7bdb7663e7e8d0150 |