From dde5211e1f66c4318e237173bbb67f2c9ab321c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=98=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=86=D0=BA=D0=B8=D0=B9?= Date: Thu, 16 Mar 2023 18:54:49 +0300 Subject: [PATCH] Added function to check the current user using subprocess --- main.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..5d68f7b --- /dev/null +++ b/main.py @@ -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() \ No newline at end of file