#!/usr/bin/env python
#
# Copyright 2017 Google Inc. All rights reserved.
#
# 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.

"""Sync the noto repos to the given tags.

This helps prepare for generating website data.  We have the option
of requiring that the noto-fonts, noto-emoji, and noto-cjk repos are at
tagged releases.  This tool lets you specify release names, ensures the
release names are valid, and checks out those releases.  Main exits with
error code 100 if there is a failure."""

import argparse
import sys

from nototools import tool_utils

_REPOS = 'fonts emoji cjk'.split()
_REPO_PATHS = [tool_utils.resolve_path('[%s]' % r) for r in _REPOS]

def noto_check_clean():
  errors = []
  for r, p in zip(_REPOS, _REPO_PATHS):
    if not tool_utils.git_is_clean(p):
      errors.append(r)

  if errors:
    print >> sys.stderr, '%s %s not clean' % (
        ' '.join(errors), 'is' if len(errors) == 1 else 'are')
    return False
  return True


def noto_checkout_master():
  """Check out the noto repos at master.  Return True if ok, else log
  error and return False."""

  if not noto_check_clean():
    return False

  for p in _REPO_PATHS:
    tool_utils.git_checkout(p, 'master')
  return True


def noto_checkout(
    fonts_tag='latest', emoji_tag='latest', cjk_tag='latest', verbose=False):
  """Check out the noto repos at the provided tags.  Return True if ok,
  else log error and return False.  Default is 'latest' for the latest
  tag."""

  if not noto_check_clean():
    return False

  requested_tags = [fonts_tag, emoji_tag, cjk_tag]
  failed_tags = []
  resolved_tags = []
  for r, p, t in zip(_REPOS, _REPO_PATHS, requested_tags):
    found = False
    tag_info = tool_utils.git_tags(p)
    for _, tag, _ in tag_info:
      if t == 'latest' or tag == t:
        resolved_tags.append(tag)
        found = True
        break
    if not found:
      failed_tags.append('%s: %s' % (r, t))

  if failed_tags:
    print >> sys.stderr, 'failed to find:\n  %s' % '\n  '.join(failed_tags)
    return False

  for p, t in zip(_REPO_PATHS, resolved_tags):
    tool_utils.git_checkout(p, t)

  if verbose:
    print 'checked out:\n  %s' % '\n  '.join(
        '%s: %s' % (r, t) for r, t in zip(_REPOS, resolved_tags))

  return True


def main():
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '-f', '--fonts_tag', help='tag for noto fonts repo (default latest)',
      metavar='tag', default='latest')
  parser.add_argument(
      '-e', '--emoji_tag', help='tag for noto emoji repo (default latest)',
      metavar='tag', default='latest')
  parser.add_argument(
      '-c', '--cjk_tag', help='tag for noto cjk repo (default latest)',
      metavar='tag', default='latest')
  parser.add_argument(
      '-m', '--master', help='use master branch for all repos',
      action='store_true')
  parser.add_argument(
      '-v', '--verbose', help='report tags chosen on success',
      action='store_true')

  args = parser.parse_args()

  if args.master:
    result = noto_checkout_master()
  else:
    result = noto_checkout(
        fonts_tag=args.fonts_tag, emoji_tag=args.emoji_tag,
        cjk_tag=args.cjk_tag, verbose=args.verbose)
  sys.exit(0 if result else 100)


if __name__ == '__main__':
  main()

@@ -1472,16 +1472,29 @@
 _master(
+dry_run=False
 ):%0A  %22%22%22
@@ -1631,24 +1631,44 @@
 turn False%0A%0A
+  if not dry_run:%0A  
   for p in _
@@ -1679,16 +1679,18 @@
 _PATHS:%0A
+  
     tool
@@ -1722,16 +1722,95 @@
 aster')%0A
+  else:%0A    print 'would have checked out master in %25s' %25 (', '.join(_REPOS))%0A%0A
   return
@@ -1907,24 +1907,43 @@
 erbose=False
+,%0A    dry_run=False
 ):%0A  %22%22%22Chec
@@ -2651,16 +2651,36 @@
  False%0A%0A
+  if not dry_run:%0A  
   for p,
@@ -2714,24 +2714,26 @@
 lved_tags):%0A
+  
     tool_uti
@@ -2767,16 +2767,27 @@
  verbose
+ or dry_run
 :%0A    pr
@@ -2791,16 +2791,18 @@
  print '
+%25s
 checked 
@@ -2807,32 +2807,84 @@
 d out:%5Cn  %25s' %25 
+(%0A        'would have ' if dry_run else '',%0A        
 '%5Cn  '.join(%0A   
@@ -2879,25 +2879,16 @@
  '.join(
-%0A        
 '%25s: %25s'
@@ -2936,16 +2936,17 @@
 d_tags))
+)
 %0A%0A  retu
@@ -3640,16 +3640,143 @@
 e_true')
+%0A  parser.add_argument(%0A      '-n', '--dry_run', help='report tags chosen but take no other action',%0A      action='store_true')
 %0A%0A  args
@@ -3851,16 +3851,28 @@
 _master(
+args.dry_run
 )%0A  else
@@ -4011,16 +4011,38 @@
 .verbose
+, dry_run=args.dry_run
 )%0A  sys.

Add dry_run option to sync_repos.