update_run_token.py 4.26 KB
Newer Older
Lachlan's avatar
Lachlan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#!/usr/bin/python3

#
#  Copyright (C) 2018 Codethink Limited
#  Copyright (C) 2018 Bloomberg Finance LP
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#  Authors:
#        Lachlan Mackenzie <lachlan.mackenzie@codethink.co.uk>

import argparse
import os
import token_file_processing
import logging
import ntpath
import tempfile
import glob
import shutil

from distutils.file_util import copy_file

# This command line executable allows the updating of a given run token file
# given a path to a set of results. The token file is processed but not
# not validated (on the basis that this has already had to be done to get to
# this point). The path for the generated results files are checked and the
# most recent file established. The new version of the token file is updated
# and copied over the original
#
# token_file - the path to the token file to be updated
# input_path - the path to the cached results files
#

def main():
   token_file = 'run_token.yml'
   results_files = 'results_cache/'

   parser = argparse.ArgumentParser()
   parser.add_argument("-t", "--token_file",
                       help="The token file that is to be updated.",
                       type=str)
   parser.add_argument("-i", "--input_path",
                       help="Path for generated results",
                       type=str)
   args = parser.parse_args()

   if bool(args.token_file):
      token_file = args.token_file

   if bool(args.input_path):
      results_files = args.input_path

   print(token_file)
   print(results_files)

   if not update_run_token(token_file=token_file, results_files=results_files):
      logging.error('Unable to update token file: ', token_file)
      os.sys.exit(1)

def update_run_token(token_file, results_files):

   # Check if token file exists and try to load
   if os.path.isfile(token_file):
      try:
          token_values=token_file_processing.process_token_file(token_file)
      except OSError as err:
         logging.error("Unable to access token file: ", token_file)
         return False
      except yaml.YAMLError as y_err:
         logging.error("Unable to parse token file: ", token_file)
         return False
   else:
      logging.error("Token file does not exist: ", token_file)
      return False

   # Check if results exist and get the last generated one
   if os.path.isdir(results_files):
      list_of_files = []
      print(results_files)
      for file in os.listdir(results_files):
         if file.endswith('.json'):
            list_of_files.append(os.path.join(results_files, file))
      if list_of_files:
         latest_file = max(list_of_files, key=os.path.getmtime)
         archive_result = ntpath.basename(latest_file)
      else:
         logging.info('No results files found from: ', results_files)
         return False
   else:
      logging.error('Results path does not exist, nothing to update: ', results_files)
      return False

   token_values['build']['result'] = archive_result
   
   buildstream_sha = os.environ.get('BUILDSTREAM_HEAD')
   if not buildstream_sha:
      logging.error('No Buildstream SHA was set')
      return False

   temp_staging_area = tempfile.mkdtemp(prefix='temp_staging_location')
   temp_file = os.path.join(temp_staging_area, 'token_temp.yaml')
   try:
      # Generate the token file.;
      token_file_processing.generate_token_file(temp_file, buildstream_sha, token_values['build']['bs_branch'], archive_result)
      # Copy the temporary token file to replace requested
      copy_file(temp_file, token_file)
   except Exception as err:
      logging.error('Unable to write token file: ', err)
      return False
   finally:
      shutil.rmtree(temp_staging_area)

   return True


if __name__ == "__main__":
   main()