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/.
13 from six
.moves
import configparser
15 from mach
.util
import get_state_dir
17 from mozlog
.structured
import commandline
19 import manifestdownload
20 from wptrunner
import wptcommandline
25 def do_delayed_imports(wpt_dir
):
28 "localpaths", os
.path
.join(wpt_dir
, "tests", "tools", "localpaths.py")
30 sys
.path
.insert(0, os
.path
.join(wpt_dir
, "tools", "manifest"))
35 p
= argparse
.ArgumentParser()
37 "--rebuild", action
="store_true", help="Rebuild manifest from scratch"
39 download_group
= p
.add_mutually_exclusive_group()
40 download_group
.add_argument(
45 help="Always download even if the local manifest is recent",
47 download_group
.add_argument(
51 help="Don't try to download the manifest",
58 help="Just download the manifest, don't update",
65 help="Path to wptrunner config file",
71 help="Force the local configuration to be regenerated",
76 default
=os
.path
.join(get_state_dir(), "cache", "wpt"),
77 help="Path to use for the metadata cache",
79 commandline
.add_logging_group(p
)
84 def ensure_kwargs(kwargs
):
85 _kwargs
= vars(create_parser().parse_args([]))
86 _kwargs
.update(kwargs
)
90 def run(src_root
, obj_root
, logger
=None, **kwargs
):
91 kwargs
= ensure_kwargs(kwargs
)
94 from wptrunner
import wptlogging
96 logger
= wptlogging
.setup(kwargs
, {"mach": sys
.stdout
})
98 src_wpt_dir
= os
.path
.join(src_root
, "testing", "web-platform")
100 do_delayed_imports(src_wpt_dir
)
102 if not kwargs
["config_path"]:
103 config_path
= generate_config(
107 os
.path
.join(obj_root
, "_tests", "web-platform"),
108 kwargs
["rewrite_config"],
111 config_path
= kwargs
["config_path"]
113 if not os
.path
.exists(config_path
):
114 logger
.critical("Config file %s does not exist" % config_path
)
117 logger
.debug("Using config path %s" % config_path
)
119 test_paths
= wptcommandline
.get_test_paths(wptcommandline
.config
.read(config_path
))
121 for paths
in six
.itervalues(test_paths
):
122 if "manifest_path" not in paths
:
123 paths
["manifest_path"] = os
.path
.join(
124 paths
["metadata_path"], "MANIFEST.json"
127 ensure_manifest_directories(logger
, test_paths
)
129 local_config
= read_local_config(src_wpt_dir
)
130 for section
in ["manifest:upstream", "manifest:mozilla"]:
131 url_base
= local_config
.get(section
, "url_base")
132 manifest_rel_path
= os
.path
.join(
133 local_config
.get(section
, "metadata"), "MANIFEST.json"
135 test_paths
[url_base
]["manifest_rel_path"] = manifest_rel_path
137 if not kwargs
["rebuild"] and kwargs
["download"] is not False:
138 force_download
= False if kwargs
["download"] is None else True
139 manifestdownload
.download_from_taskcluster(
140 logger
, src_root
, test_paths
, force
=force_download
143 logger
.debug("Skipping manifest download")
145 update
= kwargs
["update"] or kwargs
["rebuild"]
146 manifests
= load_and_update(
151 rebuild
=kwargs
["rebuild"],
152 cache_root
=kwargs
["cache_root"],
158 def ensure_manifest_directories(logger
, test_paths
):
159 for paths
in six
.itervalues(test_paths
):
160 manifest_dir
= os
.path
.dirname(paths
["manifest_path"])
161 if not os
.path
.exists(manifest_dir
):
162 logger
.info("Creating directory %s" % manifest_dir
)
163 # Even though we just checked the path doesn't exist, there's a chance
164 # of race condition with another process or thread having created it in
165 # between. This happens during tests.
167 os
.makedirs(manifest_dir
)
169 if e
.errno
!= errno
.EEXIST
:
171 elif not os
.path
.isdir(manifest_dir
):
172 raise IOError("Manifest directory is a file")
175 def read_local_config(wpt_dir
):
176 src_config_path
= os
.path
.join(wpt_dir
, "wptrunner.ini")
178 parser
= configparser
.SafeConfigParser()
179 success
= parser
.read(src_config_path
)
180 assert src_config_path
in success
184 def generate_config(logger
, repo_root
, wpt_dir
, dest_path
, force_rewrite
=False):
185 """Generate the local wptrunner.ini file to use locally"""
186 if not os
.path
.exists(dest_path
):
187 # Even though we just checked the path doesn't exist, there's a chance
188 # of race condition with another process or thread having created it in
189 # between. This happens during tests.
191 os
.makedirs(dest_path
)
193 if e
.errno
!= errno
.EEXIST
:
196 dest_config_path
= os
.path
.join(dest_path
, "wptrunner.local.ini")
198 if not force_rewrite
and os
.path
.exists(dest_config_path
):
199 logger
.debug("Config is up to date, not regenerating")
200 return dest_config_path
202 logger
.info("Creating config file %s" % dest_config_path
)
204 parser
= read_local_config(wpt_dir
)
206 for section
in ["manifest:upstream", "manifest:mozilla"]:
207 meta_rel_path
= parser
.get(section
, "metadata")
208 tests_rel_path
= parser
.get(section
, "tests")
211 section
, "manifest", os
.path
.join(dest_path
, meta_rel_path
, "MANIFEST.json")
213 parser
.set(section
, "metadata", os
.path
.join(wpt_dir
, meta_rel_path
))
214 parser
.set(section
, "tests", os
.path
.join(wpt_dir
, tests_rel_path
))
219 os
.path
.abspath(os
.path
.join(wpt_dir
, parser
.get("paths", "prefs"))),
222 with
open(dest_config_path
, "wt") as config_file
:
223 parser
.write(config_file
)
225 return dest_config_path
238 wptdir_hash
= hashlib
.sha256(os
.path
.abspath(wpt_dir
).encode()).hexdigest()
239 for url_base
, paths
in six
.iteritems(test_paths
):
240 manifest_path
= paths
["manifest_path"]
241 this_cache_root
= os
.path
.join(
242 cache_root
, wptdir_hash
, os
.path
.dirname(paths
["manifest_rel_path"])
244 m
= manifest
.manifest
.load_and_update(
251 cache_root
=this_cache_root
,
253 path_data
= {"url_base": url_base
}
254 path_data
.update(paths
)
260 def log_error(logger
, manifest_path
, msg
):
262 path
=manifest_path
, message
=msg
, lineno
=0, source
="", linter
="wpt-manifest"