Changeset 784
- Timestamp:
- Aug 26, 2009, 12:30:54 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
baculafs/trunk/baculafs.py
r783 r784 12 12 import pexpect 13 13 import sys 14 import re 14 15 15 16 fuse.fuse_python_api = (0, 2) … … 32 33 33 34 BCONSOLE_CMD_PROMPT='\*' 34 BCONSOLE_SELECT_PROMPT='Select item:.*'35 BCONSOLE_SELECT_PROMPT='Select .*:.*' 35 36 BCONSOLE_RESTORE_PROMPT='\$ ' 36 37 … … 40 41 def __init__(self, client=BACULA_CLIENT): 41 42 #logging.debug('BC init') 43 44 self.cwd = "/" 45 42 46 self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 ) 43 47 self.bconsole.setecho( False ) 44 48 self.bconsole.expect( BCONSOLE_CMD_PROMPT ) 45 49 self.bconsole.sendline( 'restore' ) 46 #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )50 self.bconsole.expect( BCONSOLE_SELECT_PROMPT ) 47 51 self.bconsole.sendline( "5" ) 48 52 #self.bconsole.expect( BCONSOLE_SELECT_PROMPT ) 49 self.bconsole.sendline( BACULA_CLIENT ) 50 self.bconsole.expect( BCONSOLE_RESTORE_PROMPT ) 53 54 55 56 #self.bconsole.sendline( BACULA_CLIENT ) 57 #self.bconsole.expect( BCONSOLE_RESTORE_PROMPT ) 51 58 #logging.debug( "BC alive: " + str(self.bconsole.isalive()) ) 52 59 #logging.debug('BC init done') 53 60 54 def cd(self, path): 55 path = path + "/" 61 def _normalize_dir( self, path ): 62 # guaranty, that directory path ends with (exactly one) "/" 63 return path.rstrip('/') + "/" 64 65 #def _strip_select_item(self,line): 66 #re_remove = re.compile('\s*[0-9]+:\s*') 67 #return re_remove.sub( '', line ) 68 69 def _get_select_items(self,lines): 70 re_select_items = re.compile('\s*[0-9]+:\s*.+') 71 return filter( re_select_items.match, lines ) 72 73 def _get_item_dict( self,lines ): 74 dictionary = {} 75 for line in self._get_select_items(lines): 76 number,item = line.split( ":", 1 ) 77 item = item.strip() 78 number = number.strip() 79 dictionary[item]=number 80 return dictionary 81 82 83 def cd(self,path): 84 path = self._normalize_dir( path ) 56 85 logging.debug( "(" + path + ")" ) 86 87 if path = "": 88 return True 89 90 try: 91 index = self.bconsole.expect( [ BCONSOLE_SELECT_PROMPT, BCONSOLE_RESTORE_PROMPT ] ) 92 if index == 0: 93 # SELECT_PROMPT 94 return self.cd_select( path ) 95 elif index == 1: 96 # RESTORE_PROMPT 97 return self.cd_restore( path ) 98 except pexpect.EOF: 99 logging.error( "EOF bconsole" ) 100 except pexpect.TIMEOUT: 101 logging.error( "TIMEOUT bconsole" ) 102 103 104 def cd_select(self, path): 105 logging.debug( "(" + path + ")" ) 106 107 lines = self.bconsole.before.splitlines() 108 items = self._get_item_dict(lines) 109 logging.debug( str( items ) ) 110 111 112 directory,sep,path=path.lstrip( "/" ).partition( "/" ) 113 logging.debug( "directory: " + directory ) 114 115 if items[directory]: 116 logging.debug( "directory: " + directory + " (" + items[directory] + ")" ) 117 self.bconsole.sendline( items[directory] ) 118 self.cd( path ) 119 return True 120 121 return False 122 123 124 125 def cd_restore(self, path): 126 #path = path + "/" 127 logging.debug( "(" + path + ")" ) 128 129 # TODO: 130 # parse for BCONSOLE_SELECT_PROMPT or BCONSOLE_RESTORE_PROMPT 131 # BCONSOLE_SELECT_PROMPT: take first part of path and try to match. send number. iterate 132 # BCONSOLE_RESTORE_PROMPT: cd to directory (as before) 57 133 58 134 self.bconsole.sendline( 'cd ' + path ) … … 165 241 logging.error( "no access to path " + path ) 166 242 self.files[path] = { 'type': TYPE_NONE } 243 167 244 logging.debug( '"' + path + '"=' + str( self.files[path] ) ) 168 245 return self.files[path]
Note:
See TracChangeset
for help on using the changeset viewer.