Added function to check the current user using subprocess

This commit is contained in:
Евгений Иваницкий 2023-03-16 18:54:49 +03:00
parent 9ada10d0fd
commit dde5211e1f
Signed by: pikone
GPG Key ID: 32193CA36A1696DA

44
main.py Normal file
View File

@ -0,0 +1,44 @@
import subprocess
# list_files = subprocess.run(["ls", "-l"])
# print("The exit code was: %d" % list_files.returncode)
def remove_last_line(s):
return s[:s.rfind('\n')]
def root_user_error():
print("You is root user. Exit")
exit(0)
def return_current_user():
user = subprocess.run(["whoami"], stdout=subprocess.PIPE, text=True)
user.stdout = remove_last_line(user.stdout)
return(user.stdout)
# def return_home_dir():
# user_directory = subprocess.run(["ls"], stdout=subprocess.PIPE, text=True, input="~")
# user_directory.stdout = remove_last_line(user_directory.stdout)
def this_command_exist_or_not(command):
result = subprocess.run([command], stdout=subprocess.DEVNULL)
if result.stdout == "":
return()
def main():
# this_command_exist_or_not("lpinfo")
# install packages from packagelist without python
user = return_current_user()
if user == "root":
root_user_error()
print("You are not root")
# get printers names
if __name__ == '__main__':
main()