|
| 1 | +#+TITLE: 让python脚本支持bash补全 |
| 2 | +#+URL: http://blog.endpoint.com/2016/04/adding-bash-completion-to-python-script.html |
| 3 | +#+AUTHOR: lujun9972 |
| 4 | +#+CATEGORY: Python Common |
| 5 | +#+DATE: [2016-06-09 四 20:18] |
| 6 | +#+OPTIONS: ^:{} |
| 7 | + |
| 8 | +Bash有一项很有用的功能,就是当你在终端输入一个命令后,按两下 =<TAB>= 就会列出该命令支持的所有可能参数. |
| 9 | + |
| 10 | +接下来,我会展示如何为一段python脚本也赋予这项能力. 而且, 该解决方案要足够的通用. 使得他能自动适应对python脚本选项或配置文件所做出的改动. |
| 11 | + |
| 12 | +该脚本接受两种类型的参数. 一类是以'--'开头的标志,一类是从chef脚本中取出来的主机名称. |
| 13 | + |
| 14 | +假设该脚本名为 =show.py= - 它的作用是显示某主机的信息. 它的使用方法为: |
| 15 | + |
| 16 | +#+BEGIN_SRC sh |
| 17 | + show.py szymon |
| 18 | +#+END_SRC |
| 19 | + |
| 20 | +其中 =szymon= 为我的LG的名字, 是从我的chef节点定义文件中抽取出来的. |
| 21 | + |
| 22 | +该脚本还支持许多的其他参数,例如: |
| 23 | +#+BEGIN_SRC sh |
| 24 | + show.py --cpu --memory --format=json |
| 25 | +#+END_SRC |
| 26 | + |
| 27 | +脚本支持的参数有两种格式: 一种是简单的字符串,还有一种是以--开头的参数 |
| 28 | + |
| 29 | +为了让该python脚本支持bash补全,首先需要先写一个简单的python脚本来列出所有的chef node名称. |
| 30 | +#+BEGIN_SRC python |
| 31 | + #!/usr/bin/env python |
| 32 | + |
| 33 | + from sys import argv |
| 34 | + import os |
| 35 | + import json |
| 36 | + |
| 37 | + if __name__ == "__main__": |
| 38 | + pattern = "" |
| 39 | + if len(argv) == 2: |
| 40 | + pattern = argv[1] |
| 41 | + |
| 42 | + chef_dir = os.environ.get('LG_CHEF_DIR', None) |
| 43 | + if not chef_dir: |
| 44 | + exit(0) |
| 45 | + node_dirs = [os.path.join(chef_dir, "nodes"), |
| 46 | + os.path.join(chef_dir, "dev_nodes")] |
| 47 | + node_names = [] |
| 48 | + |
| 49 | + for nodes_dir in node_dirs: |
| 50 | + for root, dirs, files in os.walk(nodes_dir): |
| 51 | + for f in files: |
| 52 | + try: |
| 53 | + with open(os.path.join(root, f), 'r') as nf: |
| 54 | + data = json.load(nf) |
| 55 | + node_names.append(data['normal']['liquid_galaxy']['support_name']) |
| 56 | + except: |
| 57 | + pass |
| 58 | + |
| 59 | + for name in node_names: |
| 60 | + print name |
| 61 | +#+END_SRC |
| 62 | + |
| 63 | +然后还需要一种方法来获取python脚本支持的所有选项. 我们可以通过下面这行代码实现: |
| 64 | +#+BEGIN_SRC sh |
| 65 | + $LG_CHEF_DIR/repo_scripts/show.py --help | grep ' --' | awk {'print $1'} |
| 66 | +#+END_SRC |
| 67 | + |
| 68 | +最后通过一个简单的bash脚本来整合上面两个脚本. |
| 69 | +#+BEGIN_SRC sh |
| 70 | + _lg_ssh() |
| 71 | + { |
| 72 | + local cur prev opts node_names |
| 73 | + COMPREPLY=() |
| 74 | + cur="${COMP_WORDS[COMP_CWORD]}" |
| 75 | + prev="${COMP_WORDS[COMP_CWORD-1]}" |
| 76 | + opts=`$LG_CHEF_DIR/repo_scripts/show.py --help | grep ' --' | awk {'print $1'}` |
| 77 | + node_names=`python $LG_CHEF_DIR/repo_scripts/node_names.py` |
| 78 | + |
| 79 | + if [[ ${cur} == -* ]] ; then |
| 80 | + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) |
| 81 | + return 0 |
| 82 | + fi |
| 83 | + |
| 84 | + COMPREPLY=( $(compgen -W "${node_names}" -- ${cur}) ) |
| 85 | + } |
| 86 | + |
| 87 | + complete -F _lg_ssh show.py |
| 88 | +#+END_SRC |
| 89 | + |
| 90 | +最后,我们只需要在当前bash session中使用source命令加载该文件即可. 因此,我把下面这行添加到 =~/.bashrc= 中. |
| 91 | +#+BEGIN_SRC sh |
| 92 | + source $LG_CHEF_DIR/repo_scripts/show.bash-completion |
| 93 | +#+END_SRC |
| 94 | + |
| 95 | +至此,只要在终端中按两下 =<TAB>= 就会列出所有的补全选项了. |
| 96 | +#+BEGIN_EXAMPLE |
| 97 | + $ show.py <tab><tab> |
| 98 | + Display all 42 possibilities? (y or n) |
| 99 | + ... and here go all 42 node names ... |
| 100 | + </tab></tab> |
| 101 | + |
| 102 | + $ show.py h<tab><tab> |
| 103 | + ... and here go all node names beginning with 'h' ... |
| 104 | + </tab></tab> |
| 105 | + |
| 106 | + $ show.py --<tab><tab> |
| 107 | + .. and here go all the options beginning with -- ... |
| 108 | + </tab></tab> |
| 109 | +#+END_EXAMPLE |
| 110 | + |
| 111 | +该实现最大的好处在于,任何对python script的修改或chef node名称的修改,都会自动反映出来. |
0 commit comments