Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / tools / lint / perfdocs / gatherer.py
blob118678ab6141d3bee2189984e670b4cdc0ce5546
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/.
4 import os
5 import pathlib
7 from perfdocs.framework_gatherers import (
8 AwsyGatherer,
9 MozperftestGatherer,
10 RaptorGatherer,
11 StaticGatherer,
12 TalosGatherer,
14 from perfdocs.logger import PerfDocLogger
15 from perfdocs.utils import read_yaml
17 logger = PerfDocLogger()
19 # TODO: Implement decorator/searcher to find the classes.
20 frameworks = {
21 "raptor": RaptorGatherer,
22 "mozperftest": MozperftestGatherer,
23 "talos": TalosGatherer,
24 "awsy": AwsyGatherer,
27 # List of file types allowed to be used as static files
28 ALLOWED_STATIC_FILETYPES = ("rst", "png")
31 class Gatherer(object):
32 """
33 Gatherer produces the tree of the perfdoc's entries found
34 and can obtain manifest-based test lists. Used by the Verifier.
35 """
37 def __init__(self, workspace_dir, taskgraph=None):
38 """
39 Initialzie the Gatherer.
41 :param str workspace_dir: Path to the gecko checkout.
42 """
43 self.workspace_dir = workspace_dir
44 self.taskgraph = taskgraph
45 self._perfdocs_tree = []
46 self._test_list = []
47 self.framework_gatherers = {}
49 @property
50 def perfdocs_tree(self):
51 """
52 Returns the perfdocs_tree, and computes it
53 if it doesn't exist.
55 :return dict: The perfdocs tree containing all
56 framework perfdoc entries. See `fetch_perfdocs_tree`
57 for information on the data structure.
58 """
59 if self._perfdocs_tree:
60 return self._perfdocs_tree
61 else:
62 self.fetch_perfdocs_tree()
63 return self._perfdocs_tree
65 def fetch_perfdocs_tree(self):
66 """
67 Creates the perfdocs tree with the following structure:
70 "path": Path to the perfdocs directory.
71 "yml": Name of the configuration YAML file.
72 "rst": Name of the RST file.
73 "static": Name of the static file.
74 }, ...
77 This method doesn't return anything. The result can be found in
78 the perfdocs_tree attribute.
79 """
80 exclude_dir = [
81 str(pathlib.Path(self.workspace_dir, ".hg")),
82 str(pathlib.Path("tools", "lint")),
83 str(pathlib.Path("testing", "perfdocs")),
86 for path in pathlib.Path(self.workspace_dir).rglob("perfdocs"):
87 if any(d in str(path.resolve()) for d in exclude_dir):
88 continue
89 files = [f for f in os.listdir(path)]
91 # Metrics are optional so it's only added to the matched if we
92 # find the `metrics.rst` file in the perfdocs folder
93 matched = {"path": str(path), "yml": "", "rst": "", "static": []}
95 for file in files:
96 # Add the yml/rst/static file to its key if re finds the searched file
97 if file == "config.yml" or file == "config.yaml":
98 matched["yml"] = file
99 elif file == "index.rst":
100 matched["rst"] = file
101 elif file == "metrics.rst":
102 matched["metrics"] = file
103 elif file.split(".")[-1] in ALLOWED_STATIC_FILETYPES:
104 matched["static"].append(file)
106 # Append to structdocs if all the searched files were found
107 if all(val for val in matched.values() if type(val) is not list):
108 self._perfdocs_tree.append(matched)
110 logger.log(
111 "Found {} perfdocs directories in {}".format(
112 len(self._perfdocs_tree),
113 [d["path"] for d in self._perfdocs_tree],
117 def get_test_list(self, sdt_entry):
119 Use a perfdocs_tree entry to find the test list for
120 the framework that was found.
122 :return: A framework info dictionary with fields: {
123 'yml_path': Path to YAML,
124 'yml_content': Content of YAML,
125 'name': Name of framework,
126 'test_list': Test list found for the framework
130 # If it was computed before, return it
131 yaml_path = pathlib.Path(sdt_entry["path"], sdt_entry["yml"])
132 for entry in self._test_list:
133 if entry["yml_path"] == yaml_path:
134 return entry
136 # Set up framework entry with meta data
137 yaml_content = read_yaml(yaml_path)
138 framework = {
139 "yml_content": yaml_content,
140 "yml_path": yaml_path,
141 "name": yaml_content["name"],
142 "test_list": {},
145 if yaml_content["static-only"]:
146 framework_gatherer_cls = StaticGatherer
147 else:
148 framework_gatherer_cls = frameworks[framework["name"]]
150 # Get and then store the frameworks tests
151 framework_gatherer = self.framework_gatherers[framework["name"]] = (
152 framework_gatherer_cls(
153 framework["yml_path"], self.workspace_dir, self.taskgraph
157 if not yaml_content["static-only"]:
158 framework["test_list"] = framework_gatherer.get_test_list()
160 self._test_list.append(framework)
161 return framework