You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mge 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #! /usr/bin/env python3
  2. import argparse
  3. import ntpath
  4. import os
  5. import pathlib
  6. import platform
  7. import sys
  8. from glob import glob
  9. import megengine
  10. def add_completion(tools_path, write_path) :
  11. script_files = [os.path.basename(p) for p in tools_path.glob("*.py")]
  12. param_script = []
  13. for script_name in script_files:
  14. temp = ntpath.splitext(script_name)[0]
  15. if temp=="__init__" or temp=="mge":
  16. continue
  17. param_script.append(temp)
  18. completion_template_head = '''
  19. _mge(){
  20. local cur prev script_name
  21. COMPREPLY=()
  22. cur="${COMP_WORDS[COMP_CWORD]}"
  23. prev="${COMP_WORDS[COMP_CWORD-1]}"
  24. case "${prev}" in
  25. mge)
  26. words="compare_binary_iodump draw_graph load_network_and_run network_visualize profile_analyze"
  27. COMPREPLY=( $(compgen -W "${words}" -- ${cur} ) )
  28. return
  29. ;;
  30. '''
  31. completion_template_body = ""
  32. completion_template_tail2=""
  33. for param in param_script:
  34. words_command = 'grep -Eso \'"-[[:alnum:]-]*[_[:alnum:]]*"\' {}/{}.py | xargs'.format(str(tools_path),param)
  35. words = os.popen(words_command).read().strip()
  36. completion_template_body+= '''
  37. {})
  38. words="{}"
  39. COMPREPLY=($(compgen -W "$words" -- $cur))
  40. return
  41. ;;
  42. '''.format(param ,words)
  43. completion_template_tail2+= '''
  44. {})
  45. words="{}"
  46. COMPREPLY=($(compgen -W "$words" -- $cur))
  47. return
  48. ;;
  49. '''.format(param ,words)
  50. completion_template_tail1='''
  51. esac
  52. case $cur in
  53. -*)
  54. script_name="${COMP_WORDS[1]}"
  55. case $script_name in
  56. '''
  57. completion_template_tail3='''
  58. esac
  59. ;;
  60. esac
  61. return
  62. }
  63. complete -o bashdefault -F _mge mge
  64. '''
  65. completion_template = completion_template_head + completion_template_body + completion_template_tail1 + completion_template_tail2 + completion_template_tail3
  66. wp = pathlib.Path(write_path)
  67. wp.parent.mkdir(parents=True, exist_ok = True)
  68. with open(write_path, 'w+') as f:
  69. f.write(completion_template)
  70. def init(path) :
  71. engine_path = pathlib.Path(megengine.__path__[0])
  72. add_completion(engine_path/'tools',path)
  73. def main(argv) :
  74. if len(argv) == 0:
  75. return
  76. script_file = argv[0]
  77. args = " ".join(argv[1:])
  78. call_command = "python3 -m megengine.tools.{} {}".format(script_file,args)
  79. os.system(call_command)
  80. if __name__ == "__main__":
  81. usage = 'usage: mge [-h|--help|--h] [--init] [script_name --script_param xxx]'
  82. if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] =='--help' or sys.argv[1] =='--h':
  83. print(usage)
  84. sys.exit(0)
  85. if sys.argv[1] == '--init':
  86. sysstr = platform.system()
  87. if(sysstr == "Windows"):
  88. print("WARNING: windows doesn't support hinting")
  89. else:
  90. path = "{}/.local/share/bash-completion/completions/mge".format(os.environ.get("HOME"))
  91. init(path)
  92. shell = os.environ.get("SHELL")
  93. if shell.find('zsh') != -1:
  94. print("if you don't have zsh completion init, please excute command: 'autoload -Uz compinit && compinit'")
  95. print("Guess you are using zsh, please add 'source %s' to your ~/.zshrc" % path)
  96. elif shell.find('bash') != -1:
  97. print("Guess you are using bash, please relogin or do 'source %s'" % path)
  98. else:
  99. print("Current {} doesn't support hinting shell completion".format(shell))
  100. sys.exit(0)
  101. else:
  102. main(sys.argv[1:])