[or-cvs] r17112: {updater} In python 2.6 and later, simplejson is built-in as json. (in updater/trunk: . lib/thandy)

nickm at seul.org nickm at seul.org
Wed Oct 15 21:27:16 UTC 2008


Author: nickm
Date: 2008-10-15 17:27:16 -0400 (Wed, 15 Oct 2008)
New Revision: 17112

Modified:
   updater/trunk/lib/thandy/ServerCLI.py
   updater/trunk/lib/thandy/SignerCLI.py
   updater/trunk/lib/thandy/formats.py
   updater/trunk/lib/thandy/keys.py
   updater/trunk/lib/thandy/repository.py
   updater/trunk/lib/thandy/util.py
   updater/trunk/setup.py
Log:
In python 2.6 and later, simplejson is built-in as json.

Modified: updater/trunk/lib/thandy/ServerCLI.py
===================================================================
--- updater/trunk/lib/thandy/ServerCLI.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/ServerCLI.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -5,7 +5,10 @@
 import getopt
 import time
 
-import simplejson
+try:
+    import json
+except:
+    import simplejson as json
 
 import thandy.formats
 import thandy.util
@@ -24,7 +27,7 @@
 def snarfObj(fname):
     f = open(fname, 'r')
     try:
-        return simplejson.load(f)
+        return json.load(f)
     finally:
         f.close()
 
@@ -65,7 +68,7 @@
             continue
 
         try:
-            obj = simplejson.loads(content)
+            obj = json.loads(content)
         except ValueError, e:
             print "Couldn't decode %s: %s"%(fn, e)
             continue
@@ -165,7 +168,7 @@
     for k in keydb.iterkeys():
         thandy.formats.sign(signable, k)
 
-    content = simplejson.dumps(signable, sort_keys=True)
+    content = json.dumps(signable, sort_keys=True)
     thandy.util.replaceFile(tsFname, content)
 
 def usage():

Modified: updater/trunk/lib/thandy/SignerCLI.py
===================================================================
--- updater/trunk/lib/thandy/SignerCLI.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/SignerCLI.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -4,7 +4,10 @@
 import getopt
 import sys
 import logging
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 import thandy.keys
 import thandy.formats
@@ -75,7 +78,7 @@
     location = os.path.split(package['location'])[-1]
     print "Writing signed package to %s"%location
     f = open(location, 'w')
-    simplejson.dump(signable, f, indent=1)
+    json.dump(signable, f, indent=1)
     f.close()
 
 def makebundle(args):
@@ -93,7 +96,7 @@
     for pkgFile in args[1:]:
         print "Loading", pkgFile
         f = open(pkgFile, 'r')
-        p = simplejson.load(f)
+        p = json.load(f)
         f.close()
         _, r, _ = thandy.formats.checkSignedObj(p)
         if r != 'package':
@@ -115,7 +118,7 @@
     location = os.path.split(bundleObj['location'])[-1]
     print "Writing signed bundle to %s"%location
     f = open(location, 'w')
-    simplejson.dump(signable, f, indent=1)
+    json.dump(signable, f, indent=1)
     f.close()
 
 # ------------------------------
@@ -143,14 +146,14 @@
 
     print "writing signed keylist to keys.txt"
     thandy.util.replaceFile("keys.txt",
-              simplejson.dumps(signable, indent=1, sort_keys=True),
+              json.dumps(signable, indent=1, sort_keys=True),
               textMode=True)
 
 def signkeylist(args):
     if len(args) != 1:
         usage()
 
-    keylist = simplejson.load(open(args[0], 'r'))
+    keylist = json.load(open(args[0], 'r'))
     thandy.formats.SIGNED_SCHEMA.checkMatch(keylist)
     thandy.formats.KEYLIST_SCHEMA.checkMatch(keylist['signed'])
 
@@ -162,7 +165,7 @@
 
     print "writing signed keylist to keys.txt"
     thandy.util.replaceFile("keys.txt",
-              simplejson.dumps(keylist, indent=1, sort_keys=True),
+              json.dumps(keylist, indent=1, sort_keys=True),
               textMode=True)
 
 def makemirrorlist(args):
@@ -189,7 +192,7 @@
 
     print "writing signed mirrorlist to mirrors.txt"
     thandy.util.replaceFile("mirrors.txt",
-              simplejson.dumps(signable, indent=1, sort_keys=True),
+              json.dumps(signable, indent=1, sort_keys=True),
               textMode=True)
 
 # ------------------------------
@@ -277,7 +280,7 @@
 
     for k in keys:
         data = k.format(private=includeSecret, includeRoles=True)
-        print "Key(", simplejson.dumps(data, indent=2), ")"
+        print "Key(", json.dumps(data, indent=2), ")"
 
 def usage():
     print "Known commands:"

Modified: updater/trunk/lib/thandy/formats.py
===================================================================
--- updater/trunk/lib/thandy/formats.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/formats.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -1,6 +1,10 @@
 # Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
 import time
 import re
 import binascii
@@ -154,7 +158,7 @@
     return SignatureStatus(goodSigs, badSigs, unknownSigs, tangentialSigs)
 
 def _encodeCanonical(obj, outf):
-    # Helper for encodeCanonical.  Older versions of simplejson.encoder don't
+    # Helper for encodeCanonical.  Older versions of json.encoder don't
     # even let us replace the separators.
 
     def canonical_str_encoder(s):

Modified: updater/trunk/lib/thandy/keys.py
===================================================================
--- updater/trunk/lib/thandy/keys.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/keys.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -11,9 +11,13 @@
 import os
 import struct
 import sys
-import simplejson
 import getpass
 
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
 import thandy.formats
 import thandy.util
 
@@ -380,7 +384,7 @@
         if self._encrypted:
             contents = decryptSecret(contents, password)
 
-        listOfKeys = simplejson.loads(contents)
+        listOfKeys = json.loads(contents)
         self._passwd = password # It worked.
         if not listOfKeys.has_key('keys'):
             listOfKeys['keys'] = []
@@ -409,7 +413,7 @@
                        [ key.format(private=True, includeRoles=True) for key in
                          self._keys.values() ]
                        }
-        contents = simplejson.dumps(listOfKeys)
+        contents = json.dumps(listOfKeys)
         if self._encrypted:
             contents = encryptSecret(contents, password)
         thandy.util.replaceFile(self._fname, contents)

Modified: updater/trunk/lib/thandy/repository.py
===================================================================
--- updater/trunk/lib/thandy/repository.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/repository.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -3,7 +3,11 @@
 import thandy.formats
 import thandy.util
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
 import logging
 import os
 import threading
@@ -70,7 +74,7 @@
     def _checkContent(self, content):
 
         try:
-            obj = simplejson.loads(content)
+            obj = json.loads(content)
         except ValueError, e:
             raise thandy.FormatException("Couldn't decode content: %s"%e)
 

Modified: updater/trunk/lib/thandy/util.py
===================================================================
--- updater/trunk/lib/thandy/util.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/lib/thandy/util.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -4,7 +4,10 @@
 import sys
 import tempfile
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 import thandy.formats
 import thandy.keys
@@ -60,7 +63,7 @@
     if keys_fname and os.path.exists(keys_fname):
         f = open(keys_fname, 'r')
         try:
-            obj = simplejson.load(f)
+            obj = json.load(f)
         finally:
             f.close()
         ss, role, path = thandy.formats.checkSignedObj(obj, keydb)

Modified: updater/trunk/setup.py
===================================================================
--- updater/trunk/setup.py	2008-10-15 21:12:51 UTC (rev 17111)
+++ updater/trunk/setup.py	2008-10-15 21:27:16 UTC (rev 17112)
@@ -11,12 +11,19 @@
 # System: 0==alpha, 50==beta, 98=pre, 99==release candidate, 100==release
 VERSION_INFO = (0,0,1)
 
-for name in [ "simplejson", "Crypto" ]:
+try:
+    import Crypto
+except ImportError:
+    print "Missing support for module Crypto"
+    sys.exit(1)
+
+try:
+    import json
+except ImportError:
     try:
-        __import__(name)
+        import simplejson
     except ImportError:
-        print "Missing support for module %s"%name
-        sys.exit(1)
+        print "Missing support for module simplejson"
 
 import os, re, shutil, string, struct, sys
 



More information about the tor-commits mailing list