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

[or-cvs] r17301: {updater} make log levels configurable. check file hashes correctly. (in updater/trunk/lib/thandy: . packagesys)



Author: nickm
Date: 2008-11-16 19:28:22 -0500 (Sun, 16 Nov 2008)
New Revision: 17301

Modified:
   updater/trunk/lib/thandy/ClientCLI.py
   updater/trunk/lib/thandy/download.py
   updater/trunk/lib/thandy/packagesys/
   updater/trunk/lib/thandy/repository.py
Log:
make  log levels configurable.  check file hashes correctly.

Modified: updater/trunk/lib/thandy/ClientCLI.py
===================================================================
--- updater/trunk/lib/thandy/ClientCLI.py	2008-11-16 23:58:47 UTC (rev 17300)
+++ updater/trunk/lib/thandy/ClientCLI.py	2008-11-17 00:28:22 UTC (rev 17301)
@@ -18,12 +18,15 @@
     repoRoot = thandy.util.userFilename("cache")
     options, args = getopt.getopt(args, "", [ "repo=", "no-download",
                                               "loop", "no-packagesys",
-                                              "install", "socks-port="])
+                                              "install", "socks-port=",
+                                              "debug", "info",
+                                              "warn"])
     download = True
     keep_looping = False
     use_packagesys = True
     install = False
     socksPort = None
+    logLevel = logging.INFO
 
     for o, v in options:
         if o == '--repo':
@@ -38,7 +41,15 @@
             install = True
         elif o == "--socks-port":
             socksPort = int(v)
+        elif o == '--debug':
+            logLevel = logging.DEBUG
+        elif o == '--info':
+            logLevel = logging.INFO
+        elif o == '--warn':
+            logLevel = logging.WARN
 
+    logging.basicConfig(level=logLevel)
+
     if socksPort:
         thandy.socksurls.setSocksProxy("127.0.0.1", socksPort)
 
@@ -59,7 +70,7 @@
                                       installableDict=installable)
 
         if installable and not files:
-            logging.notice("Ready to install files: %s",
+            logging.info("Ready to install files: %s",
                            ", ".join(sorted(installable.keys())))
             if install:
                 # XXXX handle ordering
@@ -95,10 +106,12 @@
         downloader = thandy.download.DownloadManager()
 
         for f in files:
-            dj = thandy.download.ThandyDownloadJob(f, repo.getFilename(f),
-                                                   mirrorlist,
-                                                   wantHash=hashes.get(f),
-                                                   useTor=(socksPort!=None))
+            dj = thandy.download.ThandyDownloadJob(
+                f, repo.getFilename(f),
+                mirrorlist,
+                wantHash=hashes.get(f),
+                repoFile=repo.getRequestedFile(f),
+                useTor=(socksPort!=None))
 
             def successCb(rp=f):
                 rf = repo.getRequestedFile(rp)
@@ -108,10 +121,10 @@
 
             downloader.addDownloadJob(dj)
 
-        logging.info("Launching downloads")
+        logging.debug("Launching downloads")
         downloader.start()
 
-        logging.info("Waiting for downloads to finish.")
+        logging.debug("Waiting for downloads to finish.")
         downloader.wait()
         logging.info("All downloads finished.")
 
@@ -123,11 +136,10 @@
     print "Known commands:"
     print "  update [--repo=repository] [--no-download] [--loop]"
     print "         [--no-packagesys] [--install] [--socks-port=port]"
+    print "         [--debug|--info|--warn]"
     sys.exit(1)
 
 def main():
-    #XXXX make this an option.
-    logging.basicConfig(level=logging.DEBUG)
 
     if len(sys.argv) < 2:
         usage()

Modified: updater/trunk/lib/thandy/download.py
===================================================================
--- updater/trunk/lib/thandy/download.py	2008-11-16 23:58:47 UTC (rev 17300)
+++ updater/trunk/lib/thandy/download.py	2008-11-17 00:28:22 UTC (rev 17301)
@@ -130,16 +130,18 @@
 class DownloadJob:
     """Abstract base class.  Represents a thing to be downloaded, and the
        knowledge of how to download it."""
-    def __init__(self, targetPath, tmpPath, wantHash=None, useTor=False):
+    def __init__(self, targetPath, tmpPath, wantHash=None, repoFile=None,
+                 useTor=False):
         """Create a new DownloadJob.  When it is finally downloaded,
            store it in targetPath.  Store partial results in tmpPath;
            if there is already a file in tmpPath, assume that it is an
            incomplete download. If wantHash, reject the file unless
            the hash is as given.  If useTor, use a socks connection."""
-
+        #DOCDODC repofile
         self._destPath = targetPath
         self._tmpPath = tmpPath
         self._wantHash = wantHash
+        self._repoFile = repoFile
         self._useTor = useTor
 
         self._success = lambda : None
@@ -235,10 +237,12 @@
             if f_out is not None:
                 f_out.close()
 
-        if self._wantHash:
+        if self._wantHash and not self._repoFile:
             gotHash = thandy.formats.getFileDigest(self._tmpPath)
             if gotHash != self._wantHash:
                 raise thandy.DownloadError("File hash was not as expected.")
+        elif self._repoFile:
+            self._repoFile.checkFile(self._tmpPath, self._wantHash)
 
         thandy.util.ensureParentDir(self._destPath)
         thandy.util.moveFile(self._tmpPath, self._destPath)
@@ -264,10 +268,10 @@
     """Thandy's subtype of DownloadJob: knows about mirrors, weighting,
        and Thandy's directory structure."""
     def __init__(self, relPath, destPath, mirrorList, wantHash=None,
-                 supportedURLTypes=None, useTor=None):
+                 supportedURLTypes=None, useTor=None, repoFile=None):
 
         DownloadJob.__init__(self, destPath, None, wantHash=wantHash,
-                             useTor=useTor)
+                             useTor=useTor, repoFile=repoFile)
         self._mirrorList = mirrorList
         self._relPath = relPath
 


Property changes on: updater/trunk/lib/thandy/packagesys
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo


Modified: updater/trunk/lib/thandy/repository.py
===================================================================
--- updater/trunk/lib/thandy/repository.py	2008-11-16 23:58:47 UTC (rev 17300)
+++ updater/trunk/lib/thandy/repository.py	2008-11-17 00:28:22 UTC (rev 17301)
@@ -128,6 +128,20 @@
 
         return signed_obj, main_obj
 
+    def checkFile(self, fname, needhash=None):
+        f = open(fname, 'r')
+        try:
+            s = f.read()
+        finally:
+            f.close()
+
+        signed, main = self._checkContent(s)
+        if needhash:
+            d = thandy.formats.getDigest(main)
+            if d != needhash:
+                raise thandy.FormatException("Content didn't match needed "
+                                             "hash.")
+
     def load(self):
         """Load this object from disk if it hasn't already been loaded."""
         if self._main_obj == None:
@@ -186,8 +200,10 @@
     def getExpectedHash(self):
         return self._needHash
 
-    def checkFile(self):
-        return self._needHash == self._repository.getFileDigest()
+    def checkFile(self, fname, needHash=None):
+        if needHash:
+            if thandy.formats.getFileDigest(fname) != needHash:
+                raise thandy.FormatException("Digest for %s not as expected.")
 
 class LocalRepository:
     """Represents a client's partial copy of a remote mirrored repository."""
@@ -262,7 +278,7 @@
 
     def getRequestedFile(self, relPath, pkgSystems=None):
         """DOCDOC"""
-        for f in self._metafiles:
+        for f in self._metaFiles:
             if f.getRelativePath() == relPath:
                 return f
         for f in self._bundleFiles.itervalues():
@@ -283,10 +299,8 @@
                          pkgSystems=None, installableDict=None):
         """Return a set of relative paths for all files that we need
            to fetch.  Assumes that we care about the bundles
-           'trackingBundles'.  If hashDict is provided, add mappings to it
-           from the relative paths we want to fecth to the hashes that we
-           want those items to have, when we know those hashes.
-           DOCDOC pkgSystems, installableDict
+           'trackingBundles'.
+           DOCDOC pkgSystems, installableDict, hashDict
         """
 
         if now == None:
@@ -390,9 +404,8 @@
                 continue
 
             rp = binfo.getRelativePath()
-            #hashDict[rp] =  #XXXX this hash needs to be calculated ovver
-            #                #     the json data.
             h_expected = binfo.getHash()
+            hashDict[rp] = h_expected
             bfile = self.getBundleFile(rp)
             try:
                 bfile.load()
@@ -425,8 +438,7 @@
                 rp = pkginfo['path']
                 pfile = self.getPackageFile(rp)
                 h_expected = thandy.formats.parseHash(pkginfo['hash'])
-                #hashDict[rp] =  #XXXX this hash needs to be calculated ovver
-                #                #     the json data.
+                hashDict[rp] = h_expected
                 try:
                     pfile.load()
                 except OSError: