-
-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathclassify_changed_files.py
73 lines (58 loc) · 1.84 KB
/
classify_changed_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations
import enum
import fnmatch
import sys
from collections import defaultdict
# This script may be run in CI before Pants is bootstrapped, so it must be kept simple
# and runnable without `./pants run`.
class Affected(enum.Enum):
docs = "docs"
rust = "rust"
release = "release"
ci_config = "ci_config"
other = "other"
_docs_globs = [
"*.md",
"**/*.md",
"docs/*",
]
_rust_globs = [
"src/rust/engine/*",
"build-support/bin/rust/*",
]
_release_globs = [
# Any changes to these files should trigger wheel building. Notes too, as they are included in
# the wheel.
"pants.toml",
"src/python/pants/VERSION",
"src/python/pants/init/BUILD",
"src/python/pants/notes/*",
"src/python/pants_release/release.py",
]
_ci_config_globs = [
"build-support/bin/classify_changed_files.py",
"src/python/pants_release/generate_github_workflows.py",
]
_affected_to_globs = {
Affected.docs: _docs_globs,
Affected.rust: _rust_globs,
Affected.release: _release_globs,
Affected.ci_config: _ci_config_globs,
}
def classify(changed_files: list[str]) -> set[Affected]:
classified: dict[Affected, set[str]] = defaultdict(set)
for affected, globs in _affected_to_globs.items():
for pattern in globs:
classified[affected].update(fnmatch.filter(changed_files, pattern))
ret = {k for k, v in classified.items() if v}
if set(changed_files) - set().union(*classified.values()):
ret.add(Affected.other)
return ret
def main() -> None:
affecteds = classify(sys.stdin.read().splitlines())
for affected in sorted([a.name for a in affecteds]):
print(affected)
if __name__ == "__main__":
main()