-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchgrp.py
More file actions
72 lines (59 loc) · 2.1 KB
/
chgrp.py
File metadata and controls
72 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 18-11-2024
Last update: 18-11-2024
Version: 1.0
Description: The chgrp command from GNU coreutils in Python3.
Example of use: python3 chgrp.py staff file.txt
'''
import os
import grp
import argparse
import sys
def validate_group(group_name):
"""Vérifie si le groupe existe."""
try:
group_info = grp.getgrnam(group_name)
return group_info.gr_gid
except KeyError:
print(f"Erreur : Le groupe '{group_name}' n'existe pas.", file=sys.stderr)
sys.exit(1)
def change_group(file_path, gid, dereference):
"""Change le groupe d'un fichier ou répertoire."""
try:
if dereference or not os.path.islink(file_path):
os.chown(file_path, -1, gid)
else:
os.lchown(file_path, -1, gid)
return True
except FileNotFoundError:
print(f"Erreur : Le fichier ou répertoire '{file_path}' n'existe pas.", file=sys.stderr)
return False
except PermissionError:
print(f"Permission refusée : '{file_path}'.", file=sys.stderr)
return False
except Exception as e:
print(f"Erreur inattendue : {e}.", file=sys.stderr)
return False
def process_files(group_name, files, dereference):
"""Traite tous les fichiers donnés."""
gid = validate_group(group_name)
for file_path in files:
if not change_group(file_path, gid, dereference):
print(f"Échec du changement de groupe pour : {file_path}")
def main():
parser = argparse.ArgumentParser(
description="Change le groupe des fichiers ou répertoires spécifiés."
)
parser.add_argument("group", help="Nom du groupe.")
parser.add_argument("files", nargs="+", help="Fichiers ou répertoires à modifier.")
parser.add_argument(
"-n", "--no-dereference", action="store_true", help="Modifier les liens symboliques eux-mêmes."
)
args = parser.parse_args()
dereference = not args.no_dereference
process_files(args.group, args.files, dereference)
if __name__ == "__main__":
main()