def get_default_option_rules(): """Return the default option rules. Returns: list of OptionRule. """ upd = os.path.dirname tools_repo_root = upd(upd(upd(upd(upd(os.path.realpath(__file__)))))) default_opts_path = os.path.join(tools_repo_root, 'etc', 'default.opts') bde_root = os.environ.get('BDE_ROOT') found_default_opts = False found_default_internal_opts = False if not os.path.isfile(default_opts_path): logutil.warn('Cannot find default.opts at %s. ' 'Trying to use $BDE_ROOT/etc/default.opts instead.' % default_opts_path) if bde_root: default_opts_path = os.path.join(bde_root, 'etc', 'default.opts') if os.path.isfile(default_opts_path): found_default_opts = True else: found_default_opts = True if not found_default_opts: raise ValueError('Cannot find default.opts') with open(default_opts_path) as f: parser = optionsparser.OptionsParser(f) parser.parse() option_rules = parser.option_rules if bde_root: default_internal_opts_path = os.path.join(bde_root, 'etc', 'default_internal.opts') if os.path.isfile(default_internal_opts_path): found_default_internal_opts = True with open(default_internal_opts_path) as f: parser = optionsparser.OptionsParser(f) parser.parse() option_rules += parser.option_rules else: logutil.warn('The BDE_ROOT environment variable is set, ' 'but $BDE_ROOT/etc/default_internal.opts does ' 'not exist.') default_paths = default_opts_path if found_default_internal_opts: default_paths += ',' + default_internal_opts_path logutil.warn('Using default option rules from: ' + default_paths) return option_rules
def test_evaluate_shell_command(self): if sysutil.unversioned_platform() != 'linux': # This test runs shell commands, so only run it on Linux for now. pass opts_string = """ !! unix- _ K1 = a \\"`echo 123`\\" b !! unix- _ K2 = $(shell echo 123) """ opts_file = StringIO(opts_string) parser = optionsparser.OptionsParser(opts_file) parser.parse() ev = optionsevaluator.OptionsEvaluator( options.Uplid.from_str('unix-linux-x86-3.2.0-gcc-4.7.2'), options.Ufid.from_str('dbg_mt_exc')) # ev.store_option_rules(parser.option_rules, ['K1', 'K2']) # ev.evaluate(['K1', 'K2']) ev.store_option_rules(parser.option_rules) ev.evaluate() expected_results = { 'K2': '123', 'K1': 'a "123" b' } self.assertEqual(ev.results, expected_results)
def _load_opts(path): """Load option rules from a file. """ if os.path.isfile(path): with open(path) as f: parser = optionsparser.OptionsParser(f) parser.parse() return parser.option_rules else: return []
def setUp(self): repos_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'repos') self.repo_root = os.path.join(repos_path, 'one') self.cap_repo_root = os.path.join(repos_path, 'two') self.flags_parser = buildflagsparser.BuildFlagsParser( '-Wl,-Bdynamic', '-Wl,-Bstatic', '-l([^ =]+)', '-L([^ =]+)', '-I([^ =]+)', '-D') default_rules_path = os.path.join(repos_path, 'repo_test_def.opts') with open(default_rules_path) as f: parser = optionsparser.OptionsParser(f) parser.parse() self.default_rules = parser.option_rules
def test_parse(self): parser = optionsparser.OptionsParser(self.opts_file) parser.parse() lines = self.opts_string.split('\n') exp_option_rules = [] exp_all_lines = [] for val in self.expected_vals: pos = val[0] rule = options.OptionRule(*val[1]) exp_option_rules.append(rule) while len(exp_all_lines) < pos: exp_all_lines.append((lines[len(exp_all_lines)], None)) exp_all_lines.append((lines[len(exp_all_lines)], rule)) exp_all_lines.append(('', None)) self.assertEqual(exp_option_rules, parser.option_rules) self.assertEqual(exp_all_lines, parser.all_lines)
def test_evaluate(self): opts_string = """ !! unix- _ K1 = V1 !! unix- dbg K2 = V2 $(K1) unix- dbg K2 = V3 !! unix- opt K2 = V4 ++ unix- _ K1 = V5 >> unix- _ K1 = V6 << unix- _ K1 = V7 -- unix- _ K1 = V8 !! unix- _ XLC_INTERNAL_PREFIX1 = IGNORED !! unix- _ BDE_COMPILER_FLAG = gcc !! windows- _ BDE_COMPILER_FLAG = msvc !! unix-*-*-*-def _ K3 = DEF_MATCH !! unix-*-*-*-clang _ K4 = DEF_MATCH !! unix- _ K5 = ++ unix- _ K5 = K5_VAL """ opts_file = StringIO(opts_string) parser = optionsparser.OptionsParser(opts_file) parser.parse() ev = optionsevaluator.OptionsEvaluator( options.Uplid.from_str('unix-linux-x86-3.2.0-gcc-4.7.2'), options.Ufid.from_str('dbg_mt_exc')) # ev.store_option_rules(parser.option_rules, ['K1', 'K2']) # ev.evaluate(['K1', 'K2']) ev.store_option_rules(parser.option_rules) ev.evaluate() expected_results = { 'K1': 'V8 V7V1 V5V6', 'K2': 'V2 V8 V7V1 V5V6 V3', 'K3': 'DEF_MATCH', 'K5': 'K5_VAL', 'BDE_COMPILER_FLAG': 'gcc' } self.assertEqual(ev.results, expected_results)
def _parse_opts_str(self, str_): opts_file = StringIO(str_) parser = optionsparser.OptionsParser(opts_file) parser.parse() return parser.option_rules