Skip to content

Commit

Permalink
Merge pull request #8 from williamcanin/main
Browse files Browse the repository at this point in the history
Update version 0.3.1
  • Loading branch information
williamcanin committed Jan 10, 2022
2 parents 7b3134e + 1509290 commit 19aa4db
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
copyright = "2021, William C. Canin"
author = "William C. Canin"

version = "0.3.0"
version = "0.3.1"
# The full version, including alpha/beta/rc tags
release = "0.3.0"
release = "0.3.1"


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT license"
name = "snakypy-helpers"
readme = "README.rst"
repository = "https://github.com/snakypy/snakypy-helpers"
version = "0.3.0"
version = "0.3.1"

packages = [
{include = "snakypy"},
Expand Down
2 changes: 1 addition & 1 deletion snakypy/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"email": "contact.snakypy@gmail.com",
"website": "https://snakypy.github.io",
"github": "https://github.com/snakypy",
"version": "0.3.0",
"version": "0.3.1",
}

# Keep the versions the same on pyproject.toml and __init__.py
Expand Down
69 changes: 65 additions & 4 deletions snakypy/helpers/subprocess/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from getpass import getpass
from subprocess import PIPE, Popen
from shutil import which
from subprocess import PIPE, Popen, run
from typing import Any, Optional, Union

from snakypy.helpers.ansi import FG, NONE
from snakypy.helpers.console import printer
from snakypy.helpers.decorators import denying_os, only_linux


# The use of static typing in this function in conjunction with "inter", caused
# errors when using Mypy, perhaps because at run time the inter would always return None,
# so it is recommended in the file mypy.ini, to use the option "strict_optional = False".
# Ref: https://stackoverflow.com/questions/57350490/mypy-complaining-that-popen-stdout-does-not-have-a-readline


def command(
cmd: str,
*args: Any,
Expand All @@ -25,7 +27,7 @@ def command(
execution in real time.
>>> from snakypy.helpers.subprocess import command
>>> url = 'git clone https://github.com/snakypy/snakypy.git'
>>> url = 'git clone https://github.com/snakypy/snakypy-helpers.git'
>>> command(url, verbose=True)
Args:
Expand Down Expand Up @@ -58,9 +60,65 @@ def command(


@denying_os("Windows", is_func=True)
def command_sudo(
commands: list, sudo_msg: str = "[ Enter the machine sudo password ]"
) -> bool:
"""
Run the command with sudo. It is the improved version of "super_command",
which will replace it.
If the password is entered wrong, it repeats the call again.
Compatible with Linux and macOS.
>>> from snakypy.helpers.subprocess import command_sudo
>>> cmd_list = ["python --version | cut -d' ' -f1", "mkdir -p /tmp/snakypy"]
>>> command_sudo(cmd_list)
''
Args:
commands: Receives a list of commands.
sudo_msg: Header message before entering password.
Returns:
[bool]: Returns boolean.
"""
try:
check: bool = False
printer(sudo_msg, foreground=FG().WARNING)
while not check:
run(["sudo", "-k"])
if which("faillock"):
run(["faillock", "--reset"])
get_pass = getpass()
for cmd in commands:
p = Popen(
"echo {} | sudo -S {};".format(get_pass, cmd),
stdin=PIPE,
stderr=PIPE,
stdout=PIPE,
universal_newlines=True,
shell=True,
)
p.communicate(get_pass + "\n")
if p.returncode != 0:
printer("Error in password authentication.", foreground=FG().ERROR)
break
else:
check = True
return check
except KeyboardInterrupt:
printer("Aborted by user.", foreground=FG().WARNING)
return False
except PermissionError:
raise PermissionError(
f"{command_sudo.__name__}: No permission to run this command"
)


@only_linux
def super_command(cmd: str) -> Union[Optional[str], None]:
"""
Allows to execute superuser command in shell.
Compatible ONLY with Linux.
>>> from snakypy.helpers.subprocess import super_command
>>> super_command("python --version | cut -d' ' -f1")
Expand All @@ -72,6 +130,9 @@ def super_command(cmd: str) -> Union[Optional[str], None]:
Returns:
[str]: Returns the result of the command if it has.
"""
printer(
f'[ WARN ] This function ({super_command.__name__}) will be discontinued. Use the "command_sudo".'
)
try:
while True:
super_password = getpass()
Expand Down Expand Up @@ -120,4 +181,4 @@ def systemctl_is_active(service: str) -> Union[tuple, None]:
return None


__all__ = ["command", "super_command", "systemctl_is_active"]
__all__ = ["command", "command_sudo", "super_command", "systemctl_is_active"]

0 comments on commit 19aa4db

Please sign in to comment.