1# Copyright 2021 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 6from recipe_engine import recipe_api 7import calendar 8 9DM_JSON = 'dm.json' 10 11class GoldUploadApi(recipe_api.RecipeApi): 12 def upload(self): 13 """Attempt to upload files to Gold. 14 This module assumes setup has occurred for the vars and flavor modules. 15 """ 16 revision = self.m.properties['revision'] 17 results_dir = self.m.flavor.host_dirs.dm_dir 18 19 # Upload the images. It is preferred that the images are uploaded first 20 # so they exist whenever the json is processed. 21 image_dest_path = 'gs://%s/dm-images-v1' % self.m.properties['gs_bucket'] 22 for ext in ['.png']: 23 files_to_upload = self.m.file.glob_paths( 24 'find %s images' % ext, 25 results_dir, 26 '*%s' % ext, 27 test_data=['someimage.png']) 28 # For some reason, glob returns results_dir when it should return nothing. 29 files_to_upload = [f for f in files_to_upload if str(f).endswith(ext)] 30 if len(files_to_upload) > 0: 31 self.m.gsutil.cp('%s images' % ext, results_dir.join('*%s' % ext), 32 image_dest_path, multithread=True) 33 34 summary_dest_path = 'gs://%s' % self.m.properties['gs_bucket'] 35 ref = revision 36 # Trybot results are siloed by issue/patchset. 37 if self.m.vars.is_trybot: 38 summary_dest_path = '/'.join([summary_dest_path, 'trybot']) 39 ref = '%s_%s' % (str(self.m.vars.issue), str(self.m.vars.patchset)) 40 41 # Compute the directory to upload results to 42 now = self.m.time.utcnow() 43 summary_dest_path = '/'.join([ 44 summary_dest_path, 45 'dm-json-v1', 46 str(now.year ).zfill(4), 47 str(now.month).zfill(2), 48 str(now.day ).zfill(2), 49 str(now.hour ).zfill(2), 50 ref, 51 self.m.vars.builder_name, 52 str(int(calendar.timegm(now.utctimetuple())))]) 53 54 # Directly upload dm.json if it exists. 55 json_file = results_dir.join(DM_JSON) 56 # -Z compresses the json file at rest with gzip. 57 self.m.gsutil.cp('dm.json', json_file, 58 summary_dest_path + '/' + DM_JSON, extra_args=['-Z']) 59 60