def _current_source_location(): """Return source location in current BUILD file""" from blade import build_manager # pylint: disable=import-outside-toplevel source_dir = Path(build_manager.instance.get_current_source_path()) return source_location(os.path.join(str(source_dir), 'BUILD'))
def glob(include, exclude=None, excludes=None, allow_empty=False): """This function can be called in BUILD to specify a set of files using patterns. Args: include:List[str], file patterns to be matched. exclude:Optional[List[str]], file patterns to be removed from the result. allow_empty:bool: Whether a empty result is a error. Patterns may contain shell-like wildcards, such as * , ? , or [charset]. Additionally, the path element '**' matches any subpath. """ from blade import build_manager # pylint: disable=import-outside-toplevel source_dir = Path(build_manager.instance.get_current_source_path()) source_loc = _current_source_location() include = var_to_list(include) severity = config.get_item('global_config', 'glob_error_severity') output = getattr(console, severity) if excludes: output('%s %s: "excludes" is deprecated, use "exclude" instead' % (source_loc, severity), prefix=False) exclude = var_to_list(exclude) + var_to_list(excludes) def includes_iterator(): results = [] for pattern in include: for path in source_dir.glob(pattern): if path.is_file() and not path.name.startswith('.'): results.append(path.relative_to(source_dir)) return results def is_special(pattern): return '*' in pattern or '?' in pattern or '[' in pattern non_special_excludes = set() match_excludes = set() for pattern in exclude: if is_special(pattern): match_excludes.add(pattern) else: non_special_excludes.add(pattern) def exclusion(path): if str(path) in non_special_excludes: return True for pattern in match_excludes: ret = path.match(pattern) if ret: return True return False result = sorted({str(p) for p in includes_iterator() if not exclusion(p)}) if not result and not allow_empty: args = repr(include) if exclude: args += ', exclude=%s' % repr(exclude) output( '%s %s: "glob(%s)" got an empty result. If it is the expected behavior, ' 'specify "allow_empty=True" to eliminate this message' % (source_loc, severity, args), prefix=False) return result
def glob(include, exclude=None, excludes=None, allow_empty=False): """This function can be called in BUILD to specify a set of files using patterns. Args: include:List(str), file patterns to be matched. exclude:List[str], file patterns to be removed from the result. allow_empty:bool: Whether a empty result is a error. Patterns may contain shell-like wildcards, such as * , ? , or [charset]. Additionally, the path element '**' matches any subpath. """ from blade import build_manager source_dir = Path(build_manager.instance.get_current_source_path()) include = var_to_list(include) if excludes: console.warning( '//%s: "glob.excludes" is deprecated, use "exclude" instead' % source_dir) exclude = var_to_list(exclude) + var_to_list(excludes) def includes_iterator(): results = [] for pattern in include: for path in source_dir.glob(pattern): if path.is_file() and not path.name.startswith('.'): results.append(path.relative_to(source_dir)) return results def is_special(pattern): return '*' in pattern or '?' in pattern or '[' in pattern non_special_excludes = set() match_excludes = set() for pattern in exclude: if is_special(pattern): match_excludes.add(pattern) else: non_special_excludes.add(pattern) def exclusion(path): if str(path) in non_special_excludes: return True for pattern in match_excludes: ret = path.match(pattern) if ret: return True return False result = sorted( set([str(p) for p in includes_iterator() if not exclusion(p)])) if not result and not allow_empty: args = repr(include) if exclude: args += ', exclude=%s' % repr(exclude) console.error( '//%s: "glob(%s)" got an empty result. If it is the expected behavior, ' 'specify "allow_empty=True" to eliminate this error' % (source_dir, args)) return result