Last active
December 28, 2015 07:29
-
-
Save plasmaman/7465224 to your computer and use it in GitHub Desktop.
Python code for extracting keys from "qstat -f". Initially made for getting the full job name (Job_Name) string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
# Short script to get the juicy stuff from "qstat -f" | |
# Note especially that you get the whole Job Name | |
# Add whatever you want in the keys array | |
############################### | |
# Preferences: | |
# Separator to use in the output | |
#sep = " - " | |
#sep = " | " | |
sep = " " | |
# This contains all the keys that we're looking for. | |
# They will be displayed in this order: | |
keys = ( | |
"Job_Name", | |
"queue", | |
"Resource_List.nodect", | |
"Resource_List.walltime", | |
"job_state", | |
"resources_used.walltime", | |
) | |
# Maybe we want to trim letters from the end? | |
cutoff = { | |
"resources_used.walltime": 3, | |
"Resource_List.walltime": 3 | |
} | |
############################### | |
# Get the output from "qstat -f" as a string: | |
p1 = subprocess.Popen(["qstat", "-f"], stdout=subprocess.PIPE) | |
s = p1.communicate()[0] | |
# Find all the jobs first: | |
start_pos = dict() | |
stop_pos = dict() | |
jobs = [] | |
# Start at the beginning of the file: | |
pos = 0 | |
while True: | |
try: | |
key = "Job Id: " | |
pos1 = s.index(key, pos) | |
# Save the stop pos for the previous job | |
try: stop_pos[jobs[-1]] = pos1 | |
except: pass | |
# Skip the host name | |
pos2 = s.index(".", pos1) | |
job_id = s[pos1+len(key):pos2] | |
jobs.append(job_id) | |
# Save the first index after the "Job Id" line for each job: | |
pos = s.index("\n", pos2) + 1 | |
start_pos[job_id] = pos | |
except: | |
# Now we can't find "Job Id: ", so quit, but save last stop pos first | |
try: stop_pos[jobs[-1]] = len(s) | |
except: pass | |
break | |
# Now fill up each line: | |
lines = [] | |
need = None | |
for job_id in jobs: | |
cols = [job_id] | |
start = start_pos[job_id] | |
stop = stop_pos[job_id] | |
for key in keys: | |
try: | |
# Always start searching at "start" because the order of the keys array is random: | |
pos1 = s.index(key, start, stop) | |
# Include till end of line (add 3 to start index because of " = "): | |
val = str(s[pos1+len(key)+3:s.index("\n", pos1)]).strip() | |
# See if we want to cut the string before the end: | |
if key in cutoff: | |
val = val[:-cutoff[key]] | |
# Add column to the line | |
cols.append(val) | |
except: | |
# If we don't find the key, move on | |
cols.append("") | |
# We have a complete line now: | |
lines.append(cols) | |
# Now we count the number of characters we need for each column: | |
if need is None: | |
need = [0 for j in range(len(cols))] | |
for j, col in enumerate(cols): | |
need[j] = max(need[j], len(col)) | |
# And finally, we can print each line: | |
for cols in lines: | |
s = "" | |
for j, col in enumerate(cols): | |
s += ("" if j==0 else sep) | |
s += col + (" " * (need[j]-len(col))) | |
print s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment