def get_command(options, vexrc, environ): """Get a command to run. :returns: a list of strings representing a command to be passed to Popen. """ command = options.rest if not command: command = vexrc.get_shell(environ) if command and command[0].startswith('--'): raise exceptions.InvalidCommand( "don't put flags like '%s' after the virtualenv name." % command[0]) if not command: raise exceptions.InvalidCommand("no command given") return command
def _main(environ, argv): """Logic for main(), with less direct system interaction. Routines called here raise InvalidArgument with messages that should be delivered on stderr, to be caught by main. """ options = get_options(argv) if options.version: return handle_version() vexrc = get_vexrc(options, environ) # Handle --shell-config as soon as its arguments are available. if options.shell_to_configure: return handle_shell_config(options.shell_to_configure, vexrc, environ) if options.list is not None: return handle_list(vexrc.get_ve_base(environ), options.list) # Do as much as possible before a possible make, so errors can raise # without leaving behind an unused virtualenv. # get_virtualenv_name is destructive and must happen before get_command cwd = get_cwd(options) ve_base = vexrc.get_ve_base(environ) ve_name = get_virtualenv_name(options) command = get_command(options, vexrc, environ) # Either we create ve_path, get it from options.path or find it # in ve_base. if options.make: if options.path: make_path = os.path.abspath(options.path) else: make_path = os.path.abspath(os.path.join(ve_base, ve_name)) handle_make(environ, options, make_path) ve_path = make_path elif options.path: ve_path = os.path.abspath(options.path) if not os.path.exists(ve_path) or not os.path.isdir(ve_path): raise exceptions.InvalidVirtualenv( "argument for --path is not a directory") else: try: ve_path = get_virtualenv_path(ve_base, ve_name) except exceptions.NoVirtualenvName: options.print_help() raise # get_environ has to wait until ve_path is defined, which might # be after a make; of course we can't run until we have env. env = get_environ(environ, vexrc['env'], ve_path) returncode = run(command, env=env, cwd=cwd) if options.remove: handle_remove(ve_path) if returncode is None: raise exceptions.InvalidCommand("command not found: {0!r}".format( command[0])) return returncode