Commit 075a3e0d authored by Sebastien Ponce's avatar Sebastien Ponce
Browse files

Improved parsing procedures

parent 36e14ae6
......@@ -643,6 +643,10 @@ def printUser((uid, gid)):
#-------------------------------------------------------------------------------
# useful parsing functions
#-------------------------------------------------------------------------------
class ParsingError(Exception):
'''base class for parsing errors'''
pass
def parseUser(rawUserGroup):
'''parses input of a given user.
format must be one of :
......@@ -665,8 +669,7 @@ def parseUser(rawUserGroup):
# suppose a uid is given
uid = int(rawUser)
if uid < 0:
print 'Invalid uid %d' % uid
usage(1)
raise ParsingError('Invalid uid %d' % uid)
except ValueError:
# was not a uid
try:
......@@ -674,8 +677,7 @@ def parseUser(rawUserGroup):
uid = pwdEntry.pw_uid
gid = pwdEntry.pw_gid
except KeyError:
print 'Unknown user %s' % rawUser
usage(1)
raise ParsingError('Unknown user %s' % rawUser)
# find out the group, if given
if rawGroup == None:
gid = None
......@@ -684,15 +686,13 @@ def parseUser(rawUserGroup):
# suppose a gid is given
gid = int(rawGroup)
if gid < 0:
print 'Invalid gid %d' % uid
usage(1)
raise ParsingError('Invalid gid %d' % uid)
except ValueError:
# was not a gid
try:
gid = grp.getgrnam(rawGroup).gr_gid
except KeyError:
print 'Unknown group %s' % rawGroup
usage(1)
raise ParsingError('Unknown group %s' % rawGroup)
return uid,gid
def parsePositiveInt(name, svalue):
......@@ -703,29 +703,28 @@ def parsePositiveInt(name, svalue):
raise ValueError
return value
except ValueError:
print 'Invalid %s %s' % (name, svalue)
usage(1)
raise ParsingError('Invalid %s %s' % (name, svalue))
def parsePositiveNonNullInt(name, svalue):
'''parses a positive, non null int value and exits with proper error message in case the value does not fit'''
value = parsePositiveInt(name, svalue)
if value == 0:
print '%s cannot be set to 0' % name
usage(1)
raise ParsingError('%s cannot be set to 0' % name)
return value
def parseBool(name, svalue):
def parseBool(name, svalue, default=None):
'''parses a boolean value and exits with proper error message in case the value does not fit'''
validTrues = ['true', '1', 't', 'y', 'yes']
validFalses = ['false', '0', 'f', 'n', 'no']
if svalue == '' and default != None:
return default
if svalue.lower() in validTrues:
return True
elif svalue.lower() in validFalses:
return False
else:
print 'Invalid %s %s' % (name, svalue)
print 'Note that accepted booleans are %s and %s' % (','.join(validTrues), ','.join(validFalses))
usage(1)
raise ParsingError('Invalid %s %s\nNote that accepted booleans are %s and %s' % \
(name, svalue, ','.join(validTrues), ','.join(validFalses)))
def parseDataAmount(name, svalue):
'''parses a value describing an amount of data. Exits with proper error message in case the value does not fit'''
......@@ -743,8 +742,7 @@ def parseDataAmount(name, svalue):
value *= exts[m.group('ext')]
return value
except ValueError:
print 'Invalid %s %s' % (name, svalue)
usage(1)
raise ParsingError('Invalid %s %s' % (name, svalue))
def parseTimeDuration(name, svalue):
'''parses a value describing a time duration. Exits with proper error message in case the value does not fit'''
......@@ -763,8 +761,7 @@ def parseTimeDuration(name, svalue):
value += partvalue
return value
except ValueError:
print 'Invalid %s %s' % (name, svalue)
usage(1)
raise ParsingError('Invalid %s %s' % (name, svalue))
#--------------------
# getCurrentUsername
......
......@@ -53,7 +53,11 @@ for f, v in options:
# Deal with arguments
recallUsers = None
if len(args) > 0:
recallUsers = set(map(castor_tools.parseUser, args))
try:
recallUsers = set(map(castor_tools.parseUser, args))
except castor_tools.ParsingError, e:
print e
usage(1)
else:
print 'missing argument'
usage(1)
......
......@@ -461,8 +461,13 @@ def deleteDrain():
drainingJobs.append((fsMountPoint, drains[0][0]))
if drainingJobs:
# get user confirmation
confirmation = castor_tools.parseBool('entry', raw_input("Is this ok ? [N/y] "))
try:
confirmation = castor_tools.parseBool('entry', raw_input("Is this ok ? [N/y] "), False)
except castor_tools.ParsingError, e:
print e
exit(1)
if not confirmation:
print 'Ok, giving up'
return
# create DrainingJob statement
stDeleteDrainingJob = 'BEGIN deleteDrainingJob(:djId); END;'
......
......@@ -51,6 +51,9 @@ def parseMapping(name, svalue):
except ValueError:
print 'Invalid %s %s' % (name, svalue)
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# first parse the options
try:
......
......@@ -48,24 +48,28 @@ minamountdata = 100*1024*1024*1024 # 100GiB
minnbfiles = 1000
maxfileage = 43200 # 12h
priority = 0
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
elif f == '--priority':
priority = castor_tools.parsePositiveInt('priority', v)
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
elif f == '--priority':
priority = castor_tools.parsePositiveInt('priority', v)
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -74,7 +74,12 @@ if len(args) < 2:
elif len(args) > 2:
print "Too many arguments"
usage(1)
uid, gid = castor_tools.parseUser(args[0])
try:
uid, gid = castor_tools.parseUser(args[0])
except castor_tools.ParsingError, e:
print e
usage(1)
recallGroup = args[1]
try:
......
......@@ -56,30 +56,34 @@ forcedfileclass = None
replicateonclose = False
gcpolicy = None
diskpools = []
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--defaultfilesize':
defaultfilesize = castor_tools.parseDataAmount('defaultfilesize', v)
elif f == '--maxreplicanb':
maxreplicanb = castor_tools.parsePositiveNonNullInt('maxreplicanb', v)
elif f == '--failjobswhennospace':
failjobswhennospace = castor_tools.parseBool('failjobswhennospace', v)
elif f == '--disk1behavior':
disk1behavior = castor_tools.parseBool('disk1behavior', v)
elif f == '--forcedfileclass':
forcedfileclass = v
elif f == '--replicateonclose':
replicateonclose = castor_tools.parseBool('replicateonclose', v)
elif f == '--gcpolicy':
gcpolicy = v
elif f == '--diskpools':
diskpools = set(v.split(':'))
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--defaultfilesize':
defaultfilesize = castor_tools.parseDataAmount('defaultfilesize', v)
elif f == '--maxreplicanb':
maxreplicanb = castor_tools.parsePositiveNonNullInt('maxreplicanb', v)
elif f == '--failjobswhennospace':
failjobswhennospace = castor_tools.parseBool('failjobswhennospace', v)
elif f == '--disk1behavior':
disk1behavior = castor_tools.parseBool('disk1behavior', v)
elif f == '--forcedfileclass':
forcedfileclass = v
elif f == '--replicateonclose':
replicateonclose = castor_tools.parseBool('replicateonclose', v)
elif f == '--gcpolicy':
gcpolicy = v
elif f == '--diskpools':
diskpools = set(v.split(':'))
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -47,22 +47,26 @@ nbdrives = 0
minamountdata = 100*1024*1024*1024 # 100GiB
minnbfiles = 1000
maxfileage = 43200 # 12h
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -53,6 +53,9 @@ def parseMapping(name, svalue):
except ValueError:
print 'Invalid %s %s' % (name, svalue)
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# first parse the options
try:
......
......@@ -48,24 +48,28 @@ minamountdata = None
minnbfiles = None
maxfileage = None
priority = None
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
elif f == '--priority':
priority = castor_tools.parsePositiveInt('priority', v)
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
elif f == '--priority':
priority = castor_tools.parsePositiveInt('priority', v)
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -57,7 +57,11 @@ if len(args) < 2:
elif len(args) > 2:
print "Too many arguments"
usage(1)
uid, gid = castor_tools.parseUser(args[0])
try:
uid, gid = castor_tools.parseUser(args[0])
except castor_tools.ParsingError, e:
print e
usage(1)
recallGroup = args[1]
try:
......
......@@ -59,33 +59,37 @@ replicateonclose = None
gcpolicy = None
adddiskpools = None
removediskpools = None
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--defaultfilesize':
defaultfilesize = castor_tools.parseDataAmount('defaultfilesize', v)
elif f == '--maxreplicanb':
maxreplicanb = castor_tools.parsePositiveNonNullInt('maxreplicanb', v)
elif f == '--failjobswhennospace':
failjobswhennospace = castor_tools.parseBool('failjobswhennospace', v)
elif f == '--disk1behavior':
disk1behavior = castor_tools.parseBool('disk1behavior', v)
elif f == '--forcedfileclass':
forcedfileclass = v
forcedfileclassset = True
elif f == '--replicateonclose':
replicateonclose = castor_tools.parseBool('replicateonclose', v)
elif f == '--gcpolicy':
gcpolicy = v
elif f == '--adddiskpools':
adddiskpools = set(v.split(':'))
elif f == '--removediskpools':
removediskpools = set(v.split(':'))
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--defaultfilesize':
defaultfilesize = castor_tools.parseDataAmount('defaultfilesize', v)
elif f == '--maxreplicanb':
maxreplicanb = castor_tools.parsePositiveNonNullInt('maxreplicanb', v)
elif f == '--failjobswhennospace':
failjobswhennospace = castor_tools.parseBool('failjobswhennospace', v)
elif f == '--disk1behavior':
disk1behavior = castor_tools.parseBool('disk1behavior', v)
elif f == '--forcedfileclass':
forcedfileclass = v
forcedfileclassset = True
elif f == '--replicateonclose':
replicateonclose = castor_tools.parseBool('replicateonclose', v)
elif f == '--gcpolicy':
gcpolicy = v
elif f == '--adddiskpools':
adddiskpools = set(v.split(':'))
elif f == '--removediskpools':
removediskpools = set(v.split(':'))
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -47,22 +47,26 @@ nbdrives = None
minamountdata = None
minnbfiles = None
maxfileage = None
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
else:
print "unknown option : " + f
usage(1)
try:
for f, v in options:
if f == '-h' or f == '--help':
usage(0)
elif f == '-v' or f == '--verbose':
verbose = True
elif f == '--nbdrives':
nbdrives = castor_tools.parsePositiveInt('nbdrives', v)
elif f == '--minamountdata':
minamountdata = castor_tools.parseDataAmount('minamountdata', v)
elif f == '--minnbfiles':
minnbfiles = castor_tools.parsePositiveInt('minnbfiles', v)
elif f == '--maxfileage':
maxfileage = castor_tools.parseTimeDuration('maxfileage', v)
else:
print "unknown option : " + f
usage(1)
except castor_tools.ParsingError, e:
print e
usage(1)
# Deal with arguments
if len(args) == 0:
......
......@@ -62,7 +62,12 @@ if recallGroup != None:
usage(1)
else:
if len(args) > 0:
recallUsers = set(map(castor_tools.parseUser, args))
try:
recallUsers = set(map(castor_tools.parseUser, args))
except castor_tools.ParsingError, e:
print e
usage(1)
try:
# connect to stager and prepare statements
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment