hooks.py 19 KB

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