dogtail r385 - in trunk: . dogtail



Author: zcerza
Date: Tue Feb 19 17:10:34 2008
New Revision: 385
URL: http://svn.gnome.org/viewvc/dogtail?rev=385&view=rev

Log:
* dogtail/distro.py: Make distribution detection code a little less
ugly. Remove docstrings from the Distro subclasses. Clean up some other
documentation.

* dogtail/i18n.py: Don't import the distro module until we actually
need it.

* dogtail/logging.py: Add an optional 'newline' arg to Logger.log(),
which defaults to True. Passing False will cause the logger to not print or
write a newline at the end of the message. Also stop catching some IOErrors, as
that made the code awkward for no good reason.



Modified:
   trunk/ChangeLog
   trunk/dogtail/distro.py
   trunk/dogtail/i18n.py
   trunk/dogtail/logging.py

Modified: trunk/dogtail/distro.py
==============================================================================
--- trunk/dogtail/distro.py	(original)
+++ trunk/dogtail/distro.py	Tue Feb 19 17:10:34 2008
@@ -34,7 +34,8 @@
 
 class PackageDb:
     """
-    Class to abstract the details of whatever software package database is in use (RPM, APT, etc)
+    Class to abstract the details of whatever software package database is in
+    use (RPM, APT, etc)
     """
     def __init__(self):
         self.prefix = '/usr'
@@ -42,20 +43,24 @@
 
     def getVersion(self, packageName):
         """
-        Method to get the version of an installed package as a Version instance (or raise an exception if not found)
+        Method to get the version of an installed package as a Version 
+        instance (or raise an exception if not found)
+        
         Note: does not know about distributions' internal revision numbers.
         """
         raise NotImplementedError
 
     def getFiles(self, packageName):
         """
-        Method to get a list of filenames owned by the package, or raise an exception if not found.
+        Method to get a list of filenames owned by the package, or raise an 
+        exception if not found.
         """
         raise NotImplementedError
 
     def getMoFiles(self, locale = None):
         """
-        Method to get a list of all .mo files on the system, optionally for a specific locale.
+        Method to get a list of all .mo files on the system, optionally for a
+        specific locale.
         """
         moFiles = {}
 
@@ -181,14 +186,17 @@
         PackageDb.__init__(self)
 
     def getVersion(self, packageName):
-        # the portage utilities are almost always going to be in /usr/lib/portage/pym
+        # the portage utilities are almost always going to be in 
+        # /usr/lib/portage/pym
         import sys
         sys.path.append ('/usr/lib/portage/pym')
         import portage
-        # FIXME: this takes the first package returned in the list, in the case that there are
-        # slotted packages, and removes the leading category such as 'sys-apps'
+        # FIXME: this takes the first package returned in the list, in the 
+        # case that there are slotted packages, and removes the leading 
+        # category such as 'sys-apps'
         gentooPackageName = portage.db["/"]["vartree"].dbapi.match(packageName)[0].split('/')[1];
-        # this removes the distribution specific versioning returning only the upstream version
+        # this removes the distribution specific versioning returning only the
+        # upstream version
         upstreamVersion = portage.pkgsplit(gentooPackageName)[1]
         #print "Version of package is: " + upstreamVersion
         return Version.fromString(upstreamVersion);
@@ -209,7 +217,6 @@
 # getVersion not implemented because on Solaris multiple modules are installed
 # in single packages, so it is hard to tell what version number of a specific
 # module.
-#
 class _SolarisPackageDb(PackageDb):
     def __init__(self):
         PackageDb.__init__(self)
@@ -236,101 +243,80 @@
     """
     Class representing a distribution.
 
-    Scripts may want to do arbitrary logic based on whichever distro is in use (e.g. handling differences in names of packages, distribution-specific patches, etc.)
+    Scripts may want to do arbitrary logic based on whichever distro is in use
+    (e.g. handling differences in names of packages, distribution-specific 
+    patches, etc.)
 
-    We can either create methods in the Distro class to handle these, or we can use constructs like isinstance(distro, Ubuntu) to handle this. We can even create hierarchies of distro subclasses to handle this kind of thing (could get messy fast though)
+    We can either create methods in the Distro class to handle these, or we 
+    can use constructs like isinstance(distro, Ubuntu) to handle this. We can
+    even create hierarchies of distro subclasses to handle this kind of thing
+    (could get messy fast though)
     """
 
-class RedHatOrFedora(Distro):
-    """
-    Class representing one of Red Hat Linux, Fedora, Red Hat Enterprise Linux, or one of the rebuild-style derivatives
-    """
+class Fedora(Distro):
     def __init__(self):
         self.packageDb = _RpmPackageDb()
 
+class RHEL(Fedora):
+    pass
+
 class Debian(Distro):
-    """
-    Class representing one of the Debian or Debian-derived distributions
-    """
     def __init__(self):
         self.packageDb = _AptPackageDb()
 
 class Ubuntu(Debian):
-    """
-    Class representing one of the Debian or Debian-derived distributions
-    """
     def __init__(self):
         self.packageDb = _UbuntuAptPackageDb()
 
 class Suse(Distro):
-    """
-    Class representing one of the SuSE or SuSE-derived distributions
-    """
     def __init__(self):
         self.packageDb = _RpmPackageDb()
 
 class Gentoo(Distro):
-    """
-    Class representing one of the Gentoo or Gentoo-derived distributions
-    """
     def __init__(self):
         self.packageDb = _PortagePackageDb()
 
 class Conary(Distro):
-    """
-    Class representing a Conary-based distribution
-    """
     def __init__(self):
         self.packageDb = _ConaryPackageDb()
 
 class Solaris(Distro):
-    """
-    Class representing a Solaris distribution
-    """
     def __init__(self):
         self.packageDb = _SolarisPackageDb()
 
 class JHBuild(Distro):
-    """
-    Class representing a JHBuild environment
-    """
     def __init__(self):
         self.packageDb = JhBuildPackageDb()
 
-message = "Detecting distribution: "
-if os.environ.get("CERTIFIED_GNOMIE", "no") == "yes":
-    logger.log(message + "JHBuild environment")
-    distro = JHBuild()
-elif os.path.exists ("/etc/SuSE-release"):
-    logger.log(message + "SuSE (or derived distribution)")
-    distro = Suse()
-elif os.path.exists ("/etc/fedora-release"):
-    logger.log(message + "Fedora (or derived distribution)")
-    distro = RedHatOrFedora()
-elif os.path.exists ("/etc/redhat-release"):
-    logger.log(message + "Red Hat/Fedora/derived distribution")
-    distro = RedHatOrFedora()
-elif os.path.exists ("/usr/share/doc/ubuntu-minimal"):
-    logger.log(message + "Ubuntu (or derived distribution)")
-    distro = Ubuntu()
-elif os.path.exists ("/etc/debian_version"):
-    logger.log(message + "Debian (or derived distribution)")
-    distro = Debian()
-elif os.path.exists ("/etc/gentoo-release"):
-    logger.log(message + "Gentoo (or derived distribution)")
-    distro = Gentoo()
-elif os.path.exists ("/etc/slackware-version"):
-    logger.log(message + "Slackware")
-    raise DistributionNotSupportedError("Slackware")
-elif os.path.exists ("/var/lib/conarydb/conarydb"):
-    logger.log(message + "Conary-based distribution")
-    distro = Conary()
-elif os.path.exists ("/etc/release") and \
-        re.match (".*Solaris", open ("/etc/release").readline()):
-    logger.log(message + "Solaris distribution")
-    distro = Solaris()
-else:
-    logger.log(message + "Unknown")
-    raise DistributionNotSupportedError("Unknown")
+def detectDistro():
+    logger.log("Detecting distribution:", newline = False)
 
+    if os.environ.get("CERTIFIED_GNOMIE", "no") == "yes":
+        distro = JHBuild()
+    elif os.path.exists("/etc/SuSE-release"):
+        distro = Suse()
+    elif os.path.exists("/etc/fedora-release"):
+        distro = Fedora()
+    elif os.path.exists("/etc/redhat-release"):
+        distro = RHEL()
+    elif os.path.exists("/usr/share/doc/ubuntu-minimal"):
+        distro = Ubuntu()
+    elif os.path.exists("/etc/debian_version"):
+        distro = Debian()
+    elif os.path.exists("/etc/gentoo-release"):
+        distro = Gentoo()
+    elif os.path.exists("/etc/slackware-version"):
+        raise DistributionNotSupportedError("Slackware")
+    elif os.path.exists("/var/lib/conarydb/conarydb"):
+        distro = Conary()
+    elif os.path.exists ("/etc/release") and \
+            re.match(".*Solaris", open ("/etc/release").readline()):
+        distro = Solaris()
+    else:
+        raise DistributionNotSupportedError("Unknown")
+    logger.log(distro.__class__.__name__)
+    return distro
+
+distro = detectDistro()
 packageDb = distro.packageDb
+

Modified: trunk/dogtail/i18n.py
==============================================================================
--- trunk/dogtail/i18n.py	(original)
+++ trunk/dogtail/i18n.py	Tue Feb 19 17:10:34 2008
@@ -7,7 +7,6 @@
 
 __author__ = """David Malcolm <dmalcolm redhat com>, Zack Cerza <zcerza redhat com>"""
 
-import distro
 import config
 
 import os
@@ -212,6 +211,8 @@
     dependencies. It is possible to restrict the results to those of a certain
     language, for example 'ja'.
     """
+    import distro
+
     result = []
     for filename in distro.packageDb.getFiles(packageName):
         if isMoFile(filename, language):
@@ -255,6 +256,7 @@
     # this special case, aside from the simple fact that there is one,
     # is that it makes automatic translations much slower.
 
+    import distro
     language = os.environ.get('LANGUAGE', os.environ['LANG'])[0:2]
     if isinstance(distro.distro, distro.Ubuntu):
         load('language-pack-gnome-%s' % language, language)

Modified: trunk/dogtail/logging.py
==============================================================================
--- trunk/dogtail/logging.py	(original)
+++ trunk/dogtail/logging.py	Tue Feb 19 17:10:34 2008
@@ -109,9 +109,6 @@
             raise IOError, \
                     "Log path %s does not exist or is not a directory" % logDir
 
-        #self.createFile()
-
-
     def findUniqueName(self):
         # generate a logfile name and check if it already exists
         self.fileName = config.logDir + self.stamper.fileStamp(self.fileName) \
@@ -129,39 +126,31 @@
 
     def createFile(self):
         # Try to create the file and write the header info
-        try:
-            print "Creating logfile at %s ..." % self.fileName
-            self.file = codecs.open(self.fileName, mode = 'wb', encoding = \
-                    'utf-8')
-            self.file.write("##### " + os.path.basename(self.fileName) + '\n')
-            self.file.flush()
-            return True
-        except IOError:
-            print "Could not create and write to " + self.fileName
-            self.file = False
-            return False
+        print "Creating logfile at %s ..." % self.fileName
+        self.file = codecs.open(self.fileName, mode = 'wb', encoding = \
+                'utf-8')
+        self.file.write("##### " + os.path.basename(self.fileName) + '\n')
+        self.file.flush()
 
-    def log(self, message):
+    def log(self, message, newline = True):
         """
         Hook used for logging messages. Might eventually be a virtual
         function, but nice and simple for now.
         """
         message = message.decode('utf-8', 'replace')
 
-        # Also write to standard out.
-        if self.stdOut and config.logDebugToStdOut: print message
-
         # Try to open and write the result to the log file.
-        if isinstance(self.file, bool): 
-            if not config.logDebugToFile: return
-            elif not self.createFile(): return
-        try:
-            #self.file = open(self.fileName, 'a')
-            self.file.write(message + '\n')
+        if isinstance(self.file, bool) and config.logDebugToFile:
+            self.createFile()
+
+        if config.logDebugToFile:
+            if newline: self.file.write(message + '\n')
+            else: self.file.write(message + ' ')
             self.file.flush()
-            #self.file.close()
-        except IOError:
-            print "Could not write to file " + self.fileName
+
+        if self.stdOut and config.logDebugToStdOut: 
+            if newline: print message
+            else: print message,
 
 class ResultsLogger(Logger):
     """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]