summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-11-22 21:09:18 +0000
committerNick Mathewson <nickm@torproject.org>2008-11-22 21:09:18 +0000
commit7f3418fcd091da3fb5cdc11c4820b43bb90d2d20 (patch)
tree0c5485644dc16e2dfc173c25dbf8ba63c028d42b /lib
parentcff6c8bac612ae4ed598ef222ada02d3a83ecaa1 (diff)
Fix a bug in userFilename that left us with a directory in ./~/. Add a facility for controller-readable log messages with --controller-log-format
git-svn-id: file:///home/or/svnrepo/updater/trunk@17367 55e972cd-5a19-0410-ae62-a4d7a52db4cd
Diffstat (limited to 'lib')
-rw-r--r--lib/thandy/ClientCLI.py85
-rw-r--r--lib/thandy/formats.py14
-rw-r--r--lib/thandy/repository.py4
-rw-r--r--lib/thandy/util.py9
4 files changed, 88 insertions, 24 deletions
diff --git a/lib/thandy/ClientCLI.py b/lib/thandy/ClientCLI.py
index d6f3adc..c416277 100644
--- a/lib/thandy/ClientCLI.py
+++ b/lib/thandy/ClientCLI.py
@@ -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 @@ except ImportError:
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.packagesys.PackageSystem
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 @@ def update(args):
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 @@ def update(args):
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 @@ def update(args):
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 @@ def update(args):
downloader.wait()
logging.info("All downloads finished.")
-
def json2xml(args):
if len(args) != 1:
usage()
diff --git a/lib/thandy/formats.py b/lib/thandy/formats.py
index 1fd0eb1..f0a4e30 100644
--- a/lib/thandy/formats.py
+++ b/lib/thandy/formats.py
@@ -157,17 +157,17 @@ def checkSignatures(signed, keyDB, role=None, path=None):
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:
diff --git a/lib/thandy/repository.py b/lib/thandy/repository.py
index a6df1bb..2dea1c4 100644
--- a/lib/thandy/repository.py
+++ b/lib/thandy/repository.py
@@ -483,6 +483,8 @@ class LocalRepository:
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 @@ class LocalRepository:
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
diff --git a/lib/thandy/util.py b/lib/thandy/util.py
index 6b75ece..1fbff81 100644
--- a/lib/thandy/util.py
+++ b/lib/thandy/util.py
@@ -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 @@ def userFilename(name):
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 @@ def getRegistryValue(keyname):
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})
+