Page MenuHomec4science

appargs.py
No OneTemporary

File Metadata

Created
Sun, Jun 30, 06:08

appargs.py

"""
Arguments parsing module
"""
import argparse
import getpass
import sys
import grp
from os import getgroups
from sausage.functions import valid_date, valid_period
from sausage.readconf import ReadConf
class AppArgs(object):
def __init__(self, cost=False):
self.response = {}
self.parser = argparse.ArgumentParser(
prog="Sausage",
description="SCITAS Account Usage.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
self.add_default_args()
if cost:
self.add_args_cost()
else:
self.add_args()
AppArgs.verbose = self.args.verbose
def add_default_args(self):
self.parser.add_argument(
"-v",
"--verbose",
help="Verbose",
action="store_true",
)
return
def add_args(self):
self.parser.add_argument(
"-u", "--user", help="If not provided whoami is considered"
)
self.parser.add_argument(
"-a",
"--all",
help="all users from an account are printed",
action="store_true",
)
self.parser.add_argument(
"-A", "--account", help="Prints account consumption per cluster"
)
self.parser.add_argument(
"-s", "--start", help="Start date - format YYYY-MM-DD", type=valid_date
)
self.parser.add_argument(
"-e", "--end", help="End date - format YYYY-MM-DD", type=valid_date
)
self.parser.add_argument(
"-c",
"--carbon",
help="Prints the carbon footprint per cluster",
action="store_true",
)
self.parser.add_argument(
"-b",
"--billing",
help="Displays the billing period - format YYYY-MM or YYYY",
type=valid_period,
)
self.parser.add_argument(
"-x",
"--csv",
help="Print result in csv style",
action="store_true",
default=False,
)
self.args = self.parser.parse_args()
if self.args.billing:
listofgroups = [grp.getgrgid(g).gr_name for g in getgroups()]
if ReadConf.billinggrp not in listofgroups:
self.parser.error(
"--billing is only available for users in "
+ ReadConf.billinggrp
+ " group"
)
if (
self.args.user
or self.args.account
or self.args.all
or self.args.start
or self.args.end
or self.args.carbon
):
self.parser.error(
"--billing is not compatible with any other option")
if self.args.start and self.args.end is None:
self.parser.error("range requires both dates (--start and --end)")
if self.args.end:
if self.args.start is None:
self.parser.error(
"range requires both dates (--start and --end)")
if self.args.end < self.args.start:
self.parser.error("start date must be earlier than end date")
if self.args.all and self.args.account is None:
self.parser.error(
"the option --all requires a valid account (--all and --account)"
)
if self.args.all and self.args.user:
self.parser.error(
"--all option is not compatible with --user option")
if len(sys.argv) <= 1 or (
(
(
all(v is not None for v in [
self.args.start, self.args.end])
or any(v is not None for v in [self.args.carbon, self.args.verbose])
)
and all(v is None for v in [self.args.account, self.args.user])
)
):
self.args.user = getpass.getuser()
AppArgs.csv = self.args.csv
AppArgs.response = {
"user": self.args.user,
"account": self.args.account,
"all": self.args.all,
"start": self.args.start,
"end": self.args.end,
"carbon": self.args.carbon,
"billing": self.args.billing,
}
def add_args_cost(self):
self.parser.add_argument(
"-N", "--nodes", help="number of (min) nodes on which to run", default=1
)
self.parser.add_argument(
"-n", "--nbtasks", help="number of tasks to run", default=1
)
self.parser.add_argument(
"-t", "--time", help="time limit in minutes", default=1
)
self.parser.add_argument(
"-p", "--partition", help="partition", default="parallel"
)
self.parser.add_argument(
"-g", "--gres", help="required generic resources per node", default="gpu:0"
)
self.parser.add_argument(
"-a", "--array", help="job array index values")
self.parser.add_argument(
"--ntasks-per-node",
help="number of tasks to invoke on each node",
)
self.parser.add_argument(
"-c", "--cpus-per-task", help="number of cpus required per task", default=1
)
self.parser.add_argument(
"--capping",
help="check if estimation cost surpass the limit for the account",
action="store_true",
)
self.args = self.parser.parse_args()
AppArgs.response = self.args
return

Event Timeline