bug 715586: checksums.py should generate sha1 and md5 checksums. r=catlee,ted
[gecko.git] / config / writemozinfo.py
blob79d7ef41e3c5c2bb40d34e908059be2aa31ff255
1 #!/usr/bin/env python
3 # This script is run during configure, taking variables set in configure
4 # and producing a JSON file that describes some portions of the build
5 # configuration, such as the target OS and CPU.
7 # The output file is intended to be used as input to the mozinfo package.
8 from __future__ import with_statement
9 import os, re, sys
11 def build_dict(env=os.environ):
12 """
13 Build a dict containing data about the build configuration from
14 the environment.
15 """
16 d = {}
17 # Check that all required variables are present first.
18 required = ["TARGET_CPU", "OS_TARGET", "MOZ_WIDGET_TOOLKIT"]
19 missing = [r for r in required if r not in env]
20 if missing:
21 raise Exception("Missing required environment variables: %s" %
22 ', '.join(missing))
23 # os
24 o = env["OS_TARGET"]
25 known_os = {"Linux": "linux",
26 "WINNT": "win",
27 "Darwin": "mac",
28 "Android": "android"}
29 if o in known_os:
30 d["os"] = known_os[o]
31 else:
32 # Allow unknown values, just lowercase them.
33 d["os"] = o.lower()
35 # Widget toolkit, just pass the value directly through.
36 d["toolkit"] = env["MOZ_WIDGET_TOOLKIT"]
38 # processor
39 p = env["TARGET_CPU"]
40 # for universal mac builds, put in a special value
41 if d["os"] == "mac" and "UNIVERSAL_BINARY" in env and env["UNIVERSAL_BINARY"] == "1":
42 p = "universal-x86-x86_64"
43 else:
44 # do some slight massaging for some values
45 #TODO: retain specific values in case someone wants them?
46 if p.startswith("arm"):
47 p = "arm"
48 elif re.match("i[3-9]86", p):
49 p = "x86"
50 d["processor"] = p
51 # hardcoded list of 64-bit CPUs
52 if p in ["x86_64", "ppc64"]:
53 d["bits"] = 64
54 # hardcoded list of known 32-bit CPUs
55 elif p in ["x86", "arm", "ppc"]:
56 d["bits"] = 32
57 # other CPUs will wind up with unknown bits
59 # debug
60 d["debug"] = 'MOZ_DEBUG' in env and env['MOZ_DEBUG'] == '1'
62 # crashreporter
63 d["crashreporter"] = 'MOZ_CRASHREPORTER' in env and env['MOZ_CRASHREPORTER'] == '1'
64 return d
66 #TODO: replace this with the json module when Python >= 2.6 is a requirement.
67 class JsonValue:
68 """
69 A class to serialize Python values into JSON-compatible representations.
70 """
71 def __init__(self, v):
72 if v is not None and not (isinstance(v,str) or isinstance(v,bool) or isinstance(v,int)):
73 raise Exception("Unhandled data type: %s" % type(v))
74 self.v = v
75 def __repr__(self):
76 if self.v is None:
77 return "null"
78 if isinstance(self.v,bool):
79 return str(self.v).lower()
80 return repr(self.v)
82 def jsonify(d):
83 """
84 Return a JSON string of the dict |d|. Only handles a subset of Python
85 value types: bool, str, int, None.
86 """
87 jd = {}
88 for k, v in d.iteritems():
89 jd[k] = JsonValue(v)
90 return repr(jd)
92 def write_json(file, env=os.environ):
93 """
94 Write JSON data about the configuration specified in |env|
95 to |file|, which may be a filename or file-like object.
96 See build_dict for information about what environment variables are used,
97 and what keys are produced.
98 """
99 s = jsonify(build_dict(env))
100 if isinstance(file, basestring):
101 with open(file, "w") as f:
102 f.write(s)
103 else:
104 file.write(s)
106 if __name__ == '__main__':
107 try:
108 write_json(sys.argv[1] if len(sys.argv) > 1 else sys.stdout)
109 except Exception, e:
110 print >>sys.stderr, str(e)
111 sys.exit(1)