Merge pull request 'ruff-4-perf-fixes' (#4935) from ruff-4-perf-fixes into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4935
This commit is contained in:
Mic92
2025-08-25 13:12:14 +00:00
11 changed files with 79 additions and 87 deletions

View File

@@ -77,26 +77,28 @@ class SecretStore(SecretStoreBase):
check=False,
).stdout.strip(),
)
for symlink in Path(password_store).glob(f"machines/{self.machine.name}/**/*"):
if symlink.is_symlink():
hashes.append(
subprocess.run(
nix_shell(
["git"],
[
"git",
"-C",
password_store,
"log",
"-1",
"--format=%H",
str(symlink),
],
),
stdout=subprocess.PIPE,
check=False,
).stdout.strip(),
)
hashes.extend(
subprocess.run(
nix_shell(
["git"],
[
"git",
"-C",
password_store,
"log",
"-1",
"--format=%H",
str(symlink),
],
),
stdout=subprocess.PIPE,
check=False,
).stdout.strip()
for symlink in Path(password_store).glob(
f"machines/{self.machine.name}/**/*",
)
if symlink.is_symlink()
)
# we sort the hashes to make sure that the order is always the same
hashes.sort()

View File

@@ -23,13 +23,9 @@ sops_groups_folder = gen_sops_subfolder("groups")
def list_objects(path: Path, is_valid: Callable[[str], bool]) -> list[str]:
objs: list[str] = []
if not path.exists():
return objs
for f in path.iterdir():
if is_valid(f.name):
objs.append(f.name)
return objs
return []
return [f.name for f in path.iterdir() if is_valid(f.name)]
def remove_object(path: Path, name: str) -> list[Path]:

View File

@@ -64,17 +64,17 @@ def list_groups(flake_dir: Path) -> list[Group]:
if not group_folder.is_dir():
continue
machines_path = machines_folder(flake_dir, group.name)
machines = []
if machines_path.is_dir():
for f in machines_path.iterdir():
if validate_hostname(f.name):
machines.append(f.name)
machines = (
[f.name for f in machines_path.iterdir() if validate_hostname(f.name)]
if machines_path.is_dir()
else []
)
users_path = users_folder(flake_dir, group.name)
users = []
if users_path.is_dir():
for f in users_path.iterdir():
if VALID_USER_NAME.match(f.name):
users.append(f.name)
users = (
[f.name for f in users_path.iterdir() if VALID_USER_NAME.match(f.name)]
if users_path.is_dir()
else []
)
groups.append(Group(flake_dir, group.name, machines, users))
return groups
@@ -270,11 +270,11 @@ def get_groups(flake_dir: Path, what: str, name: str) -> list[str]:
if not groups_dir.exists():
return []
groups = []
for group in groups_dir.iterdir():
if group.is_dir() and (group / what / name).is_symlink():
groups.append(group.name)
return groups
return [
group.name
for group in groups_dir.iterdir()
if group.is_dir() and (group / what / name).is_symlink()
]
def add_secret_command(args: argparse.Namespace) -> None:

View File

@@ -41,7 +41,7 @@ log = logging.getLogger(__name__)
def list_generators_secrets(generators_path: Path) -> list[Path]:
paths = []
paths: list[Path] = []
for generator_path in generators_path.iterdir():
if not generator_path.is_dir():
continue
@@ -49,11 +49,13 @@ def list_generators_secrets(generators_path: Path) -> list[Path]:
def validate(generator_path: Path, name: str) -> bool:
return has_secret(generator_path / name)
for obj in list_objects(
generator_path,
functools.partial(validate, generator_path),
):
paths.append(generator_path / obj)
paths.extend(
generator_path / obj
for obj in list_objects(
generator_path,
functools.partial(validate, generator_path),
)
)
return paths

View File

@@ -58,10 +58,7 @@ def ssh_command(args: argparse.Namespace) -> None:
raise ClanError(msg)
# Convert ssh_option list to dictionary
ssh_options = {}
if args.ssh_option:
for name, value in args.ssh_option:
ssh_options[name] = value
ssh_options = dict(args.ssh_option) if args.ssh_option else {}
remote = remote.override(
host_key_check=args.host_key_check,

View File

@@ -63,7 +63,7 @@ def find_dataclasses_in_directory(
and isinstance(deco.func, ast.Name)
and deco.func.id == "dataclass"
):
dataclass_files.append((file_path, node.name))
dataclass_files.append((file_path, node.name)) # noqa: PERF401
except (SyntaxError, UnicodeDecodeError) as e:
print(f"Error parsing {file_path}: {e}")

View File

@@ -164,11 +164,12 @@ class SecretStore(StoreBase):
from clan_cli.vars.generator import Generator
manifest = []
generators = Generator.get_machine_generators(machine, self.flake)
for generator in generators:
for file in generator.files:
manifest.append(f"{generator.name}/{file.name}".encode())
manifest = [
f"{generator.name}/{file.name}".encode()
for generator in generators
for file in generator.files
]
manifest.append(git_hash)
return b"\n".join(manifest)