Home | History | Annotate | Download | only in bots
      1 # Copyright 2017 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Update and upload markdown files using the output of fiddlecli."""
      6 
      7 
      8 import argparse
      9 import os
     10 import subprocess
     11 import sys
     12 
     13 import git_utils
     14 
     15 
     16 SKIA_COMMITTER_EMAIL = 'update-docs (at] skia.org'
     17 SKIA_COMMITTER_NAME = 'Update Docs'
     18 SKIA_REPO = 'https://skia.googlesource.com/skia.git'
     19 COMMIT_MSG = '''Update markdown files
     20 
     21 Automatic commit by the Housekeeper-Nightly-Bookmaker bot.
     22 
     23 TBR=%s
     24 NO_MERGE_BUILDS
     25 ''' % SKIA_COMMITTER_EMAIL
     26 CC_LIST = ['rmistry (at] google.com', 'caryclark (at] google.com']
     27 
     28 
     29 def main():
     30   parser = argparse.ArgumentParser()
     31   parser.add_argument("--gitcookies")
     32   parser.add_argument("--bookmaker_binary")
     33   parser.add_argument("--fiddlecli_output")
     34   args = parser.parse_args()
     35 
     36   with git_utils.NewGitCheckout(repository=SKIA_REPO):
     37     config_dict = {
     38         'user.name': SKIA_COMMITTER_NAME,
     39         'user.email': SKIA_COMMITTER_EMAIL,
     40         'http.cookiefile': args.gitcookies,
     41     }
     42     # Skip GCE Auth in depot_tools/gerrit_utils.py. Use gitcookies instead.
     43     os.environ['SKIP_GCE_AUTH_FOR_GIT'] = 'True'
     44     os.environ['GIT_COOKIES_PATH'] = args.gitcookies
     45 
     46     with git_utils.GitLocalConfig(config_dict):
     47       with git_utils.GitBranch(branch_name='update_md_files',
     48                                commit_msg=COMMIT_MSG,
     49                                commit_queue=True,
     50                                upload=False,
     51                                cc_list=CC_LIST) as git_branch:
     52         # Run bookmaker binary.
     53         cmd = [args.bookmaker_binary,
     54                '-b', 'docs',
     55                '-f', args.fiddlecli_output,
     56                '-r', 'site/user/api',
     57                ]
     58         try:
     59           subprocess.check_call(cmd)
     60         except subprocess.CalledProcessError as e:
     61           print >> sys.stderr, (
     62               'Running %s failed, not uploading markdowns update:\n\n%s' % (
     63                   cmd, e.output))
     64           sys.exit(1)
     65 
     66           # Verify that only files in the expected directory are going to be
     67           # committed and uploaded.
     68         diff_files = subprocess.check_output(['git', 'diff', '--name-only'])
     69         for diff_file in diff_files.split():
     70           if not diff_file.startswith('site/user/api/'):
     71             print >> sys.stderr, (
     72               'Some files in %s were not in the site/user/api dir. '
     73               'Not uploading them' % diff_files)
     74             sys.exit(1)
     75         if diff_files:
     76           subprocess.check_call(['git', 'add', '-u'])
     77           git_branch.commit_and_upload(True)
     78         else:
     79           print 'No changes so nothing to upload.'
     80 
     81 
     82 if '__main__' == __name__:
     83   main()
     84