Changeset 784 for baculafs/trunk


Ignore:
Timestamp:
Aug 26, 2009, 12:30:54 AM (15 years ago)
Author:
joergs
Message:

initial support for different clients

File:
1 edited

Legend:

Unmodified
Added
Removed
  • baculafs/trunk/baculafs.py

    r783 r784  
    1212import pexpect
    1313import sys
     14import re
    1415
    1516fuse.fuse_python_api = (0, 2)
     
    3233
    3334BCONSOLE_CMD_PROMPT='\*'
    34 BCONSOLE_SELECT_PROMPT='Select item:.*'
     35BCONSOLE_SELECT_PROMPT='Select .*:.*'
    3536BCONSOLE_RESTORE_PROMPT='\$ '
    3637
     
    4041    def __init__(self, client=BACULA_CLIENT):
    4142        #logging.debug('BC init')
     43
     44        self.cwd = "/"
     45
    4246        self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 )
    4347        self.bconsole.setecho( False )
    4448        self.bconsole.expect( BCONSOLE_CMD_PROMPT )
    4549        self.bconsole.sendline( 'restore' )
    46         #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
     50        self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
    4751        self.bconsole.sendline( "5" )
    4852        #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 )
    5158        #logging.debug( "BC alive: " + str(self.bconsole.isalive()) )
    5259        #logging.debug('BC init done')
    5360
    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 )
    5685        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)
    57133
    58134        self.bconsole.sendline( 'cd ' + path )
     
    165241            logging.error( "no access to path " + path )
    166242            self.files[path] = { 'type': TYPE_NONE }
     243
    167244        logging.debug( '"' + path + '"=' + str( self.files[path] )  )
    168245        return self.files[path]
Note: See TracChangeset for help on using the changeset viewer.