pshell 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import readline
  5. import subprocess
  6. from plumbum import local
  7. readline.parse_and_bind('tab:complete')
  8. def vi(*args):
  9. subprocess.call('vi')
  10. def ll(*args):
  11. print local['ls']['-la']()
  12. # is a builtin
  13. def cd(*args):
  14. # TODO: no param, go to home
  15. # param is - : go back
  16. print args
  17. if len(args[0]) == 0:
  18. args = '/home/woelper'
  19. else:
  20. args = args[0]
  21. print 'cd to', args[0]
  22. local.cwd.chdir(args[0])
  23. def exec_if_builtin(strlist):
  24. print 'STR', strlist
  25. command = strlist[0]
  26. parameters = strlist[1:]
  27. possibles = globals().copy()
  28. possibles.update(locals())
  29. method = possibles.get(command)
  30. if method:
  31. method(parameters)
  32. return True
  33. print 'Did not detect a function by that name'
  34. def as_plumbum(strlist):
  35. print 'Running cmd through plumbum'
  36. # TODO: until pipe or special char
  37. # split the string
  38. #print string
  39. command = strlist[0]
  40. parameters = strlist[1:]
  41. #print command, parameters
  42. try:
  43. print local[command][parameters]()
  44. except:
  45. print 'Could not execute command'
  46. def as_python(string):
  47. print 'running native python code'
  48. exec(string)
  49. try:
  50. exec(string)
  51. except:
  52. print 'error in command'
  53. def get_exec_method(string):
  54. # native : assignments
  55. # '=' is in first command or equals second
  56. exec(input_string)
  57. """
  58. strlist = string.split()
  59. if '=' in strlist[0]:
  60. exec(string)
  61. return
  62. else:
  63. if len(strlist) > 1:
  64. if '=' in strlist[1]:
  65. exec(string)
  66. return
  67. if not exec_if_builtin(strlist):
  68. exec(string)
  69. try:
  70. exec(string)
  71. except:
  72. print 'probably NOT native code.'
  73. """
  74. def is_assignment(strlist):
  75. if '=' in strlist[0]:
  76. return True
  77. if len(strlist) > 1:
  78. if '=' in strlist[1]:
  79. return True
  80. # line for line
  81. while True:
  82. # we need to exec here so definitions stay global
  83. input_string = raw_input(local.cwd + ' $ ')
  84. #if get_exec_method(input_string)
  85. strlist = input_string.split()
  86. if is_assignment(strlist):
  87. exec(input_string)
  88. else:
  89. if not exec_if_builtin(strlist):
  90. try:
  91. exec(input_string)
  92. except:
  93. print 'probably NOT native code.'
  94. as_plumbum(strlist)