-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwho.py
81 lines (70 loc) · 2.48 KB
/
who.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
import csv
import subprocess
import datetime
def lname(path):
lnameDict = {}
with open(path, 'r') as ldb:
ldbreader = csv.reader(ldb, delimiter=':', quotechar='|')
for row in ldbreader:
firstComma = row[1].find(',')
lnameDict[row[0]] = {
"careerAcc": row[0],
"name": row[1][:firstComma],
"email": row[2],
}
return lnameDict
def runWhoLocally():
# Run + split who on new lines
return subprocess.check_output("who").decode().split('\n')
def formatWho(who, lnameDict):
who = list(filter(None, who))
# First col (username)
whoCol1 = [line.split()[0] for line in who]
# Second col (tty/pts)
whoCol2 = [line.split()[1] for line in who]
# Fourth col (idle time)
whoCol4 = [line.split()[3] for line in who]
# Last col (what)
whoCol5 = [' '.join(line.split()[6:]) for line in who]
whoZip = list(zip(whoCol1, whoCol2, whoCol4, whoCol5))
whoList = []
for (careerAcc, device, idle, what) in whoZip:
found = False
for data in whoList:
if data['lname']['careerAcc'] == careerAcc:
data['devices'].append(device)
data['idle_times'].append(idle)
data['what'].append(what)
found = True
if not found:
whoList.append({
'lname': lnameDict[careerAcc] if careerAcc in lnameDict else 'None',
'timestamp': datetime.datetime.now().isoformat(),
'devices': [device],
'idle_times': [idle],
'what': [what],
})
return whoList
def freeLabCount(whoData):
ret = {}
for cluster, hosts in whoData.items():
if cluster not in ret:
ret[cluster] = {
'taken': 0,
'free': len(hosts),
'total': len(hosts)
}
for host, hostData in hosts.items():
for person in hostData:
for device in person['devices']:
if device.startswith('tty'):
if cluster in ret:
ret[cluster]['taken'] += 1
ret[cluster]['free'] -= 1
else:
ret[cluster] = {
'taken': 1,
'free': len(hosts) - 1,
'total': len(hosts)
}
return ret