def do_delegation(self, cmd, args): load_plugins() # project precondition proj_path = registry.pathfor(cmd) if proj_path is not None: os.chdir(proj_path) cmd = args[0] args = args[1:] if '-' in cmd: cmd, newargs = cmd.split('-', 1) args = [newargs, ] + args comcls = command.get(cmd) or command.get('help') try: if comcls: comobj = comcls(args) if comobj.is_relevant(): comobj._execute() else: helpcls = command.get('help') helpobj = helpcls((cmd,)) helpobj._execute() print "\n'%s %s' command not relevant here." % (cmd, ' '.join(args).strip()) else: self.do_default() except KeyboardInterrupt: print "\n" pass
def execute(self): # pdb.set_trace() name = self.options.name project = getattr(self.options, 'project', None) if project: proj_path = registry.pathfor(project) if not proj_path: print "* Project", project, "not found." return elif self.root: proj_path = self.root else: print "Must provide -p/--project option, if outside of pin project." return if name in registry._aliases: if proj_path != registry._aliases[name]: print "Alias `{alias}' already exists for other project:".format( alias=name) print registry._aliases[name] return else: print "Alias `{alias}' already set.".format(alias=name) return else: current_alias = registry._projects[proj_path].get('alias') if current_alias: prompt = "Alias `{alias}' already set, overwrite?".format( alias=current_alias) answer = option_select(['y', 'n'], prompt) if answer == 'n': print "Aborted." return _projects[proj_path]['alias'] = name _aliases[name] = proj_path registry.save_registry() print "Alias `{alias}' has been set for, {path}".format( alias=name, path=proj_path) return True
def execute(self): # pdb.set_trace() name = self.options.name project = getattr(self.options, 'project', None) if project: proj_path = registry.pathfor(project) if not proj_path: print "* Project", project, "not found." return elif self.root: proj_path = self.root else: print "Must provide -p/--project option, if outside of pin project." return if name in registry._aliases: if proj_path != registry._aliases[name]: print "Alias `{alias}' already exists for other project:".format(alias=name) print registry._aliases[name] return else: print "Alias `{alias}' already set.".format(alias=name) return else: current_alias = registry._projects[proj_path].get('alias') if current_alias: prompt = "Alias `{alias}' already set, overwrite?".format(alias=current_alias) answer = option_select(['y','n'], prompt) if answer == 'n': print "Aborted." return _projects[proj_path]['alias'] = name _aliases[name] = proj_path registry.save_registry() print "Alias `{alias}' has been set for, {path}".format(alias=name, path=proj_path) return True
def test_raw_pathfor(): assert registry.pathfor('tmp') == '/tmp'
def execute(self): self.path = registry.pathfor(self.options.project, ask=True) if self.path: return self.path else: print "There are no registered pin projects."
def compgen(): '''Do command completion for bash.''' # requires plugins to be loaded load_plugins() from pin import command, registry # get argument information nargs, args = int(sys.argv[1]), sys.argv[2:] """ The offset here is a pointer to the argument being completed. Since pin allows you to call commands against remote projects we need to adjust the completion algorithm accordingly. Each time another level of indirection is added, the offset is increased. A visual example is appropriate here. The offset starts at 1: pin ini[tab] : off=1, nargs=1 - base command pin go mypro[tab] : off=1, nargs=2 - subcommand Indirections like help cause the offset to change: pin help destr[tab] : off=2, nargs=2 - base command pin help fab deplo[tab] : off=2, nargs=3 - subcommand So now the method of the algorithm is clear. If the offset is equal to the number of arguments, we know we need to complete a base command name. If the offset is one less than the number of arguments we know to complete a subcommand. Even with a project prefix this works: pin myproj help fab deplo[tab] : off = (1 + myproj + help) = 3 nargs = 4 = subcommand! """ off = 1 proj_path = None # # # # # # # # # Project Name if args: proj_path = registry.pathfor(args[0]) # only increase the offset if we're not completing # the first argument. if nargs > 1 and proj_path is not None: # increase the offset off += 1 # change to project directory os.chdir(proj_path) # # # # # # # # # Help command if args: # complete help command if nargs == off and "help".startswith(args[-1]): return 'help' # or increase offset by 1 elif "help" in args: off += 1 # # # # # # # # # base-command if nargs == off: arg = '' # default to empty arg if len(args) == off: # set working arg to item at offset arg = args[off-1] choices = " ".join([c for c in command._commands if c.startswith(arg) and command.get(c)([]).is_relevant()]) # return the choices if there are any if choices: return choices # we want commands to complete before # project names, so if we don't return any # choices above, complete a project name now # if we're completing the first argument if nargs == 1: if proj_path is not None: return os.path.basename(proj_path) # # # # # # # # # sub-commands elif nargs == off + 1: # get our parent command com = args[off-1] # get its class comcls = command.get(com) # if it is a delegate command if hasattr(comcls, 'get_subcommands'): # get partial subcommand name if user has typed anything # or else use empty string to complete all subcom = args[off] if len(args) == off+1 else '' # get a list of the parent command's subcommands subcom_choices = comcls.get_subcommands() # if there are any subcommands if subcom_choices: # clean subcommand names (they use command-subcommand format) choices = [k.split('-')[-1] for k in subcom_choices] # return the subcommands that start with the partial subcommand name return " ".join([c for c in choices if c.startswith(subcom)]) # return nothing return ""
def compgen(): '''Do command completion for bash.''' # requires plugins to be loaded load_plugins() from pin import command, registry # get argument information nargs, args = int(sys.argv[1]), sys.argv[2:] """ The offset here is a pointer to the argument being completed. Since pin allows you to call commands against remote projects we need to adjust the completion algorithm accordingly. Each time another level of indirection is added, the offset is increased. A visual example is appropriate here. The offset starts at 1: pin ini[tab] : off=1, nargs=1 - base command pin go mypro[tab] : off=1, nargs=2 - subcommand Indirections like help cause the offset to change: pin help destr[tab] : off=2, nargs=2 - base command pin help fab deplo[tab] : off=2, nargs=3 - subcommand So now the method of the algorithm is clear. If the offset is equal to the number of arguments, we know we need to complete a base command name. If the offset is one less than the number of arguments we know to complete a subcommand. Even with a project prefix this works: pin myproj help fab deplo[tab] : off = (1 + myproj + help) = 3 nargs = 4 = subcommand! """ off = 1 proj_path = None # # # # # # # # # Project Name if args: proj_path = registry.pathfor(args[0]) # only increase the offset if we're not completing # the first argument. if nargs > 1 and proj_path is not None: # increase the offset off += 1 # change to project directory os.chdir(proj_path) # # # # # # # # # Help command if args: # complete help command if nargs == off and "help".startswith(args[-1]): return 'help' # or increase offset by 1 elif "help" in args: off += 1 # # # # # # # # # base-command if nargs == off: arg = '' # default to empty arg if len(args) == off: # set working arg to item at offset arg = args[off - 1] choices = " ".join([ c for c in command._commands if c.startswith(arg) and command.get(c)([]).is_relevant() ]) # return the choices if there are any if choices: return choices # we want commands to complete before # project names, so if we don't return any # choices above, complete a project name now # if we're completing the first argument if nargs == 1: if proj_path is not None: return os.path.basename(proj_path) # # # # # # # # # sub-commands elif nargs == off + 1: # get our parent command com = args[off - 1] # get its class comcls = command.get(com) # if it is a delegate command if hasattr(comcls, 'get_subcommands'): # get partial subcommand name if user has typed anything # or else use empty string to complete all subcom = args[off] if len(args) == off + 1 else '' # get a list of the parent command's subcommands subcom_choices = comcls.get_subcommands() # if there are any subcommands if subcom_choices: # clean subcommand names (they use command-subcommand format) choices = [k.split('-')[-1] for k in subcom_choices] # return the subcommands that start with the partial subcommand name return " ".join([c for c in choices if c.startswith(subcom)]) # return nothing return ""