Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -133,6 +133,7 @@ import re
HAVE_SELINUX = False
try:
import selinux
HAVE_SELINUX = True
except ImportError:
pass
@@ -157,78 +158,76 @@ def selinux_context(path):
if ret[0] != -1:
# Limit split to 4 because the selevel, the last in the list,
# may contain ':' characters
context = ret[1].split(':', 3)
context = ret[1].split(":", 3)
return context
def file_props(root, path):
''' Returns dictionary with file properties, or return None on failure '''
"""Returns dictionary with file properties, or return None on failure"""
abspath = os.path.join(root, path)
try:
st = os.lstat(abspath)
except OSError as e:
display.warning(f'filetree: Error using stat() on path {abspath} ({e})')
display.warning(f"filetree: Error using stat() on path {abspath} ({e})")
return None
ret = dict(root=root, path=path)
if stat.S_ISLNK(st.st_mode):
ret['state'] = 'link'
ret['src'] = os.readlink(abspath)
ret["state"] = "link"
ret["src"] = os.readlink(abspath)
elif stat.S_ISDIR(st.st_mode):
ret['state'] = 'directory'
ret["state"] = "directory"
elif stat.S_ISREG(st.st_mode):
ret['state'] = 'file'
ret['src'] = abspath
ret["state"] = "file"
ret["src"] = abspath
else:
display.warning(f'filetree: Error file type of {abspath} is not supported')
display.warning(f"filetree: Error file type of {abspath} is not supported")
return None
ret['uid'] = st.st_uid
ret['gid'] = st.st_gid
ret["uid"] = st.st_uid
ret["gid"] = st.st_gid
try:
ret['owner'] = pwd.getpwuid(st.st_uid).pw_name
ret["owner"] = pwd.getpwuid(st.st_uid).pw_name
except KeyError:
ret['owner'] = st.st_uid
ret["owner"] = st.st_uid
try:
ret['group'] = to_text(grp.getgrgid(st.st_gid).gr_name)
ret["group"] = to_text(grp.getgrgid(st.st_gid).gr_name)
except KeyError:
ret['group'] = st.st_gid
ret['mode'] = f'0{stat.S_IMODE(st.st_mode):03o}'
ret['size'] = st.st_size
ret['mtime'] = st.st_mtime
ret['ctime'] = st.st_ctime
ret["group"] = st.st_gid
ret["mode"] = f"0{stat.S_IMODE(st.st_mode):03o}"
ret["size"] = st.st_size
ret["mtime"] = st.st_mtime
ret["ctime"] = st.st_ctime
if HAVE_SELINUX and selinux.is_selinux_enabled() == 1:
context = selinux_context(abspath)
ret['seuser'] = context[0]
ret['serole'] = context[1]
ret['setype'] = context[2]
ret['selevel'] = context[3]
ret["seuser"] = context[0]
ret["serole"] = context[1]
ret["setype"] = context[2]
ret["selevel"] = context[3]
return ret
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
self.set_options(var_options=variables, direct=kwargs)
basedir = self.get_basedir(variables)
# Regular expression for exclude
exclude = self.get_option('exclude')
exclude = self.get_option("exclude")
exclude_pattern = re.compile(exclude) if exclude else None
ret = []
for term in terms:
term_file = os.path.basename(term)
dwimmed_path = self._loader.path_dwim_relative(basedir, 'files', os.path.dirname(term))
dwimmed_path = self._loader.path_dwim_relative(basedir, "files", os.path.dirname(term))
path = os.path.join(dwimmed_path, term_file)
display.debug(f"Walking '{path}'")
for root, dirs, files in os.walk(path, topdown=True):
# Filter files and directories using a regular expression
if exclude_pattern is not None:
dirs[:] = [d for d in dirs if not exclude_pattern.match(d)]
@@ -238,7 +237,7 @@ class LookupModule(LookupBase):
relpath = os.path.relpath(os.path.join(root, entry), path)
# Skip if relpath was already processed (from another root)
if relpath not in [entry['path'] for entry in ret]:
if relpath not in [entry["path"] for entry in ret]:
props = file_props(path, relpath)
if props is not None:
display.debug(f" found '{os.path.join(path, relpath)}'")