Best Python code snippet using autotest_python
service_helper.py
Source:service_helper.py
...47 group_info = grp.getgrgid(group)48 else:49 group_info = grp.getgrnam(group)50 return group_info[0], group_info[2]51def set_group_user(group=None, user=None):52 """53 Set the group and user id if gid or uid is defined.54 @param group: Change the group id the program is run under55 @param user: Change the user id the program is run under56 """57 if group:58 _, gid = get_group_name_id(group)59 os.setgid(gid)60 os.setegid(gid)61 if user:62 username, uid = get_user_name_id(user)63 # Set environment for programs that use those to find running user64 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):65 os.environ[name] = username66 os.setuid(uid)67 os.seteuid(uid)68def main():69 parser = optparse.OptionParser()70 parser.allow_interspersed_args = False71 parser.add_option('-l', '--logfile', action='store',72 default=None,73 help='File to redirect stdout to')74 parser.add_option('-c', '--chdir', action='store',75 default=None,76 help='Change to dir before starting the process')77 parser.add_option('-s', '--start-service', action='store_true',78 default=False,79 help='Start service')80 parser.add_option('-k', '--stop-service', action='store_true',81 default=False,82 help='Stop service')83 parser.add_option('-p', '--pidfile', action='store',84 default=None,85 help='Pid file location (Required)')86 parser.add_option('-u', '--chuid', action='store',87 default=None,88 help='UID to run process as')89 parser.add_option('-g', '--chgid', action='store',90 default=None,91 help='GID to run process as')92 options, args = parser.parse_args()93 if not options.pidfile:94 print 'A pidfile must always be supplied'95 parser.print_help()96 sys.exit(1)97 set_group_user(group=options.chgid, user=options.chuid)98 if options.start_service:99 start_service(args, options.pidfile, options.logfile, options.chdir)100 elif options.stop_service:101 stop_service(options.pidfile)102 else:103 print 'Nothing to do, you must specify to start or stop a service'104 parser.print_help()105if __name__ == '__main__':...
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!!