Ficus
  • Overview
  • Repository
  • Tickets
  • Statistics
  • Projects

Repository

Logging and error handling imporvements

Parent commits : 501f30c6213779f98b27ad3f7830a512262e466e,
Children commits : dd06a90d232e375730f3ff54720ec4a5c1ade819,

By Laurent Defert on 2012-09-27 15:11:18
Logging and error handling imporvements

Browse content
Difference with parent commit 501f30c6213779f98b27ad3f7830a512262e466e
Files modified:
ficus/client.py
--- 
+++ 
@@ -1,7 +1,9 @@
 import sys
 import logging
+import errno
 
 from ficus.connection import Connection
+from ficus.fs import FSException
 
 def _get_typed_args(args, arg_types):
     typed_args = []
@@ -15,6 +17,7 @@
     return typed_args
 
 def cmd_arg_types(*arg_types):
+    """Decorator to cast network messages paramaters to the appropriate type"""
     def build_args(func, arg_types):
         # Take arguments skiping the first argument (self).
         args = func.func_code.co_varnames[1:func.func_code.co_argcount]
@@ -28,6 +31,7 @@
     return lambda func: build_args(func, arg_types)
 
 def bin_cmd_arg_types(*arg_types):
+    """Decorator to cast network messages paramaters to the appropriate type"""
     def build_args(func, arg_types):
         # Take arguments skiping the first argument (self).
         args = func.func_code.co_varnames[1:func.func_code.co_argcount]
@@ -53,6 +57,7 @@
         self.answer_msg_id = None
 
     def _get_msg_id(self):
+        """Increment and return the current message id"""
         if self.answer_msg_id is not None:
             return self.answer_msg_id
 
@@ -62,17 +67,21 @@
         return self.msg_id
 
     def add_callback(self, msg_id, callback):
+        """Add a callback that will be called when we receive a response to the
+        the message that is currently being sent"""
         if callback is not None:
             logging.debug('Adding callback %s to message %i' % (callback, msg_id))
             self.callbacks[msg_id] = callback
 
     def send(self, cmd, callback=None):
+        """Send a message to this client"""
         msg_id = self._get_msg_id()
         buf = '%i %s\n' % (msg_id, cmd)
         Connection.send(self, buf)
         self.add_callback(msg_id, callback)
 
     def send_bin(self, cmd, data, callback=None):
+        """Send a message with emebeded binary data"""
         msg_id = self._get_msg_id()
         buf = 'binary %i %i %s\n%s' % (len(data), self._get_msg_id(), cmd, data)
         logging.debug('sending %s' % cmd)
@@ -80,6 +89,7 @@
         self.add_callback(msg_id, callback)
 
     def handle_cmd(self, cmd, data=None):
+        """Handle a message sent by this client"""
         self.answer_msg_id, cmd = cmd.split(' ', 1)
         self.answer_msg_id = int(self.answer_msg_id)
 
@@ -91,18 +101,21 @@
         if hasattr(self, 'cmd_' + cmd):
             func = getattr(self, 'cmd_' + cmd)
             if callable(func):
-                logging.error("Handling command no %i from %s: %s %s", self.answer_msg_id, self.client_id, cmd, args)
-                if data is not None:
-                    func(args, data)
-                else:
-                    func(args)
+                logging.debug("Handling command no %i from %s: %s %s", self.answer_msg_id, self.client_id, cmd, args)
+                try:
+                    if data is not None:
+                        func(args, data)
+                    else:
+                        func(args)
+                except FSException, e:
+                    logging.debug('Got an FSException, returning error : %i %s', e.errno, errno.errorcode[e.errno])
+                    self.send('errno %i' % e.errno)
+        else:
+            logging.error('Invalid network message received')
 
-        print "callbacks:", self.callbacks
         if self.answer_msg_id in self.callbacks:
             callback = self.callbacks.pop(self.answer_msg_id)
-            logging.debug('Triggering callback %s for msg no %i' % (callback, self.answer_msg_id))
+            logging.debug('Triggering callback %s for msg no %i', callback, self.answer_msg_id)
             callback()
 
         self.answer_msg_id = None
-        if not hasattr(self, 'cmd_' + cmd):
-            logging.error("Unknown command: %s %s", cmd, args)

ficus/conf.py
--- 
+++ 
@@ -1,5 +1,6 @@
 import optparse
 import ConfigParser
+import logging
 
 from ficus.constants import VERSION, DEFAULT_CONFFILE
 
@@ -12,7 +13,7 @@
             optparse.Option("-f", "--conf",       dest="conf_file", help="Path to configuration file", metavar="FILE", default=DEFAULT_CONFFILE),
             optparse.Option("-b", "--bootstrap",  dest="bootstrap", help="Bootstrap a new network", action="store_true", default=False),])
         (parsed_options, args) = parser.parse_args()
-        print "Reading:", parsed_options.conf_file
+        logging.info("Loading configuration file: %s", parsed_options.conf_file)
         fp = open(parsed_options.conf_file)
         self.readfp(fp)
 

ficus/ficus_client.py
--- 
+++ 
@@ -26,11 +26,11 @@
             owners = self.server.get_root_owners()
         else:
             owners = self.fs.get_owners(path)
-        print "owners of %s: %s" % (path, repr(owners))
+        logging.debug("owners of %s: %s", path, repr(owners))
         return owners
 
     def on_disconnection(self):
-        print '%s disconnected'% self.client_id
+        logging.info('%s disconnected', self.client_id)
         if self.client_id != UNKNWON_ID:
             if self.client_id in self.server.clients_map:
                 del self.server.clients_map[self.client_id]
@@ -77,7 +77,7 @@
     @cmd_arg_types()
     def cmd_ok(self):
         # Do nothing
-        # This message can be send when a command has been successfully
+        # This message can be sent when a command has been successfully
         # handled to trigger callbacks
         pass
 
@@ -123,7 +123,6 @@
 
     @cmd_arg_types(normpath)
     def cmd_get_node(self, path):
-        print "path:", path
         self.send_bin('node %s' % path, self.fs.get_node(path))
 
     @bin_cmd_arg_types(normpath)
@@ -154,7 +153,7 @@
                 return
 
             owners = self.get_owners(parent)
-            print "current owners:", owners
+            logging.debug("current owners: %s", repr(owners))
 
             self.server.send(owners, 'add_owner %s %s' % (self.server.get_id(), path))
 
@@ -229,13 +228,12 @@
 
     @cmd_arg_types(normpath)
     def cmd_dget_dir(self, full_path):
-        print "cmd_dget_dir"
         if not self.dretrieve_dir(full_path, lambda: self.cmd_dget_dir(full_path)):
             return
         self.cmd_get_dir(full_path)
 
     def get_dir(self, path, callback):
-        print "get_dir:", path
+        logging.debug("get_dir: %s", path)
         path = normpath(path)
         owners = self.get_owners(path)
 
@@ -255,7 +253,7 @@
         parent, filename = self.fs.get_parent(path)
 
         if not self.fs.has_file(parent):
-            print 'doing get_dir before rmdir'
+            logging.debug('doing get_dir before rmdir')
             self.dretrieve_dir(parent, lambda: self.cmd_drmdir(path))
             return
 
@@ -287,7 +285,7 @@
         parent, filename = self.fs.get_parent(path)
 
         if not self.fs.has_file(parent):
-            print 'doing get_dir before dmkdir'
+            logging.debug('doing get_dir before dmkdir')
             self.dretrieve_dir(parent, lambda: self.cmd_dmkdir(path, mode, gid, uid))
             return
 
@@ -312,7 +310,6 @@
 
     @cmd_arg_types(normpath)
     def cmd_dget_node(self, path):
-        print "path:", path
         if not self.dretrieve_dir(path, lambda: self.cmd_dget_node(path)):
             return
         self.cmd_get_node(path)

ficus/ficus_server.py
--- 
+++ 
@@ -56,11 +56,9 @@
         owners = self.ficus_data.get('main', 'root_owner', '')
         if owners == '':
             return set()
-        print "get_root_owners:", owners
         return set(owners.split(','))
 
     def set_root_owners(self, owners):
-        print "set_root_owners:", owners
         self.ficus_data.set('main', 'root_owner', ','.join(owners))
 
     def add_root_owners(self, owner):
@@ -71,12 +69,12 @@
     def send(self, clients, *args, **kw):
         for client in clients:
             if client == self.get_id():
-                print "Ignoring message to be sent to myself:", args
+                logging.error("Ignoring message to be sent to myself: %s", args)
                 continue
             self.clients_map[client].send(*args, **kw)
 
     def send_bin(self, clients, *args, **kw):
         for client in clients:
             if client == self.get_id():
-                print "Ignoring message to be sent to myself:", args
+                logging.error("Ignoring message to be sent to myself: %s", args)
             self.clients_map[client].send_bin(*args, **kw)

ficus/fs.py
--- 
+++ 
@@ -1,9 +1,14 @@
 import logging
 import time
 import stat
+import errno
 
 from ficus.fs_history import FSHistory
 from ficus.store import Store, Dir, File, Node
+
+class FSException(Exception):
+    def __init__(self, errno):
+        self.errno = errno
 
 class FS(Store):
     def __init__(self, data_dir):
@@ -31,7 +36,7 @@
         f.close()
 
     def mkdir(self, dirname, mode, gid, uid):
-        logging.debug('mkdir %s' % dirname)
+        logging.debug('mkdir %s', dirname)
         filename = self.hdd_filename(dirname)
         entry = Dir(filename)
         entry.attrs['mode'] = stat.S_IFDIR | mode,
@@ -44,9 +49,9 @@
         entry.save()
 
     def rmdir(self, dirname):
-        logging.debug('rmdir %s' % dirname)
+        logging.debug('rmdir %s', dirname)
         if dirname == '/':
-            print 'Cannot delete / directory'
+            logging.error('Cannot delete / directory')
             return
 
         parent, filename = self.get_parent(dirname)

ficus/fs_history.py
--- 
+++ 
@@ -12,6 +12,7 @@
         try:
             self.load()
         except IOError:
+            # Ignore errors that happen on first run
             pass
 
     def add_file(self, path, owner, mtime):
@@ -19,13 +20,13 @@
             self.changes_history.remove(path)
         self.files[path] = (mtime, owner)
         self.changes_history.append(path)
-        logging.debug('Adding to changes history: %s' % path)
+        logging.debug('Adding to changes history: %s', path)
 
         deprecated = time.time() - HISTORY_TIME
         # Cleanup old entries:
         while len(self.changes_history) > 0:
             if self.files[path][0] < deprecated:
-                logging.debug('Removing deprecated entry from changes history: %s' % path)
+                logging.debug('Removing deprecated entry from changes history: %s', path)
             else:
                 return
 

ficus/server.py
--- 
+++ 
@@ -26,7 +26,7 @@
 
         # Queue of the last messages that has been broadcasted and should be ignored
         self.fs = fs
-        logging.info('Starting listener on %s:%i' % (address[0], address[1]))
+        logging.info('Starting listener on %s:%i', address[0], address[1])
 
     def handle_error(self, msg, level=logging.ERROR, exc_info=True):
         logging.log(level, "{0}: {1} --> stopping".format(self, msg),
@@ -73,8 +73,8 @@
         for host in self.clients_map.itervalues():
             if host.want_broadcast and host != orig_client:
                 host.send('broadcast ' + args)
-                logging.debug('relaying %s to %s' % (args, host))
+                logging.debug('relaying %s to %s', args, host)
 
         if not orig_client is None:
-            print "handling broadcasted command: %s" % args
+            logging.debug("handling broadcasted command: %s", args)
             orig_client.handle_cmd('0 ' + args)

Generated with KisssPM