Best Python code snippet using autotest_python
root.py
Source:root.py
...18 print(message, '\n')19 return False20 return True21# check if openpyn itself has been started with root access.22def verify_running_as_root():23 if os.getuid() == 0:24 # print(message, '\n')25 return True26 return False27def obtain_root_access():28 # asks for sudo password to be cached29 try: # try accessing root read only file "600" permission, ask for sudo pass30 subprocess.call(31 ["sudo", "cat", "/etc/resolv.conf"],32 stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)33 except subprocess.CalledProcessError:34 print("except occured while running obtain_root_access() 'sudo ls' command")35 except KeyboardInterrupt:36 print('\n(KeyboardInterrupt) Ctr+C received, Bye\n')37 sys.exit()38def logged_in_user_is_root(username):39 user_record = pwd.getpwnam(username)40 user_id = user_record.pw_gid41 # print(user_record, user_id)42 if user_id == 0:43 return True44 return False45def running_with_sudo():46 if verify_running_as_root():47 try:48 logged_in_user = os.getlogin()49 if logged_in_user_is_root(logged_in_user):50 return False # when logged in as 'root' user notifications will work.51 return True # 'sudo' is used notification won't work.52 except FileNotFoundError:53 print("os.getlogin(), returned FileNotFoundError, \54assuming 'openpyn' is running with 'SUDO'")55 return True56 except OSError:57 print("os.getlogin(), returned error, assuming \58'openpyn' is running with 'SUDO'")59 return True60 return False # regular user without 'sudo'
credentials.py
Source:credentials.py
...6credentials_file_path = __basefilepath__ + "credentials"7def check_credentials():8 return os.path.exists(credentials_file_path)9def save_credentials():10 if root.verify_running_as_root() is False:11 print(Fore.RED + "\nPlease run as 'sudo openpyn --init' the first time. Root access is",12 "needed to store credentials in " + "'" + credentials_file_path + "'" + "." + Style.RESET_ALL)13 sys.exit()14 else:15 print(Fore.BLUE + "Storing credentials in " + "'" + credentials_file_path + "'" + " with openvpn",16 "compatible 'auth-user-pass' file format\n")17 username = input("Enter your username for NordVPN, i.e youremail@yourmail.com: ")18 password = input("Enter the password for NordVPN: ")19 try:20 with open(credentials_file_path, 'w') as creds:21 creds.write(username + "\n")22 creds.write(password + "\n")23 creds.close()24 # Change file permission to 600...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!