-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpast-jobs
executable file
·193 lines (162 loc) · 7.03 KB
/
past-jobs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# Author: Sara Willis
# Date : February 23, 2022
import getopt, subprocess, sys, os, getpass
from datetime import datetime, timedelta
'''
Usage: past-jobs [-h] [-d n|--days=n] [-u USER|--user=USER]
Retrieves a user's jobs submitted within the last n days. If no timeframe is specified,
defaults to current day.
------------------------------------------------------------------------------------------
##########################################################################################
Subroutines
##########################################################################################
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
Error Message
Used to format an error message to use red text.
'''
def error_message(error_string):
ERRCOLOR="\033[0;31m"
ENDCOLOR="\033[0m"
print(ERRCOLOR + error_string +ENDCOLOR)
return
'''
------------------------------------------------------------------------------------------
Usage
Only used to print the usage message. Exits with provided value after print
'''
def usage(ExitCode):
print("\nUsage")
print("------------------------------------------------------------------------------")
print("Command: past-jobs")
print("Retrieves user's past job IDs. Defaults to jobs submitted on current day.")
print("If -d N included, where N is an integer, retrieves jobs run in last N days\n")
print("Usage: past-jobs [-h|--help] [-d N|--days=N] [-u USER|--user=USER")
print("Example: past-jobs -d 2\n")
print("------------------------------------------------------------------------------")
sys.exit(ExitCode)
'''
------------------------------------------------------------------------------------------
Job Options
Options include -h|--help,-d N|--days=N|--user=USER
Omitted option to select custom user. Could change at some point.
'''
def args(argv):
d=0
u=""
try:
opts, args = getopt.getopt(argv, "hd:u:",["help","days=","user="])
if len(opts) ==0:
pass
except getopt.GetoptError:
if len(argv) == 1 and "-d" in argv:
error_message("\nError: Days value missing.")
else:
error_message("\nError: Unrecognized option(s).")
usage(1)
try:
for opt, arg in opts:
if opt in ("-h","--help") :
usage(0)
if opt in ("-d","--days"):
d = arg
try:
d = int(d)
except ValueError:
error_message("\nError: Days must be an integer")
print("\nDays: " + d)
usage(1)
sys.exit(1)
if opt in ("-u","--user"):
u = arg
try:
u = str(u)
except ValueError:
error_message("\nError: User must be a string")
usage(1)
sys.exit(1)
except UnboundLocalError:
error_message("\nError: Days value missing.")
usage(1)
if d < 0:
error_message("\nMissing technology necessary to retrieve data from the future. Please choose a day value >=0")
usage(1)
return d, u
'''
------------------------------------------------------------------------------------------
Pull Job Data
Executes SLURM's sacct to pull user jobs run in the last N days.
'''
def user_jobs(user,days):
today = datetime.now()
# Decided to shift days back one to make request more intuitive. This makes -d 1 query current day
# rather than last two days
try:
if days == 0:
start_date = today-timedelta(days)
else:
start_date = today-timedelta(days-1)
start_date = str((start_date)).split(' ')[0]
except OverflowError:
error_message("\nWoah, that's a huge timeframe! Your job likely started running after 0 BCE. Try a smaller date range.\n\n")
usage(1)
try:
acct = subprocess.run(['sacct','-u',user,'-S',start_date,'-P','--format=JobID,Start,User,JobName%11,Partition,Account,State,ExitCode'],stdout=subprocess.PIPE)
acct_out = acct.stdout.decode("utf-8",'ignore').split('\n')
header = acct_out[0].split("|")
user_data=[header]
for entry in acct_out[1:-1]:
item=entry.split("|")
user_data.append(item)
return user_data
except FileNotFoundError:
print("Oops! Something has gone wrong. Check that SLURM is available to run sacct.")
sys.exit(1)
'''
------------------------------------------------------------------------------------------
Display Past Jobs
Reformats and prints past job information in a user-friendly format.
'''
def display_past_jobs(user,user_data,days):
header = user_data[0]
formatting = "{:<8} {:<11} {:<15} {:<15} {:<10} {:<10} {:<10} {:>9}"
length = sum([int(i) for i in formatting.replace('{:<','').replace('{:>','').replace('}','').split(' ')])
MESSAGECOLOR="\033[38;5;21m"
ENDCOLOR="\033[0m"
if days in (0,1):
display_message = "Jobs submitted by user "+user+ " today."
elif days == 2:
display_message = "Jobs submitted by user "+user+" yesterday and today."
else:
display_message = "Jobs submitted by user "+user+ " in last " + str(days) + " days."
print("\n"+MESSAGECOLOR +display_message.center(length + 10)+ENDCOLOR+"\n")
print(formatting.format(*tuple(header)))
print('-'*(length+8))
for job in user_data[1:]:
if "." in job[0]:
pass
else:
job[1] = job[1].split('T')[0]
job = [i.split(' ')[0][0:11] for i in job]
print(formatting.format(*tuple(job)))
'''
------------------------------------------------------------------------------------------
##########################################################################################
Program Executes Below
##########################################################################################
------------------------------------------------------------------------------------------
'''
if __name__ == '__main__':
days, user_netid = args(sys.argv[1:])
if user_netid == "":
user_netid = getpass.getuser()
# It would be really weird if the active user weren't an active user, but what do I know?
getent_user = subprocess.Popen(["getent passwd %s"%user_netid],stdout=subprocess.PIPE,shell=True).communicate()[0].decode("utf-8",'ignore')
if getent_user == "":
error_message("Oops! User NetID invalid.")
sys.exit(1)
else:
pass
user_data = user_jobs(user_netid,days)
display_past_jobs(user_netid,user_data,days)