def colourify_content(content, root="", target=False): """Colourify content name using LS_COLORS. If target is True and content is a symbolic link, colourify content.target instead of content.name.""" content_name = rootjoin(content.location_key().parse_value(), root) codes, special_codes = parse_ls_colours() if not codes and not special_codes: return content_name if isinstance(content, ContentsDirEntry): return "\033[" + special_codes.get("di", "00") + "m" + content_name + "\033[m" if isinstance(content, ContentsFileEntry): return colourify_file(content_name, codes, special_codes) elif isinstance(content, ContentsSymEntry): if not target: return "\033[" + special_codes.get("ln", "00") + "m" + content_name + "\033[m" elif os.path.isabs(content.target_key().parse_value()): content_target = rootjoin(content.target_key().parse_value(), root) return colourify_file(content_target, codes, special_codes) else: dname = os.path.dirname(content_name) abstarget = rootjoin(content.target_key().parse_value(), dname) return colourify_file(abstarget, codes, special_codes).replace( dname + os.path.sep, '') else: return content_name
def no_colourify_content(content, root="", target=False): """Dummy replacement for colourify_content() with no colouring.""" content_name = rootjoin(content.location_key().parse_value(), root) if target: if os.path.isabs(content.target_key().parse_value()): return rootjoin(content.target_key().parse_value(), root) else: dname = os.path.dirname(content_name) return rootjoin(content.target_key().parse_value(), dname).replace( dname + os.path.sep, '') else: return content_name
def search_contents(path, env, matcher="exact", ignore_case=False, requested_instances=[object]): # {{{ """Search filename in contents of installed packages.""" # Get package ids of all installed packages ids = env[Selection.AllVersionsGroupedBySlot(Generator.Matches.All() | Filter.InstalledAtRoot(env.root))] if matcher == "fnmatch": if ignore_case: fnmatch_matcher = fnmatch.fnmatchcase else: fnmatch_matcher = fnmatch.fnmatch elif matcher == "regex": if ignore_case: regex_matcher = re.compile(path, re.IGNORECASE) else: regex_matcher = re.compile(path) for package_id in ids: if package_id.contents_key() is None: Log.instance.message( "vdb.no_contents", LogLevel.WARNING, LogContext.NO_CONTEXT, "'%s' does not provide a contents key." % package_id.name, ) else: for content in package_id.contents_key().parse_value(): if not True in [isinstance(content, i) for i in requested_instances]: continue content_path = rootjoin(content.location_key().parse_value(), env.root) if matcher == "exact" and (path == content_path or path == os.path.basename(content_path)): yield package_id, content elif matcher == "simple" and path in content_path: yield package_id, content elif matcher == "fnmatch" and fnmatch_matcher(content_path, path): yield package_id, content elif matcher == "regex" and regex_matcher.search(content_path): yield package_id, content
def get_contents( package, env, source_repos=[], requested_instances=[object], selection=Selection.AllVersionsGroupedBySlot, fnpattern=None, regexp=None, ignore_case=False, ): """Get contents of package""" # {{{Pattern matching if regexp is not None: if ignore_case: pattern = re.compile(regexp, re.IGNORECASE) else: pattern = re.compile(regexp) # }}} # {{{Get PackageDepSpec filter_installed = Filter.InstalledAtRoot(env.system_root_key) allow_wildcards = UserPackageDepSpecOption.ALLOW_WILDCARDS package_dep_spec = parse_user_package_dep_spec(package, env, [allow_wildcards], filter_installed) # }}} # {{{Get CONTENTS ids = env[selection(Generator.Matches(package_dep_spec, MatchPackageOptions()) | filter_installed)] for package_id in ids: if package_id.contents_key() is None: Log.instance.message( "vdb.no_contents", LogLevel.WARNING, LogContext.NO_CONTEXT, "'%s' does not provide a contents key." % package_id.name, ) else: # {{{Match by source repository if source_repos: if package_id.from_repositories_key() is None: Log.instance.message( "vdb.no_source_origin", LogLevel.WARNING, LogContext.NO_CONTEXT, "'%s' does not provide a from repositories key." % package_id.name, ) else: repo_origin = package_id.from_repositories_key() if not any(repo in source_repos for repo in repo_origin.parse_value()): continue # }}} requested_contents = list() for content in package_id.contents_key().parse_value(): if not any([isinstance(content, i) for i in requested_instances]): continue content_path = rootjoin(content.location_key().parse_value(), env.root) if fnpattern is not None: if ignore_case: if not fnmatch.fnmatchcase(content_path, fnpattern): continue else: if not fnmatch.fnmatch(content_path, fnpattern): continue if regexp is not None and pattern.search(content_path) is None: continue requested_contents.append(content) yield package_id, requested_contents