-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-sync.py
More file actions
executable file
·203 lines (158 loc) · 6.17 KB
/
post-sync.py
File metadata and controls
executable file
·203 lines (158 loc) · 6.17 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
# Copyright 2026 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Repo post-sync hook dispatcher.
This script acts as an entry point for repo post-sync hooks. It reads a
configuration file from the manifest repository to discover and execute
registered post-sync hooks.
"""
import argparse
from pathlib import Path
import sys
from typing import List, Optional
# Assert some minimum Python versions as we don't test or support any others.
if sys.version_info < (3, 6):
print("repohooks: error: Python-3.6+ is required", file=sys.stderr)
sys.exit(1)
THIS_FILE = Path(__file__).resolve()
THIS_DIR = THIS_FILE.parent
sys.path.insert(0, str(THIS_DIR.parent))
# We have to import our local modules after the sys.path tweak. We can't use
# relative imports because this is an executable program, not a module.
# pylint: disable=wrong-import-position
import rh.config # isort: skip
import rh.git # isort: skip
import rh.hooks # isort: skip
import rh.terminal # isort: skip
import rh.utils # isort: skip
class PostSyncPlaceholders(rh.hooks.Placeholders):
"""Placeholders for post-sync hooks."""
def __init__(self, repo_root: Path, sync_duration: Optional[int] = None):
"""Initialize.
Args:
repo_root: The top level of the repo checkout.
sync_duration: The total time taken by the sync operation.
"""
super().__init__()
self._repo_root = repo_root
self._sync_duration = sync_duration
@property
def var_REPO_ROOT(self) -> str:
"""The absolute path of the root of the repo checkout."""
return str(self._repo_root)
@property
def var_REPO_OUTER_ROOT(self) -> str:
"""The absolute path of the outermost root of the repo checkout."""
return str(self._repo_root)
@property
def var_REPO_SYNC_DURATION(self) -> str:
"""The total time taken by the sync operation.
Validation of this value is deferred to the hook scripts.
"""
return (
str(self._sync_duration) if self._sync_duration is not None else ""
)
def _run_post_sync_hooks(
repo_root_path: Path, sync_duration_seconds: Optional[int]
) -> int:
"""Run the registered post-sync hooks."""
config_file = repo_root_path / ".repo" / "manifests" / "GLOBAL-POSTSYNC.cfg"
if not config_file.exists():
return 0
try:
settings = rh.config.PostSyncSettings(str(config_file))
except rh.config.ValidationError as e:
print(f"error: invalid config: {e}", file=sys.stderr)
return 1
if not settings.custom_hooks:
return 0
# Prepare environment for the subprocess calls.
extra_env = {
"REPO_ROOT": str(repo_root_path),
}
if sync_duration_seconds is not None:
extra_env["REPO_HOOK_SYNC_DURATION_SECONDS"] = str(
sync_duration_seconds
)
exit_code = 0
placeholders = PostSyncPlaceholders(repo_root_path, sync_duration_seconds)
color = rh.terminal.Color()
for name in settings.custom_hooks:
cmd = settings.custom_hook(name)
if not cmd:
continue
# Expand placeholders in the command arguments.
cmd = placeholders.expand_vars(cmd)
# Resolve the hook path relative to the repo root if it is not absolute.
hook_path = Path(cmd[0])
if not hook_path.is_absolute():
hook_path = repo_root_path / hook_path
if not hook_path.exists():
return 1
# Replace the first element with the resolved path.
cmd[0] = str(hook_path.resolve())
# Print running status.
status_line = f"[{color.color(color.YELLOW, 'RUNNING')}] {name}"
rh.terminal.print_status_line(status_line)
# Execute the hook as a subprocess.
result = rh.utils.run(
cmd, cwd=repo_root_path, extra_env=extra_env, check=False
)
if result.returncode:
exit_code = result.returncode
status_line = f"[{color.color(color.RED, 'FAILED')}] {name}"
rh.terminal.print_status_line(status_line, print_newline=True)
else:
status_line = f"[{color.color(color.GREEN, 'PASSED')}] {name}"
rh.terminal.print_status_line(status_line, print_newline=True)
return exit_code
def main(repo_topdir=None, **kwargs) -> int:
"""Main function invoked directly by repo.
We must use the name "main" as that is what repo requires.
Args:
repo_topdir: The absolute path to the top-level directory of the repo
workspace.
kwargs: Leave this here for forward-compatibility.
"""
if not repo_topdir:
try:
repo_root = rh.git.find_repo_root()
except Exception as e: # pylint: disable=broad-exception-caught
print(f"error: {e}", file=sys.stderr)
return 1
else:
repo_root = repo_topdir
sync_duration_seconds = kwargs.get("sync_duration_seconds")
return _run_post_sync_hooks(Path(repo_root), sync_duration_seconds)
def direct_main(argv: List[str]) -> int:
"""Run hooks directly (outside of the context of repo).
Args:
argv: The command line args to process.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--repo-root", help="The top level of the repo checkout."
)
parser.add_argument(
"--sync-duration-seconds",
default="",
help="The total time taken by the sync operation.",
)
opts = parser.parse_args(argv)
return main(
repo_topdir=opts.repo_root,
sync_duration_seconds=opts.sync_duration_seconds,
)
if __name__ == "__main__":
sys.exit(direct_main(sys.argv[1:]))