Created
December 5, 2018 22:59
-
-
Save stevekm/7831fac98473ea17d781330baa0dd7aa to your computer and use it in GitHub Desktop.
Convert SLURM squeue output to JSON format
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Convert SLURM squeue output to JSON format | |
""" | |
import subprocess as sp | |
import json | |
process = sp.Popen(['squeue', '-o', '%all'], stdout = sp.PIPE, stderr = sp.PIPE, shell = False, universal_newlines = True) | |
proc_stdout, proc_stderr = process.communicate() | |
lines = proc_stdout.split('\n') | |
header_line = lines.pop(0) | |
header_cols = header_line.split('|') | |
entries = [] | |
error_lines = [] # do something with this later | |
for line in lines: | |
parts = line.split('|') | |
d = {} | |
if len(parts) != len(header_cols): | |
error_lines.append((len(parts), line, parts)) | |
else: | |
for i, key in enumerate(header_cols): | |
d[key] = parts[i] | |
entries.append(d) | |
print(json.dumps(entries, indent = 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this piece of code however I noticed that entries were duplicated
line 24 there is an indent that shouldn't be there if you only want one copy of the job information (the append should be outside the loop)
Cheers!