def _readline_getc(self): do_comp = self.callbacks["get_var"]("input_do_completions") # Don't flush, because readline loses keys. r = self.get_key(False) if r == curses.KEY_BACKSPACE: r = ord("\b") if chr(r) == '\t' and do_comp: self.input_box.rotate_completions() return # Accept current completion if chr(r) in " \b\n" and do_comp: comp = self.input_box.break_completion() if comp: log.debug("inserting: %s" % comp) readline.insert_text(comp) # Discard current completion else: self.input_box.break_completion() log.debug("KEY: %s" % r) return r
def insert_text ( self, line ): if util.isPlatformWindows (): import pyreadline Readline ().insert_text ( line ) else: import readline readline.insert_text ( line )
def rl_autoindent(): """Auto-indent upon typing a new line according to the contents of the previous line. This function will be used as Readline's pre-input-hook. """ hist_len = readline.get_current_history_length() last_input = readline.get_history_item(hist_len) try: last_indent_index = last_input.rindex(" ") except: last_indent = 0 else: last_indent = int(last_indent_index / 4) + 1 if len(last_input.strip()) > 1: if last_input.count("(") > last_input.count(")"): indent = ''.join([" " for n in range(last_indent + 2)]) elif last_input.count(")") > last_input.count("("): indent = ''.join([" " for n in range(last_indent - 1)]) elif last_input.count("[") > last_input.count("]"): indent = ''.join([" " for n in range(last_indent + 2)]) elif last_input.count("]") > last_input.count("["): indent = ''.join([" " for n in range(last_indent - 1)]) elif last_input.count("{") > last_input.count("}"): indent = ''.join([" " for n in range(last_indent + 2)]) elif last_input.count("}") > last_input.count("{"): indent = ''.join([" " for n in range(last_indent - 1)]) elif last_input[-1] == ":": indent = ''.join([" " for n in range(last_indent + 1)]) else: indent = ''.join([" " for n in range(last_indent)]) readline.insert_text(indent)
def pre_input_hook(): readline.insert_text(display.display( thing, shortid=False, colored=False)) readline.redisplay() # Unset the hook again readline.set_pre_input_hook(None)
def defaulter(): """define default behavior startup""" if _readline_available: readline.insert_text(default) readline.set_startup_hook(defaulter) readline.get_completer() readline.set_completer(completer)
def startup_hook(): """startup_hook() -- internal readline callback. Do not call. """ if (Readline.interruption != None): readline.insert_text(Readline.interruption[0]) readline.set_point(Readline.interruption[1]) readline.set_mark(Readline.interruption[2]) Readline.interruption = None
def _fred_completer(text, state): """Custom completer function called when the user presses TAB.""" s_current_cmd = readline.get_line_buffer() # Write partial command+\t to debuggerso it can do the completion. result = get_child_response(s_current_cmd + '\t') # Erase what text we already have: result = result.replace(s_current_cmd, "") readline.insert_text(result)
def getUserInWithDef(msg, default): readline.set_startup_hook(lambda: readline.insert_text(default)) var = raw_input(colors.BOLDON + "%s: " % (msg) + colors.BOLDOFF) readline.set_startup_hook(lambda: readline.insert_text('')) if var == "": print("No input given, try again.") return getUserIn(msg) return var
def printf(self, string): #readline.set_startup_hook(lambda: readline.insert_text("{0}".format(str)) or readline.redisplay()) self.stdout.write(str("\n{0}\n".format(string))+"\n") self.stdout.flush() #readline.parse_and_bind('\C-M') readline.insert_text("oi\n") #readline.parse_and_bind('\\n') readline.redisplay()
def pre_input_hook(self): """If a line buffer has been filled then inject it into the readline buffer for the user to edit.""" if self.line_buffer: readline.insert_text( self.line_buffer.decode(ex_config.FILE_ENCODING).rstrip('\n')) readline.redisplay() self.line_buffer = None
def complete(self, line, buf, state, run=False, full_line=None): logger.debug('Walked to: %s' % self.name) if line and self.dynamic_args and len(line) > 1: logger.debug("Dynamic arg '%s' found" % line[0]) has_arg_completed = True # Dynamic arg already filled, jump to next word next_command = line[1] line.pop(0) elif line: next_command = line[0] has_arg_completed = False candidates = self.get_candidates(next_command) if candidates and len(candidates) > 1 and buf: logger.debug("More than one candidate, not walking in %s" % buf) return self._next_command(state, buf) elif candidates and next_command in candidates: logger.debug("Found %s in childs, walking." % next_command) cmd = candidates.pop(next_command) return cmd.complete(line[1:], buf, state, run, full_line) else: logger.debug('Not walking because %s was not found in childs' % next_command) if has_arg_completed: return self._next_command(state, next_command) logger.debug('Line=>%s, Buf=>%s, state=>%s' % (line, buf, state)) if run: logger.debug('Executing %s' % self.name) return self.run(full_line.rstrip()) logger.debug('Starting arg complete') # Checking if user already typed a valid arg without space at end if self.dynamic_args: if len(line) and buf and line[0] in self.args(): readline.insert_text(" ") logger.debug("Inserted blank space") elif len(line) and not buf and line[0] not in self.args(): logger.debug("Found an unknown arg, suggesting next command.") return self._next_command(state, buf) if buf == self.name: readline.insert_text(" ") logger.debug("Inserted blank space") if not self.dynamic_args: return self._next_command(state, buf) if self.dynamic_args: if (buf.strip() in self.args()): return self._next_command(state, buf) elif line and line[0] in self.args(): return self._next_command(state, buf) else: return self._dynamic_args(state, buf) else: return self._next_command(state, buf)
def complete(self,text,state): if text.endswith(".__doc__"): new_text = text.replace(".__doc__","") help(eval(new_text)) readline.redisplay() readline.insert_text(new_text) else: value = Completer.complete(self,text,state) return value
def complete(self, text, state): if not text: readline.insert_text(' ') if TabCompleter.uses_editline: # It won't move the cursor for us, apparently sys.stdout.write('\x1b[4C') readline.redisplay() sys.stdout.flush() return None else: return rlcompleter.Completer.complete(self, text, state)
def autocompleter(self, txt, state): chopped = [] try: r = [] self._line = readline.get_line_buffer() self.cursorix(readline.get_endidx()) last = self.lastword() if self.atendofcmd() or self.cmd() == "help": for c in command.allcmds(): if c.startswith(txt): r.append(c + " ") else: # print "<%s, %s>" % (txt, state) curacct = self.currentaccount() if not last: acct = curacct else: if last.startswith("/"): acct = accounts.getroot() last = last.lstrip("/") else: acct = curacct chopped = last.rsplit("/", 1) tmpacct = acct.getsubaccount(chopped[0]) if tmpacct: acct = tmpacct if len(chopped) > 1: txt = chopped[1] for a in acct.accounts(): if txt == "" or a.name().startswith(txt): r.append(a.name() + "/") if len(r) == 1: readline.insert_text("/") try: return r[state] except IndexError: return None except Exception, ex: print "%s:\n%s\n%s" % ("autocompleter exception", type(ex), str(ex)) print "txt: %s; state: %s" % (txt, state) print "chopped: " + str(chopped) print "last: " + last
def rlinput(prompt, prefill=''): """kind of like ras_input but with readline support and pre-filled answer""" readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt) finally: readline.set_startup_hook()
def do_history(self, args): """ Prints cloudmonkey history """ if self.pipe_runner("history " + args): return startIdx = 1 endIdx = readline.get_current_history_length() numLen = len(str(endIdx)) historyArg = args.split(' ')[0] if historyArg.isdigit(): startIdx = endIdx - long(historyArg) if startIdx < 1: startIdx = 1 elif historyArg == "clear" or historyArg == "c": readline.clear_history() print "CloudMonkey history cleared" return elif len(historyArg) > 1 and historyArg[0] == "!" and historyArg[1:].isdigit(): command = readline.get_history_item(long(historyArg[1:])) readline.set_startup_hook(lambda: readline.insert_text(command)) self.hook_count = 1 return for idx in xrange(startIdx, endIdx): self.monkeyprint("%s %s" % (str(idx).rjust(numLen), readline.get_history_item(idx)))
def _rlinput(self, prompt, prefill): readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt) finally: readline.set_startup_hook() return prefill
def get(self, field, value=None): """Gets :field: value from stdin. :field: Field's name. :value: Default value for editing. :returns: Value got from stdin. """ readline.set_startup_hook(lambda: readline.insert_text( value is not None and str(value) or "" )) try: value = input("{0}> ".format(field)) except KeyboardInterrupt: print() exit(0) readline.set_startup_hook() if field == 'priority': try: value = int(value) except ValueError: if value == '': return None print("Invalid priority value!") exit(0) else: if value not in [1, 2, 3, 4, 5]: print("Invalid priority value!") exit(0) return value return value
def rlinput(prompt, prefill=''): readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: # This was raw_input, but that's deprecated in py3 return input(prompt) finally: readline.set_startup_hook()
def silent_invariant_raw_input(prompt,comp=None,wipe=False, echo = True,color = Colors.NONE,completion=None): prompt = color+prompt+Colors.ENDC if not Control.silent: readline.set_startup_hook(lambda: readline.insert_text(comp)) if completion=="Files": readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(fileCompleter) elif not completion is None: readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(lambda a,b:listCompleter(a,b,completion)) try: if Control.silent: prompt = "" if echo: ret = raw_input(prompt) else: ret = getpass.getpass(prompt) wipe = False if wipe: l = readline.get_current_history_length() if l > 0: readline.remove_history_item(l-1) return ret finally: if not Control.silent: readline.set_startup_hook() if not completion is None: readline.parse_and_bind("set disable-completion On") readline.set_completer(None) #readline.parse_and_bind("tab: \t") readline.set_completer_delims("")
def prompt(prompt, prefill=""): readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: data = raw_input(prompt) print return data finally: readline.set_startup_hook()
def getWithDefault(question, value): question += ": " readline.set_startup_hook(lambda: readline.insert_text(value)) try: return ask(question) finally: readline.set_startup_hook()
def read_input(prompt, prefill = ''): """ Function to read input from stdin Args: prompt: Prompt for the input prefill: A default value for the input """ try: if not prefill: raise Exception #if readline is present, use to prefill the input import readline as c_readline value, prefill = prefill, '' #in this case we are resetting the prefill so as to not return the prefill c_readline.set_startup_hook(lambda: c_readline.insert_text(value)) except ImportError: #otherwise modify the prompt to show the prefill c_readline = None prompt = '%s[%s] ' % (prompt, prefill) except: c_readline = None #Python 2.x vs 3.x try: c_input = raw_input except: c_input = input try: #read the input or return any prefill values return c_input(prompt) or prefill finally: c_readline and c_readline.set_startup_hook() #unbind startup hook
def get_user_input(obj, attr, opts, no_prefill=False): if attr == 'collection' and not getattr(obj, attr) and obj.project: setattr(obj, attr, obj.project.collection) if opts: optlist = ', '.join(['%s=%s' % (k, v) for k, v in opts.items()]) prompt = '%s [Options: %s]: ' % (attr, optlist) else: prompt = '%s: ' % attr if no_prefill or not getattr(obj, attr): prefill = '' else: if (hasattr(obj, 'relations') and attr in obj.relations) \ or (getattr(obj, attr) and hasattr(getattr(obj, attr), 'id')): prefill = getattr(obj, attr).id else: prefill = getattr(obj, attr) readline.set_startup_hook(lambda: readline.insert_text(prefill)) readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(complete) try: return raw_input(prompt) finally: readline.set_startup_hook()
def export_gcal(v, y, m, d, tid, sid): cmd = [] cmd.append("gcalcli") cmd.append("--calendar") readline.set_startup_hook(lambda: readline.insert_text("")) t = tabCompleter() t.createListCompleter(["calender", "NSC absences", "NSC shared calender"]) readline.set_completer_delims('\t') readline.parse_and_bind("tab: complete") readline.set_completer(t.listCompleter) calender = raw_input("Name of calender: ").strip() cmd.append(calender) cmd.append("--title") v[y][m][d][tid][sid]["subtask title"] cmd.append(v[y][m][d][tid][sid]["subtask title"]) cmd.append("--where") cmd.append("''") cmd.append("--when") dt = str(m)+ "/" + str(d) + "/" + str(y) + " " + v[y][m][d][tid][sid]["start"] cmd.append(dt) cmd.append("--duration") (h1, m1) = tuple(v[y][m][d][tid][sid]["start"].split(':')) (h2, m2) = tuple(v[y][m][d][tid][sid]["end"].split(':')) dur = str((int(h2) - int(h1)) * 60 + (int(m2) -int(m1))) cmd.append(dur) cmd.append("--description") cmd.append("''") cmd.append("--reminder") cmd.append("0") cmd.append("add") job = subprocess.Popen(cmd) job.wait() raw_input("Press enter to continue")
def raw_input_default(prompt, value = None): if value: readline.set_startup_hook(lambda: readline.insert_text(value)) try: return raw_input(prompt) finally: if value: readline.set_startup_hook(None)
def prefill_input(prompt, prefill): """prompt for input with supplied prefill text""" readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: result = input(prompt) finally: readline.set_startup_hook() return result
def __exec_line__(self, line): r"""Execute the input line. emptyline: no-op unknown command: print error message known command: invoke the corresponding method The parser method, parse_line(), can be overriden in subclasses to apply different parsing rules. Please refer to the doc string of parse_line() for complete information. Arguments: line: A string, representing a line of input from the shell. This string is preprocessed by cmdloop() to convert the EOF character to '\x04', i.e., 'D' - 64, if the EOF character is the only character from the shell. """ # Ignoe empty lines and lines starting with a pound sign. if not line or line.rstrip().startswith('#'): return cmd, args = ( '', [] ) toks = shlex.split(line) if not toks: return if line == _ShellBase.EOF: # This is a hack to allow the EOF character to behave exactly like # typing the 'exit' command. readline.insert_text('exit\n') readline.redisplay() cmd = 'exit' elif toks and toks[0] in self._cmd_map_internal.keys(): cmd = toks[0] args = toks[1:] if len(toks) > 1 else [] else: cmd, args = self.parse_line(line) if not cmd in self._cmd_map_all.keys(): self.stderr.write("{}: command not found\n".format(cmd)) return func_name = self._cmd_map_all[cmd] func = getattr(self, func_name) return func(cmd, args)
def default_input(prompt, prefill=''): ''' get CLI input, with a default value already specified ''' readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt) finally: readline.set_startup_hook()
def read_input_prefill(prompt, prefill = ''): """ Provides user input through readline with 'prefill' default values. """ readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt) finally: readline.set_startup_hook()
def hook(): readline.insert_text(prefill) readline.redisplay()
def prompt_for_new_plot_args( *, old_argv: List[str], old_args: argparse.Namespace, parser: argparse.ArgumentParser, ) -> Tuple[dict, argparse.Namespace]: """Given some old arguments, prompts for new ones and returns a new list of argument values and the parsed argparse namespace result. Args: old_argv (List[str]): The old argument value list old_args (argparse.Namespace): The old set of parsed arguments parser (argparse.ArgumentParser): The parser to use for evaluating the newly specified argument value list Returns: Tuple[dict, argparse.Namespace]: The new argument values list and the parsed argument namespace. Raises: ValueError: Upon error in parsing the new arguments. """ # Specify those arguments that may not be given in the prompt DISALLOWED_ARGS = ('run_cfg_path', 'run_dir_path', 'set_cfg', 'cluster_mode', 'suppress_data_tree', 'full_data_tree') # Create a new argument list for querying the user. For that, remove # those entries from the argvs that are meant to be in the query. prefix_argv = ('--interactive', old_args.model_name) to_query = [arg for arg in old_argv if arg not in prefix_argv] to_query_str = " ".join(to_query) + (" " if to_query else "") # Now, setup the startup hook with a callable that inserts those # arguments that shall be editable by the user. Configure readline to # allow tab completion for file paths after certain delimiters. readline.set_startup_hook(lambda: readline.insert_text(to_query_str)) readline.parse_and_bind("tab: complete") readline.set_completer_delims(' \t\n=') # Generate the prompt and store the result, stripping whitespace prompt_str = ("\n{ansi.CYAN}${ansi.MAGENTA} " "utopia eval -i {}" "{ansi.RESET} ".format(old_args.model_name, ansi=ANSIesc)) input_res = input(prompt_str).strip() print("") # Reset the startup hook to do nothing readline.set_startup_hook() # Prepare the new list of argument values. add_argv = input_res.split(' ') if input_res else [] new_argv = list(prefix_argv) + add_argv # ... and parse it to the eval subparser. new_args = parser.parse_args(new_argv) # NOTE This may raise SystemExit upon the --help argument or other # arguments that are not properly parsable. # Check that bad arguments were not used bad_args = [ arg for arg in DISALLOWED_ARGS if getattr(new_args, arg) != parser.get_default(arg) ] if bad_args: print("{ansi.RED}During interactive plotting, arguments that are used " "to update the Multiverse meta-configuration cannot be used!" "{ansi.RESET}".format(ansi=ANSIesc)) print("{ansi.DIM}Remove the offending argument{} ({}) and try again. " "Consult --help to find out the available plotting-related " "arguments." "{ansi.RESET}".format("s" if len(bad_args) != 1 else "", ", ".join(bad_args), ansi=ANSIesc)) raise ValueError("Cannot specify arguments that are used for updating " "the (already-in-use) meta-configuration of the " "current Multiverse instance. Disallowed arguments: " "{}".format(", ".join(DISALLOWED_ARGS))) return new_argv, new_args
def startup_hook(): lg.debug("readline starup hook ({0})".format(default)) readline.insert_text(str(default))
def pre_input_hook(): readline.insert_text(line) readline.redisplay() # Unset the hook again readline.set_pre_input_hook(None)
def startup_hook(): readline.insert_text(default)
def input_hook(self): # insert the given text just right before the input prompt is displayed. readline.insert_text(self.value) readline.redisplay()
def st_hook(): readline.insert_text(insert_txt)
def _input_hook(self): """displays the current value in the input line""" if self.oldvalue: text = self.oldvalue.encode(tel.CONFIG.STDOUT_ENCODING) readline.insert_text(text) readline.redisplay()
def auto_indent_hook(self): """Hook called by readline between printing the prompt and starting to read input. """ readline.insert_text(self._indent) readline.redisplay()
def defaulter(): """define default behavior startup""" readline.insert_text(default)
def startup_hook(): readline.insert_text('from start up_hook')
def pre_input_hook(): readline.insert_text(src) readline.redisplay()
def computer_configure(self, *args): """ Configure the authentication information for a given computer """ if not is_dbenv_loaded(): load_dbenv() import readline import inspect from django.core.exceptions import ObjectDoesNotExist from aiida.common.exceptions import ( NotExistent, ValidationError) from aiida.backends.utils import get_automatic_user from aiida.common.utils import get_configured_user_email from aiida.backends.settings import BACKEND from aiida.backends.profile import BACKEND_SQLA, BACKEND_DJANGO import argparse parser = argparse.ArgumentParser( prog=self.get_full_command_name(), description='Configure a computer for a given AiiDA user.') # The default states are those that are shown if no option is given parser.add_argument('-u', '--user', type=str, metavar='EMAIL', help="Configure the computer for the given AiiDA user (otherwise, configure the current default user)", ) parser.add_argument('computer', type=str, help="The name of the computer that you want to configure") parsed_args = parser.parse_args(args) user_email = parsed_args.user computername = parsed_args.computer try: computer = self.get_computer(name=computername) except NotExistent: print >> sys.stderr, "No computer exists with name '{}'".format( computername) sys.exit(1) if user_email is None: user = get_automatic_user() else: from aiida.orm.querybuilder import QueryBuilder qb = QueryBuilder() qb.append(type="user", filters={'email': user_email}) user = qb.first() if user is None: print >> sys.stderr, ("No user with email '{}' in the " "database.".format(user_email)) sys.exit(1) if BACKEND == BACKEND_DJANGO: from aiida.backends.djsite.db.models import DbAuthInfo try: authinfo = DbAuthInfo.objects.get( dbcomputer=computer.dbcomputer, aiidauser=user) old_authparams = authinfo.get_auth_params() except ObjectDoesNotExist: authinfo = DbAuthInfo(dbcomputer=computer.dbcomputer, aiidauser=user) old_authparams = {} elif BACKEND == BACKEND_SQLA: from aiida.backends.sqlalchemy.models.authinfo import DbAuthInfo from aiida.backends.sqlalchemy import session authinfo = session.query(DbAuthInfo).filter( DbAuthInfo.dbcomputer == computer.dbcomputer ).filter( DbAuthInfo.aiidauser == user ).first() if authinfo is None: authinfo = DbAuthInfo( dbcomputer=computer.dbcomputer, aiidauser=user ) old_authparams = {} else: old_authparams = authinfo.get_auth_params() else: raise Exception( "Unknown backend {}".format(BACKEND) ) Transport = computer.get_transport_class() print ("Configuring computer '{}' for the AiiDA user '{}'".format( computername, user.email)) print "Computer {} has transport of type {}".format(computername, computer.get_transport_type()) if user.email != get_configured_user_email(): print "*" * 72 print "** {:66s} **".format("WARNING!") print "** {:66s} **".format( " You are configuring a different user.") print "** {:66s} **".format( " Note that the default suggestions are taken from your") print "** {:66s} **".format( " local configuration files, so they may be incorrect.") print "*" * 72 valid_keys = Transport.get_valid_auth_params() default_authparams = {} for k in valid_keys: if k in old_authparams: default_authparams[k] = old_authparams.pop(k) if old_authparams: print ("WARNING: the following keys were previously in the " "authorization parameters,") print "but have not been recognized and have been deleted:" print ", ".join(old_authparams.keys()) if not valid_keys: print "There are no special keys to be configured. Configuration completed." authinfo.set_auth_params({}) authinfo.save() return print "" print "Note: to leave a field unconfigured, leave it empty and press [Enter]" # I strip out the old auth_params that are not among the valid keys new_authparams = {} for k in valid_keys: key_set = False while not key_set: try: converter_name = '_convert_{}_fromstring'.format(k) try: converter = dict(inspect.getmembers( Transport))[converter_name] except KeyError: print >> sys.stderr, ("Internal error! " "No {} defined in Transport {}".format( converter_name, computer.get_transport_type())) sys.exit(1) if k in default_authparams: readline.set_startup_hook(lambda: readline.insert_text(str(default_authparams[k]))) else: # Use suggestion only if parameters were not already set suggester_name = '_get_{}_suggestion_string'.format(k) try: suggester = dict(inspect.getmembers( Transport))[suggester_name] suggestion = suggester(computer) readline.set_startup_hook(lambda: readline.insert_text(suggestion)) except KeyError: readline.set_startup_hook() txtval = raw_input("=> {} = ".format(k)) if txtval: new_authparams[k] = converter(txtval) key_set = True except ValidationError as e: print "Error in the inserted value: {}".format(e.message) authinfo.set_auth_params(new_authparams) authinfo.save() print "Configuration stored for your user on computer '{}'.".format( computername)
def prompt_for_computer_configuration(computer): import inspect, readline from aiida.orm.computer import Computer as Computer from aiida.common.exceptions import ValidationError for internal_name, name, desc, multiline in ( Computer._conf_attributes): # Check if I should skip this entry shouldcall_name = '_shouldcall_{}'.format(internal_name) try: shouldcallfunc = dict(inspect.getmembers( computer))[shouldcall_name] shouldcall = shouldcallfunc() except KeyError: shouldcall = True if not shouldcall: # Call cleanup code, if present cleanup_name = '_cleanup_{}'.format(internal_name) try: cleanup = dict(inspect.getmembers( computer))[cleanup_name] cleanup() except KeyError: # No cleanup function: this is not a problem, simply # no cleanup is needed pass # Skip this question continue getter_name = '_get_{}_string'.format(internal_name) try: getter = dict(inspect.getmembers( computer))[getter_name] except KeyError: print >> sys.stderr, ("Internal error! " "No {} getter defined in Computer".format(getter_name)) sys.exit(1) previous_value = getter() setter_name = '_set_{}_string'.format(internal_name) try: setter = dict(inspect.getmembers( computer))[setter_name] except KeyError: print >> sys.stderr, ("Internal error! " "No {} setter defined in Computer".format(setter_name)) sys.exit(1) valid_input = False while not valid_input: if multiline: newlines = [] print "=> {}: ".format(name) print " # This is a multiline input, press CTRL+D on a" print " # empty line when you finish" try: for l in previous_value.splitlines(): while True: readline.set_startup_hook(lambda: readline.insert_text(l)) input_txt = raw_input() if input_txt.strip() == '?': print [" > {}".format(descl) for descl in "HELP: {}".format(desc).split('\n')] continue else: newlines.append(input_txt) break # Reset the hook (no default text printed) readline.set_startup_hook() print " # ------------------------------------------" print " # End of old input. You can keep adding " print " # lines, or press CTRL+D to store this value" print " # ------------------------------------------" while True: input_txt = raw_input() if input_txt.strip() == '?': print "\n".join([" > {}".format(descl) for descl in "HELP: {}".format(desc).split('\n')]) continue else: newlines.append(input_txt) except EOFError: # Ctrl+D pressed: end of input. pass input_txt = "\n".join(newlines) else: # No multiline readline.set_startup_hook(lambda: readline.insert_text( previous_value)) input_txt = raw_input("=> {}: ".format(name)) if input_txt.strip() == '?': print "HELP:", desc continue try: setter(input_txt) valid_input = True except ValidationError as e: print >> sys.stderr, "Invalid input: {}".format(e.message) print >> sys.stderr, "Enter '?' for help".format(e.message)
def pre_input_hook(): readline.insert_text(default) readline.redisplay()
def create_configuration(profile='default'): """ :param profile: The profile to be configured :return: The populated profile that was also stored. """ import readline from aiida.common.exceptions import ConfigurationError from validate_email import validate_email aiida_dir = os.path.expanduser(AIIDA_CONFIG_FOLDER) print("Setting up profile {}.".format(profile)) try: confs = get_config() except ConfigurationError: # No configuration file found confs = {} # first time creation check try: confs['profiles'] except KeyError: confs['profiles'] = {} # load the old configuration for the given profile try: this_existing_confs = confs['profiles'][profile] except KeyError: this_existing_confs = {} # if there is an existing configuration, print it and ask if the user wants # to modify it. updating_existing_prof = False if this_existing_confs: print("The following configuration found corresponding to " "profile {}.".format(profile)) for k, v in this_existing_confs.iteritems(): if key_explanation.has_key(k): print("{}: {}".format(key_explanation.get(k), v)) else: print("{}: {}".format(k, v)) answ = query_yes_no("Would you like to change it?", "no") # If the user doesn't want to change it, we abandon if answ is False: return this_existing_confs # Otherwise, we continue. else: updating_existing_prof = True this_new_confs = {} try: # Defining the backend to be used aiida_backend = this_existing_confs.get('AIIDADB_BACKEND') if updating_existing_prof: print("The backend of already stored profiles can not be " "changed. The current backend is {}.".format(aiida_backend)) this_new_confs['AIIDADB_BACKEND'] = aiida_backend else: backend_possibilities = ['django', 'sqlalchemy'] if len(backend_possibilities) > 0: valid_aiida_backend = False while not valid_aiida_backend: backend_ans = raw_input( 'AiiDA backend (available: {} - sqlalchemy is in beta mode): ' .format(', '.join(backend_possibilities))) if backend_ans in backend_possibilities: valid_aiida_backend = True else: print "* ERROR! Invalid backend inserted." print ("* The available middlewares are {}" .format(', '.join(backend_possibilities))) this_new_confs['AIIDADB_BACKEND'] = backend_ans aiida_backend = backend_ans # Setting the timezone timezone = ask_for_timezone( existing_timezone=this_existing_confs.get('TIMEZONE', None)) this_new_confs['TIMEZONE'] = timezone # Setting the email valid_email = False readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get(DEFAULT_USER_CONFIG_FIELD, DEFAULT_AIIDA_USER))) while not valid_email: this_new_confs[DEFAULT_USER_CONFIG_FIELD] = raw_input( 'Default user email: ') valid_email = validate_email( this_new_confs[DEFAULT_USER_CONFIG_FIELD]) if not valid_email: print "** Invalid email provided!" # Setting the database engine db_possibilities = [] if aiida_backend == 'django': db_possibilities.extend(['postgresql_psycopg2', 'sqlite', 'mysql']) elif aiida_backend == 'sqlalchemy': db_possibilities.extend(['postgresql_psycopg2']) if len(db_possibilities) > 0: db_engine = this_existing_confs.get('AIIDADB_ENGINE', db_possibilities[0]) readline.set_startup_hook(lambda: readline.insert_text( db_engine)) valid_db_engine = False while not valid_db_engine: db_engine_ans = raw_input( 'Database engine (available: {} - mysql is deprecated): ' .format(', '.join(db_possibilities))) if db_engine_ans in db_possibilities: valid_db_engine = True else: print "* ERROR! Invalid database engine inserted." print ("* The available engines are {}" .format(', '.join(db_possibilities))) this_new_confs['AIIDADB_ENGINE'] = db_engine_ans if 'sqlite' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'sqlite3' readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', os.path.join(aiida_dir, "aiida.db")))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database location: ') this_new_confs['AIIDADB_HOST'] = "" this_new_confs['AIIDADB_PORT'] = "" this_new_confs['AIIDADB_USER'] = "" this_new_confs['AIIDADB_PASS'] = "" elif 'postgresql_psycopg2' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'postgresql_psycopg2' old_host = this_existing_confs.get('AIIDADB_HOST', 'localhost') if not old_host: old_host = 'localhost' readline.set_startup_hook(lambda: readline.insert_text( old_host)) this_new_confs['AIIDADB_HOST'] = raw_input('PostgreSQL host: ') old_port = this_existing_confs.get('AIIDADB_PORT', '5432') if not old_port: old_port = '5432' readline.set_startup_hook(lambda: readline.insert_text( old_port)) this_new_confs['AIIDADB_PORT'] = raw_input('PostgreSQL port: ') readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', 'aiidadb'))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database name: ') old_user = this_existing_confs.get('AIIDADB_USER', 'aiida') if not old_user: old_user = '******' readline.set_startup_hook(lambda: readline.insert_text( old_user)) this_new_confs['AIIDADB_USER'] = raw_input('AiiDA Database user: '******'AIIDADB_PASS'))) this_new_confs['AIIDADB_PASS'] = raw_input('AiiDA Database password: '******'mysql' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'mysql' old_host = this_existing_confs.get('AIIDADB_HOST', 'localhost') if not old_host: old_host = 'localhost' readline.set_startup_hook(lambda: readline.insert_text( old_host)) this_new_confs['AIIDADB_HOST'] = raw_input('mySQL host: ') old_port = this_existing_confs.get('AIIDADB_PORT', '3306') if not old_port: old_port = '3306' readline.set_startup_hook(lambda: readline.insert_text( old_port)) this_new_confs['AIIDADB_PORT'] = raw_input('mySQL port: ') readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', 'aiidadb'))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database name: ') old_user = this_existing_confs.get('AIIDADB_USER', 'aiida') if not old_user: old_user = '******' readline.set_startup_hook(lambda: readline.insert_text( old_user)) this_new_confs['AIIDADB_USER'] = raw_input('AiiDA Database user: '******'AIIDADB_PASS'))) this_new_confs['AIIDADB_PASS'] = raw_input('AiiDA Database password: '******'sqlite', 'mysql', 'postgres')") # This part for the time being is a bit oddly written # it should change in the future to add the possibility of having a # remote repository. Atm, I act as only a local repo is possible existing_repo = this_existing_confs.get('AIIDADB_REPOSITORY_URI', os.path.join(aiida_dir, "repository-{}/".format(profile))) default_protocol = 'file://' if existing_repo.startswith(default_protocol): existing_repo = existing_repo[len(default_protocol):] readline.set_startup_hook(lambda: readline.insert_text(existing_repo)) new_repo_path = raw_input('AiiDA repository directory: ') new_repo_path = os.path.expanduser(new_repo_path) if not os.path.isabs(new_repo_path): raise ValueError("You must specify an absolute path") if (not os.path.isdir(new_repo_path)): old_umask = os.umask(DEFAULT_UMASK) try: os.makedirs(new_repo_path) finally: os.umask(old_umask) this_new_confs['AIIDADB_REPOSITORY_URI'] = 'file://' + new_repo_path confs['profiles'][profile] = this_new_confs backup_config() store_config(confs) return this_new_confs finally: readline.set_startup_hook(lambda: readline.insert_text(""))
try: import readline except ImportError: import pyreadline as readline s = 'Sikorskiy' readline.set_startup_hook((lambda: readline.insert_text(input(s))))
def create_configuration_old(profile='default'): """ :param database: create the configuration file """ import readline from aiida.backends import settings from aiida.common.exceptions import ConfigurationError # BE CAREFUL: THIS IS THE DJANGO VALIDATIONERROR from django.core.exceptions import ValidationError as DjangoValidationError from django.core.validators import EmailValidator aiida_dir = os.path.expanduser(AIIDA_CONFIG_FOLDER) try: confs = get_config() except ConfigurationError: # No configuration file found confs = {} # first time creation check try: confs['profiles'] except KeyError: confs['profiles'] = {} # load the old configuration for the given profile try: this_existing_confs = confs['profiles'][profile] except KeyError: this_existing_confs = {} this_new_confs = {} # Set the timezone timezone = ask_for_timezone(existing_timezone=this_existing_confs.get('TIMEZONE', None)) this_new_confs['TIMEZONE'] = timezone try: valid_email = False email_validator = EmailValidator() readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get(DEFAULT_USER_CONFIG_FIELD, DEFAULT_AIIDA_USER))) while not valid_email: this_new_confs[DEFAULT_USER_CONFIG_FIELD] = raw_input('Default user email: ') try: email_validator(this_new_confs[DEFAULT_USER_CONFIG_FIELD]) valid_email = True except DjangoValidationError: print "** Invalid email provided!" readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_ENGINE', 'postgres'))) this_new_confs['AIIDADB_ENGINE'] = raw_input('Database engine: ') if 'sqlite' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'sqlite3' readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', os.path.join(aiida_dir, "aiida.db")))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database location: ') this_new_confs['AIIDADB_HOST'] = "" this_new_confs['AIIDADB_PORT'] = "" this_new_confs['AIIDADB_USER'] = "" this_new_confs['AIIDADB_PASS'] = "" elif 'postgre' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'postgresql_psycopg2' old_host = this_existing_confs.get('AIIDADB_HOST', 'localhost') if not old_host: old_host = 'localhost' readline.set_startup_hook(lambda: readline.insert_text( old_host)) this_new_confs['AIIDADB_HOST'] = raw_input('PostgreSQL host: ') old_port = this_existing_confs.get('AIIDADB_PORT', '5432') if not old_port: old_port = '5432' readline.set_startup_hook(lambda: readline.insert_text( old_port)) this_new_confs['AIIDADB_PORT'] = raw_input('PostgreSQL port: ') readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', 'aiidadb'))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database name: ') old_user = this_existing_confs.get('AIIDADB_USER', 'aiida') if not old_user: old_user = '******' readline.set_startup_hook(lambda: readline.insert_text( old_user)) this_new_confs['AIIDADB_USER'] = raw_input('AiiDA Database user: '******'AIIDADB_PASS', 'aiida_password'))) this_new_confs['AIIDADB_PASS'] = raw_input('AiiDA Database password: '******'django', 'sqlalchemy'] possibilities = ['django'] if len(possibilities) > 0: aiida_backend = this_existing_confs.get('AIIDADB_BACKEND', 'django') readline.set_startup_hook(lambda: readline.insert_text( aiida_backend)) valid_aiida_backend = False while not valid_aiida_backend: backend_ans = raw_input('AiiDA backend (available: {}): ' .format(', '.join(possibilities))) if backend_ans in possibilities: valid_aiida_backend = True else: print "* ERROR! Invalid backend inserted." print "* The available middlewares are {}"\ .format(', '.join(possibilities)) this_new_confs['AIIDADB_BACKEND'] = backend_ans elif 'mysql' in this_new_confs['AIIDADB_ENGINE']: this_new_confs['AIIDADB_ENGINE'] = 'mysql' old_host = this_existing_confs.get('AIIDADB_HOST', 'localhost') if not old_host: old_host = 'localhost' readline.set_startup_hook(lambda: readline.insert_text( old_host)) this_new_confs['AIIDADB_HOST'] = raw_input('mySQL host: ') old_port = this_existing_confs.get('AIIDADB_PORT', '3306') if not old_port: old_port = '3306' readline.set_startup_hook(lambda: readline.insert_text( old_port)) this_new_confs['AIIDADB_PORT'] = raw_input('mySQL port: ') readline.set_startup_hook(lambda: readline.insert_text( this_existing_confs.get('AIIDADB_NAME', 'aiidadb'))) this_new_confs['AIIDADB_NAME'] = raw_input('AiiDA Database name: ') old_user = this_existing_confs.get('AIIDADB_USER', 'aiida') if not old_user: old_user = '******' readline.set_startup_hook(lambda: readline.insert_text( old_user)) this_new_confs['AIIDADB_USER'] = raw_input('AiiDA Database user: '******'AIIDADB_PASS', 'aiida_password'))) this_new_confs['AIIDADB_PASS'] = raw_input('AiiDA Database password: '******'sqlite', 'mysql', 'postgres')") # This part for the time being is a bit oddly written # it should change in the future to add the possibility of having a # remote repository. Atm, I act as only a local repo is possible existing_repo = this_existing_confs.get('AIIDADB_REPOSITORY_URI', os.path.join(aiida_dir, "repository-{}/".format( settings.AIIDADB_PROFILE))) default_protocol = 'file://' if existing_repo.startswith(default_protocol): existing_repo = existing_repo[len(default_protocol):] readline.set_startup_hook(lambda: readline.insert_text(existing_repo)) new_repo_path = raw_input('AiiDA repository directory: ') new_repo_path = os.path.expanduser(new_repo_path) if not os.path.isabs(new_repo_path): raise ValueError("You must specify an absolute path") if (not os.path.isdir(new_repo_path)): old_umask = os.umask(DEFAULT_UMASK) try: os.makedirs(new_repo_path) finally: os.umask(old_umask) this_new_confs['AIIDADB_REPOSITORY_URI'] = 'file://' + new_repo_path confs['profiles'][profile] = this_new_confs backup_config() store_config(confs) finally: readline.set_startup_hook(lambda: readline.insert_text(""))
def prefill_input(text=None): if text: readline.set_startup_hook(lambda: readline.insert_text(text)) else: readline.set_startup_hook()
def raw_input(prompt, suggestion=''): readline.set_startup_hook(lambda: readline.insert_text(suggestion)) s = _raw_input(prompt) readline.set_startup_hook() return s
def pre_input_hook(): readline.insert_text('from pre_input_hook') readline.redisplay()
def setup_vagrant_pattern(shutit, skel_path, skel_delivery, skel_domain, skel_module_name, skel_shutitfiles, skel_domain_hash, skel_depends, skel_vagrant_num_machines, skel_vagrant_machine_prefix, skel_vagrant_ssh_access, skel_vagrant_docker, skel_vagrant_snapshot, skel_vagrant_upload, skel_vagrant_image_name): ################################################################################ # BEGIN ARGUMENT HANDLING ################################################################################ # Gather requirements for multinode vagrant setup: options = [] if skel_vagrant_num_machines is None: options.append({ 'name': 'num_machines', 'question': 'How many machines do you want?', 'value': '3', 'ok_values': [] }) else: num_machines = skel_vagrant_num_machines if skel_vagrant_machine_prefix is None: options.append({ 'name': 'machine_prefix', 'question': 'What do you want to call the machines (eg superserver)?', 'value': 'machine', 'ok_values': [] }) else: machine_prefix = skel_vagrant_machine_prefix if skel_vagrant_ssh_access is None: options.append({ 'name': 'ssh_access', 'question': 'Do you want to have open ssh access between machines (yes or no)?', 'value': 'yes', 'ok_values': ['yes', 'no'] }) else: ssh_access = skel_vagrant_ssh_access if skel_vagrant_docker is None: options.append({ 'name': 'docker', 'question': 'Do you want Docker on the machine (yes or no)?', 'value': 'no', 'ok_values': ['yes', 'no'] }) else: docker = skel_vagrant_docker if skel_vagrant_snapshot is None: options.append({ 'name': 'snapshot', 'question': 'Do you want to snapshot the machine on completion (yes or no)?', 'value': 'no', 'ok_values': ['yes', 'no'] }) else: snapshot = skel_vagrant_snapshot if skel_vagrant_upload is None: options.append({ 'name': 'upload', 'question': 'Do you want to upload the snapshot on completion (yes or no)?', 'value': 'no', 'ok_values': ['yes', 'no'] }) else: upload = skel_vagrant_upload if skel_vagrant_image_name is None: options.append({ 'name': 'image_name', 'question': 'What base vagrant image you want?', 'value': 'ubuntu/xenial64', 'ok_values': [] }) else: image_name = skel_vagrant_image_name options.append({ 'name': 'sudo_password', 'question': 'Input sudo password to save time (will be saved in readonly-by-you file)', 'value': '', 'ok_values': [] }) if options: while True: table = texttable.Texttable() count = 1 rows = [['No.', 'Option', 'Current value']] for opt in options: rows.append([str(count), opt['question'], opt['value']]) count += 1 #table.set_deco(texttable.Texttable.HEADER) table.set_cols_dtype(['i', 'a', 'a']) table.set_cols_align(['r', "l", "r"]) table.add_rows(rows) print(table.draw() + '\n') readline.set_startup_hook(lambda: readline.insert_text('')) for choice_li in ('5', '4', '3', '2', '1'): readline.add_history(choice_li) choice = shutit_util.util_raw_input(prompt=''' Choose an item to change if you want to change the default. Input 'c' to continue to the build. If you want to change a config, choose the number: ''') readline.set_startup_hook() if choice in ('c', '1', '2', '3', '4', '5', '6', '7'): if choice == 'c': break try: choice = int(choice) except ValueError: print('Bad value, ignoring') continue else: print('Bad value, ignoring') continue # Print out the actual choice.... print(rows[choice][1]) # off by one choice -= 1 item = options[choice] value = shutit_util.get_input(msg='Input the value: ', color=None) if item['ok_values'] and value not in item['ok_values']: print('Bad value, ignoring') continue item['value'] = value for opt in options: if opt['name'] == 'num_machines': num_machines = int(opt['value']) if opt['name'] == 'machine_prefix': machine_prefix = opt['value'] if opt['name'] == 'ssh_access': if opt['value'] == 'no': ssh_access = False elif opt['value'] == 'yes': ssh_access = True else: shutit.fail('Bad value for ssh_access') if opt['name'] == 'docker': if opt['value'] == 'no': docker = False elif opt['value'] == 'yes': docker = True else: shutit.fail('Bad value for docker') if opt['name'] == 'snapshot': if opt['value'] == 'no': snapshot = False elif opt['value'] == 'yes': snapshot = True else: shutit.fail('Bad value for snapshot') if opt['name'] == 'upload': if opt['value'] == 'no': upload = False elif opt['value'] == 'yes': upload = True else: shutit.fail('Bad value for upload') if opt['name'] == 'image_name': image_name = opt['value'] if opt['name'] == 'sudo_password': sudo_password = opt['value'] num_machines = int(num_machines) ################################################################################ # END ARGUMENT HANDLING ################################################################################ ################################################################################ # BEGIN TEXT GOBBETS ################################################################################ # Set up Vagrantfile data for the later machine_stanzas = '' vagrant_up_section = '' machines_update = ''' # machines is a dict of dicts containing information about each machine for you to use. machines = {}''' for m in range(1, num_machines + 1): machine_name = machine_prefix + str(m) machine_fqdn = machine_name + '.vagrant.test' # vagrant_image is calculated within the code later machine_stanzas += (''' config.vm.define "''' + machine_name + '''" do |''' + machine_name + '''| ''' + machine_name + """.vm.box = ''' + '"' + vagrant_image + '"' + ''' """ + machine_name + '''.vm.hostname = "''' + machine_fqdn + '''"''' + ''' config.vm.provider :virtualbox do |vb| vb.name = "''' + skel_module_name + '_' + str(m) + '''" end end''') machines_update += """ machines.update({'""" + machine_name + """':{'fqdn':'""" + machine_fqdn + """'}})""" vagrant_up_section += ''' try: pw = file('secret').read().strip() except IOError: pw = '' if pw == '': shutit.log("""You can get round this manual step by creating a 'secret' with your password: '******'""",level=logging.CRITICAL) pw = shutit.get_env_pass() import time time.sleep(10)''' vagrant_dir_section_1 = """ if shutit.build['vagrant_run_dir'] is None: shutit.build['vagrant_run_dir'] = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))) + '/vagrant_run' timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S") shutit.build['module_name'] = '""" + skel_module_name + """_' + timestamp shutit.build['this_vagrant_run_dir'] = shutit.build['vagrant_run_dir'] + '/' + timestamp shutit.send(' command rm -rf ' + shutit.build['this_vagrant_run_dir'] + ' && command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir'])""" vagrant_dir_section_n = """ shutit.send(' command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir'])""" vagrant_setup = r""" # Set up the sessions shutit_sessions = {} for machine in sorted(machines.keys()): shutit_sessions.update({machine:shutit.create_session('bash')}) # Set up and validate landrush for machine in sorted(machines.keys()): shutit_session = shutit_sessions[machine] shutit_session.send('cd ' + shutit.build['this_vagrant_run_dir']) # Remove any existing landrush entry. shutit_session.send('vagrant landrush rm ' + machines[machine]['fqdn']) # Needs to be done serially for stability reasons. try: shutit_session.multisend('vagrant up --provider ' + shutit.cfg['shutit-library.virtualization.virtualization.virtualization']['virt_method'] + machine_name,{'assword for':pw,'assword:':pw}) except NameError: shutit.multisend('vagrant up ' + machine,{'assword for':pw,'assword:':pw},timeout=99999) if shutit.send_and_get_output("vagrant status 2> /dev/null | grep -w ^" + machine + " | awk '{print $2}'") != 'running': shutit.pause_point("machine: " + machine + " appears not to have come up cleanly") ip = shutit.send_and_get_output('''vagrant landrush ls 2> /dev/null | grep -w ^''' + machines[machine]['fqdn'] + ''' | awk '{print $2}' ''') machines.get(machine).update({'ip':ip}) # Check that the landrush entry is there. shutit_session.send('vagrant landrush ls | grep -w ' + machines[machine]['fqdn']) shutit_session.login(command='vagrant ssh ' + machine) shutit_session.login(command='sudo su - ') # Correct /etc/hosts shutit_session.send(r'''cat <(echo -n $(ip -4 -o addr show scope global | grep -v 10.0.2.15 | head -1 | awk '{print $4}' | sed 's/\(.*\)\/.*/\1/') $(hostname)) <(cat /etc/hosts | grep -v $(hostname -s)) > /tmp/hosts && mv -f /tmp/hosts /etc/hosts''') # Correct any broken ip addresses. if shutit_session.send_and_get_output('''vagrant landrush ls | grep ''' + machine + ''' | grep 10.0.2.15 | wc -l''') != '0': shutit_session.log('A 10.0.2.15 landrush ip was detected for machine: ' + machine + ', correcting.',level=logging.WARNING) # This beaut gets all the eth0 addresses from the machine and picks the first one that it not 10.0.2.15. while True: ipaddr = shutit_session.send_and_get_output(r'''ip -4 -o addr show scope global | grep -v 10.0.2.15 | head -1 | awk '{print $4}' | sed 's/\(.*\)\/.*/\1/' ''') if ipaddr[0] not in ('1','2','3','4','5','6','7','8','9'): time.sleep(10) else: break # Send this on the host (shutit, not shutit_session) shutit.send('vagrant landrush set ' + machines[machine]['fqdn'] + ' ' + ipaddr) # Gather landrush info for machine in sorted(machines.keys()): ip = shutit.send_and_get_output('''vagrant landrush ls 2> /dev/null | grep -w ^''' + machines[machine]['fqdn'] + ''' | awk '{print $2}' ''') machines.get(machine).update({'ip':ip})""" if ssh_access: copy_keys_code = ''' for machine in sorted(machines.keys()): shutit_session = shutit_sessions[machine] root_password = '******' shutit_session.install('net-tools') # netstat needed if not shutit_session.command_available('host'): shutit_session.install('bind-utils') # host needed shutit_session.multisend('passwd',{'assword:':root_password}) shutit_session.send("""sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config""") shutit_session.send("""sed -i 's/.*PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config""") shutit_session.send('service ssh restart || systemctl restart sshd') shutit_session.multisend('ssh-keygen',{'Enter':'','verwrite':'n'}) for machine in sorted(machines.keys()): for copy_to_machine in machines: for item in ('fqdn','ip'): shutit_session.multisend('ssh-copy-id root@' + machines[copy_to_machine][item],{'assword:':root_password,'ontinue conn':'yes'})''' else: copy_keys_code = '' if docker: docker_code = ''' for machine in sorted(machines.keys()): shutit_session = shutit_sessions[machine] # Workaround for docker networking issues + landrush. shutit_session.install('docker') shutit_session.insert_text('Environment=GODEBUG=netdns=cgo','/lib/systemd/system/docker.service',pattern='.Service.') shutit_session.send('mkdir -p /etc/docker',note='Create the docker config folder') shutit_session.send_file('/etc/docker/daemon.json',"""{ "dns": ["8.8.8.8"] }""",note='Use the google dns server rather than the vagrant one. Change to the value you want if this does not work, eg if google dns is blocked.') shutit_session.send('systemctl daemon-reload && systemctl restart docker')''' else: docker_code = '' user_code = ''' for machine in sorted(machines.keys()): shutit_session = shutit_sessions[machine] shutit_session.run_script(r\'\'\'#!/bin/sh # See https://raw.githubusercontent.com/ianmiell/vagrant-swapfile/master/vagrant-swapfile.sh fallocate -l \'\'\' + shutit.cfg[self.module_id]['swapsize'] + r\'\'\' /swapfile ls -lh /swapfile chown root:root /swapfile chmod 0600 /swapfile ls -lh /swapfile mkswap /swapfile swapon /swapfile swapon -s grep -i --color swap /proc/meminfo echo "\n/swapfile none swap sw 0 0" >> /etc/fstab\'\'\') shutit_session.multisend('adduser person',{'Enter new UNIX password':'******','Retype new UNIX password:'******'person','Full Name':'','Phone':'','Room':'','Other':'','Is the information correct':'Y'})''' machine_seed_code = ''' for machine in sorted(machines.keys()): shutit_session = shutit_sessions[machine] shutit_session.send('hostname')''' if snapshot: # TODO: add 'copy to snapshot folder function' # TODO: create snapshot subfolder snapshot_code = ''' for machine in sorted(machines.keys()): shutit.send('vagrant snapshot save ' + machine,note='Snapshot the vagrant machine')''' else: snapshot_code = '' if upload: upload_code = ''' # Create a stable box name for this particular build boxname_base = shutit.build['module_name'] + '_' + str(int(time.time())) for machine in sorted(machines.keys()): boxname = boxname_base + '_' + machine + '.box' shutit.send('vagrant package --output ' + boxname + ' --vagrantfile Vagrantfile ' + machine,note='Package the vagrant machine') shutit.send('mvn deploy:deploy-file -DgroupId=com.meirionconsulting -DartifactId=' + boxname + ' -Dversion=0.0.0.1 -Dpackaging=tar.gz -DrepositoryId=nexus.meirionconsulting.com -Durl=http://nexus.meirionconsulting.com/repository/maven-releases -Dfile=' + boxname,note='Push the vagrant box')''' else: upload_code = '' final_message = """ shutit.log('''******************************************************************************** # Vagrantfile created in: ''' + shutit.build['vagrant_run_dir'] + ''' # Run: cd ''' + shutit.build['vagrant_run_dir'] + ''' && vagrant status && vagrant landrush ls # to get information about your machines' setup. ********************************************************************************''',add_final_message=True,level=logging.DEBUG)""" if snapshot: final_message += """ shutit.log('''******************************************************************************** Your VM images have been snapshotted in the folder ''' + shutit.build['vagrant_run_dir'] + ''' ******************************************************************************** ''')""" get_config_section = """ def get_config(self, shutit): shutit.get_config(self.module_id,'vagrant_image',default='""" + image_name + """') shutit.get_config(self.module_id,'vagrant_provider',default='virtualbox') shutit.get_config(self.module_id,'gui',default='false') shutit.get_config(self.module_id,'memory',default='1024') shutit.get_config(self.module_id,'swapsize',default='2G') return True""" header = '# Generated by shutit skeleton\n' shared_imports = '''import random import datetime import logging import string import os import inspect''' ################################################################################ # END TEXT GOBBETS ################################################################################ ################################################################################ # FILE SETUP BEGIN ################################################################################ # Set up files: # .gitignore gitignore_filename = skel_path + '/.gitignore' gitignore_file = open(gitignore_filename, 'w+') gitignore_file.write('''*pyc vagrant_run secret''') gitignore_file.close() os.chmod(gitignore_filename, 0700) secretfile_filename = skel_path + '/secret' secretfile_file = open(secretfile_filename, 'w+') secretfile_file.write(sudo_password) secretfile_file.close() os.chmod(secretfile_filename, 0400) # README.md readme_filename = skel_path + '/README.md' readme_file = open(readme_filename, 'w+') readme_file.write(''' ## Install - virtualbox - vagrant - git - python-pip ## Run ``` git clone --recursive [this repo] cd [this repo file] ./run.sh ``` ''') readme_file.close() os.chmod(readme_filename, 0700) # run.sh runsh_filename = skel_path + '/run.sh' runsh_file = open(runsh_filename, 'w+') runsh_file.write('''#!/bin/bash set -e [[ -z "$SHUTIT" ]] && SHUTIT="$1/shutit" [[ ! -a "$SHUTIT" ]] || [[ -z "$SHUTIT" ]] && SHUTIT="$(which shutit)" if [[ ! -a "$SHUTIT" ]] then echo "Must have shutit on path, eg export PATH=$PATH:/path/to/shutit_dir" exit 1 fi ./destroy_vms.sh $SHUTIT build --echo -d bash -m shutit-library/vagrant -m shutit-library/virtualization -l debug "$@" if [[ $? != 0 ]] then exit 1 fi''') runsh_file.close() os.chmod(runsh_filename, 0o755) # destroy_vms.sh destroyvmssh_filename = skel_path + '/destroy_vms.sh' destroyvmssh_file = open(destroyvmssh_filename, 'w+') destroyvmssh_file_contents = '''#!/bin/bash''' if snapshot: destroyvmssh_file_contents += ''' FOLDER=$( ls $( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd )/vagrant_run 2> /dev/null) ANSWER='y' if [[ $FOLDER != '' ]] then echo "This is snapshotted - sure you want to continue deleting? (y/n)" echo See folder: vagrant_run/${FOLDER} read ANSWER fi if [[ ${ANSWER} != 'y' ]] then echo Refusing to continue exit 1 fi''' destroyvmssh_file_contents += ''' MODULE_NAME=''' + skel_module_name + ''' rm -rf $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/vagrant_run/* XARGS_FLAG='--no-run-if-empty' if echo '' | xargs --no-run-if-empty then XARGS_FLAG='' fi if [[ $(command -v VBoxManage) != '' ]] then while true do VBoxManage list runningvms | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -IXXX VBoxManage controlvm 'XXX' poweroff && VBoxManage list vms | grep ''' + skel_module_name + ''' | awk '{print $1}' | xargs -IXXX VBoxManage unregistervm 'XXX' --delete # The xargs removes whitespace if [[ $(VBoxManage list vms | grep ${MODULE_NAME} | wc -l | xargs) -eq '0' ]] then break else ps -ef | grep virtualbox | grep ${MODULE_NAME} | awk '{print $2}' | xargs kill sleep 10 fi done fi if [[ $(command -v virsh) ]] && [[ $(kvm-ok 2>&1 | command grep 'can be used') != '' ]] then virsh list | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -n1 virsh destroy fi''' destroyvmssh_file.write(destroyvmssh_file_contents) destroyvmssh_file.close() os.chmod(destroyvmssh_filename, 0o755) # build.cnf os.system('mkdir -p ' + skel_path + '/configs') # git setup os.system('git init') os.system('git submodule init') os.system('git submodule add https://github.com/ianmiell/shutit-library') os.system('git submodule update') # User message log_message = ''' # Run: cd ''' + skel_path + ''' && ./run.sh # to run. ''' if upload: log_message += r''' As you have chosen to upload, you may want to install maven and set your ~/.m2/settings.xml file to contain these settings: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers> <server> <id>nexus.meirionconsulting.com</id> <username>uploader</username> <password>uploader</password> </server> </servers> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings> so you can upload vagrant boxes. ''' shutit.log(log_message, transient=True) ################################################################################ # FILE SETUP END ################################################################################ ################################################################################ # BEGIN MODULE SETUP ################################################################################ ################################################################################ # BEGIN SHUTITFILE HANDLING ################################################################################ # Handle shutitfiles. If there are no shutitfiles, handle separately. # If there are more than one, you need to treat the first one differently. if skel_shutitfiles: _total = len(skel_shutitfiles) _count = 0 for skel_shutitfile in skel_shutitfiles: _count += 1 module_modifier = '_' + str(_count) new_module_filename = skel_path + '/' + os.path.join( skel_module_name + module_modifier + '.py') shutit.cfg['skeleton']['module_modifier'] = module_modifier (sections, skel_module_id, skel_module_name, _, _) = shutitfile.shutitfile_to_shutit_module( shutit, skel_shutitfile, skel_path, skel_domain, skel_module_name, skel_domain_hash, skel_delivery, skel_depends, _count, _total, module_modifier) shutit.cfg['skeleton']['header_section'] = sections[ 'header_section'] shutit.cfg['skeleton']['config_section'] = sections[ 'config_section'] shutit.cfg['skeleton']['build_section'] = sections['build_section'] shutit.cfg['skeleton']['finalize_section'] = sections[ 'finalize_section'] shutit.cfg['skeleton']['test_section'] = sections['test_section'] shutit.cfg['skeleton']['isinstalled_section'] = sections[ 'isinstalled_section'] shutit.cfg['skeleton']['start_section'] = sections['start_section'] shutit.cfg['skeleton']['stop_section'] = sections['stop_section'] shutit.cfg['skeleton']['final_section'] = sections['final_section'] module_file = open(new_module_filename, 'w+') # We only write out the heavy stuff for vagrant on the first time round if _count == 1: module_file.write( header + shared_imports + """ """ + shutit.cfg['skeleton']['header_section'] + """ def build(self, shutit): shutit.run_script('''""" + destroyvmssh_file_contents + """''') vagrant_image = shutit.cfg[self.module_id]['vagrant_image'] vagrant_provider = shutit.cfg[self.module_id]['vagrant_provider'] gui = shutit.cfg[self.module_id]['gui'] memory = shutit.cfg[self.module_id]['memory'] """ + vagrant_dir_section_1 + """ if shutit.send_and_get_output('vagrant plugin list | grep landrush') == '': shutit.send('vagrant plugin install landrush') shutit.send('vagrant init ' + vagrant_image) shutit.send_file(shutit.build['this_vagrant_run_dir'] + '/Vagrantfile','''Vagrant.configure("2") do |config| config.landrush.enabled = true config.vm.provider "virtualbox" do |vb| vb.gui = ''' + gui + ''' vb.memory = "''' + memory + '''" end """ + machine_stanzas + """ end''') """ + machines_update + """ """ + vagrant_up_section + """ """ + vagrant_setup + """ """ + copy_keys_code + """ """ + docker_code + """ """ + user_code + """ """ + shutit.cfg['skeleton']['build_section'] + """ """ + final_message + """ """ + snapshot_code + """ """ + upload_code + """ return True """ + get_config_section + """ """ + shutit.cfg['skeleton']['config_section'] + """ return True def test(self, shutit): """ + shutit.cfg['skeleton']['test_section'] + """ return True def finalize(self, shutit): """ + shutit.cfg['skeleton']['finalize_section'] + """ return True def is_installed(self, shutit): """ + shutit.cfg['skeleton']['isinstalled_section'] + """ return False def start(self, shutit): """ + shutit.cfg['skeleton']['start_section'] + """ return True def stop(self, shutit): """ + shutit.cfg['skeleton']['stop_section'] + """ return True def module(): return """ + skel_module_name + module_modifier + """( '""" + skel_module_id + """', """ + skel_domain_hash + """.000""" + str(_count) + """, description='', maintainer='', delivery_methods=['bash'], depends=['""" + skel_depends + """','shutit-library.virtualization.virtualization.virtualization','tk.shutit.vagrant.vagrant.vagrant'] )""") # In the non-first one, we don't have all the setup stuff (but we do have some!) else: module_file.write( header + shared_imports + """ """ + shutit.cfg['skeleton']['header_section'] + """ def build(self, shutit): shutit.run_script('''""" + destroyvmssh_file_contents + """''') """ + vagrant_dir_section_n + """ """ + shutit.cfg['skeleton']['build_section'] + """ """ + shutit.cfg['skeleton']['config_section'] + """ return True def test(self, shutit): """ + shutit.cfg['skeleton']['test_section'] + """ return True def finalize(self, shutit): """ + shutit.cfg['skeleton']['finalize_section'] + """ return True def is_installed(self, shutit): """ + shutit.cfg['skeleton']['isinstalled_section'] + """ return False def start(self, shutit): """ + shutit.cfg['skeleton']['start_section'] + """ return True def stop(self, shutit): """ + shutit.cfg['skeleton']['stop_section'] + """ return True def module(): return """ + skel_module_name + module_modifier + """( '""" + skel_module_id + """',""" + skel_domain_hash + """.000""" + str(_count) + """, description='', maintainer='', delivery_methods=['bash'], depends=['""" + skel_depends + """','shutit-library.virtualization.virtualization.virtualization','tk.shutit.vagrant.vagrant.vagrant'] )""") module_file.close() # Set up build.cnf build_cnf_filename = skel_path + '/configs/build.cnf' if _count == 1: build_cnf_file = open(build_cnf_filename, 'w+') build_cnf_file.write( '''############################################################################### # PLEASE NOTE: This file should be changed only by the maintainer. # PLEASE NOTE: This file is only sourced if the "shutit build" command is run # and this file is in the relative path: configs/build.cnf # This is to ensure it is only sourced if _this_ module is the # target. ############################################################################### # When this module is the one being built, which modules should be built along with it by default? # This feeds into automated testing of each module. [''' + skel_module_id + '''] shutit.core.module.build:yes''') build_cnf_file.close() else: build_cnf_file = open(build_cnf_filename, 'a') build_cnf_file.write(''' [''' + skel_domain + '''.''' + skel_module_name + module_modifier + '''] shutit.core.module.build:yes''') build_cnf_file.close() os.chmod(build_cnf_filename, 0o400) ################################################################################ # END SHUTITFILE HANDLING ################################################################################ else: # No shutitfiles to consider, so simpler logic here. shutit.cfg['skeleton'][ 'header_section'] = 'from shutit_module import ShutItModule\n\nclass ' + skel_module_name + '(ShutItModule):\n' new_module_filename = skel_path + '/' + skel_module_name + '.py' module_file = open(new_module_filename, 'w+') module_file.write( header + shared_imports + """ """ + shutit.cfg['skeleton']['header_section'] + """ def build(self, shutit): shutit.run_script('''""" + destroyvmssh_file_contents + """''') vagrant_image = shutit.cfg[self.module_id]['vagrant_image'] vagrant_provider = shutit.cfg[self.module_id]['vagrant_provider'] gui = shutit.cfg[self.module_id]['gui'] memory = shutit.cfg[self.module_id]['memory'] shutit.build['vagrant_run_dir'] = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))) + '/vagrant_run' shutit.build['module_name'] = '""" + skel_module_name + """_' + ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(6)) shutit.build['this_vagrant_run_dir'] = shutit.build['vagrant_run_dir'] + '/' + shutit.build['module_name'] shutit.send(' command rm -rf ' + shutit.build['this_vagrant_run_dir'] + ' && command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir']) shutit.send('command rm -rf ' + shutit.build['this_vagrant_run_dir'] + ' && command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir']) if shutit.send_and_get_output('vagrant plugin list | grep landrush') == '': shutit.send('vagrant plugin install landrush') shutit.send('vagrant init ' + vagrant_image) shutit.send_file(shutit.build['this_vagrant_run_dir'] + '/Vagrantfile','''Vagrant.configure("2") do |config| config.landrush.enabled = true config.vm.provider "virtualbox" do |vb| vb.gui = ''' + gui + ''' vb.memory = "''' + memory + '''" end """ + machine_stanzas + """ end''') """ + machines_update + """ """ + vagrant_up_section + """ """ + vagrant_setup + """ """ + copy_keys_code + """ """ + docker_code + """ """ + user_code + """ """ + machine_seed_code + """ """ + final_message + """ """ + snapshot_code + """ """ + upload_code + """ return True """ + get_config_section + """ def test(self, shutit): return True def finalize(self, shutit): return True def is_installed(self, shutit): return False def start(self, shutit): return True def stop(self, shutit): return True def module(): return """ + skel_module_name + """( '""" + skel_domain + '''.''' + skel_module_name + """', """ + skel_domain_hash + """.0001, description='', maintainer='', delivery_methods=['bash'], depends=['""" + skel_depends + """','shutit-library.virtualization.virtualization.virtualization','tk.shutit.vagrant.vagrant.vagrant'] )""") module_file.close() build_cnf_filename = skel_path + '/configs/build.cnf' build_cnf_file = open(build_cnf_filename, 'w+') build_cnf_file.write( '''############################################################################### # PLEASE NOTE: This file should be changed only by the maintainer. # PLEASE NOTE: This file is only sourced if the "shutit build" command is run # and this file is in the relative path: configs/build.cnf # This is to ensure it is only sourced if _this_ module is the # target. ############################################################################### # When this module is the one being built, which modules should be built along with it by default? # This feeds into automated testing of each module. [''' + skel_domain + '''.''' + skel_module_name + '''] shutit.core.module.build:yes''') build_cnf_file.close() os.chmod(build_cnf_filename, 0o400)
def inserter(): readline.insert_text(s) readline.redisplay()
def rlinput(prompt, prefill=''): readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt) finally: readline.set_startup_hook()
def prepend_previous_text(self): if self.prepend_text: readline.insert_text(self.prepend_text) readline.redisplay() self.prepend_text = None
def pre_input_hook(): readline.insert_text(opvraging) readline.redisplay()
def insert_default_text(prompt, text): '''Helper function''' time.sleep(0.01) libedit.rl_set_prompt(prompt) readline.insert_text(text) libedit.rl_forced_update_display()
def readline_hook(): readline.insert_text(toedit) readline.redisplay() readline.set_pre_input_hook(lambda: False)
def hook(): readline.insert_text(text) readline.redisplay()
def do_configure(email, first_name, last_name, institution, no_password): from aiida.backends.utils import is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() import readline import getpass from aiida.orm.implementation import User # if len(args) != 1: # print >> sys.stderr, ("You have to pass (only) one parameter after " # "'user configure', the email of") # print >> sys.stderr, "the user to be configured." # sys.exit(1) # email = args[0] user_list = User.search_for_users(email=email) if user_list is not None and len(user_list) >= 1: user = user_list[0] print "" print ("An AiiDA user for email '{}' is already present " "in the DB:".format(email)) # print "First name: {}".format(user.first_name) # print "Last name: {}".format(user.last_name) # print "Institution: {}".format(user.institution) configure_user = False reply = click.confirm("Do you want to reconfigure it?") # if not reply: # pass # elif reply.lower() == 'n': # pass if reply: configure_user = True # else: # print "Invalid answer, assuming answer was 'NO'" else: configure_user = True user = User(email=email) print "Configuring a new user with email '{}'".format(email) if configure_user: try: kwargs = {} # for field in models.DbUser.REQUIRED_FIELDS: for field in User.REQUIRED_FIELDS: verbose_name = field.capitalize() readline.set_startup_hook(lambda: readline.insert_text( getattr(user, field))) if field == 'first_name' and first_name: kwargs[field] = first_name elif field == 'last_name' and last_name: kwargs[field] = last_name elif field == 'institution' and institution: kwargs[field] = institution else: kwargs[field] = raw_input('{}: '.format(verbose_name)) finally: readline.set_startup_hook(lambda: readline.insert_text("")) for k, v in kwargs.iteritems(): setattr(user, k, v) change_password = False if no_password: user.password = None else: if user.has_usable_password(): reply = raw_input("Do you want to replace the user password? [y/N] ") reply = reply.strip() if not reply: pass elif reply.lower() == 'n': pass elif reply.lower() == 'y': change_password = True else: print "Invalid answer, assuming answer was 'NO'" else: reply = raw_input("The user has no password, do you want to set one? [y/N] ") reply = reply.strip() if not reply: pass elif reply.lower() == 'n': pass elif reply.lower() == 'y': change_password = True else: print "Invalid answer, assuming answer was 'NO'" if change_password: match = False while not match: new_password = getpass.getpass("Insert the new password: "******"Insert the new password (again): ") if new_password == new_password_check: match = True else: print "ERROR, the two passwords do not match." ## Set the password here user.password = new_password else: user.password = None user.force_save() print ">> User {} {} saved. <<".format(user.first_name, user.last_name) if not user.has_usable_password(): print "** NOTE: no password set for this user, " print " so he/she will not be able to login" print " via the REST API and the Web Interface."