-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojet.py
More file actions
157 lines (99 loc) · 4.3 KB
/
projet.py
File metadata and controls
157 lines (99 loc) · 4.3 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os,sys,time
import lexer as ssp
# **** fonction pour excuter*****
def executerCommandeSimple(processus, entreeProcessus=0, sortieProcessus=1, sortiePrec = None, premierFils = False):
(rfd,wfd)=os.pipe() # derscrtiteur de lecture et dectiture de pipe
pid=os.fork() #
if(pid==0): # le fils s'ocuupe de l'excution de la commande
commande=processus._cmd.getCommand()
argCommande=processus._cmd.getArgs()
redirEntree=filtrerRedirectionsEntree(processus)
if redirEntree:
#print(redirEntree._filespec)
re_re_fd=os.open(redirEntree._filespec, os.O_RDONLY)
os.dup2(re_re_fd, 0)
# os.close(re_re_fd)
elif entreeProcessus != 0 and sortiePrec:
os.close(sortiePrec)
afficherErreur("je vais fermer sortieProcessus")
# os.close(sortiePrec)
afficherErreur("lire depuis " + str(entreeProcessus) + " au lieu de 0 pour " + commande)
os.dup2(entreeProcessus, 0)
#os.close(entreeProcessus)
re_wr_fd=None
redirSortie=filtrerRedirectionsSortie(processus) # redirection de la sortie gerer par le pere
## redirection entre *************
if redirSortie:
if redirSortie.isAppend():
re_wr_fd=os.open(redirSortie._filespec, os.O_WRONLY | os.O_APPEND | os.O_CREAT )
else:
re_wr_fd=os.open(redirSortie._filespec, os.O_WRONLY| os.O_CREAT | os.O_TRUNC)
os.dup2(re_wr_fd,1)
# fin ***********************************
elif sortieProcessus != 1:
os.close(entreeProcessus)
afficherErreur('ecrire dans ' + str(sortieProcessus) + ' au lieu de 1 pour ' + processus._cmd.getCommand())
os.close(1)
os.dup2(sortieProcessus,1)
redirErreur=filtrerRedirectionsErreur(processus)# redirection d'erreur gerer par le fils
re_Er_fd=None
if redirErreur:
if redirErreur.isAppend():
re_Er_fd=os.open(redirErreur._filespec, os.O_WRONLY | os.O_APPEND | os.O_CREAT)
else:
re_Er_fd=os.open(redirErreur._filespec, os.O_WRONLY| os.O_CREAT | os.O_TRUNC)
os.dup2(re_Er_fd,2)
argCommande=[commande]+ argCommande # pour tt les fonction exec le 1er argCommande doit etre la commande lui meme
try: # essaye cette commande
os.execv("/bin/"+commande,argCommande)
except OSError as e:
if e.errno==2: # si il y a une erreur
try:
afficherErreur( commande +" va executer")
os.execv("/usr/bin/"+commande,argCommande) # esaye l'autre commande
except OSError as e:
afficherErreur(e.strerror)
if e.errno==2:
try:
afficherErreur( commande +" va executer 3")
os.execv("./"+commande, argCommande)
except OSError as e:
afficherErreur("commande echoue")
afficherErreur(e.strerror)
sys.exit(-1)
else: # pere
pass
def log(msg): # pour voir les erreur sur le code
log_fd=os.open("log.txt", os.O_CREAT | os.O_WRONLY | os.O_APPEND )
os.write(log_fd,bytearray(msg + '\n',"utf-8"))
os.close(log_fd)
def afficherErreur(msg):
os.write(2, bytearray(msg+'\n',"utf-8"))
def filtrerRedirectionsEntree(processus):
return next((re for re in processus._redirs._redirs if re.__class__.__name__ == "INREDIR"), None)
def filtrerRedirectionsSortie(processus):
return next((re for re in processus._redirs._redirs if re.__class__.__name__ == "OUTREDIR"), None)
def filtrerRedirectionsErreur(processus):
return next((re for re in processus._redirs._redirs if re.__class__.__name__ == "ERRREDIR"), None)
if __name__ =='__main__':
pl = ssp.get_parser().parse("ps -aux | sort -k1n | wc -c ")
#pl = ssp.get_parser().parse("ls -al | sort -k1 | tr ‘A-Z’ ‘a-z’")
#pl=ssp.get_parser().parse("ps -aux | sort -k1n | tr 'a-z' 'A-Z' | tee toto.txt | wc -c")
tubesEnchainement = []
for i in range(len(pl)):
tubesEnchainement.append(os.pipe())
# afficherErreur(str(pl) + ' ' + str(len(pl)))
for i in range(len(pl)):
p = pl[i]
if(len(pl) == 1):
executerCommandeSimple(p, entreeProcessus =0, sortieProcessus= 1)
elif i == 0:
executerCommandeSimple(p, 0, tubesEnchainement[i][1], premierFils = True)
elif i == len(pl)-1:
executerCommandeSimple(p, tubesEnchainement[i-1][0], sortieProcessus=1, sortiePrec=tubesEnchainement[i-1][1])
else:
executerCommandeSimple(p, tubesEnchainement[i-1][0], tubesEnchainement[i][1], tubesEnchainement[i-1][1])
afficherErreur("avant fermeture fils")
for i in range(len(pl)):
(p,es) = os.waitpid(-1,os.WNOHANG)
afficherErreur("fin du programe")