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

[or-cvs] r17367: {updater} Fix a bug in userFilename that left us with a directory in . (updater/trunk/lib/thandy)



Author: nickm
Date: 2008-11-22 16:09:18 -0500 (Sat, 22 Nov 2008)
New Revision: 17367

Modified:
   updater/trunk/lib/thandy/ClientCLI.py
   updater/trunk/lib/thandy/formats.py
   updater/trunk/lib/thandy/repository.py
   updater/trunk/lib/thandy/util.py
Log:
Fix a bug in userFilename that left us with a directory in ./~/.  Add a facility for controller-readable log messages with --controller-log-format

Modified: updater/trunk/lib/thandy/ClientCLI.py
===================================================================
--- updater/trunk/lib/thandy/ClientCLI.py	2008-11-22 19:13:10 UTC (rev 17366)
+++ updater/trunk/lib/thandy/ClientCLI.py	2008-11-22 21:09:18 UTC (rev 17367)
@@ -3,8 +3,10 @@
 import getopt
 import logging
 import os
+import re
 import sys
 import time
+import traceback
 try:
     import json
 except ImportError:
@@ -12,6 +14,7 @@
 
 import thandy.formats
 import thandy.util
+from thandy.util import logCtrl
 import thandy.repository
 import thandy.download
 import thandy.master_keys
@@ -19,13 +22,64 @@
 import thandy.socksurls
 import thandy.encodeToXML
 
+class ControlLogFormatter:
+    def _formatStr(self, s):
+        s = '"%s"' % re.sub(r'(["\\])', r'\\\1', s)
+        s = s.replace("\n", "\\n")
+        return s
+
+    def format(self, record):
+        name = record.name
+        if name == 'thandy-ctrl':
+            parts = [ record.msg ]
+            parts.extend(
+                "%s=%s"%(k, self._formatStr(v))
+                for k,v in sorted(getattr(record, 'cmd_args', {}).iteritems()))
+            return " ".join(parts)
+        else:
+            m = record.getMessage()
+            return "%s msg=%s"%(record.levelname, self._formatStr(m))
+
+    def formatException(self, exc_info):
+        return repr(traceback.print_exception())
+
+class RegularLogFilter:
+    def filter(self, record):
+        return record.name != "thandy-ctrl"
+
+def configureLogs(options):
+    logLevel = logging.INFO
+    cLogFormat = False
+    for o,v in options:
+        if o == '--debug':
+            logLevel = logging.DEBUG
+        elif o == '--info':
+            logLevel = logging.INFO
+        elif o == '--warn':
+            logLevel = logging.WARN
+        elif o == '--controller-log-format':
+            cLogFormat = True
+
+    console = logging.StreamHandler()
+    console.setLevel(logLevel)
+    logger = logging.getLogger("")
+    logger.addHandler(console)
+    logger.setLevel(logLevel)
+    if cLogFormat:
+        #formatter = logging.Formatter("%(names)s %(levelname)s %(message)r")
+        formatter = ControlLogFormatter()
+    else:
+        formatter = logging.Formatter("%(levelname)s:%(message)s")
+        console.addFilter(RegularLogFilter())
+    console.setFormatter(formatter)
+
 def update(args):
     repoRoot = thandy.util.userFilename("cache")
-    options, args = getopt.getopt(args, "", [ "repo=", "no-download",
-                                              "loop", "no-packagesys",
-                                              "install", "socks-port=",
-                                              "debug", "info",
-                                              "warn", "force-check"])
+    options, args = getopt.getopt(args, "",
+        [ "repo=", "no-download", "loop", "no-packagesys",
+          "install", "socks-port=", "debug", "info",
+          "warn", "force-check", "controller-log-format"
+          ])
     download = True
     keep_looping = False
     use_packagesys = True
@@ -47,16 +101,10 @@
             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
         elif o == '--force-check':
             forceCheck = True
 
-    logging.basicConfig(level=logLevel)
+    configureLogs(options)
 
     if socksPort:
         thandy.socksurls.setSocksProxy("127.0.0.1", socksPort)
@@ -85,12 +133,17 @@
             forceCheck = False
 
         if installable and not files:
-            logging.info("Ready to install files: %s",
+            for p, d in installable.items():
+                for n in d.keys():
+                    logCtrl("CAN_INSTALL", PKG=p, ITEM=n)
+
+            logging.info("Ready to install packages for files: %s",
                            ", ".join(sorted(installable.keys())))
             if install:
                 # XXXX handle ordering
-                for h in installable.values():
-                    h.install()
+                for p in installable.values():
+                    for h in p.values():
+                        h.install()
             return
 
         elif not files:
@@ -109,6 +162,7 @@
             time.sleep(delay)
             continue
 
+        for f in files: logCtrl("WANTFILE", FILENAME=f)
         logging.info("Files to download are: %s", ", ".join(sorted(files)))
 
         if not download:
@@ -157,7 +211,6 @@
         downloader.wait()
         logging.info("All downloads finished.")
 
-
 def json2xml(args):
     if len(args) != 1:
         usage()

Modified: updater/trunk/lib/thandy/formats.py
===================================================================
--- updater/trunk/lib/thandy/formats.py	2008-11-22 19:13:10 UTC (rev 17366)
+++ updater/trunk/lib/thandy/formats.py	2008-11-22 21:09:18 UTC (rev 17367)
@@ -157,17 +157,17 @@
 
     return SignatureStatus(goodSigs, badSigs, unknownSigs, tangentialSigs)
 
+def canonical_str_encoder(s):
+    s = '"%s"' % re.sub(r'(["\\])', r'\\\1', s)
+    if isinstance(s, unicode):
+        return s.encode("utf-8")
+    else:
+        return s
+
 def _encodeCanonical(obj, outf):
     # Helper for encodeCanonical.  Older versions of json.encoder don't
     # even let us replace the separators.
 
-    def canonical_str_encoder(s):
-        s = '"%s"' % re.sub(r'(["\\])', r'\\\1', s)
-        if isinstance(s, unicode):
-            return s.encode("utf-8")
-        else:
-            return s
-
     if isinstance(obj, basestring):
         outf(canonical_str_encoder(obj))
     elif obj is True:

Modified: updater/trunk/lib/thandy/repository.py
===================================================================
--- updater/trunk/lib/thandy/repository.py	2008-11-22 19:13:10 UTC (rev 17366)
+++ updater/trunk/lib/thandy/repository.py	2008-11-22 21:09:18 UTC (rev 17367)
@@ -483,6 +483,8 @@
                         if h.isInstalled():
                             alreadyInstalled[h.getRelativePath()] = h
 
+            pkg_rp = pfile.getRelativePath()
+
             for f in package['files']:
                 rp, h = f[:2]
                 if alreadyInstalled.has_key(rp):
@@ -505,7 +507,7 @@
                     need.add(rp)
                 else:
                     if allHandles.has_key(rp):
-                        installableDict[rp] = allHandles[rp]
+                        installableDict.setdefault(pkg_rp, {})[rp] = allHandles[rp]
 
         # Okay; these are the files we need.
         return need

Modified: updater/trunk/lib/thandy/util.py
===================================================================
--- updater/trunk/lib/thandy/util.py	2008-11-22 19:13:10 UTC (rev 17366)
+++ updater/trunk/lib/thandy/util.py	2008-11-22 21:09:18 UTC (rev 17367)
@@ -1,5 +1,6 @@
 # Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.
 
+import logging
 import os
 import sys
 import tempfile
@@ -53,6 +54,7 @@
     except KeyError:
         base = "~/.thandy"
 
+    base = os.path.expanduser(base)
     result = os.path.normpath(os.path.join(base, name))
     ensureParentDir(result)
     return result
@@ -141,3 +143,10 @@
     finally:
         if settings is not None:
             settings.Close()
+
+_controlLog = logging.getLogger("thandy-ctrl")
+
+def logCtrl(key, **args):
+    """DOCDOC"""
+    _controlLog.log(logging.INFO, key, extra={'cmd_args':args})
+