1# -*- coding: utf-8 -*- 2# Copyright 2019 The ChromiumOS Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Module to get the settings from experiment file.""" 7 8 9from cros_utils import logger 10from cros_utils import misc 11from download_images import ImageDownloader 12 13 14class Settings(object): 15 """Class representing settings (a set of fields) from an experiment file.""" 16 17 def __init__(self, name, settings_type): 18 self.name = name 19 self.settings_type = settings_type 20 self.fields = {} 21 self.parent = None 22 23 def SetParentSettings(self, settings): 24 """Set the parent settings which these settings can inherit from.""" 25 self.parent = settings 26 27 def AddField(self, field): 28 name = field.name 29 if name in self.fields: 30 raise SyntaxError("Field %s defined previously." % name) 31 self.fields[name] = field 32 33 def SetField(self, name, value, append=False): 34 if name not in self.fields: 35 raise SyntaxError( 36 "'%s' is not a valid field in '%s' settings" 37 % (name, self.settings_type) 38 ) 39 if append: 40 self.fields[name].Append(value) 41 else: 42 self.fields[name].Set(value) 43 44 def GetField(self, name): 45 """Get the value of a field with a given name.""" 46 if name not in self.fields: 47 raise SyntaxError( 48 "Field '%s' not a valid field in '%s' settings." 49 % (name, self.name) 50 ) 51 field = self.fields[name] 52 if not field.assigned and field.required: 53 raise SyntaxError( 54 "Required field '%s' not defined in '%s' settings." 55 % (name, self.name) 56 ) 57 return self.fields[name].Get() 58 59 def Inherit(self): 60 """Inherit any unset values from the parent settings.""" 61 for name in self.fields: 62 if ( 63 not self.fields[name].assigned 64 and self.parent 65 and name in self.parent.fields 66 and self.parent.fields[name].assigned 67 ): 68 self.fields[name].Set(self.parent.GetField(name), parse=False) 69 70 def Override(self, settings): 71 """Override settings with settings from a different object.""" 72 for name in settings.fields: 73 if name in self.fields and settings.fields[name].assigned: 74 self.fields[name].Set(settings.GetField(name), parse=False) 75 76 def Validate(self): 77 """Check that all required fields have been set.""" 78 for name in self.fields: 79 if not self.fields[name].assigned and self.fields[name].required: 80 raise SyntaxError("Field %s is invalid." % name) 81 82 def GetXbuddyPath( 83 self, 84 path_str, 85 autotest_path, 86 debug_path, 87 board, 88 chromeos_root, 89 log_level, 90 download_debug, 91 ): 92 prefix = "remote" 93 l = logger.GetLogger() 94 if ( 95 path_str.find("trybot") < 0 96 and path_str.find("toolchain") < 0 97 and path_str.find(board) < 0 98 and path_str.find(board.replace("_", "-")) 99 ): 100 xbuddy_path = "%s/%s/%s" % (prefix, board, path_str) 101 else: 102 xbuddy_path = "%s/%s" % (prefix, path_str) 103 image_downloader = ImageDownloader(l, log_level) 104 # Returns three variables: image, autotest_path, debug_path 105 return image_downloader.Run( 106 misc.CanonicalizePath(chromeos_root), 107 xbuddy_path, 108 autotest_path, 109 debug_path, 110 download_debug, 111 ) 112