| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import argparse | 5 import argparse |
| 6 import collections | 6 import collections |
| 7 import json | 7 import json |
| 8 import os | 8 import os |
| 9 import sys | |
| 10 import urlparse | 9 import urlparse |
| 11 | 10 |
| 12 from infra.libs import git2 | 11 from infra.libs import git2 |
| 13 from infra.libs import infra_types | |
| 14 from infra.libs import logs | 12 from infra.libs import logs |
| 15 from infra.libs.service_utils import outer_loop | 13 from infra.libs.service_utils import outer_loop |
| 16 | 14 |
| 17 from infra.services.gnumbd import gnumbd | 15 from infra.services.gsubtreed import gsubtreed |
| 18 | 16 |
| 19 | 17 |
| 20 # Return value of parse_args. | 18 # Return value of parse_args. |
| 21 Options = collections.namedtuple('Options', 'repo loop_opts json_output') | 19 Options = collections.namedtuple('Options', 'repo loop_opts json_output') |
| 22 | 20 |
| 23 | 21 |
| 24 def parse_args(args): # pragma: no cover | 22 def parse_args(args): # pragma: no cover |
| 25 def check_url(s): | 23 def check_url(s): |
| 26 parsed = urlparse.urlparse(s) | 24 parsed = urlparse.urlparse(s) |
| 27 if parsed.scheme not in ('https', 'git', 'file'): | 25 if parsed.scheme not in ('https', 'git', 'file'): |
| 28 raise argparse.ArgumentTypeError( | 26 raise argparse.ArgumentTypeError( |
| 29 'Repo URL must use https, git or file protocol.') | 27 'Repo URL must use https, git or file protocol.') |
| 30 if not parsed.path.strip('/'): | 28 if not parsed.path.strip('/'): |
| 31 raise argparse.ArgumentTypeError('URL is missing a path?') | 29 raise argparse.ArgumentTypeError('URL is missing a path?') |
| 32 return git2.Repo(s) | 30 return git2.Repo(s) |
| 33 | 31 |
| 34 parser = argparse.ArgumentParser('python -m %s' % __package__) | 32 parser = argparse.ArgumentParser('./run.py %s' % __package__) |
| 35 parser.add_argument('--dry_run', action='store_true', | 33 parser.add_argument('--dry_run', action='store_true', |
| 36 help='Do not actually push anything.') | 34 help='Do not actually push anything.') |
| 37 parser.add_argument('--repo_dir', metavar='DIR', default='gnumbd_repos', | 35 parser.add_argument('--repo_dir', metavar='DIR', default='gsubtreed_repos', |
| 38 help=('The directory to use for git clones ' | 36 help=('The directory to use for git clones ' |
| 39 '(default: %(default)s)')) | 37 '(default: %(default)s)')) |
| 40 parser.add_argument('--json_output', metavar='PATH', | 38 parser.add_argument('--json_output', metavar='PATH', |
| 41 help='Path to write JSON with results of the run to') | 39 help='Path to write JSON with results of the run to') |
| 42 parser.add_argument('repo', nargs=1, help='The url of the repo to act on.', | 40 parser.add_argument('repo', nargs=1, help='The url of the repo to act on.', |
| 43 type=check_url) | 41 type=check_url) |
| 44 logs.add_argparse_options(parser) | 42 logs.add_argparse_options(parser) |
| 45 outer_loop.add_argparse_options(parser) | 43 outer_loop.add_argparse_options(parser) |
| 46 | 44 |
| 47 opts = parser.parse_args(args) | 45 opts = parser.parse_args(args) |
| 48 | 46 |
| 49 logs.process_argparse_options(opts) | 47 logs.process_argparse_options(opts) |
| 50 loop_opts = outer_loop.process_argparse_options(opts) | 48 loop_opts = outer_loop.process_argparse_options(opts) |
| 51 | 49 |
| 52 repo = opts.repo[0] | 50 repo = opts.repo[0] |
| 53 repo.dry_run = opts.dry_run | 51 repo.dry_run = opts.dry_run |
| 54 repo.repos_dir = os.path.abspath(opts.repo_dir) | 52 repo.repos_dir = os.path.abspath(opts.repo_dir) |
| 55 | 53 |
| 56 return Options(repo, loop_opts, opts.json_output) | 54 return Options(repo, loop_opts, opts.json_output) |
| 57 | 55 |
| 58 | 56 |
| 59 def main(args): # pragma: no cover | 57 def main(args): # pragma: no cover |
| 60 opts = parse_args(args) | 58 opts = parse_args(args) |
| 61 cref = gnumbd.GnumbdConfigRef(opts.repo) | 59 cref = gsubtreed.GsubtreedConfigRef(opts.repo) |
| 62 opts.repo.reify() | 60 opts.repo.reify() |
| 63 | 61 |
| 64 all_commits = [] | 62 summary = collections.defaultdict(int) |
| 65 def outer_loop_iteration(): | 63 def outer_loop_iteration(): |
| 66 success, commits = gnumbd.inner_loop(opts.repo, cref) | 64 success, paths_counts = gsubtreed.inner_loop(opts.repo, cref) |
| 67 all_commits.extend(commits) | 65 for path, count in paths_counts.iteritems(): |
| 66 summary[path] += count |
| 68 return success | 67 return success |
| 69 | 68 |
| 70 # TODO(iannucci): sleep_timeout should be an exponential backon/off. | |
| 71 # Whenever we push, we should decrease the interval at 'backon_rate' | |
| 72 # until we hit 'min_interval'. | |
| 73 # Whenever we fail/NOP, we should back off at 'backoff_rate' until we | |
| 74 # hit 'max_interval'. | |
| 75 # | |
| 76 # When all is going well, this should be looping at < 1 sec. If things | |
| 77 # start going sideways, we should automatically back off. | |
| 78 loop_results = outer_loop.loop( | 69 loop_results = outer_loop.loop( |
| 79 task=outer_loop_iteration, | 70 task=outer_loop_iteration, |
| 80 sleep_timeout=lambda: cref['interval'], | 71 sleep_timeout=lambda: cref['interval'], |
| 81 **opts.loop_opts) | 72 **opts.loop_opts) |
| 82 | 73 |
| 83 if opts.json_output: | 74 if opts.json_output: |
| 84 with open(opts.json_output, 'w') as f: | 75 with open(opts.json_output, 'w') as f: |
| 85 json.dump({ | 76 json.dump({ |
| 86 'error_count': loop_results.error_count, | 77 'error_count': loop_results.error_count, |
| 87 'synthesized_commits': [ | 78 'summary': summary, |
| 88 { | |
| 89 'commit': c.hsh, | |
| 90 'footers': infra_types.thaw(c.data.footers), | |
| 91 } for c in all_commits | |
| 92 ], | |
| 93 }, f) | 79 }, f) |
| 94 | 80 |
| 95 return 0 if loop_results.success else 1 | 81 return 0 if loop_results.success else 1 |
| 96 | |
| 97 | |
| 98 if __name__ == '__main__': | |
| 99 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |