Bug 1560374 - Set testharness and reftest web-platform-tests to Tier-1; r=jmaher...
[gecko.git] / build / variables.py
blob86dfe508295edf5acf4ef431e7d9a6cf4245aa69
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__ import print_function, unicode_literals
7 import os
8 import subprocess
9 import sys
10 from datetime import datetime
12 SOURCESTAMP_FILENAME = 'sourcestamp.txt'
15 def buildid_header(output):
16 buildid = os.environ.get('MOZ_BUILD_DATE')
17 if buildid and len(buildid) != 14:
18 print('Ignoring invalid MOZ_BUILD_DATE: %s' % buildid, file=sys.stderr)
19 buildid = None
20 if not buildid:
21 buildid = datetime.now().strftime('%Y%m%d%H%M%S')
22 output.write("#define MOZ_BUILDID %s\n" % buildid)
25 def get_program_output(*command):
26 try:
27 with open(os.devnull) as stderr:
28 return subprocess.check_output(command, stderr=stderr)
29 except Exception:
30 return ''
33 def get_hg_info(workdir):
34 repo = get_program_output('hg', '-R', workdir, 'path', 'default')
35 if repo:
36 repo = repo.strip()
37 if repo.startswith('ssh://'):
38 repo = 'https://' + repo[6:]
39 repo = repo.rstrip('/')
41 changeset = get_hg_changeset(workdir)
43 return repo, changeset
46 def get_hg_changeset(path):
47 return get_program_output('hg', '-R', path, 'parent', '--template={node}')
50 def get_info_from_sourcestamp(sourcestamp_path):
51 """Read the repository and changelog information from the sourcestamp
52 file. This assumes that the file exists and returns the results as a list
53 (either strings or None in case of error).
54 """
56 # Load the content of the file.
57 lines = None
58 with open(sourcestamp_path) as f:
59 lines = f.read().splitlines()
61 # Parse the repo and the changeset. The sourcestamp file is supposed to
62 # contain two lines: the first is the build id and the second is the source
63 # URL.
64 if len(lines) != 2 or not lines[1].startswith('http'):
65 # Just return if the file doesn't contain what we expect.
66 return None, None
68 # Return the repo and the changeset.
69 return lines[1].split('/rev/')
72 def source_repo_header(output):
73 # We allow the source repo and changeset to be specified via the
74 # environment (see configure)
75 import buildconfig
76 repo = buildconfig.substs.get('MOZ_SOURCE_REPO')
77 changeset = buildconfig.substs.get('MOZ_SOURCE_CHANGESET')
78 source = ''
80 if not repo:
81 sourcestamp_path = os.path.join(
82 buildconfig.topsrcdir, SOURCESTAMP_FILENAME)
83 if os.path.exists(os.path.join(buildconfig.topsrcdir, '.hg')):
84 repo, changeset = get_hg_info(buildconfig.topsrcdir)
85 elif os.path.exists(sourcestamp_path):
86 repo, changeset = get_info_from_sourcestamp(sourcestamp_path)
87 elif not changeset:
88 changeset = get_hg_changeset(buildconfig.topsrcdir)
89 if not changeset:
90 raise Exception('could not resolve changeset; '
91 'try setting MOZ_SOURCE_CHANGESET')
93 if changeset:
94 output.write('#define MOZ_SOURCE_STAMP %s\n' % changeset)
96 if repo and buildconfig.substs.get('MOZ_INCLUDE_SOURCE_INFO'):
97 source = '%s/rev/%s' % (repo, changeset)
98 output.write('#define MOZ_SOURCE_REPO %s\n' % repo)
99 output.write('#define MOZ_SOURCE_URL %s\n' % source)
102 def main(args):
103 if (len(args)):
104 func = globals().get(args[0])
105 if func:
106 return func(sys.stdout, *args[1:])
109 if __name__ == '__main__':
110 sys.exit(main(sys.argv[1:]))