* Use getopt to handle command-line option.

* Include a help command.
master
Wirawan Purwanto 8 years ago
parent 67bc899f4a
commit 483c6874c0
  1. 58
      sge/show-cluster-usage.py

@ -27,6 +27,14 @@ import re
import subprocess import subprocess
import sys import sys
class ParseError(RuntimeError):
pass
class ProgramError(RuntimeError):
pass
MYSELF = 'show-cluster-usage.py'
def analyze_cluster_usage_by_users(qstat_f): def analyze_cluster_usage_by_users(qstat_f):
"""Provides a summary analysis of cluster usage by users. """Provides a summary analysis of cluster usage by users.
@ -91,20 +99,60 @@ def print_cluster_usage_by_users(usage):
len(urec['xjobids']))) len(urec['xjobids'])))
def help():
msg = """\
%(CMD)s - Shows cluster usage from SGE information
def main_default(save_qstat=True): The information is mainly drawn from `qstat -f` output,
and analyzes the usage of the cluster in various ways..
Usage:
%(CMD)s
%(CMD)s [qstat_file] [--save]
Shows the cluster usage aggregated per user.
""" \
% dict(CMD=MYSELF)
print(msg)
def main_default(argv):
"""Main default function: """Main default function:
- By default we invoke qstat -f and prints the analysis. - By default we invoke qstat -f and prints the analysis.
- If argv[1] is given, then we read in the file and - If argv[1] is given, then we read in the file and
use that for the analysis. use that for the analysis.
""" """
from time import localtime, strftime from time import localtime, strftime
from getopt import getopt, GetoptError
dtime = localtime() dtime = localtime()
dtimestr = strftime("%Y%m%d-%H%M", dtime) dtimestr = strftime("%Y%m%d-%H%M", dtime)
if len(sys.argv) > 1: # Skip program name and first command:
qstat_f_current = open(sys.argv[1], "r").read().splitlines() cmdargs_in = argv[1:]
try:
cmdopts, cmdargs = getopt(cmdargs_in,
"hs",
["save",
"help"])
except GetoptError as err:
sys.stderr.writelines([str(err), "\n"])
return 2
# Process flag arguments
show_disabled_nodes = False
save_qstat = False
for o,a in cmdopts:
if o in ('-h', '--help'):
help()
return 0
elif o in ('-s', '--save'):
save_qstat = True
else:
raise ProgramError, "Unhandled option in main program: %s %s" % (o,a)
if len(cmdargs) > 1:
qstat_f_current = open(cmdargs[1], "r").read().splitlines()
else: else:
qstat_f_current = pipe_out(('qstat', '-f'), split=True) qstat_f_current = pipe_out(('qstat', '-f'), split=True)
if save_qstat: if save_qstat:
@ -114,7 +162,7 @@ def main_default(save_qstat=True):
summary = analyze_cluster_usage_by_users(qstat_f_current) summary = analyze_cluster_usage_by_users(qstat_f_current)
print_cluster_usage_by_users(summary) print_cluster_usage_by_users(summary)
return 0
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@ -164,4 +212,4 @@ def str_fmt_heading(fmt):
# stub main code # stub main code
if __name__ == "__main__" and not "get_ipython" in globals(): if __name__ == "__main__" and not "get_ipython" in globals():
main_default() sys.exit(main_default(sys.argv))

Loading…
Cancel
Save