-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtee.py
More file actions
71 lines (59 loc) · 2.07 KB
/
tee.py
File metadata and controls
71 lines (59 loc) · 2.07 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
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 17-11-2024
Last update: 17-11-2024
Version: 1.0
Description: The tee command from GNU coreutils in Python3.
Example of use: echo "Hello, World!" | python3 tee.py output.txt
'''
#!/usr/bin/env python3
import argparse
import os
import signal
import sys
def tee(files, append=False, ignore_sigint=False):
"""Implémente la commande tee."""
mode = "a" if append else "w"
open_files = []
if ignore_sigint:
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Ouvrir les fichiers spécifiés
for file in files:
try:
open_files.append(open(file, mode))
except OSError as e:
print(f"tee: {file}: {e}", file=sys.stderr)
try:
while True:
data = sys.stdin.read(1024)
if not data:
break
# Écrire dans la sortie standard
sys.stdout.write(data)
sys.stdout.flush()
# Écrire dans chaque fichier
for f in open_files:
try:
f.write(data)
f.flush()
except OSError as e:
print(f"tee: Error writing to {f.name}: {e}", file=sys.stderr)
except KeyboardInterrupt:
if not ignore_sigint:
raise
finally:
# Fermer tous les fichiers ouverts
for f in open_files:
f.close()
def main():
parser = argparse.ArgumentParser(description="Duplicate standard input to standard output and files.")
parser.add_argument("files", nargs="*", help="Files to write to.")
parser.add_argument("-a", "--append", action="store_true", help="Append to the files instead of overwriting.")
parser.add_argument("-i", "--ignore-sigint", action="store_true", help="Ignore the SIGINT signal.")
parser.add_argument("--version", action="version", version="tee.py 1.0")
args = parser.parse_args()
tee(args.files, append=args.append, ignore_sigint=args.ignore_sigint)
if __name__ == "__main__":
main()