Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/util/treeherder.py

raw
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
5 import re
6
7 _JOINED_SYMBOL_RE = re.compile(r"([^(]*)\(([^)]*)\)$")
8
9
10 def split_symbol(treeherder_symbol):
11 """Split a symbol expressed as grp(sym) into its two parts. If no group is
12 given, the returned group is '?'"""
13 groupSymbol = "?"
14 symbol = treeherder_symbol
15 if "(" in symbol:
16 match = _JOINED_SYMBOL_RE.match(symbol)
17 if match:
18 groupSymbol, symbol = match.groups()
19 else:
20 raise Exception(f"`{symbol}` is not a valid treeherder symbol.")
21 return groupSymbol, symbol
22
23
24 def join_symbol(group, symbol):
25 """Perform the reverse of split_symbol, combining the given group and
26 symbol. If the group is '?', then it is omitted."""
27 if group == "?":
28 return symbol
29 return f"{group}({symbol})"
30
31
32 def add_suffix(treeherder_symbol, suffix):
33 """Add a suffix to a treeherder symbol that may contain a group."""
34 group, symbol = split_symbol(treeherder_symbol)
35 symbol += str(suffix)
36 return join_symbol(group, symbol)
37
38
39 def replace_group(treeherder_symbol, new_group):
40 """Add a suffix to a treeherder symbol that may contain a group."""
41 _, symbol = split_symbol(treeherder_symbol)
42 return join_symbol(new_group, symbol)
43
44
45 def inherit_treeherder_from_dep(task, dep_task):
46 """Inherit treeherder defaults from dep_task"""
47 treeherder = task.get("treeherder", {})
48
49 dep_th_platform = (
50 dep_task.task.get("extra", {})
51 .get("treeherder", {})
52 .get("machine", {})
53 .get("platform", "")
54 )
55 dep_th_collection = list(
56 dep_task.task.get("extra", {})
57 .get("treeherder", {})
58 .get("collection", {})
59 .keys()
60 )[0]
61 treeherder.setdefault("platform", f"{dep_th_platform}/{dep_th_collection}")
62 treeherder.setdefault(
63 "tier", dep_task.task.get("extra", {}).get("treeherder", {}).get("tier", 1)
64 )
65 # Does not set symbol
66 treeherder.setdefault("kind", "build")
67 return treeherder
68
69
70 def treeherder_defaults(kind, label):
71 defaults = {
72 # Despite its name, this is expected to be a platform+collection
73 "platform": "default/opt",
74 "tier": 1,
75 }
76 if "build" in kind:
77 defaults["kind"] = "build"
78 elif "test" in kind:
79 defaults["kind"] = "test"
80 else:
81 defaults["kind"] = "other"
82
83 # Takes the uppercased first letter of each part of the kind name, eg:
84 # apple-banana -> AB
85 defaults["symbol"] = "".join([c[0] for c in kind.split("-")]).upper()
86
87 return defaults