Debian Codegraph @

infra/bots/find_tasks.py

raw
1 #!/usr/bin/env python
2 #
3 # Copyright 2024 Google LLC
4 #
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8
9 '''Simple tool for finding tasks in tasks.json which match search terms.
10
11 Example usage:
12
13 Find tasks with dimension "os:Mac-14.5"
14 $ find_tasks.py ^os:Mac-14.5$
15
16 Find tasks with "ANGLE" in dimensions or name:
17 $ find_tasks.py ANGLE
18
19 Find tasks with dimension "os:Mac-14.5" and "ANGLE" in dimensions or name:
20 $ find_tasks.py ^os:Mac-14.5$ ANGLE
21 '''
22
23
24 import json
25 import os
26 import re
27 import sys
28
29
30 # match_dimensions returns true iff the given search term matches at least one
31 # of the task's dimensions.
32 def match_dimensions(term, task):
33 for dim in task['dimensions']:
34 if re.search(term, dim):
35 return True
36 return False
37
38
39 # match_name returns true iff the given search term matches the task's name.
40 def match_name(term, name):
41 return re.search(term, name)
42
43
44 # match_task returns true iff all search terms match some part of the task.
45 def match_task(terms, name, task):
46 for term in terms:
47 if not (match_name(term, name)
48 or match_dimensions(term, task)):
49 return False
50 return True
51
52
53 def main(terms):
54 dir = os.path.dirname(os.path.realpath(__file__))
55 tasks_json = os.path.join(dir, 'tasks.json')
56 with open(tasks_json) as f:
57 taskCfg = json.load(f)
58 for name, task in taskCfg['tasks'].items():
59 if match_task(terms, name, task):
60 print(name)
61
62
63 if __name__ == '__main__':
64 main(sys.argv[1:])