-
Notifications
You must be signed in to change notification settings - Fork 2
/
runEccs2.py
executable file
·131 lines (97 loc) · 4.27 KB
/
runEccs2.py
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
#!/usr/bin/env python3
import argparse
import asyncio
import datetime
import eccs2properties
import json
import time
from utils import getListFeds, getListEccsIdps, getRegAuthDict, getIdpList
from eccs2properties import ECCS2FAILEDCMD, ECCS2FAILEDCMDIDP, ECCS2STDOUT, ECCS2STDERR, ECCS2STDOUTIDP, ECCS2STDERRIDP, ECCS2DIR, ECCS2NUMPROCESSES, ECCS2LISTIDPSURL, ECCS2LISTIDPSFILE, ECCS2LISTFEDSURL, ECCS2LISTFEDSFILE
from subprocess import PIPE
# Run Command
async def run(name,queue,stdout_file,stderr_file,cmd_file):
while True:
# Get a "cmd item" out of the queue.
cmd = await queue.get()
# Elaborate "cmd" from shell.
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
if stdout:
stdout_file.write('-----\n[cmd-out]\n%s\n\n[stdout]\n%s' % (cmd,stdout.decode()))
if stderr:
stderr_file.write('-----\n[cmd-err]\n%s\n\n[stderr]\n%s' % (cmd,stderr.decode()))
cmd_file.write('%s\n' % cmd)
# Notify the queue that the "work cmd" has been processed.
queue.task_done()
async def main(cmd_list,stdout_file,stderr_file,cmd_file):
# Create a queue that we will use to store our "workload".
queue = asyncio.Queue()
# Put all commands into the queue.
for cmd in cmd_list:
queue.put_nowait(cmd)
# Create worker tasks to process the queue concurrently.
tasks = []
for i in range(ECCS2NUMPROCESSES):
task = asyncio.create_task(run("cmd-{%d}" % i, queue, stdout_file, stderr_file, cmd_file))
tasks.append(task)
# Wait until the queue is fully processed.
started_at = time.monotonic()
await queue.join()
total_slept_for = time.monotonic() - started_at
# Cancel our worker tasks.
for task in tasks:
task.cancel()
# Wait until all worker tasks are cancelled.
await asyncio.gather(*tasks, return_exceptions=True)
# MAIN
if __name__=="__main__":
parser = argparse.ArgumentParser(description='This script will call another script in parallel to check one or more IdP on the correct consuming of the eduGAIN metadata')
parser.add_argument("--idp", metavar="entityid", dest="idp_entityid", nargs=1, help="An IdP entityID")
parser.add_argument("--test", action='store_true', dest="test", help="Test without effects")
args = parser.parse_args()
start = time.time()
# Setup list_feds
url = ECCS2LISTFEDSURL
dest_file = ECCS2LISTFEDSFILE
list_feds = getListFeds(url, dest_file)
# Setup list_eccs_idps
url = ECCS2LISTIDPSURL
dest_file = ECCS2LISTIDPSFILE
list_eccs_idps = getListEccsIdps(url, dest_file)
if (args.idp_entityid):
stdout_file = open(ECCS2STDOUTIDP,"w+")
stderr_file = open(ECCS2STDERRIDP,"w+")
cmd_file = open(ECCS2FAILEDCMDIDP,"w+")
idpJsonList = getIdpList(list_eccs_idps,idp_entityid=args.idp_entityid[0])
if (args.test is not True):
cmd = "%s/eccs2.py \'%s\'" % (ECCS2DIR,json.dumps(idpJsonList[0]))
else:
cmd = "%s/eccs2.py \'%s\' --test" % (ECCS2DIR,json.dumps(idpJsonList[0]))
proc_list = [cmd]
asyncio.run(main(proc_list,stdout_file,stderr_file,cmd_file))
else:
stdout_file = open(ECCS2STDOUT,"w+")
stderr_file = open(ECCS2STDERR,"w+")
cmd_file = open(ECCS2FAILEDCMD,"w+")
# Prepare input file for ECCS2
regAuthDict = getRegAuthDict(list_feds)
for name,regAuth in regAuthDict.items():
idpJsonList = getIdpList(list_eccs_idps,regAuth)
num_idps = len(idpJsonList)
if (args.test is not True):
cmd_list = [["%s/eccs2.py \'%s\'" % (ECCS2DIR, json.dumps(idp))] for idp in idpJsonList]
else:
cmd_list = [["%s/eccs2.py \'%s\' --test" % (ECCS2DIR, json.dumps(idp))] for idp in idpJsonList]
proc_list = []
count = 0
while (count < num_idps):
cmd = "".join(cmd_list.pop())
proc_list.append(cmd)
count = count + 1
asyncio.run(main(proc_list,stdout_file,stderr_file,cmd_file))
end = time.time()
print("Time taken in hh:mm:ss - ", str(datetime.timedelta(seconds=end - start)))