|
1 | | -import os |
2 | | -import re |
3 | 1 | import xml.etree.ElementTree as Xml |
4 | | -from collections import defaultdict |
5 | | -from pathlib import Path |
6 | | -from typing import Dict, List |
7 | 2 |
|
8 | 3 | from buildbot.process.buildstep import BuildStepFailed, BuildStep, ShellMixin |
9 | | -from buildbot.process.results import SUCCESS, FAILURE, WARNINGS |
10 | 4 | from buildbot.steps.worker import CompositeStepMixin |
11 | 5 | from twisted.internet import defer |
12 | 6 |
|
13 | | -__all__ = ["CleanOldFiles", "CTest", "SetPropertiesFromCMakeCache"] |
14 | | - |
15 | | - |
16 | | -class SetPropertiesFromCMakeCache(CompositeStepMixin, BuildStep): |
17 | | - name = "set-properties-from-cmake-cache" |
18 | | - |
19 | | - renderables = ["props"] |
20 | | - |
21 | | - # Parsing with regex is safe because the CMakeCache.txt format |
22 | | - # hasn't changed since 2006, according to `git blame`. Caveat: |
23 | | - # they have backwards compatibility code for parsing entries with |
24 | | - # quoted names and missing types. We don't bother with that here. |
25 | | - _cache_re = re.compile( |
26 | | - r""" |
27 | | - ^(?!//|\#) # Ignore comment lines. |
28 | | - ([^:=]+?) # Get the variable name, |
29 | | - (-ADVANCED)? # which might be marked as advanced, |
30 | | - :([^=]*) # and will have a type. |
31 | | - =(.*)$ # The value extends through the end of the line. |
32 | | - """, |
33 | | - re.VERBOSE, |
34 | | - ) |
35 | | - |
36 | | - def __init__(self, *, props=None, normalize_bools=False, expand_lists=False, **kwargs): |
37 | | - super().__init__(**kwargs) |
38 | | - self.props = props or [] |
39 | | - self.normalize_bools = normalize_bools |
40 | | - self.expand_lists = expand_lists |
41 | | - |
42 | | - @defer.inlineCallbacks |
43 | | - def run(self): |
44 | | - if not self.props: |
45 | | - return SUCCESS |
46 | | - |
47 | | - log = yield self.addLog("props") |
48 | | - |
49 | | - cache = yield self.getFileContentFromWorker(f"{self.workdir}/CMakeCache.txt", abandonOnFailure=True) |
50 | | - cache = self._parse_cache(cache) |
51 | | - |
52 | | - to_find = set(self.props) |
53 | | - found = to_find & cache.keys() |
54 | | - not_found = to_find - cache.keys() |
55 | | - |
56 | | - for key in found: |
57 | | - log.addStdout(f"{key}={cache[key]}\n") |
58 | | - self.setProperty(key, cache[key], "CMakeCache") |
59 | | - |
60 | | - for key in not_found: |
61 | | - log.addStderr(f"Cache entry not found: {key}\n") |
62 | | - self.setProperty(key, "", "CMakeCache") |
63 | | - |
64 | | - yield log.finish() |
65 | | - return WARNINGS if not_found else SUCCESS |
66 | | - |
67 | | - def _parse_cache(self, cache: str): |
68 | | - result = {} |
69 | | - for entry in cache.splitlines(): |
70 | | - match = self._cache_re.match(entry) |
71 | | - if match: |
72 | | - key, is_advanced, ty, value = match.groups() |
73 | | - if ty == "BOOL" and self.normalize_bools: |
74 | | - value = self._normalize_bools(value) |
75 | | - if self.expand_lists: |
76 | | - value = self._expand_lists(value) |
77 | | - result[key] = value |
78 | | - return result |
79 | | - |
80 | | - @staticmethod |
81 | | - def _expand_lists(value: str): |
82 | | - if ";" in value: |
83 | | - return value.split(";") |
84 | | - return value |
85 | | - |
86 | | - @staticmethod |
87 | | - def _normalize_bools(value: str): |
88 | | - value = value.upper().strip() |
89 | | - if value.endswith("-NOTFOUND"): |
90 | | - return "0" |
91 | | - if value in {"1", "ON", "YES", "TRUE", "Y"}: |
92 | | - return "1" |
93 | | - if value in {"0", "OFF", "NO", "FALSE", "N", "IGNORE", "NOTFOUND"}: |
94 | | - return "0" |
95 | | - raise ValueError(f'Invalid CMake bool "{value}"') |
96 | | - |
97 | | - |
98 | | -class CleanOldFiles(BuildStep): |
99 | | - name = "clean-old" |
100 | | - |
101 | | - def __init__(self, *, groupfn, workdir, keep=1, **kwargs): |
102 | | - super().__init__(**kwargs) |
103 | | - self.groupfn = groupfn |
104 | | - self.workdir = workdir |
105 | | - self.keep = keep |
106 | | - |
107 | | - @defer.inlineCallbacks |
108 | | - def run(self): |
109 | | - stdio = yield self.addLog("stdio") |
110 | | - status = SUCCESS |
111 | | - |
112 | | - # Group files in workdir together using the supplied function. |
113 | | - groups: Dict[str, List[Path]] = defaultdict(list) |
114 | | - for entry in Path(self.workdir).iterdir(): |
115 | | - gid = self.groupfn(entry) |
116 | | - if gid: |
117 | | - groups[gid].append(entry) |
118 | | - |
119 | | - # Delete all but the newest self.keep files with the same key. |
120 | | - for group in groups.values(): |
121 | | - group.sort(key=os.path.getmtime, reverse=True) |
122 | | - for file in group[self.keep :]: |
123 | | - try: |
124 | | - file.unlink() |
125 | | - stdio.addStdout(f"Removed: {file.resolve()}\n") |
126 | | - except (FileNotFoundError, OSError) as e: |
127 | | - stdio.addStderr(f"Could not delete {file.resolve()}: {e}\n") |
128 | | - status = FAILURE |
129 | | - |
130 | | - yield stdio.finish() |
131 | | - return status |
| 7 | +__all__ = ["CTest"] |
132 | 8 |
|
133 | 9 |
|
134 | 10 | class CTest(ShellMixin, CompositeStepMixin, BuildStep): |
|
0 commit comments