Back to index

enigmail  1.4.3
writemozinfo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # This script is run during configure, taking variables set in configure
00004 # and producing a JSON file that describes some portions of the build
00005 # configuration, such as the target OS and CPU.
00006 #
00007 # The output file is intended to be used as input to the mozinfo package.
00008 from __future__ import with_statement
00009 import os, re, sys
00010 
00011 def build_dict(env=os.environ):
00012     """
00013     Build a dict containing data about the build configuration from
00014     the environment.
00015     """
00016     d = {}
00017     # Check that all required variables are present first.
00018     required = ["TARGET_CPU", "OS_TARGET", "MOZ_WIDGET_TOOLKIT"]
00019     missing = [r for r in required if r not in env]
00020     if missing:
00021         raise Exception("Missing required environment variables: %s" %
00022                         ', '.join(missing))
00023     # os
00024     o = env["OS_TARGET"]
00025     known_os = {"Linux": "linux",
00026                 "WINNT": "win",
00027                 "Darwin": "mac",
00028                 "Android": "android"}
00029     if o in known_os:
00030         d["os"] = known_os[o]
00031     else:
00032         # Allow unknown values, just lowercase them.
00033         d["os"] = o.lower()
00034 
00035     # Widget toolkit, just pass the value directly through.
00036     d["toolkit"] = env["MOZ_WIDGET_TOOLKIT"]
00037     
00038     # processor
00039     p = env["TARGET_CPU"]
00040     # for universal mac builds, put in a special value
00041     if d["os"] == "mac" and "UNIVERSAL_BINARY" in env and env["UNIVERSAL_BINARY"] == "1":
00042         p = "universal-x86-x86_64"
00043     else:
00044         # do some slight massaging for some values
00045         #TODO: retain specific values in case someone wants them?
00046         if p.startswith("arm"):
00047             p = "arm"
00048         elif re.match("i[3-9]86", p):
00049             p = "x86"
00050     d["processor"] = p
00051     # hardcoded list of 64-bit CPUs
00052     if p in ["x86_64", "ppc64"]:
00053         d["bits"] = 64
00054     # hardcoded list of known 32-bit CPUs
00055     elif p in ["x86", "arm", "ppc"]:
00056         d["bits"] = 32
00057     # other CPUs will wind up with unknown bits
00058 
00059     # debug
00060     d["debug"] = 'MOZ_DEBUG' in env and env['MOZ_DEBUG'] == '1'
00061 
00062     # crashreporter
00063     d["crashreporter"] = 'MOZ_CRASHREPORTER' in env and env['MOZ_CRASHREPORTER'] == '1'
00064     return d
00065 
00066 #TODO: replace this with the json module when Python >= 2.6 is a requirement.
00067 class JsonValue:
00068     """
00069     A class to serialize Python values into JSON-compatible representations.
00070     """
00071     def __init__(self, v):
00072         if v is not None and not (isinstance(v,str) or isinstance(v,bool) or isinstance(v,int)):
00073             raise Exception("Unhandled data type: %s" % type(v))
00074         self.v = v
00075     def __repr__(self):
00076         if self.v is None:
00077             return "null"
00078         if isinstance(self.v,bool):
00079             return str(self.v).lower()
00080         return repr(self.v)
00081 
00082 def jsonify(d):
00083     """
00084     Return a JSON string of the dict |d|. Only handles a subset of Python
00085     value types: bool, str, int, None.
00086     """
00087     jd = {}
00088     for k, v in d.iteritems():
00089         jd[k] = JsonValue(v)
00090     return repr(jd)
00091 
00092 def write_json(file, env=os.environ):
00093     """
00094     Write JSON data about the configuration specified in |env|
00095     to |file|, which may be a filename or file-like object.
00096     See build_dict for information about what  environment variables are used,
00097     and what keys are produced.
00098     """
00099     s = jsonify(build_dict(env))
00100     if isinstance(file, basestring):
00101         with open(file, "w") as f:
00102             f.write(s)
00103     else:
00104         file.write(s)
00105 
00106 if __name__ == '__main__':
00107     try:
00108         write_json(sys.argv[1] if len(sys.argv) > 1 else sys.stdout)
00109     except Exception, e:
00110         print >>sys.stderr, str(e)
00111         sys.exit(1)