Bug 1687064 [wpt PR 27207] - Update wpt metadata, a=testonly
[gecko.git] / build / variables.py
blobef2d6e6800f3c5a09585f52990c827c91c28e652
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(
29 command, stderr=stderr, universal_newlines=True
31 except Exception:
32 return ""
35 def get_hg_info(workdir):
36 repo = get_program_output("hg", "-R", workdir, "path", "default")
37 if repo:
38 repo = repo.strip()
39 if repo.startswith("ssh://"):
40 repo = "https://" + repo[6:]
41 repo = repo.rstrip("/")
43 changeset = get_hg_changeset(workdir)
45 return repo, changeset
48 def get_hg_changeset(path):
49 return get_program_output("hg", "-R", path, "parent", "--template={node}")
52 def get_info_from_sourcestamp(sourcestamp_path):
53 """Read the repository and changelog information from the sourcestamp
54 file. This assumes that the file exists and returns the results as a list
55 (either strings or None in case of error).
56 """
58 # Load the content of the file.
59 lines = None
60 with open(sourcestamp_path) as f:
61 lines = f.read().splitlines()
63 # Parse the repo and the changeset. The sourcestamp file is supposed to
64 # contain two lines: the first is the build id and the second is the source
65 # URL.
66 if len(lines) != 2 or not lines[1].startswith("http"):
67 # Just return if the file doesn't contain what we expect.
68 return None, None
70 # Return the repo and the changeset.
71 return lines[1].split("/rev/")
74 def source_repo_header(output):
75 # We allow the source repo and changeset to be specified via the
76 # environment (see configure)
77 import buildconfig
79 repo = buildconfig.substs.get("MOZ_SOURCE_REPO")
80 changeset = buildconfig.substs.get("MOZ_SOURCE_CHANGESET")
81 source = ""
83 if not repo:
84 sourcestamp_path = os.path.join(buildconfig.topsrcdir, SOURCESTAMP_FILENAME)
85 if os.path.exists(os.path.join(buildconfig.topsrcdir, ".hg")):
86 repo, changeset = get_hg_info(buildconfig.topsrcdir)
87 elif os.path.exists(sourcestamp_path):
88 repo, changeset = get_info_from_sourcestamp(sourcestamp_path)
89 elif not changeset:
90 changeset = get_hg_changeset(buildconfig.topsrcdir)
91 if not changeset:
92 raise Exception(
93 "could not resolve changeset; " "try setting MOZ_SOURCE_CHANGESET"
96 if changeset:
97 output.write("#define MOZ_SOURCE_STAMP %s\n" % changeset)
99 if repo and buildconfig.substs.get("MOZ_INCLUDE_SOURCE_INFO"):
100 source = "%s/rev/%s" % (repo, changeset)
101 output.write("#define MOZ_SOURCE_REPO %s\n" % repo)
102 output.write("#define MOZ_SOURCE_URL %s\n" % source)
105 def main(args):
106 if len(args):
107 func = globals().get(args[0])
108 if func:
109 return func(sys.stdout, *args[1:])
112 if __name__ == "__main__":
113 sys.exit(main(sys.argv[1:]))