def _resolve_fuzzy(self, to_module): file_basename = StringTools.strip_suffix(os.path.basename( to_module.get_resource().name()), VSConstants.VCXProj_EXTENSION, ignore_case=True) candidates = list( filename for (filename, modulename) in self.__file_to_module_name_map.iteritems() if modulename == file_basename or os.path.basename(filename) == file_basename) candidates = sorted( candidates, key=lambda x: -len(self.__file_to_module_name_map[x])) if len(candidates): if len(candidates) > 1: self.__logger.warning( "Could not resolve project reference to %s exactly, but multiple fuzzy matches could be found in %s, using %s" % (to_module.get_resource().name(), ", ".join(candidates), candidates[0])) else: self.__logger.warning( "Could not resolve project reference to %s exactly, but a fuzzy match could be found in %s" % (to_module.get_resource().name(), candidates[0])) return self.__file_to_module_name_map[candidates[0]] return None
def __to_rel_to_project_root_path(self, abspath): """ Converts an absolute path to a path relative to the project's base path. @param abspath: an absolute path @type abspath: basestring @rtype: str or unicode (depending on type(abspath)) """ # TODO this should use self.__project_dir_resolver instead, since BasicConfig.get_local_source_base_dir() is deprecated # TODO a utility function should be added to commons.os_util which does this in general for an absolute path and a LocalPathResolver # TODO is it guaranteed that the string returned by get_local_source_base_dir never ends with os.path.sep? return StringTools.strip_prefix(abspath, self.__basic_config.get_local_source_base_dir() + os.path.sep)
def __init__(self, file_to_module_map, modules, local_output_aggregate_for_individual_func, optimiseNodeNames=False, optimiseModuleNames=True, get_raw_modulename=lambda x: x): self.__selected_input_aggregates = modules self.__individual_to_input_aggregate_map = file_to_module_map self.__local_output_aggregate_to_input_aggregate_map = None self.__filter_input_aggregate_func = get_raw_modulename self.__local_output_aggregate_for_individual_func = local_output_aggregate_for_individual_func if optimiseNodeNames and len(self.__selected_input_aggregates) > 1: self.__local_output_aggregate_to_input_aggregate_map = \ dict(self.__local_output_aggregate_to_input_aggregate_iteritems()) if optimiseModuleNames: self.__selected_input_aggregates_common_prefix, self.__optimised_input_aggregates = \ StringTools.prefix_optimise(self.__selected_input_aggregates) else: self.__optimised_input_aggregates = dict() self.__selected_input_aggregates_common_prefix = ""
def _do_task(self, executable, filename, result, resultlock, binary_util): self.__logger.debug("Worker %s processes file %s", threading.currentThread().name, filename) cmdline = (executable, filename) process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, bufsize=1) mapper = lambda filename: StringTools.strip_suffixes( os.path.basename(filename.strip()), binary_util.get_binary_extensions(), ignore_case=True) self.__process_data(filename, result, resultlock, mapper, stream=process.stdout) process.wait() self.__logger.debug("%s: return value is %x", threading.currentThread().name, process.returncode) if process.returncode not in [0, 255]: raise CalledProcessError(process.returncode, cmdline) self.__logger.debug("Worker %s finished processing directory %s", threading.currentThread().name, filename) self.__logger.debug("Remaining threads: %s", threading.enumerate())
def test_strip_all_prefixes_4(self): self.assertEquals('abcd', StringTools.strip_all_prefixes('abcd', []))
def test_strip_all_prefixes_1(self): self.assertEquals('', StringTools.strip_all_prefixes('abcd', ['d', 'c', 'b', 'a']))
def test_strip_first_prefix_2(self): self.assertEquals('bcd', StringTools.strip_first_prefix('abcd', ['a', 'b', 'c', 'd']))
def test_strip_all_suffixes_4(self): self.assertEquals('abcd', StringTools.strip_all_suffixes('abcd', ['a', 'b', 'c']))
def test_strip_first_suffix_3(self): self.assertEquals('', StringTools.strip_first_suffix('', ['a', 'b', 'c', 'd']))
def test_strip_suffix_6(self): self.assertEquals('', StringTools.strip_suffix("", ""))
def test_get_common_prefix_3(self): self.assertEquals('', StringTools.get_common_prefix(('dyn', 'prio4dyn', 'prid4dyn')))
def test_get_common_prefix_2(self): self.assertEquals('', StringTools.get_common_prefix(set(('ABC', 'ABCD', 'ABCE', 'XYZ'))))
def test_get_common_prefix_1(self): self.assertEquals('ABC', StringTools.get_common_prefix(iter(('ABC', 'ABCD', 'ABCE'))))
def test_get_common_prefix_len_7(self): self.assertEquals(1, StringTools.get_common_prefix_len('ab', 'aB'))
def filename_to_cpp_pseudomodule_func(cpp_extensions): return lambda filename: StringTools.strip_suffixes( os.path.basename(filename), cpp_extensions)
def test_strip_suffix_2(self): self.assertEquals('a', StringTools.strip_suffix("abcd", "BCD", ignore_case=True))
def test_strip_suffix_5(self): self.assertEquals('abcd', StringTools.strip_suffix("abcd", ""))
def test_get_common_prefix_4(self): self.assertEquals('', StringTools.get_common_prefix(('A', 'B')))
def test_strip_first_suffix_1(self): self.assertEquals('abc', StringTools.strip_first_suffix('abcd', ['d', 'c', 'b', 'a']))
def test_get_common_prefix_5(self): self.assertEquals(None, StringTools.get_common_prefix(()))
def test_strip_first_suffix_non_fitting(self): self.assertEquals('abcd',StringTools.strip_first_suffix('abcd',['e','f','g','h']))
def test_get_common_prefix_6(self): self.assertEquals((1, 2, 3), StringTools.get_common_prefix(((1,2,3),(1,2,3,4))))
def test_strip_all_suffixes_none(self): self.assertEquals('abcd', StringTools.strip_all_suffixes('abcd', []))
def test_optimise_prefix_strings(self): (prefix, optimised_map) = StringTools.prefix_optimise(("ABCD", "ABCE")) self.assertEquals('ABC', prefix) self.assertEquals([('ABCD', 'D'), ('ABCE', 'E')], sorted(optimised_map.iteritems()))
def test_strip_first_prefix_none(self): self.assertEquals('abcd', StringTools.strip_first_prefix('abcd', []))
def test_strip_suffix_1(self): self.assertEquals('abcd', StringTools.strip_suffix("abcd", "BCD", ignore_case=False))
def test_strip_all_prefixes_3(self): self.assertEquals('cd', StringTools.strip_all_prefixes('abcd', ['a', 'b', 'd']))
def test_optimise_prefix_tuples(self): (prefix, optimised_map) = StringTools.prefix_optimise(((1, 2, 3), (1, 2, 4))) self.assertEquals((1,2), prefix) self.assertEquals([((1, 2, 3), (3,)), ((1, 2, 4), (4,))], sorted(optimised_map.iteritems()))
def test_get_common_prefix_len_2(self): self.assertEquals(0, StringTools.get_common_prefix_len('a', ''))
def test_optimise_prefix_strings_null(self): (prefix, optimised_map) = StringTools.prefix_optimise(("ABCD", "XYZ")) self.assertEquals('', prefix) self.assertEquals([('ABCD', 'ABCD'), ('XYZ', 'XYZ')], sorted(optimised_map.iteritems()))