def create_user(context,galaxy,email,public_name,password,only_check, message_template): """ Create new Galaxy user. Creates a new user in GALAXY, using EMAIL for the username. If PUBLIC_NAME is not supplied then it will be generated automatically from the leading part of the email address. If a password for the new account is not supplied using the --password option then nebulizer will prompt for one. """ # Check message template is a .mako file if message_template: if not message_template.endswith(".mako"): logger.critical("Message template '%s' is not a .mako file" % message_template) return 1 # Get a Galaxy instance gi = context.galaxy_instance(galaxy) if gi is None: logger.critical("Failed to connect to Galaxy instance") return 1 # Sort out email and public name if public_name: if not users.check_username_format(public_name): logger.critical("Invalid public name: must contain only " "lower-case letters, numbers and '-'") return 1 else: # No public name supplied, make from email address public_name = users.get_username_from_login(email) # Create user print "Email : %s" % email print "Name : %s" % public_name return users.create_user(gi,email,public_name,password, only_check=only_check, mako_template=message_template)
def manage_users(args=None): """ Implements the 'manage_users' utility """ deprecation_warning() if args is None: args = sys.argv[1:] p = base_parser(usage=\ "\n\t%prog list GALAXY_URL [options]" "\n\t%prog create GALAXY_URL EMAIL [PUBLIC_NAME]" "\n\t%prog create GALAXY_URL -t TEMPLATE START [END]" "\n\t%prog create GALAXY_URL -b FILE [options]", description="Manage and create users in a Galaxy " "instance") commands = ['list','create'] # Get compulsory arguments if len(args) == 1: if args[0] == '-h' or args[0] == '--help': p.print_usage() elif args[0] == '--version': p.print_version() sys.exit(0) if len(args) < 2: p.error("need to supply a command and a Galaxy URL/alias") command = args[0] galaxy_url = args[1] # Setup additional command line options if command not in commands: p.error("unrecognised command: '%s'" % command) elif command == 'list': p.set_usage("%prog list GALAXY_URL [options]") p.add_option('--name',action='store',dest='name',default=None, help="specific emails/user name(s) to list") p.add_option('-l',action='store_true', dest='long_listing_format',default=False, help="use a long listing format (include ids, " "disk usage and admin status)") elif command == 'create': p.set_usage("\n\t%prog create GALAXY_URL EMAIL [PUBLIC_NAME]" "\n\t%prog create GALAXY_URL -t TEMPLATE START [END]" "\n\t%prog create GALAXY_URL -b FILE [options]") p.add_option('-p','--password',action='store',dest='passwd', default=None, help="specify password for new user account " "(otherwise program will prompt for password)") p.add_option('-c','--check',action='store_true',dest='check', default=False, help="check user details but don't try to create " "the new account") p.add_option('-t','--template',action='store_true', dest='template',default=False, help="indicates that EMAIL is actually a " "'template' email address which includes a '#' " "symbol as a placeholder where an integer index " "should be substituted to make multiple accounts " "(e.g. 'student#@galaxy.ac.uk').") p.add_option('-b','--batch',action='store_true',dest='batch', default=False, help="create multiple users reading details from " "TSV file (columns should be: " "email,password[,public_name])") p.add_option('-m','--message',action='store', dest='message_template',default=None, help="populate and output Mako template " "MESSAGE_TEMPLATE") # Process remaining arguments on command line if args[1] in ('-h','--help','--version'): args = args[1:] else: args = args[2:] options,args = p.parse_args(args) handle_debug(debug=options.debug) handle_suppress_warnings(suppress_warnings=options.suppress_warnings) handle_ssl_warnings(verify=(not options.no_verify)) # Handle password if required email,password = handle_credentials(options.username, options.galaxy_password, prompt="Password for %s: " % galaxy_url) # Get a Galaxy instance gi = get_galaxy_instance(galaxy_url,api_key=options.api_key, email=email,password=password, verify_ssl=(not options.no_verify)) if gi is None: logger.critical("Failed to connect to Galaxy instance") sys.exit(1) # Execute command if command == 'list': users.list_users(gi,name=options.name,long_listing_format= options.long_listing_format) elif command == 'create': # Check message template is .mako file if options.message_template: if not os.path.isfile(options.message_template): logger.critical("Message template '%s' not found" % options.message_template) sys.exit(1) elif not options.message_template.endswith(".mako"): logger.critical("Message template '%s' is not a .mako file" % options.message_template) sys.exit(1) if options.template: # Get the template and range of indices template = args[0] start = int(args[1]) try: end = int(args[2]) except IndexError: end = start # Create users retval = users.create_users_from_template(gi,template, start,end,options.passwd, only_check=options.check) elif options.batch: # Get the file with the user data tsvfile = args[0] # Create users retval = users.create_batch_of_users(gi,tsvfile, only_check=options.check, mako_template=options.message_template) else: # Collect email and (optionally) public name email = args[0] try: name = args[1] if not users.check_username_format(name): logger.critical("Invalid name: must contain only " "lower-case letters, numbers and " "'-'") sys.exit(1) except IndexError: # No public name supplied, make from email address name = users.get_username_from_login(email) # Create user print "Email : %s" % email print "Name : %s" % name retval = users.create_user(gi,email,name,options.passwd, only_check=options.check, mako_template=options.message_template)