control_data.py revision ec1d47d85cc83f30631518d8fbb6406036a3ac39
16f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet# pylint: disable-msg=C0111 299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh# Copyright 2008 Google Inc. Released under the GPL v2 399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 489d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnetteimport warnings 589d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnettewith warnings.catch_warnings(): 689d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnette # The 'compiler' module is gone in Python 3.0. Let's not say 789d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnette # so in every log file. 889d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnette warnings.simplefilter("ignore", DeprecationWarning) 989d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnette import compiler 1089d5d21ec72e55ca0ca9e50d488210e2c5bb34e7J. Richard Barnetteimport logging, textwrap 1199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 12ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shifrom autotest_lib.client.common_lib import enum 1399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 148ad52b6b61905f9f6b05af76e9607cdd29c038d0Scott ZawalskiREQUIRED_VARS = set(['author', 'doc', 'name', 'time', 'test_type']) 154238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard BarnetteOBSOLETE_VARS = set(['experimental']) 1699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 173dd8beb386f7298ffe84d7410d00cce26973e170Aviv KeshetCONTROL_TYPE = enum.Enum('Server', 'Client', start_value=1) 183dd8beb386f7298ffe84d7410d00cce26973e170Aviv KeshetCONTROL_TYPE_NAMES = enum.Enum(*CONTROL_TYPE.names, string_values=True) 193dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet 2099c2c6f54e962fe4e7701cd9aaa645fdabacd203mblighclass ControlVariableException(Exception): 2199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh pass 2299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 2399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 2499c2c6f54e962fe4e7701cd9aaa645fdabacd203mblighclass ControlData(object): 25ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi # Available TIME settings in control file, the list must be in lower case 26ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi # and in ascending order, test running faster comes first. 27ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi TEST_TIME_LIST = ['fast', 'short', 'medium', 'long', 'lengthy'] 28ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi TEST_TIME = enum.Enum(*TEST_TIME_LIST, string_values=False) 29ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi 30ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi @staticmethod 31ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi def get_test_time_index(time): 32ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi """ 33ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi Get the order of estimated test time, based on the TIME setting in 34ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi Control file. Faster test gets a lower index number. 35ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi """ 36ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi try: 37ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi return ControlData.TEST_TIME.get_value(time.lower()) 38ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi except AttributeError: 39ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi # Raise exception if time value is not a valid TIME setting. 40ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi error_msg = '%s is not a valid TIME.' % time 41ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi logging.error(error_msg) 42ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi raise ControlVariableException(error_msg) 43ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi 44ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi 45d7cd983031fc77a247d022aef67b481892681ddcmbligh def __init__(self, vars, path, raise_warnings=False): 4699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh # Defaults 47d7cd983031fc77a247d022aef67b481892681ddcmbligh self.path = path 4899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self.dependencies = set() 494238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette # TODO(jrbarnette): This should be removed once outside 504238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette # code that uses can be changed. 5199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self.experimental = False 5299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self.run_verify = True 5399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self.sync_count = 1 54861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.test_parameters = set() 558ad52b6b61905f9f6b05af76e9607cdd29c038d0Scott Zawalski self.test_category = '' 568ad52b6b61905f9f6b05af76e9607cdd29c038d0Scott Zawalski self.test_class = '' 576f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet self.retries = 0 58e3bc24b7b77480d4bb0b1562bb30ea79eff85626Fang Deng self.job_retries = 0 59ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi # Default to require server-side package. Unless require_ssp is 60ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi # explicitly set to False, server-side package will be used for the 61ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi # job. This can be overridden by global config 62ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi # AUTOSERV/enable_ssp_container 63ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi self.require_ssp = None 6499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 6599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh diff = REQUIRED_VARS - set(vars) 664238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette if diff: 674238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette warning = ('WARNING: Not all required control ' 684238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette 'variables were specified in %s. Please define ' 694238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette '%s.') % (self.path, ', '.join(diff)) 704238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette if raise_warnings: 714238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette raise ControlVariableException(warning) 724238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette print textwrap.wrap(warning, 80) 734238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette 744238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette obsolete = OBSOLETE_VARS & set(vars) 754238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette if obsolete: 764238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette warning = ('WARNING: Obsolete variables were ' 774238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette 'specified in %s. Please remove ' 784238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette '%s.') % (self.path, ', '.join(obsolete)) 7999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if raise_warnings: 8099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise ControlVariableException(warning) 8199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh print textwrap.wrap(warning, 80) 8299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 8399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh for key, val in vars.iteritems(): 8499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh try: 8599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self.set_attr(key, val, raise_warnings) 8699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh except Exception, e: 8799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if raise_warnings: 8899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise 894238478cce8e1c0ddd29748be570abba5b6e9fc6J. Richard Barnette print 'WARNING: %s; skipping' % e 9099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 9199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 9299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_attr(self, attr, val, raise_warnings=False): 9399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh attr = attr.lower() 9499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh try: 9599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh set_fn = getattr(self, 'set_%s' % attr) 9699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh set_fn(val) 9799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh except AttributeError: 9899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh # This must not be a variable we care about 9999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh pass 10099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 10199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 10299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def _set_string(self, attr, val): 10399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = str(val) 10499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh setattr(self, attr, val) 10599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 10699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 10799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def _set_option(self, attr, val, options): 10899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = str(val) 10999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if val.lower() not in [x.lower() for x in options]: 11099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise ValueError("%s must be one of the following " 11199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh "options: %s" % (attr, 11299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh ', '.join(options))) 11399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh setattr(self, attr, val) 11499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 11599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 11699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def _set_bool(self, attr, val): 11799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = str(val).lower() 11899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if val == "false": 11999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = False 12099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh elif val == "true": 12199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = True 12299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh else: 12399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh msg = "%s must be either true or false" % attr 12499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise ValueError(msg) 12599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh setattr(self, attr, val) 12699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 12799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 12899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def _set_int(self, attr, val, min=None, max=None): 12999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = int(val) 13099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if min is not None and min > val: 13199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise ValueError("%s is %d, which is below the " 13299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh "minimum of %d" % (attr, val, min)) 13399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh if max is not None and max < val: 13499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh raise ValueError("%s is %d, which is above the " 13599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh "maximum of %d" % (attr, val, max)) 13699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh setattr(self, attr, val) 13799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 13899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 13999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def _set_set(self, attr, val): 14099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh val = str(val) 14199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh items = [x.strip() for x in val.split(',')] 14299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh setattr(self, attr, set(items)) 14399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 14499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 14599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_author(self, val): 14699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_string('author', val) 14799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 14899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 14999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_dependencies(self, val): 15099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_set('dependencies', val) 15199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 15299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 15399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_doc(self, val): 15499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_string('doc', val) 15599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 15699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 15799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_name(self, val): 15899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_string('name', val) 15999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 16099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 16199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_run_verify(self, val): 16299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_bool('run_verify', val) 16399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 16499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 16599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_sync_count(self, val): 16699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_int('sync_count', val, min=1) 16799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 16899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 1696fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone def set_suite(self, val): 1706fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone self._set_string('suite', val) 1716fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone 1726fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone 17399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_time(self, val): 174ef5b53f9c7fe279a7b89f9e9c731e7c1ea523d92Dan Shi self._set_option('time', val, ControlData.TEST_TIME_LIST) 17599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 17699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 17799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_test_class(self, val): 17899c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_string('test_class', val.lower()) 17999c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 18099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 18199c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_test_category(self, val): 18299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh self._set_string('test_category', val.lower()) 18399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 18499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 18599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh def set_test_type(self, val): 1863dd8beb386f7298ffe84d7410d00cce26973e170Aviv Keshet self._set_option('test_type', val, list(CONTROL_TYPE.names)) 18799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 188861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li 189d3e8a3bf27615ef88c80efdf7b3dcbf52c30f071Eric Li def set_test_parameters(self, val): 190d3e8a3bf27615ef88c80efdf7b3dcbf52c30f071Eric Li self._set_set('test_parameters', val) 191d3e8a3bf27615ef88c80efdf7b3dcbf52c30f071Eric Li 19299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 1936f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet def set_retries(self, val): 1946f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet self._set_int('retries', val) 1956f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet 196ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi 197e3bc24b7b77480d4bb0b1562bb30ea79eff85626Fang Deng def set_job_retries(self, val): 198e3bc24b7b77480d4bb0b1562bb30ea79eff85626Fang Deng self._set_int('job_retries', val) 199e3bc24b7b77480d4bb0b1562bb30ea79eff85626Fang Deng 2006f455262ded933c1699fea410a9ddaf967df6330Aviv Keshet 201ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B def set_bug_template(self, val): 202ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B if type(val) == dict: 203ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B setattr(self, 'bug_template', val) 20493f4209487841147d8fae40ce28beb310663b77ambligh 20593f4209487841147d8fae40ce28beb310663b77ambligh 206ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi def set_require_ssp(self, val): 207ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi self._set_bool('require_ssp', val) 208ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi 209ec1d47d85cc83f30631518d8fbb6406036a3ac39Dan Shi 210ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_const(expr): 211ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.__class__ == compiler.ast.Const) 212ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.value.__class__ in (str, int, float, unicode)) 213ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return str(expr.value).strip() 214ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 215ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 216ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_dict(expr): 217ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.__class__ == compiler.ast.Dict) 218ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.items.__class__ == list) 219ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B cf_dict = {} 220ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B for key, value in expr.items: 221ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B try: 222ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B key = _extract_const(key) 223ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B val = _extract_expression(value) 224ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B except (AssertionError, ValueError): 225ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B pass 226ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B else: 227ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B cf_dict[key] = val 228ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return cf_dict 22993f4209487841147d8fae40ce28beb310663b77ambligh 23093f4209487841147d8fae40ce28beb310663b77ambligh 231ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_list(expr): 232ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.__class__ == compiler.ast.List) 233ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B list_values = [] 234ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B for value in expr.nodes: 235ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B try: 236ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B list_values.append(_extract_expression(value)) 237ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B except (AssertionError, ValueError): 238ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B pass 239ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return list_values 240ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 241ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 242ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_name(expr): 243ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.__class__ == compiler.ast.Name) 244ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B assert(expr.name in ('False', 'True', 'None')) 245ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return str(expr.name) 246ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 247ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 248ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_expression(expr): 249ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B if expr.__class__ == compiler.ast.Const: 250ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return _extract_const(expr) 251ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B if expr.__class__ == compiler.ast.Name: 252ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return _extract_name(expr) 253ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B if expr.__class__ == compiler.ast.Dict: 254ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return _extract_dict(expr) 255ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B if expr.__class__ == compiler.ast.List: 256ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B return _extract_list(expr) 257ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B raise ValueError('Unknown rval %s' % expr) 258ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 259ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B 260ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth Bdef _extract_assignment(n): 26193f4209487841147d8fae40ce28beb310663b77ambligh assert(n.__class__ == compiler.ast.Assign) 26293f4209487841147d8fae40ce28beb310663b77ambligh assert(n.nodes.__class__ == list) 26393f4209487841147d8fae40ce28beb310663b77ambligh assert(len(n.nodes) == 1) 26493f4209487841147d8fae40ce28beb310663b77ambligh assert(n.nodes[0].__class__ == compiler.ast.AssName) 26593f4209487841147d8fae40ce28beb310663b77ambligh assert(n.nodes[0].flags.__class__ == str) 26693f4209487841147d8fae40ce28beb310663b77ambligh assert(n.nodes[0].name.__class__ == str) 26793f4209487841147d8fae40ce28beb310663b77ambligh 268ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B val = _extract_expression(n.expr) 26993f4209487841147d8fae40ce28beb310663b77ambligh key = n.nodes[0].name.lower() 27093f4209487841147d8fae40ce28beb310663b77ambligh 27193f4209487841147d8fae40ce28beb310663b77ambligh return (key, val) 27293f4209487841147d8fae40ce28beb310663b77ambligh 27393f4209487841147d8fae40ce28beb310663b77ambligh 2746fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masonedef parse_control_string(control, raise_warnings=False): 2756fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone try: 2766fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone mod = compiler.parse(control) 2776fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone except SyntaxError, e: 2786fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone raise ControlVariableException("Error parsing data because %s" % e) 2796fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone return finish_parse(mod, '', raise_warnings) 2806fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone 2816fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone 28299c2c6f54e962fe4e7701cd9aaa645fdabacd203mblighdef parse_control(path, raise_warnings=False): 283d7cd983031fc77a247d022aef67b481892681ddcmbligh try: 284d7cd983031fc77a247d022aef67b481892681ddcmbligh mod = compiler.parseFile(path) 285d7cd983031fc77a247d022aef67b481892681ddcmbligh except SyntaxError, e: 2864b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh raise ControlVariableException("Error parsing %s because %s" % 2874b5c31e1821307b880b57a41f6d8ea21948fc9e8mbligh (path, e)) 2886fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone return finish_parse(mod, path, raise_warnings) 2896fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masone 29099c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 2916fed646ae4c833b537ea24b6161b81e0f50c0e32Chris Masonedef finish_parse(mod, path, raise_warnings): 29299c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh assert(mod.__class__ == compiler.ast.Module) 29399c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh assert(mod.node.__class__ == compiler.ast.Stmt) 29499c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh assert(mod.node.nodes.__class__ == list) 29599c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh 29699c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh vars = {} 29799c2c6f54e962fe4e7701cd9aaa645fdabacd203mbligh for n in mod.node.nodes: 298ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B try: 299ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B key, val = _extract_assignment(n) 300ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B vars[key] = val 301ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B except (AssertionError, ValueError): 302ee70731b31f66ecf8043b9fa6d1f86a52753a22fPrashanth B pass 303d7cd983031fc77a247d022aef67b481892681ddcmbligh return ControlData(vars, path, raise_warnings) 304