From 37282b4b9c5b1d9a1ff07f7f0686a81b65a0a5c6 Mon Sep 17 00:00:00 2001 From: Doug Anderson Date: Fri, 4 Mar 2011 11:54:18 -0800 Subject: Support repo-level pre-upload hook and prep for future hooks. All repo-level hooks are expected to live in a single project at the top level of that project. The name of the hooks project is provided in the manifest.xml. The manifest also lists which hooks are enabled to make it obvious if a file somehow failed to sync down (or got deleted). Before running any hook, we will prompt the user to make sure that it is OK. A user can deny running the hook, allow once, or allow "forever" (until hooks change). This tries to keep with the git spirit of not automatically running anything on the user's computer that got synced down. Note that individual repo commands can add always options to avoid these prompts as they see fit (see below for the 'upload' options). When hooks are run, they are loaded into the current interpreter (the one running repo) and their main() function is run. This mechanism is used (instead of using subprocess) to make it easier to expand to a richer hook interface in the future. During loading, the interpreter's sys.path is updated to contain the directory containing the hooks so that hooks can be split into multiple files. The upload command has two options that control hook behavior: - no-verify=False, verify=False (DEFAULT): If stdout is a tty, can prompt about running upload hooks if needed. If user denies running hooks, the upload is cancelled. If stdout is not a tty and we would need to prompt about upload hooks, upload is cancelled. - no-verify=False, verify=True: Always run upload hooks with no prompt. - no-verify=True, verify=False: Never run upload hooks, but upload anyway (AKA bypass hooks). - no-verify=True, verify=True: Invalid Sample bit of manifest.xml code for enabling hooks (assumes you have a project named 'hooks' where hooks are stored): Sample main() function in pre-upload.py in hooks directory: def main(project_list, **kwargs): print ('These projects will be uploaded: %s' % ', '.join(project_list)) print ('I am being a good boy and ignoring anything in kwargs\n' 'that I don\'t understand.') print 'I fail 50% of the time. How flaky.' if random.random() <= .5: raise Exception('Pre-upload hook failed. Have a nice day.') Change-Id: I5cefa2cd5865c72589263cf8e2f152a43c122f70 --- subcmds/upload.py | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'subcmds/upload.py') diff --git a/subcmds/upload.py b/subcmds/upload.py index 20822096..c561b8aa 100644 --- a/subcmds/upload.py +++ b/subcmds/upload.py @@ -19,7 +19,8 @@ import sys from command import InteractiveCommand from editor import Editor -from error import UploadError +from error import HookError, UploadError +from project import RepoHook UNUSUAL_COMMIT_THRESHOLD = 5 @@ -120,6 +121,29 @@ Gerrit Code Review: http://code.google.com/p/gerrit/ type='string', action='append', dest='cc', help='Also send email to these email addresses.') + # Options relating to upload hook. Note that verify and no-verify are NOT + # opposites of each other, which is why they store to different locations. + # We are using them to match 'git commit' syntax. + # + # Combinations: + # - no-verify=False, verify=False (DEFAULT): + # If stdout is a tty, can prompt about running upload hooks if needed. + # If user denies running hooks, the upload is cancelled. If stdout is + # not a tty and we would need to prompt about upload hooks, upload is + # cancelled. + # - no-verify=False, verify=True: + # Always run upload hooks with no prompt. + # - no-verify=True, verify=False: + # Never run upload hooks, but upload anyway (AKA bypass hooks). + # - no-verify=True, verify=True: + # Invalid + p.add_option('--no-verify', + dest='bypass_hooks', action='store_true', + help='Do not run the upload hook.') + p.add_option('--verify', + dest='allow_all_hooks', action='store_true', + help='Run the upload hook without prompting.') + def _SingleBranch(self, opt, branch, people): project = branch.project name = branch.name @@ -313,17 +337,27 @@ Gerrit Code Review: http://code.google.com/p/gerrit/ reviewers = [] cc = [] + for project in project_list: + avail = project.GetUploadableBranches() + if avail: + pending.append((project, avail)) + + if pending and (not opt.bypass_hooks): + hook = RepoHook('pre-upload', self.manifest.repo_hooks_project, + self.manifest.topdir, abort_if_user_denies=True) + pending_proj_names = [project.name for (project, avail) in pending] + try: + hook.Run(opt.allow_all_hooks, project_list=pending_proj_names) + except HookError, e: + print >>sys.stderr, "ERROR: %s" % str(e) + return + if opt.reviewers: reviewers = _SplitEmails(opt.reviewers) if opt.cc: cc = _SplitEmails(opt.cc) people = (reviewers,cc) - for project in project_list: - avail = project.GetUploadableBranches() - if avail: - pending.append((project, avail)) - if not pending: print >>sys.stdout, "no branches ready for upload" elif len(pending) == 1 and len(pending[0][1]) == 1: -- cgit v1.2.3-54-g00ecf