Add Bash and Zsh auto-completions.
Review Request #7668 — Created Sept. 30, 2015 and submitted
Bash and Zsh are Unix shells that have auto-completion features.
The command
rbt setup-completion
allows the user to install completions for Zsh or Bash via the command. However, as Bash and Zsh are in protected directories that the user cannot write to, sudo is needed to install the scripts. A manual installation method is also available in the documentation.
Ran unit tests for rbt.
Successfully installed completion for Bash and Zsh on OSX.
Successfully installed completion for Bash and Zsh on Ubuntu.
Successfully built documentation.
Description | From | Last Updated |
---|---|---|
Can we name this rbt-completion-setup.py ? |
brennie | |
Why are we including this and tools/rbt-bash-completion? They look very similar... If they should be the same, can we just … |
brennie | |
Likewise, this looks very similar to contrib/toolsrbt-zsh-completion. |
brennie | |
This should use a with statement, e.g.: try: with open(location, 'w') as f: f.write('...') except IOError as e: print('ERR ...') … |
brennie | |
One thing we could do here would would be super neat is to auto-detect what the login shell is. The … |
brennie | |
This should be: Usage: rbt-completion-setup <shell> |
brennie | |
<option> |
brennie | |
Trailing whitespace. |
brennie | |
This should probably say bash not <option>. |
brennie | |
lowercase bash |
brennie | |
Trailing whitespace. |
brennie | |
lowercase zsh |
brennie | |
lowercase bash. |
brennie | |
lowercase zsh |
brennie | |
You should mention you can install for bash without using sudo. |
brennie | |
Why does this file begin with an underscore? |
brennie | |
``<shell>`` |
brennie | |
:cmd:`sudo` |
brennie | |
:cmd:`sudo` |
brennie | |
Instead of using this as an option, can we just use the args provided to main ? |
brennie | |
This should take shell=None as an argument so we don't have to use a flag. |
brennie | |
You should fetch the shell with: login_shell = os.environ.get(b'SHELL') if shell: login_shell = os.path.basename(shell) We are not guaranteed that 'SHELL' … |
brennie | |
Given the above comments, this can just compare against shell. |
brennie | |
We should mention the shell they give is unsupported. |
brennie | |
'Option' imported but unused |
reviewbot | |
These code paths don't ensure that /etc/bash_completion.d/ exists. |
brennie | |
I feel like we should have a map of: SHELLS = { 'bash': { 'Linux': { 'src': 'conf/rbt-bash-completion', 'dest': '/etc/bash_completion.d', … |
brennie | |
Blank line between statement and block |
brennie | |
We should probably check if no shell was found and report that case to the user, too. |
brennie | |
Blank line between these. |
brennie | |
bash or zsh. |
brennie | |
Please give this a doc-comment, e.g. #: This comment will appear in documetation SHELLS = { # ... } |
brennie | |
Another note: it would be better if all FS paths were built with os.path.join. |
brennie | |
os.path.join(self.SHELLS[shell][system]['dest'], '_rbt') |
brennie | |
here too |
brennie | |
This will print as "... directory.Refer ..." |
brennie | |
This should be os.path.join(os.sep, 'etc', 'rbt-bash-completion') Likewise for all other paths. |
brennie | |
Col: 68 W291 trailing whitespace |
reviewbot | |
Can this be two print statements? |
brennie | |
Can we print twice and sys.exit(1) |
brennie | |
Can this be two individual print statements? |
brennie | |
--shell does not appear to be a defined option in completetion_setup.py. This also appears two more times below. |
gmyers | |
For bash on CentOS 7.0 and Fedora 15 boxes I do not have an /etc/bash_completion (but there is an /etc/bash_completion.d/). … |
gmyers | |
Typo -- terminal. |
gmyers | |
Add period at end of string. |
gmyers | |
One issue I ran into is that this will fail with "bash: rbt: command not found..." if rbt is not … |
gmyers | |
Each shell contains paths to their -> Each shell contains paths to its |
CH chronicleyu | |
:ref:`rbt completion-setup <rbt-completion-setup>` |
brennie | |
space between rbt-completion-setup and <rbt-completion-setup> |
brennie | |
To install, run:: |
brennie | |
``sudo`` |
brennie | |
To install, run:: |
brennie | |
"the following command" |
brennie | |
This should begin with from __future__ import unicode_literals |
brennie | |
This should be formatted as: #: Single line summary. #: #: Multi-line description. |
brennie | |
The filename can go in dest for all the shells. Its one less call to os.path.join. |
brennie | |
The shell-specific bits should be in the SHELLS dict above. |
brennie | |
this should probably use logging.error. |
brennie | |
Instead of saying enter a specific shell we should tell the user to re-run the program with a shell as … |
brennie | |
This will go into 0.8. |
chipx86 | |
Two blank lines between sections. |
chipx86 | |
Two blank lines between sections. |
chipx86 | |
Two blank lines between sections. |
chipx86 | |
Two blank lines between sections. |
chipx86 | |
Blank line between these. |
chipx86 | |
Just need to import os. os.path comes along for the ride. |
chipx86 | |
This should offer a more detailed explanation of what this command does. Pretend these are docs. (They someday might be.) |
chipx86 | |
os.path.join is great for cross-platform code, but we know the environments these are going to be in, since we're keying … |
chipx86 | |
Missing an "Args" section. |
chipx86 | |
You're not going to be able to reliably do this and read from these files. Depending on how RBTools is … |
chipx86 | |
Missing "Args". |
chipx86 | |
These should be a logging.error. |
chipx86 | |
logging.error. |
chipx86 | |
logging.error. |
chipx86 | |
logging.error. |
chipx86 | |
How about setup-completion? Better matches setup-repo. |
chipx86 | |
"OS X" |
mike_conley | |
"OS X" |
mike_conley | |
Out of curiosity (and maybe this was answered earlier in the reviews and I missed it), why is this called … |
mike_conley | |
Typo: "currentlly" -> "currently" |
mike_conley |
-
-
-
Why are we including this and
tools/rbt-bash-completion
? They look very similar...If they should be the same, can we just package those two files and read them in this script?
-
-
This should use a
with
statement, e.g.:try: with open(location, 'w') as f: f.write('...') except IOError as e: print('ERR ...') sys.exit()
This ensures that the file handle is closed even in the case of an exception.
-
One thing we could do here would would be super neat is to auto-detect what the login shell is. The
$SHELL
environment variable corresponds to the user's login shell (e.g.,/bin/bash
). We can split this up and check if the last part of the path is one ofzsh
orbash
and do the right thing in that case.(FYI, the
os.environ
dict contains all the environment variable mappings.) -
-
-
-
Can't we autodetect
~/.{bash,zsh}{rc,_profile}
and stick it in the appropriate place?This also might be super awkward and hacky, so feel free to ignore this if its too hard.
-
-
-
-
-
-
-
Tool: Pyflakes Processed Files: setup.py Ignored Files: docs/rbtools/rbt/configuration/index.rst docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst contrib/tools/rbt-bash-completion contrib/tools/rbt-completion-setup contrib/tools/_rbt-zsh-completion Tool: PEP8 Style Checker Processed Files: setup.py Ignored Files: docs/rbtools/rbt/configuration/index.rst docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst contrib/tools/rbt-bash-completion contrib/tools/rbt-completion-setup contrib/tools/_rbt-zsh-completion
- Description:
-
Bash and Zsh are Unix shells that have auto-completion features.
~ The command
rbt-completion-setup
is available to the user after they install rbt. This allows them to install completions for Zsh or Bash via the command. However, as Bash and Zsh are in protected directories that the user cannot write to, sudo is needed to install the scripts. A manual installation method is also available in the documentation.~ The command
rbt completion-setup
allows the user to install completions for Zsh or Bash via the command. However, as Bash and Zsh are in protected directories that the user cannot write to, sudo is needed to install the scripts. A manual installation method is also available in the documentation.
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
-
-
-
-
-
-
You should fetch the shell with:
login_shell = os.environ.get(b'SHELL') if shell: login_shell = os.path.basename(shell)
We are not guaranteed that
'SHELL'
is an environment variable (e.g., the user could be on Windows) and in this case.get()
will returnNone
instead of throwing aKeyError
.Also, we should use
b'ENV_VAR_NAME'
because all of our literals are unicode literals and this will blow up on Windows.os.path.basename
will give you thebash
part of/usr/bin/bash
but it will work in a platform-independant way.Finally, you only need to fetch the login shell if
shell
isNone
, e.g.:if not shell: shell = os.environ.get(b'SHELL') # ...
-
-
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
-
-
I feel like we should have a map of:
SHELLS = { 'bash': { 'Linux': { 'src': 'conf/rbt-bash-completion', 'dest': '/etc/bash_completion.d', }, 'Darwin': { # ... } }, 'zsh': { # ... }, }
That way, if we ever support another shell (say,
fish
), we can just add to that mapping instead of adding a bunch moreif/else
blocks.This will also allow the completion setup to be more succinct and can probably be made into a generic function that just looks up stuff in the mapping.
-
-
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
Just a few more minor issues and this is good to go :)
If you haven't, you should build the docs and mention that in your testing done.
-
-
-
-
-
-
- Testing Done:
-
Ran unit tests for rbt.
Successfully installed completion for Bash and Zsh on OSX.
Successfully installed completion for Bash and Zsh on Ubuntu.
+ + Successfully built documentation.
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/index.rst docs/rbtools/rbt/configuration/completion.rst MANIFEST.in docs/rbtools/rbt/configuration/index.rst rbtools/commands/conf/rbt-bash-completion
-
<p>I know you've got <code>docs/rbtools/rbt/configuration/completion.rst</code>, but should there also be a <code>docs/rbtools/rbt/commands/completion-setup.rst</code> to provide details about the new command in a manner that is consistent with all other commands?</p>
-
--shell
does not appear to be a defined option incompletetion_setup.py
. This also appears two more times below. -
For bash on CentOS 7.0 and Fedora 15 boxes I do not have an
/etc/bash_completion
(but there is an/etc/bash_completion.d/
). This causes the existence check on line 58 to fail and exit with the "Could not locate bash completion file." error message.If I manually copy
rbt-bash-completion
to/etc/bash_completion.d/rbt
then completions do work successfully, so it seems like the required existence of/etc/bash_completion
is not viable for all linux distros. You've already confirmed thatdest
exists, so why is the check ofcompletion_file
even necessary?As an aside Ubuntu 12.04.5 LTS does have
/etc/bash_completion
, so this obviously exists for some distros. -
-
-
One issue I ran into is that this will fail with "bash: rbt: command not found..." if
rbt
is not on the path, which is probably unlikely but it could happen. I suppose grep could also be unknown or not on the path, but that seems even more unlikely.Switching to something like the snippet below (thanks to http://stackoverflow.com/a/677212, which may not necessarily be the best solution) resolves this issue:
if hash rbt 2>/dev/null; then for opt in `rbt --help | grep -o "^ [A-Za-z\-]*\S"` do if [[ $opt != "command"* && $opt != "-"* ]]; then opts+=" ${opt}" fi done fi
I'm sure there would be a similar problem with zsh.
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst
-
-
Two things.... 1) I think my prior feedback with respect to rbt being on the path may have been misguided. In testing with git completions, I though there were no error messages produced in the case where git was not on the path. But, I don't know what I was doing because I can no longer replicate that and bash will in fact complain about git and egrep not being found. Also, for what ever reason the new message you are echoing looks totally wonky on my terminal: $ rbt be on path. rbt requires Script the So, it might be best to just go back to what you had originally. Sorry about that. 2) This one I've tested more and believe it is legitimate. Tab completion does not revert back to "normal" behavior after the rbt command is identified. What I mean is if I type "rbt po<tab>po<tab>po" I'll get 'rbt post post post'. I think this will be problematic for commands like post and diff where you can explicitly include files with the -I/--include option. For example if I would like to produce a diff for the file page.cpp, I'd like to type "rbt di<tab>-I pa<tab>" and get 'rbt diff -I page.cpp', but instead I end up with 'rbt diff -I patch'.
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst
-
Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst Tool: Pyflakes Processed Files: rbtools/commands/completion_setup.py setup.py Ignored Files: rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion docs/rbtools/rbt/commands/completion.rst MANIFEST.in docs/rbtools/index.rst
-
-
-
-
-
-
-
-
-
This should offer a more detailed explanation of what this command does. Pretend these are docs. (They someday might be.)
-
os.path.join
is great for cross-platform code, but we know the environments these are going to be in, since we're keying off by platform. It's fine to just set absolute paths.For the source path, it's actually going to need to use "/" no matter what. (See below about
pkg_resources
for why.) -
-
You're not going to be able to reliably do this and read from these files. Depending on how RBTools is packaged on a given system, these might be stored in a zip file.
You'll need to instead make use of
pkg_resources.resource_string
, which will give you the contents of the file. This will take the module name ("rbtools") and a path within it. -
-
-
-
-
-
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/setup_completion.py setup.py Ignored Files: docs/rbtools/rbt/commands/setup-completion.rst rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion MANIFEST.in docs/rbtools/index.rst Tool: Pyflakes Processed Files: rbtools/commands/setup_completion.py setup.py Ignored Files: docs/rbtools/rbt/commands/setup-completion.rst rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion MANIFEST.in docs/rbtools/index.rst
- Description:
-
Bash and Zsh are Unix shells that have auto-completion features.
~ The command
rbt completion-setup
allows the user to install completions for Zsh or Bash via the command. However, as Bash and Zsh are in protected directories that the user cannot write to, sudo is needed to install the scripts. A manual installation method is also available in the documentation.~ The command
rbt setup-completion
allows the user to install completions for Zsh or Bash via the command. However, as Bash and Zsh are in protected directories that the user cannot write to, sudo is needed to install the scripts. A manual installation method is also available in the documentation.
-
Tool: PEP8 Style Checker Processed Files: rbtools/commands/setup_completion.py setup.py Ignored Files: docs/rbtools/rbt/commands/setup-completion.rst rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion MANIFEST.in docs/rbtools/index.rst Tool: Pyflakes Processed Files: rbtools/commands/setup_completion.py setup.py Ignored Files: docs/rbtools/rbt/commands/setup-completion.rst rbtools/commands/conf/rbt-bash-completion rbtools/commands/conf/_rbt-zsh-completion MANIFEST.in docs/rbtools/index.rst