Skip to content

Commit edccdde

Browse files
committed
Use smbclient to delete obsolet directories. Experimental.
1 parent 9daefa9 commit edccdde

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterCli/typers/check_attic.py‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from rich.table import Table
66

77
from linuxmusterTools.common.attic import check_attic_dir
8+
from linuxmusterTools.smbclient import LMNSMBClient
9+
from linuxmusterTools.lmnconfig import SAMBA_DOMAIN
810
from .state import state
911
from .format import printf
1012

@@ -26,46 +28,43 @@ def check(
2628

2729
attic = Table(title=f"User's status in attic")
2830
attic.add_column("Username", style="cyan")
31+
attic.add_column("School", style="cyan")
2932
attic.add_column("Status", style="yellow")
3033
attic.add_column("Directory path", style="green")
3134

32-
commands = ""
33-
cmd_count = 0
3435
to_delete = []
36+
cmd = ''
3537

3638
data = [[c.header for c in attic.columns]]
3739
for user,details in result.items():
3840
if details['start']:
3941
if details['status'] == "killed":
40-
to_delete.append(f"{attic_dir}/{user}")
41-
commands += f"\trm -rf {attic_dir}/{user}\n"
42+
to_delete.append({'user': user, 'school':details['school']})
4243
status = f"Account killed at {details['end']}"
43-
cmd_count += 1
44+
cmd += "smbclient" \
45+
" -U administrator %$(cat /etc/linuxmuster/.secret/administrator)" \
46+
f" //{SAMBA_DOMAIN}/{details['school']} -c 'deltree \"students/attic/{user}\";'\n"
4447
elif details['status'] != 'killable':
4548
status = f"Account {details['status']} from {details['start']} to {details['end']}"
4649
else:
4750
status = f"Account {details['status']} since {details['start']}"
4851
else:
4952
status = "No information found"
5053

51-
data.append([user, status, f"{attic_dir}/{user}"])
52-
attic.add_row(user, status, f"{attic_dir}/{user}")
54+
data.append([user, status, details['school'], f"{attic_dir}/{user}"])
55+
attic.add_row(user, status, details['school'], f"{attic_dir}/{user}")
5356

5457
if state.format:
5558
printf.format(data)
5659
else:
5760
console.print(attic)
58-
if cmd_count > 0:
61+
if len(to_delete) > 0:
5962
console.rule(style="blue")
6063

61-
delete_msg = typer.style(commands, fg=typer.colors.RED, bold=True)
62-
if cmd_count == 1:
63-
typer.confirm(f"You can delete the unnecessary directory by running:\n\n{delete_msg}\nRun all this command now ?", abort=True)
64-
for d in to_delete:
65-
shutil.rmtree(d)
66-
typer.echo(typer.style(f"{d} deleted!", fg=typer.colors.RED))
67-
elif cmd_count > 1:
68-
typer.confirm(f"You can delete the unnecessary directories by running:\n\n{delete_msg}\nRun all these commands now ?", abort=True)
69-
for d in to_delete:
70-
shutil.rmtree(d)
71-
typer.echo(typer.style(f"{d} deleted!", fg=typer.colors.RED))
64+
typer.confirm(f"You can delete the unnecessary directory by running:\n\n{cmd}\nRun this command now ?", abort=True)
65+
client = LMNSMBClient()
66+
for entry in to_delete:
67+
print(entry)
68+
client.switch(entry['school'])
69+
client.deltree(f"students/attic/{entry['user']}")
70+
typer.echo(typer.style(f"Attic directory of {user} deleted!", fg=typer.colors.RED))

0 commit comments

Comments
 (0)