Ignore:
Timestamp:
Aug 27, 2009, 11:54:48 PM (15 years ago)
Author:
joergs
Message:

initial version with working Client selection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • baculafs/trunk/baculafs.py

    r786 r787  
    55# $Id$
    66
     7import fuse
    78import logging
    8 import fuse
    99import stat
    1010import errno
     
    3333BCONSOLE_RESTORE_PROMPT='\$ '
    3434
    35 
     35# define the states bconsole can be in
    3636class BconsoleState:
    37     UNKNOWN = ''
    38     SELECT_PROMPT  = BCONSOLE_SELECT_PROMPT
    39     RESTORE_PROMPT = BCONSOLE_RESTORE_PROMPT
    40     ERROR          = "error"
    41 
    42 
     37    UNKNOWN         = ''
     38    CMD_PROMPT      = BCONSOLE_CMD_PROMPT
     39    SELECT_PROMPT   = BCONSOLE_SELECT_PROMPT
     40    RESTORE_PROMPT  = BCONSOLE_RESTORE_PROMPT
     41    ERROR           = "error"
     42
     43# direct access to the program bconsole
    4344class Bconsole:
    4445
     
    4849        self.cwd = "/"
    4950        self.state = BconsoleState.UNKNOWN
     51        self.last_items_dict = {}
    5052
    5153        self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 )
     
    5557        self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
    5658        self.bconsole.sendline( "5" )
    57         #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
    58 
    59         #self.bconsole.sendline( BACULA_CLIENT )
    60         #self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
    61         #logging.debug( "BC alive: " + str(self.bconsole.isalive()) )
    62         #logging.debug('BC init done')
     59        self.wait_for_prompt()
     60
    6361
    6462    def _normalize_dir( self, path ):
    65         # guaranty, that directory path ends with (exactly one) "/"
     63        # guarantee that directory path ends with (exactly one) "/"
    6664        return path.rstrip('/') + "/"
    67 
    68     #def _strip_select_item(self,line):
    69         #re_remove = re.compile('\s*[0-9]+:\s*')
    70         #return re_remove.sub( '', line )
    7165
    7266    def _get_select_items(self,lines):
     
    7872        for line in self._get_select_items(lines):
    7973            number,item = line.split( ":", 1 )
    80             item = item.strip()
     74            item = self._normalize_dir( item.strip() )
    8175            number = number.strip()
    8276            dictionary[item]=number
     
    9791                # SELECT_PROMPT
    9892                self.state=BconsoleState.SELECT_PROMPT
    99                 return BCONSOLE_SELECT_PROMPT
     93                lines = self.bconsole.before.splitlines()
     94                self.last_items_dict = self._get_item_dict(lines)
     95                logging.debug( str( self.last_items_dict ) )
    10096            elif index == 1:
    10197                # RESTORE_PROMPT
    10298                self.state=BconsoleState.RESTORE_PROMPT
    103                 return BCONSOLE_RESTORE_PROMPT
    10499            else:
    105100                logging.error( "unexpected result" )
     
    112107
    113108
     109
    114110    def cd(self,path):
    115111        path = self._normalize_dir( path )
     
    123119            return True
    124120
    125         state = self.wait_for_prompt()
    126         if state == BconsoleState.SELECT_PROMPT:
     121        if path == "/":
     122            return True
     123
     124        if self.state == BconsoleState.SELECT_PROMPT:
    127125            return self.cd_select( path )
    128         elif state == BconsoleState.RESTORE_PROMPT:
     126        elif self.state == BconsoleState.RESTORE_PROMPT:
    129127            return self.cd_restore( path )
    130128        # else error
     
    135133        logging.debug( "(" + path + ")" )
    136134
    137         lines = self.bconsole.before.splitlines()
    138         items = self._get_item_dict(lines)
    139         logging.debug( str( items ) )
    140 
    141 
     135        # get top level directory
    142136        directory,sep,path=path.lstrip( "/" ).partition( "/" )
     137        directory=self._normalize_dir(directory)
    143138        logging.debug( "directory: " + directory )
    144139
    145         if items[directory]:
    146             logging.debug( "directory: " + directory  + " (" + items[directory] + ")" )
    147             self.bconsole.sendline( items[directory] )
     140        if self.last_items_dict[directory]:
     141            logging.debug( "directory: " + directory  + " (" + self.last_items_dict[directory] + ")" )
     142            self.bconsole.sendline( self.last_items_dict[directory] )
     143            self.wait_for_prompt()
    148144            self.cd( path )
    149145            return True
     
    182178
    183179        if self.cd( path ):
    184             self.bconsole.sendline( 'ls' )
    185             self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
    186             lines = self.bconsole.before.splitlines()
    187             #logging.debug( str(lines) )
    188             return lines
     180            if self.state == BconsoleState.SELECT_PROMPT:
     181                return self.last_items_dict.keys()
     182            elif self.state == BconsoleState.RESTORE_PROMPT:
     183                return self.ls_restore( path )
    189184        else:
    190185            return
     186
     187
     188    def ls_restore( self, path ):
     189        self.bconsole.sendline( 'ls' )
     190        self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
     191        lines = self.bconsole.before.splitlines()
     192        #logging.debug( str(lines) )
     193        return lines
     194
    191195
    192196###############
     
    222226        return self.files[path]
    223227
     228    # TODO: only works after readdir for the directory (eg. ls)
     229    def getattr(self, path):
     230
     231        # TODO: may cause problems with filenames that ends with "/"
     232        path = path.rstrip( '/' )
     233        logging.debug( '"' + path + '"' )
     234
     235        st = fuse.Stat()
     236
     237        if not (path in self.files):
     238            self._getattr(path)
     239
     240        if not (path in self.files):
     241            return -errno.ENOENT
     242
     243        file = self.files[path]
     244
     245        if file['type'] == self.TYPE_FILE:
     246            st.st_mode = stat.S_IFREG | 0444
     247            st.st_nlink = 1
     248            st.st_size  = 0
     249            return st
     250        elif file['type'] == self.TYPE_DIR:
     251            st.st_mode = stat.S_IFDIR | 0755
     252            if 'dirs' in file:
     253                st.st_nlink = len(file['dirs'])
     254            else:
     255                st.st_nlink = 2
     256            return st
     257
     258        # TODO: check for existens
     259        return -errno.ENOENT
    224260
    225261
     
    264300            logging.exception(e)
    265301            logging.error( "no access to path " + path )
    266             self.files[path] = { 'type': TYPE_NONE }
     302            self.files[path] = { 'type': self.TYPE_NONE }
    267303
    268304        logging.debug( '"' + path + '"=' + str( self.files[path] )  )
    269305        return self.files[path]
    270 
    271 
    272 
    273     # TODO: only works after readdir for the directory (eg. ls)
    274     def getattr(self, path):
    275 
    276         # TODO: may cause problems with filenames that ends with "/"
    277         path = path.rstrip( '/' )
    278         logging.debug( '"' + path + '"' )
    279 
    280         st = fuse.Stat()
    281 
    282         if not (path in self.files):
    283             self._getattr(path)
    284 
    285         if not (path in self.files):
    286             return -errno.ENOENT
    287 
    288         file = self.files[path]
    289 
    290         if file['type'] == self.TYPE_FILE:
    291             st.st_mode = stat.S_IFREG | 0444
    292             st.st_nlink = 1
    293             st.st_size  = 0
    294             return st
    295         elif file['type'] == self.TYPE_DIR:
    296             st.st_mode = stat.S_IFDIR | 0755
    297             if 'dirs' in file:
    298                 st.st_nlink = len(file['dirs'])
    299             else:
    300                 st.st_nlink = 2
    301             return st
    302 
    303         # TODO: check for existens
    304         return -errno.ENOENT
    305        
    306306
    307307
Note: See TracChangeset for help on using the changeset viewer.