PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/commandline.py

https://github.com/fmarani/localXchange
Python | 344 lines | 327 code | 13 blank | 4 comment | 1 complexity | 0b0d1e82dcee67d5ce1539cfb9dd2a17 MD5 | raw file
  1. #!/usr/bin/python
  2. import BroadcastGrouping
  3. import XMLRPCLocalInterface
  4. import XMLRPCRemoteInterface
  5. import common
  6. from cmd import Cmd
  7. from threading import Thread
  8. from time import sleep
  9. import sys
  10. import xmlrpclib
  11. import logging
  12. import StringIO
  13. import ConfigParser
  14. import os.path
  15. import string
  16. class Client(Cmd):
  17. """
  18. A simple text-based interface to the NodeLocalFunctions class.
  19. """
  20. prompt = '> '
  21. intro = "LocalXchange V."+common.VERSION
  22. def __init__(self):
  23. Cmd.__init__(self)
  24. systems = common.start_system()
  25. self.log = systems['log']
  26. self.nodemanager = systems['nodemanager']
  27. self.remotefunc = systems['remotefunc']
  28. self.clientfunc = systems['clientfunc']
  29. def do_log(self,arg):
  30. print self.log.getvalue()
  31. def help_log(self):
  32. print "print the log"
  33. def do_getnodesinfo(self,arg):
  34. for nodeinfo in self.clientfunc.getnodesinfo():
  35. print nodeinfo[0], "-->", nodeinfo[1]
  36. def help_getnodesinfo(self):
  37. print """
  38. Get nodes information for each connected node
  39. Usage: getnodesinfo
  40. """
  41. def do_node_shares(self,arg):
  42. try:
  43. args = self.parseargs(arg)
  44. node = args[0]
  45. except IndexError:
  46. print "wrong parameters."
  47. self.help_node_shares()
  48. else:
  49. try:
  50. print node, " shares:"
  51. for dir in self.clientfunc.node_shares(node):
  52. print "-> ",dir
  53. except XMLRPCLocalInterface.HostNotFoundFault:
  54. print "The node doesn't exist"
  55. def help_node_shares(self):
  56. print """
  57. List shares on a certain node
  58. Usage: node_shares <nodename>
  59. """
  60. def complete_node_shares(self,text, line, begidx, endidx):
  61. args = self.parseargs(line)
  62. is_typing_arg = text != ""
  63. at_arg = len(args)
  64. if not is_typing_arg:
  65. at_arg+=1
  66. if (at_arg == 2):
  67. return [x for x in self.nodemanager if x.startswith(text)]
  68. return []
  69. def do_node_share_list(self,arg):
  70. try:
  71. args = self.parseargs(arg)
  72. node = args[0]
  73. share = args[1]
  74. if len(args) == 3:
  75. dir = args[2]
  76. else:
  77. dir = ""
  78. except IndexError:
  79. print "wrong parameters."
  80. self.help_node_share_list()
  81. else:
  82. try:
  83. print node, " share ",share, " dir ",dir," has:"
  84. for f in self.clientfunc.node_share_list(node,share,dir):
  85. print "-> ",f[0].ljust(32)," (",f[1],")"
  86. except XMLRPCLocalInterface.HostNotFoundFault:
  87. print "The node doesn't exist"
  88. except xmlrpclib.Fault, fault:
  89. if fault.faultCode == XMLRPCRemoteInterface.ACCESS_DENIED:
  90. print "Access denied: "+fault.faultString
  91. else:
  92. print "Remote error: "+fault.faultString
  93. def help_node_share_list(self):
  94. print """
  95. List files in a node share
  96. Usage: node_share_list <nodename> <share> [dir]
  97. """
  98. def complete_node_share_list(self,text, line, begidx, endidx):
  99. args = self.parseargs(line)
  100. is_typing_arg = text != ""
  101. at_arg = len(args)
  102. if not is_typing_arg:
  103. at_arg+=1
  104. if (at_arg == 2):
  105. return [x for x in self.nodemanager if x.startswith(text)]
  106. if (at_arg == 3):
  107. args = self.parseargs(line)
  108. node = args[1]
  109. try:
  110. shares = self.clientfunc.node_shares(node)
  111. return [x for x in shares if x.startswith(text)]
  112. except:
  113. return []
  114. #===============================================================================
  115. # if (at_arg == 4):
  116. # args = self.parseargs(line)
  117. # node = args[1]
  118. # share = args[2]
  119. # if is_typing_arg:
  120. # dir = args[3]
  121. # else:
  122. # dir = ""
  123. # try:
  124. # parent = string.join(dir.split("/")[0:-1],"/") # PARENT DIR
  125. # ls = self.clientfunc.node_share_list(node,share,parent)
  126. # #print ls
  127. # return [x[0]+"/" for x in ls if (x[1]['type'] == "D" and x[0].startswith(dir))]
  128. # except:
  129. # return []
  130. #===============================================================================
  131. return []
  132. def do_search(self,arg):
  133. try:
  134. arg = self.parseargs(arg)
  135. kw = {}
  136. for i in range(0,len(arg),2):
  137. kw[arg[i]] = arg[i+1]
  138. except IndexError:
  139. print "wrong parameters"
  140. self.help_search()
  141. else:
  142. print "Search results of: ",kw
  143. for noderesult in self.clientfunc.search(kw):
  144. print noderesult[0], " results:"
  145. for shareresult in noderesult[1]:
  146. print "IN SHARE: ",shareresult[0]
  147. for f in shareresult[1]:
  148. print "-> ",f[0].ljust(32)," subdir:",f[1].ljust(30)," (",f[2],")"
  149. def help_search(self):
  150. print """
  151. Search files over network, case insensitive
  152. Usage: search <attribute value>+
  153. where attribute can be: name, startdate
  154. Example:
  155. search name The_red_fox
  156. search startdate 1929934788
  157. search name fox startdate 1928349049
  158. """
  159. def complete_search(self,text, line, begidx, endidx):
  160. args = self.parseargs(line)
  161. is_typing_arg = text != ""
  162. at_arg = len(args)
  163. if not is_typing_arg:
  164. at_arg+=1
  165. if (at_arg % 2 == 0):
  166. return [x for x in ["name","startdate"] if x.startswith(text)]
  167. return []
  168. def do_node_search(self,arg):
  169. try:
  170. arg = self.parseargs(arg)
  171. node = arg[0]
  172. del arg[0]
  173. kw = {}
  174. for i in range(0,len(arg),2):
  175. kw[arg[i]] = arg[i+1]
  176. except IndexError:
  177. print "wrong parameters"
  178. self.help_node_search()
  179. else:
  180. try:
  181. print "Node search results of: ",kw
  182. noderesult = self.clientfunc.node_search(node,kw)
  183. print noderesult[0], " results:"
  184. for shareresult in noderesult[1]:
  185. print "IN SHARE: ",shareresult[0]
  186. for f in shareresult[1]:
  187. print "-> ",f[0].ljust(32)," subdir:",f[1].ljust(30)," (",f[2],")"
  188. except XMLRPCLocalInterface.HostNotFoundFault:
  189. print "The node doesn't exist"
  190. def help_node_search(self):
  191. print """
  192. Search files on a node, case insensitive
  193. Usage: search <attribute value>+
  194. See help search for more information
  195. """
  196. def complete_node_search(self,text, line, begidx, endidx):
  197. args = self.parseargs(line)
  198. is_typing_arg = text != ""
  199. at_arg = len(args)
  200. if not is_typing_arg:
  201. at_arg+=1
  202. if (at_arg == 2):
  203. return [x for x in self.nodemanager if x.startswith(text)]
  204. if (at_arg % 2 == 1):
  205. return [x for x in ["name","startdate"] if x.startswith(text)]
  206. return []
  207. def do_fetch(self,arg):
  208. try:
  209. node, share, dir, filename = self.parseargs(arg)
  210. except ValueError:
  211. print "wrong parameters"
  212. self.help_fetch()
  213. else:
  214. try:
  215. self.clientfunc.fetch(node, share, dir, filename)
  216. except XMLRPCLocalInterface.HostNotFoundFault:
  217. print "the node doesn't exist"
  218. except xmlrpclib.Fault, fault:
  219. if fault.faultCode == XMLRPCRemoteInterface.ACCESS_DENIED:
  220. print "Access denied"
  221. else:
  222. if fault.faultCode == XMLRPCRemoteInterface.FILE_NOT_FOUND:
  223. print "File ",filename," not found"
  224. else:
  225. print "Remote error"
  226. def help_fetch(self):
  227. print """
  228. Fetch a file in a share on a node
  229. Usage: fetch <node> <share> <dir> <filename>
  230. """
  231. def complete_fetch(self,text, line, begidx, endidx):
  232. args = self.parseargs(line)
  233. is_typing_arg = text != ""
  234. at_arg = len(args)
  235. if not is_typing_arg:
  236. at_arg+=1
  237. if (at_arg == 2):
  238. return [x for x in self.nodemanager if x.startswith(text)]
  239. if (at_arg == 3):
  240. args = self.parseargs(line)
  241. node = args[1]
  242. try:
  243. shares = self.clientfunc.node_shares(node)
  244. return [x for x in shares if x.startswith(text)]
  245. except:
  246. return []
  247. return []
  248. def parseargs(self,args_string):
  249. args = []
  250. buffer = ""
  251. in_brackets = False
  252. for i in args_string:
  253. if (i == "\""):
  254. in_brackets = not in_brackets
  255. continue
  256. if (i not in string.whitespace or in_brackets):
  257. buffer += i
  258. continue
  259. if (i in string.whitespace and len(buffer) > 0):
  260. args.append(buffer)
  261. buffer = ""
  262. continue
  263. if (len(buffer) > 0):
  264. args.append(buffer)
  265. return args
  266. def do_transferstatus(self,arg):
  267. for i in self.clientfunc.TCPtransfers:
  268. print i.filename, "-->", i.state
  269. def help_transferstatus(self):
  270. print """
  271. See status of current session transfers
  272. Usage: transferstatus
  273. """
  274. def do_exit(self, arg):
  275. "Exit the program."
  276. print "Exit..."
  277. self.nodemanager.close()
  278. self.clientfunc.closeremote()
  279. while (self.nodemanager.state != "CLOSED" and self.remotefunc.state != "CLOSED"):
  280. pass
  281. sys.exit()
  282. def help_exit(self):
  283. print "Exit the program"
  284. do_EOF = do_exit # End-Of-File is synonymous with 'exit'
  285. def main():
  286. client = Client()
  287. try:
  288. client.cmdloop()
  289. except:
  290. sys.exit()
  291. if __name__ == '__main__': main()