Transition to libbe.LOG for logging
authorW. Trevor King <wking@tremily.us>
Thu, 24 Jan 2013 07:31:52 +0000 (02:31 -0500)
committerW. Trevor King <wking@tremily.us>
Thu, 24 Jan 2013 07:36:22 +0000 (02:36 -0500)
This makes it easier to tweak log verbosity and redirect logs to other
handlers.  For example, the WSGI servers are unstable with stderr
closed, and crash with an IOError if they try to print a warning to
stderr.

libbe/__init__.py
libbe/bug.py
libbe/bugdir.py
libbe/command/import_xml.py
libbe/comment.py
libbe/storage/vcs/base.py
libbe/util/subproc.py

index efd744a335009db20bd7e1194e3bed4c6d5d4b7d..b29763626cefcfc2db1a9421951bca1c10063861 100644 (file)
@@ -37,6 +37,14 @@ The available submodules are:
 * :py:mod:`libbe._version`
 """
 
 * :py:mod:`libbe._version`
 """
 
+import logging as _logging
+
+
+LOG = _logging.getLogger('be')
+LOG.addHandler(_logging.StreamHandler())
+LOG.setLevel(_logging.ERROR)
+
+
 TESTING = False
 """Flag controlling test-suite generation.
 
 TESTING = False
 """Flag controlling test-suite generation.
 
index 66510ba049dcc31f82f71ea44beae8ddf03698d5..bb7a37da963a8db0bead90bf4f3ab1dd47a8eca9 100644 (file)
@@ -439,7 +439,7 @@ class Bug (settings_object.SavedSettingsObject):
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
-    def from_xml(self, xml_string, preserve_uuids=False, verbose=True):
+    def from_xml(self, xml_string, preserve_uuids=False):
         u"""
         Note: If a bug uuid is given, set .alt_id to it's value.
         >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()")
         u"""
         Note: If a bug uuid is given, set .alt_id to it's value.
         >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()")
@@ -451,7 +451,7 @@ class Bug (settings_object.SavedSettingsObject):
         >>> commC = commA.new_reply(body='comment C')
         >>> xml = bugA.xml(show_comments=True)
         >>> bugB = Bug()
         >>> commC = commA.new_reply(body='comment C')
         >>> xml = bugA.xml(show_comments=True)
         >>> bugB = Bug()
-        >>> bugB.from_xml(xml, verbose=True)
+        >>> bugB.from_xml(xml)
         >>> bugB.xml(show_comments=True) == xml
         False
         >>> bugB.uuid = bugB.alt_id
         >>> bugB.xml(show_comments=True) == xml
         False
         >>> bugB.uuid = bugB.alt_id
@@ -489,8 +489,7 @@ class Bug (settings_object.SavedSettingsObject):
                 pass
             elif child.tag == 'comment':
                 comm = comment.Comment(bug=self)
                 pass
             elif child.tag == 'comment':
                 comm = comment.Comment(bug=self)
-                comm.from_xml(
-                    child, preserve_uuids=preserve_uuids, verbose=verbose)
+                comm.from_xml(child, preserve_uuids=preserve_uuids)
                 comments.append(comm)
                 continue
             elif child.tag in tags:
                 comments.append(comm)
                 continue
             elif child.tag in tags:
@@ -515,9 +514,10 @@ class Bug (settings_object.SavedSettingsObject):
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
-            elif verbose == True:
-                print >> sys.stderr, 'Ignoring unknown tag %s in %s' \
-                    % (child.tag, comment.tag)
+            else:
+                libbe.LOG.warning(
+                    'ignoring unknown tag {0} in {1}'.format(
+                        child.tag, comment.tag))
         if uuid != self.uuid:
             if not hasattr(self, 'alt_id') or self.alt_id == None:
                 self.alt_id = uuid
         if uuid != self.uuid:
             if not hasattr(self, 'alt_id') or self.alt_id == None:
                 self.alt_id = uuid
@@ -610,8 +610,9 @@ class Bug (settings_object.SavedSettingsObject):
                 parent = uuid_map[c.in_reply_to]
             except KeyError:
                 if ignore_missing_references == True:
                 parent = uuid_map[c.in_reply_to]
             except KeyError:
                 if ignore_missing_references == True:
-                    print >> sys.stderr, \
-                        'Ignoring missing reference to %s' % c.in_reply_to
+                    libbe.LOG.warning(
+                        'ignoring missing reference to {0}'.format(
+                            c.in_reply_to))
                     parent = default_parent
                     if parent.uuid != comment.INVALID_UUID:
                         c.in_reply_to = parent.uuid
                     parent = default_parent
                     if parent.uuid != comment.INVALID_UUID:
                         c.in_reply_to = parent.uuid
index 40097f714fd6562b4ec7eb242b7b6f002cfe556f..8b9e1e75d9b180881e84640a1d440249b5374aec 100644 (file)
@@ -396,7 +396,7 @@ class BugDir (list, settings_object.SavedSettingsObject):
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
-    def from_xml(self, xml_string, preserve_uuids=False, verbose=True):
+    def from_xml(self, xml_string, preserve_uuids=False):
         """
         Note: If a bugdir uuid is given, set .alt_id to it's value.
         >>> bug.load_severities(bug.severity_def)
         """
         Note: If a bugdir uuid is given, set .alt_id to it's value.
         >>> bug.load_severities(bug.severity_def)
@@ -457,8 +457,7 @@ class BugDir (list, settings_object.SavedSettingsObject):
                 pass
             elif child.tag == 'bug':
                 bg = bug.Bug(bugdir=self)
                 pass
             elif child.tag == 'bug':
                 bg = bug.Bug(bugdir=self)
-                bg.from_xml(
-                    child, preserve_uuids=preserve_uuids, verbose=verbose)
+                bg.from_xml(child, preserve_uuids=preserve_uuids)
                 self.append(bg, update=True)
                 continue
             elif child.tag in tags:
                 self.append(bg, update=True)
                 continue
             elif child.tag in tags:
@@ -513,8 +512,9 @@ class BugDir (list, settings_object.SavedSettingsObject):
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
-            elif verbose == True:
-                sys.stderr.write('Ignoring unknown tag {} in {}\n'.format(
+            else:
+                libbe.LOG.warning(
+                    'ignoring unknown tag {0} in {1}'.format(
                         child.tag, bugdir.tag))
         if uuid != self.uuid:
             if not hasattr(self, 'alt_id') or self.alt_id == None:
                         child.tag, bugdir.tag))
         if uuid != self.uuid:
             if not hasattr(self, 'alt_id') or self.alt_id == None:
index a16b0b0c18c6bfb5d0fb77222056ee3d05c9a0bb..fbf456b1a652617ab2fd7516c3a7264e9aac0451 100644 (file)
@@ -203,11 +203,11 @@ class Import_XML (libbe.command.Command):
                         text = text.decode('unicode_escape').strip()
                         version[child.tag] = text
                     else:
                         text = text.decode('unicode_escape').strip()
                         version[child.tag] = text
                     else:
-                        sys.stderr.write(
-                            'ignoring unknown tag {} in {}\n'.format(
+                        libbe.LOG.warning(
+                            'ignoring unknown tag {0} in {1}\n'.format(
                                 gchild.tag, child.tag))
             else:
                                 gchild.tag, child.tag))
             else:
-                sys.stderr.write('ignoring unknown tag {} in {}\n'.format(
+                libbe.LOG.warning('ignoring unknown tag {0} in {1}\n'.format(
                         child.tag, be_xml.tag))
         return (version, root_bugdirs, root_bugs, root_comments)
 
                         child.tag, be_xml.tag))
         return (version, root_bugdirs, root_bugs, root_comments)
 
index a669e4e5012e180f229ca324426dbee53d64994b..9bef50af850a07379a788f3e42f8adaf783c68c7 100644 (file)
@@ -333,7 +333,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
-    def from_xml(self, xml_string, preserve_uuids=False, verbose=True):
+    def from_xml(self, xml_string, preserve_uuids=False):
         u"""
         Note: If alt-id is not given, translates any <uuid> fields to
         <alt-id> fields.
         u"""
         Note: If alt-id is not given, translates any <uuid> fields to
         <alt-id> fields.
@@ -344,7 +344,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         >>> commA.extra_strings += ['TAG: very helpful']
         >>> xml = commA.xml()
         >>> commB = Comment()
         >>> commA.extra_strings += ['TAG: very helpful']
         >>> xml = commA.xml()
         >>> commB = Comment()
-        >>> commB.from_xml(xml, verbose=True)
+        >>> commB.from_xml(xml)
         >>> commB.explicit_attrs
         ['author', 'date', 'content_type', 'body', 'alt_id']
         >>> commB.xml() == xml
         >>> commB.explicit_attrs
         ['author', 'date', 'content_type', 'body', 'alt_id']
         >>> commB.xml() == xml
@@ -397,9 +397,10 @@ class Comment (Tree, settings_object.SavedSettingsObject):
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
                 attr_name = child.tag.replace('-','_')
                 self.explicit_attrs.append(attr_name)
                 setattr(self, attr_name, text)
-            elif verbose == True:
-                print >> sys.stderr, 'Ignoring unknown tag %s in %s' \
-                    % (child.tag, comment.tag)
+            else:
+                libbe.LOG.warning(
+                    'ignoring unknown tag {0} in {1}'.format(
+                        child.tag, comment.tag))
         if uuid != self.uuid and self.alt_id == None:
             self.explicit_attrs.append('alt_id')
             self.alt_id = uuid
         if uuid != self.uuid and self.alt_id == None:
             self.explicit_attrs.append('alt_id')
             self.alt_id = uuid
index 845336daf86dad8587e706678ca38bb718ae33f3..671df43330dbef33b99eb1b29a9211709a37def2 100644 (file)
@@ -207,7 +207,7 @@ class CachedPathID (object):
         self._cache_path = os.path.join(
             self._root, self._spacer_dirs[0], 'id-cache')
 
         self._cache_path = os.path.join(
             self._root, self._spacer_dirs[0], 'id-cache')
 
-    def init(self, verbose=True, cache=None):
+    def init(self, cache=None):
         """Create cache file for an existing .be directory.
 
         The file contains multiple lines of the form::
         """Create cache file for an existing .be directory.
 
         The file contains multiple lines of the form::
@@ -227,8 +227,10 @@ class CachedPathID (object):
                 id = self.id(dirpath)
                 relpath = dirpath[len(self._root + os.path.sep):]
                 if id.count('/') == 0:
                 id = self.id(dirpath)
                 relpath = dirpath[len(self._root + os.path.sep):]
                 if id.count('/') == 0:
-                    if verbose == True and id in self._cache:
-                        print >> sys.stderr, 'Multiple paths for %s: \n  %s\n  %s' % (id, self._cache[id], relpath)
+                    if id in self._cache:
+                        libbe.LOG.warning(
+                            'multiple paths for {0}:\n  {1}\n  {2}'.format(
+                                id, self._cache[id], relpath))
                     self._cache[id] = relpath
             except InvalidPath:
                 pass
                     self._cache[id] = relpath
             except InvalidPath:
                 pass
@@ -271,7 +273,7 @@ class CachedPathID (object):
         else:
             extra = fields[1:]
         if uuid not in self._cache:
         else:
             extra = fields[1:]
         if uuid not in self._cache:
-            self.init(verbose=False, cache=self._cache)
+            self.init(cache=self._cache)
             if uuid not in self._cache:
                 raise InvalidID(uuid)
         if relpath == True:
             if uuid not in self._cache:
                 raise InvalidID(uuid)
         if relpath == True:
@@ -355,7 +357,6 @@ class VCS (libbe.storage.base.VersionedStorage):
         libbe.storage.base.VersionedStorage.__init__(self, *args, **kwargs)
         self.versioned = False
         self.interspersed_vcs_files = False
         libbe.storage.base.VersionedStorage.__init__(self, *args, **kwargs)
         self.versioned = False
         self.interspersed_vcs_files = False
-        self.verbose_invoke = False
         self._cached_path_id = CachedPathID()
         self._rooted = False
 
         self._cached_path_id = CachedPathID()
         self._rooted = False
 
@@ -923,8 +924,6 @@ class VCS (libbe.storage.base.VersionedStorage):
     def _u_invoke(self, *args, **kwargs):
         if 'cwd' not in kwargs:
             kwargs['cwd'] = self.repo
     def _u_invoke(self, *args, **kwargs):
         if 'cwd' not in kwargs:
             kwargs['cwd'] = self.repo
-        if 'verbose' not in kwargs:
-            kwargs['verbose'] = self.verbose_invoke
         if 'encoding' not in kwargs:
             kwargs['encoding'] = self.encoding
         return invoke(*args, **kwargs)
         if 'encoding' not in kwargs:
             kwargs['encoding'] = self.encoding
         return invoke(*args, **kwargs)
index 08980c98564884d38c747813e00db25327e04dad..f1f04c10abeea3fdb5ae1159cebde59534cfff56 100644 (file)
@@ -47,8 +47,7 @@ class CommandError(Exception):
         self.stderr = stderr
 
 def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,),
         self.stderr = stderr
 
 def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,),
-           cwd=None, shell=None, unicode_output=True, verbose=False,
-           encoding=None, **kwargs):
+           cwd=None, shell=None, unicode_output=True, encoding=None, **kwargs):
     """
     expect should be a tuple of allowed exit codes.  cwd should be
     the directory from which the command will be executed.  When
     """
     expect should be a tuple of allowed exit codes.  cwd should be
     the directory from which the command will be executed.  When
@@ -63,8 +62,7 @@ def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,),
     else:
         list_args = args
         str_args = ' '.join(args)  # sloppy, but just for logging
     else:
         list_args = args
         str_args = ' '.join(args)  # sloppy, but just for logging
-    if verbose == True:
-        print >> sys.stderr, '%s$ %s' % (cwd, str_args)
+    libbe.LOG.debug('{0}$ {1}'.format(cwd, str_args))
     try :
         if _POSIX:
             if shell is None:
     try :
         if _POSIX:
             if shell is None:
@@ -89,8 +87,7 @@ def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,),
             stdout = unicode(stdout, encoding)
         if stderr != None:
             stderr = unicode(stderr, encoding)
             stdout = unicode(stdout, encoding)
         if stderr != None:
             stderr = unicode(stderr, encoding)
-    if verbose == True:
-        print >> sys.stderr, '%d\n%s%s' % (status, stdout, stderr)
+    libbe.LOG.debug('{0}\n{1}{2}'.format(status, stdout, stderr))
     if status not in expect:
         raise CommandError(list_args, status, stdout, stderr)
     return status, stdout, stderr
     if status not in expect:
         raise CommandError(list_args, status, stdout, stderr)
     return status, stdout, stderr