blob: f805de6b29a89dcdd59e748cec2d7969ed7cca7d [file] [log] [blame]
Remy Bohmer16c13282020-09-10 10:38:04 +02001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020015import errno
Remy Bohmer16c13282020-09-10 10:38:04 +020016import json
17import os
18import re
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020019import subprocess
Remy Bohmer16c13282020-09-10 10:38:04 +020020import sys
21import traceback
22
23from error import HookError
24from git_refs import HEAD
25
26from pyversion import is_python3
27if is_python3():
28 import urllib.parse
29else:
30 import imp
31 import urlparse
32 urllib = imp.new_module('urllib')
33 urllib.parse = urlparse
34 input = raw_input # noqa: F821
35
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020036
Remy Bohmer16c13282020-09-10 10:38:04 +020037class RepoHook(object):
38 """A RepoHook contains information about a script to run as a hook.
39
40 Hooks are used to run a python script before running an upload (for instance,
41 to run presubmit checks). Eventually, we may have hooks for other actions.
42
43 This shouldn't be confused with files in the 'repo/hooks' directory. Those
44 files are copied into each '.git/hooks' folder for each project. Repo-level
45 hooks are associated instead with repo actions.
46
47 Hooks are always python. When a hook is run, we will load the hook into the
48 interpreter and execute its main() function.
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020049
50 Combinations of hook option flags:
51 - no-verify=False, verify=False (DEFAULT):
52 If stdout is a tty, can prompt about running hooks if needed.
53 If user denies running hooks, the action is cancelled. If stdout is
54 not a tty and we would need to prompt about hooks, action is
55 cancelled.
56 - no-verify=False, verify=True:
57 Always run hooks with no prompt.
58 - no-verify=True, verify=False:
59 Never run hooks, but run action anyway (AKA bypass hooks).
60 - no-verify=True, verify=True:
61 Invalid
Remy Bohmer16c13282020-09-10 10:38:04 +020062 """
63
64 def __init__(self,
65 hook_type,
66 hooks_project,
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020067 repo_topdir,
Remy Bohmer16c13282020-09-10 10:38:04 +020068 manifest_url,
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020069 bypass_hooks=False,
70 allow_all_hooks=False,
71 ignore_hooks=False,
Remy Bohmer16c13282020-09-10 10:38:04 +020072 abort_if_user_denies=False):
73 """RepoHook constructor.
74
75 Params:
76 hook_type: A string representing the type of hook. This is also used
77 to figure out the name of the file containing the hook. For
78 example: 'pre-upload'.
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020079 hooks_project: The project containing the repo hooks.
80 If you have a manifest, this is manifest.repo_hooks_project.
81 OK if this is None, which will make the hook a no-op.
82 repo_topdir: The top directory of the repo client checkout.
83 This is the one containing the .repo directory. Scripts will
84 run with CWD as this directory.
85 If you have a manifest, this is manifest.topdir.
Remy Bohmer16c13282020-09-10 10:38:04 +020086 manifest_url: The URL to the manifest git repo.
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020087 bypass_hooks: If True, then 'Do not run the hook'.
88 allow_all_hooks: If True, then 'Run the hook without prompting'.
89 ignore_hooks: If True, then 'Do not abort action if hooks fail'.
90 abort_if_user_denies: If True, we'll abort running the hook if the user
Remy Bohmer16c13282020-09-10 10:38:04 +020091 doesn't allow us to run the hook.
92 """
93 self._hook_type = hook_type
94 self._hooks_project = hooks_project
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020095 self._repo_topdir = repo_topdir
Remy Bohmer16c13282020-09-10 10:38:04 +020096 self._manifest_url = manifest_url
Remy Bohmer7f7acfe2020-08-01 18:36:44 +020097 self._bypass_hooks = bypass_hooks
98 self._allow_all_hooks = allow_all_hooks
99 self._ignore_hooks = ignore_hooks
Remy Bohmer16c13282020-09-10 10:38:04 +0200100 self._abort_if_user_denies = abort_if_user_denies
101
102 # Store the full path to the script for convenience.
103 if self._hooks_project:
104 self._script_fullpath = os.path.join(self._hooks_project.worktree,
105 self._hook_type + '.py')
106 else:
107 self._script_fullpath = None
108
109 def _GetHash(self):
110 """Return a hash of the contents of the hooks directory.
111
112 We'll just use git to do this. This hash has the property that if anything
113 changes in the directory we will return a different has.
114
115 SECURITY CONSIDERATION:
116 This hash only represents the contents of files in the hook directory, not
117 any other files imported or called by hooks. Changes to imported files
118 can change the script behavior without affecting the hash.
119
120 Returns:
121 A string representing the hash. This will always be ASCII so that it can
122 be printed to the user easily.
123 """
124 assert self._hooks_project, "Must have hooks to calculate their hash."
125
126 # We will use the work_git object rather than just calling GetRevisionId().
127 # That gives us a hash of the latest checked in version of the files that
128 # the user will actually be executing. Specifically, GetRevisionId()
129 # doesn't appear to change even if a user checks out a different version
130 # of the hooks repo (via git checkout) nor if a user commits their own revs.
131 #
132 # NOTE: Local (non-committed) changes will not be factored into this hash.
133 # I think this is OK, since we're really only worried about warning the user
134 # about upstream changes.
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200135 return self._hooks_project.work_git.rev_parse(HEAD)
Remy Bohmer16c13282020-09-10 10:38:04 +0200136
137 def _GetMustVerb(self):
138 """Return 'must' if the hook is required; 'should' if not."""
139 if self._abort_if_user_denies:
140 return 'must'
141 else:
142 return 'should'
143
144 def _CheckForHookApproval(self):
145 """Check to see whether this hook has been approved.
146
147 We'll accept approval of manifest URLs if they're using secure transports.
148 This way the user can say they trust the manifest hoster. For insecure
149 hosts, we fall back to checking the hash of the hooks repo.
150
151 Note that we ask permission for each individual hook even though we use
152 the hash of all hooks when detecting changes. We'd like the user to be
153 able to approve / deny each hook individually. We only use the hash of all
154 hooks because there is no other easy way to detect changes to local imports.
155
156 Returns:
157 True if this hook is approved to run; False otherwise.
158
159 Raises:
160 HookError: Raised if the user doesn't approve and abort_if_user_denies
161 was passed to the consturctor.
162 """
163 if self._ManifestUrlHasSecureScheme():
164 return self._CheckForHookApprovalManifest()
165 else:
166 return self._CheckForHookApprovalHash()
167
168 def _CheckForHookApprovalHelper(self, subkey, new_val, main_prompt,
169 changed_prompt):
170 """Check for approval for a particular attribute and hook.
171
172 Args:
173 subkey: The git config key under [repo.hooks.<hook_type>] to store the
174 last approved string.
175 new_val: The new value to compare against the last approved one.
176 main_prompt: Message to display to the user to ask for approval.
177 changed_prompt: Message explaining why we're re-asking for approval.
178
179 Returns:
180 True if this hook is approved to run; False otherwise.
181
182 Raises:
183 HookError: Raised if the user doesn't approve and abort_if_user_denies
184 was passed to the consturctor.
185 """
186 hooks_config = self._hooks_project.config
187 git_approval_key = 'repo.hooks.%s.%s' % (self._hook_type, subkey)
188
189 # Get the last value that the user approved for this hook; may be None.
190 old_val = hooks_config.GetString(git_approval_key)
191
192 if old_val is not None:
193 # User previously approved hook and asked not to be prompted again.
194 if new_val == old_val:
195 # Approval matched. We're done.
196 return True
197 else:
198 # Give the user a reason why we're prompting, since they last told
199 # us to "never ask again".
200 prompt = 'WARNING: %s\n\n' % (changed_prompt,)
201 else:
202 prompt = ''
203
204 # Prompt the user if we're not on a tty; on a tty we'll assume "no".
205 if sys.stdout.isatty():
206 prompt += main_prompt + ' (yes/always/NO)? '
207 response = input(prompt).lower()
208 print()
209
210 # User is doing a one-time approval.
211 if response in ('y', 'yes'):
212 return True
213 elif response == 'always':
214 hooks_config.SetString(git_approval_key, new_val)
215 return True
216
217 # For anything else, we'll assume no approval.
218 if self._abort_if_user_denies:
219 raise HookError('You must allow the %s hook or use --no-verify.' %
220 self._hook_type)
221
222 return False
223
224 def _ManifestUrlHasSecureScheme(self):
225 """Check if the URI for the manifest is a secure transport."""
226 secure_schemes = ('file', 'https', 'ssh', 'persistent-https', 'sso', 'rpc')
227 parse_results = urllib.parse.urlparse(self._manifest_url)
228 return parse_results.scheme in secure_schemes
229
230 def _CheckForHookApprovalManifest(self):
231 """Check whether the user has approved this manifest host.
232
233 Returns:
234 True if this hook is approved to run; False otherwise.
235 """
236 return self._CheckForHookApprovalHelper(
237 'approvedmanifest',
238 self._manifest_url,
239 'Run hook scripts from %s' % (self._manifest_url,),
240 'Manifest URL has changed since %s was allowed.' % (self._hook_type,))
241
242 def _CheckForHookApprovalHash(self):
243 """Check whether the user has approved the hooks repo.
244
245 Returns:
246 True if this hook is approved to run; False otherwise.
247 """
248 prompt = ('Repo %s run the script:\n'
249 ' %s\n'
250 '\n'
251 'Do you want to allow this script to run')
252 return self._CheckForHookApprovalHelper(
253 'approvedhash',
254 self._GetHash(),
255 prompt % (self._GetMustVerb(), self._script_fullpath),
256 'Scripts have changed since %s was allowed.' % (self._hook_type,))
257
258 @staticmethod
259 def _ExtractInterpFromShebang(data):
260 """Extract the interpreter used in the shebang.
261
262 Try to locate the interpreter the script is using (ignoring `env`).
263
264 Args:
265 data: The file content of the script.
266
267 Returns:
268 The basename of the main script interpreter, or None if a shebang is not
269 used or could not be parsed out.
270 """
271 firstline = data.splitlines()[:1]
272 if not firstline:
273 return None
274
275 # The format here can be tricky.
276 shebang = firstline[0].strip()
277 m = re.match(r'^#!\s*([^\s]+)(?:\s+([^\s]+))?', shebang)
278 if not m:
279 return None
280
281 # If the using `env`, find the target program.
282 interp = m.group(1)
283 if os.path.basename(interp) == 'env':
284 interp = m.group(2)
285
286 return interp
287
288 def _ExecuteHookViaReexec(self, interp, context, **kwargs):
289 """Execute the hook script through |interp|.
290
291 Note: Support for this feature should be dropped ~Jun 2021.
292
293 Args:
294 interp: The Python program to run.
295 context: Basic Python context to execute the hook inside.
296 kwargs: Arbitrary arguments to pass to the hook script.
297
298 Raises:
299 HookError: When the hooks failed for any reason.
300 """
301 # This logic needs to be kept in sync with _ExecuteHookViaImport below.
302 script = """
303import json, os, sys
304path = '''%(path)s'''
305kwargs = json.loads('''%(kwargs)s''')
306context = json.loads('''%(context)s''')
307sys.path.insert(0, os.path.dirname(path))
308data = open(path).read()
309exec(compile(data, path, 'exec'), context)
310context['main'](**kwargs)
311""" % {
312 'path': self._script_fullpath,
313 'kwargs': json.dumps(kwargs),
314 'context': json.dumps(context),
315 }
316
317 # We pass the script via stdin to avoid OS argv limits. It also makes
318 # unhandled exception tracebacks less verbose/confusing for users.
319 cmd = [interp, '-c', 'import sys; exec(sys.stdin.read())']
320 proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
321 proc.communicate(input=script.encode('utf-8'))
322 if proc.returncode:
323 raise HookError('Failed to run %s hook.' % (self._hook_type,))
324
325 def _ExecuteHookViaImport(self, data, context, **kwargs):
326 """Execute the hook code in |data| directly.
327
328 Args:
329 data: The code of the hook to execute.
330 context: Basic Python context to execute the hook inside.
331 kwargs: Arbitrary arguments to pass to the hook script.
332
333 Raises:
334 HookError: When the hooks failed for any reason.
335 """
336 # Exec, storing global context in the context dict. We catch exceptions
337 # and convert to a HookError w/ just the failing traceback.
338 try:
339 exec(compile(data, self._script_fullpath, 'exec'), context)
340 except Exception:
341 raise HookError('%s\nFailed to import %s hook; see traceback above.' %
342 (traceback.format_exc(), self._hook_type))
343
344 # Running the script should have defined a main() function.
345 if 'main' not in context:
346 raise HookError('Missing main() in: "%s"' % self._script_fullpath)
347
348 # Call the main function in the hook. If the hook should cause the
349 # build to fail, it will raise an Exception. We'll catch that convert
350 # to a HookError w/ just the failing traceback.
351 try:
352 context['main'](**kwargs)
353 except Exception:
354 raise HookError('%s\nFailed to run main() for %s hook; see traceback '
355 'above.' % (traceback.format_exc(), self._hook_type))
356
357 def _ExecuteHook(self, **kwargs):
358 """Actually execute the given hook.
359
360 This will run the hook's 'main' function in our python interpreter.
361
362 Args:
363 kwargs: Keyword arguments to pass to the hook. These are often specific
364 to the hook type. For instance, pre-upload hooks will contain
365 a project_list.
366 """
367 # Keep sys.path and CWD stashed away so that we can always restore them
368 # upon function exit.
369 orig_path = os.getcwd()
370 orig_syspath = sys.path
371
372 try:
373 # Always run hooks with CWD as topdir.
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200374 os.chdir(self._repo_topdir)
Remy Bohmer16c13282020-09-10 10:38:04 +0200375
376 # Put the hook dir as the first item of sys.path so hooks can do
377 # relative imports. We want to replace the repo dir as [0] so
378 # hooks can't import repo files.
379 sys.path = [os.path.dirname(self._script_fullpath)] + sys.path[1:]
380
381 # Initial global context for the hook to run within.
382 context = {'__file__': self._script_fullpath}
383
384 # Add 'hook_should_take_kwargs' to the arguments to be passed to main.
385 # We don't actually want hooks to define their main with this argument--
386 # it's there to remind them that their hook should always take **kwargs.
387 # For instance, a pre-upload hook should be defined like:
388 # def main(project_list, **kwargs):
389 #
390 # This allows us to later expand the API without breaking old hooks.
391 kwargs = kwargs.copy()
392 kwargs['hook_should_take_kwargs'] = True
393
394 # See what version of python the hook has been written against.
395 data = open(self._script_fullpath).read()
396 interp = self._ExtractInterpFromShebang(data)
397 reexec = False
398 if interp:
399 prog = os.path.basename(interp)
400 if prog.startswith('python2') and sys.version_info.major != 2:
401 reexec = True
402 elif prog.startswith('python3') and sys.version_info.major == 2:
403 reexec = True
404
405 # Attempt to execute the hooks through the requested version of Python.
406 if reexec:
407 try:
408 self._ExecuteHookViaReexec(interp, context, **kwargs)
409 except OSError as e:
410 if e.errno == errno.ENOENT:
411 # We couldn't find the interpreter, so fallback to importing.
412 reexec = False
413 else:
414 raise
415
416 # Run the hook by importing directly.
417 if not reexec:
418 self._ExecuteHookViaImport(data, context, **kwargs)
419 finally:
420 # Restore sys.path and CWD.
421 sys.path = orig_syspath
422 os.chdir(orig_path)
423
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200424 def _CheckHook(self):
425 # Bail with a nice error if we can't find the hook.
426 if not os.path.isfile(self._script_fullpath):
427 raise HookError('Couldn\'t find repo hook: %s' % self._script_fullpath)
428
429 def Run(self, **kwargs):
Remy Bohmer16c13282020-09-10 10:38:04 +0200430 """Run the hook.
431
432 If the hook doesn't exist (because there is no hooks project or because
433 this particular hook is not enabled), this is a no-op.
434
435 Args:
436 user_allows_all_hooks: If True, we will never prompt about running the
437 hook--we'll just assume it's OK to run it.
438 kwargs: Keyword arguments to pass to the hook. These are often specific
439 to the hook type. For instance, pre-upload hooks will contain
440 a project_list.
441
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200442 Returns:
443 True: On success or ignore hooks by user-request
444 False: The hook failed. The caller should respond with aborting the action.
445 Some examples in which False is returned:
446 * Finding the hook failed while it was enabled, or
447 * the user declined to run a required hook (from _CheckForHookApproval)
448 In all these cases the user did not pass the proper arguments to
449 ignore the result through the option combinations as listed in
450 AddHookOptionGroup().
Remy Bohmer16c13282020-09-10 10:38:04 +0200451 """
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200452 # Do not do anything in case bypass_hooks is set, or
453 # no-op if there is no hooks project or if hook is disabled.
454 if (self._bypass_hooks or
455 not self._hooks_project or
456 self._hook_type not in self._hooks_project.enabled_repo_hooks):
457 return True
Remy Bohmer16c13282020-09-10 10:38:04 +0200458
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200459 passed = True
460 try:
461 self._CheckHook()
Remy Bohmer16c13282020-09-10 10:38:04 +0200462
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200463 # Make sure the user is OK with running the hook.
464 if self._allow_all_hooks or self._CheckForHookApproval():
465 # Run the hook with the same version of python we're using.
466 self._ExecuteHook(**kwargs)
467 except SystemExit as e:
468 passed = False
469 print('ERROR: %s hooks exited with exit code: %s' % (self._hook_type, str(e)),
470 file=sys.stderr)
471 except HookError as e:
472 passed = False
473 print('ERROR: %s' % str(e), file=sys.stderr)
Remy Bohmer16c13282020-09-10 10:38:04 +0200474
Remy Bohmer7f7acfe2020-08-01 18:36:44 +0200475 if not passed and self._ignore_hooks:
476 print('\nWARNING: %s hooks failed, but continuing anyways.' % self._hook_type,
477 file=sys.stderr)
478 passed = True
479
480 return passed
481
482 @classmethod
483 def FromSubcmd(cls, manifest, opt, *args, **kwargs):
484 """Method to construct the repo hook class
485
486 Args:
487 manifest: The current active manifest for this command from which we
488 extract a couple of fields.
489 opt: Contains the commandline options for the action of this hook.
490 It should contain the options added by AddHookOptionGroup() in which
491 we are interested in RepoHook execution.
492 """
493 for key in ('bypass_hooks', 'allow_all_hooks', 'ignore_hooks'):
494 kwargs.setdefault(key, getattr(opt, key))
495 kwargs.update({
496 'hooks_project': manifest.repo_hooks_project,
497 'repo_topdir': manifest.topdir,
498 'manifest_url': manifest.manifestProject.GetRemote('origin').url,
499 })
500 return cls(*args, **kwargs)
501
502 @staticmethod
503 def AddOptionGroup(parser, name):
504 """Help options relating to the various hooks."""
505
506 # Note that verify and no-verify are NOT opposites of each other, which
507 # is why they store to different locations. We are using them to match
508 # 'git commit' syntax.
509 group = parser.add_option_group(name + ' hooks')
510 group.add_option('--no-verify',
511 dest='bypass_hooks', action='store_true',
512 help='Do not run the %s hook.' % name)
513 group.add_option('--verify',
514 dest='allow_all_hooks', action='store_true',
515 help='Run the %s hook without prompting.' % name)
516 group.add_option('--ignore-hooks',
517 action='store_true',
518 help='Do not abort if %s hooks fail.' % name)