Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F67003993
bdparser.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Jun 19, 09:04
Size
9 KB
Mime Type
text/x-python
Expires
Fri, Jun 21, 09:04 (2 d)
Engine
blob
Format
Raw Data
Handle
18327788
Attached To
R3127 blackdynamite
bdparser.py
View Options
#!/usr/bin/env python
from
__future__
import
print_function
import
sys
import
re
import
os
import
base
import
argcomplete
,
argparse
from
argcomplete.completers
import
EnvironCompleter
#debugfname = "debug-completion"
debugfname
=
None
class
BDParser
(
object
):
"""
"""
def
completer
(
self
,
prefix
,
**
kwargs
):
if
debugfname
:
fh
=
open
(
debugfname
,
"a"
)
fh
.
write
(
"prefix "
+
str
(
prefix
)
+
"
\n
"
)
for
k
,
v
in
kwargs
.
iteritems
():
fh
.
write
(
"kwargs["
+
str
(
k
)
+
"] = "
+
str
(
v
)
+
"
\n
"
)
fh
.
write
(
"dest "
+
str
(
vars
(
kwargs
[
"action"
])[
"dest"
])
+
"
\n
"
)
params
=
vars
(
kwargs
[
"parsed_args"
])
for
k
in
params
.
keys
():
if
params
[
k
]
is
None
:
del
params
[
k
]
key
=
vars
(
kwargs
[
"action"
])[
"dest"
]
if
debugfname
:
fh
.
write
(
"key "
+
str
(
key
)
+
"
\n
"
)
if
(
key
==
"BDconf"
):
return
self
.
listPossibleConf
()
if
(
key
==
"host"
):
return
[
"localhost"
,
"lsmssrv1.epfl.ch"
]
if
(
key
==
"study"
):
if
(
"password"
in
params
):
del
params
[
"password"
]
params
[
"should_not_check_study"
]
=
True
if
(
"host"
not
in
params
):
params
[
"host"
]
=
"lsmssrv1.epfl.ch"
try
:
mybase
=
base
.
Base
(
**
params
)
except
:
if
debugfname
:
fh
.
write
(
"params "
+
str
(
params
)
+
"
\n
"
)
return
[
"ConnectionError"
]
return
mybase
.
getSchemaList
()
return
[]
def
listPossibleConf
(
self
):
files
=
[]
for
dir
in
[
"./"
,
os
.
path
.
expanduser
(
"~/.blackdynamite"
)]:
for
filename
in
os
.
listdir
(
dir
):
fileName
,
fileExtension
=
os
.
path
.
splitext
(
filename
)
if
(
fileExtension
==
".bd"
):
files
.
append
(
filename
)
# print (files)
return
files
def
readConfFile
(
self
,
read_params
,
fname
):
for
dir
in
[
"./"
,
os
.
path
.
expanduser
(
"~/.blackdynamite"
)]:
fullpath
=
os
.
path
.
join
(
dir
,
fname
)
# print ("searching BDconf " + fname + " in dir " + dir)
if
(
os
.
path
.
isfile
(
fullpath
)):
fname
=
fullpath
break
# print (fname)
with
open
(
fname
)
as
fh
:
lines
=
[
line
.
strip
()
for
line
in
fh
]
regex
=
"(.*)=(.*)"
for
line
in
lines
:
match
=
re
.
match
(
regex
,
line
)
if
(
not
match
):
print
(
"malformed line:"
+
line
)
sys
.
exit
(
-
1
)
param
=
match
.
group
(
1
)
.
strip
()
val
=
match
.
group
(
2
)
.
strip
()
if
(
param
in
self
.
admissible_params
):
read_params
[
param
]
=
val
def
checkParam
(
self
,
p
,
dico
):
if
not
p
in
dico
:
print
(
"****************"
)
print
(
"Parameter '"
+
p
+
"' must be provided at command line"
)
print
(
"FATAL => ABORT"
)
print
(
"****************"
)
sys
.
exit
(
-
1
)
def
declareBDParameters
(
self
,
additional_params
=
None
,
default_additional_params
=
None
,
additional_help
=
None
):
if
(
additional_params
is
not
None
):
self
.
admissible_params
.
update
(
additional_params
)
if
(
default_additional_params
is
not
None
):
self
.
default_params
.
update
(
default_additional_params
)
if
(
additional_help
is
not
None
):
self
.
help
.
update
(
additional_help
)
def
parseBDParameters
(
self
):
read_params
=
{}
parser
=
argparse
.
ArgumentParser
(
description
=
"BlackDynamite option parser"
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
for
param
,
typ
in
self
.
admissible_params
.
iteritems
():
p_help
=
""
if
(
param
in
self
.
help
):
p_help
=
self
.
help
[
param
]
if
(
typ
is
None
):
parser
.
add_argument
(
"--"
+
param
,
help
=
p_help
,
action
=
'store_true'
)
elif
(
typ
is
list
):
parser
.
add_argument
(
"--"
+
param
,
action
=
'append'
,
help
=
p_help
)
.
completer
=
self
.
completer
else
:
parser
.
add_argument
(
"--"
+
param
,
help
=
p_help
)
.
completer
=
self
.
completer
#print (self.default_params)
parser
.
set_defaults
(
**
self
.
default_params
)
argcomplete
.
autocomplete
(
parser
)
pre_args
=
parser
.
parse_args
()
# print (pre_args)
for
opt
,
args
in
vars
(
pre_args
)
.
iteritems
():
if
(
args
is
None
):
continue
if
(
not
type
(
args
)
==
list
):
args
=
[
args
]
for
arg
in
args
:
if
(
arg
is
None
):
continue
if
(
opt
==
"BDconf"
):
self
.
readConfFile
(
read_params
,
arg
)
continue
for
param
,
typ
in
self
.
admissible_params
.
iteritems
():
# print ("AAAAAAA " + str(typ) + str(opt) + " " + str(param))
if
opt
==
param
:
if
(
typ
==
bool
):
# print ("AAAAAAAAAAAAAAA " + str(arg))
if
(
arg
==
"true"
):
read_params
[
param
]
=
True
else
:
read_params
[
param
]
=
False
elif
(
type
(
typ
)
==
list
):
args
=
arg
.
split
(
","
)
if
(
not
param
in
read_params
):
read_params
[
param
]
=
[]
if
(
not
len
(
typ
)
==
1
):
subtype
=
str
else
:
subtype
=
typ
[
0
]
for
i
in
range
(
0
,
len
(
args
)):
read_params
[
param
]
.
append
(
subtype
(
args
[
i
]))
else
:
# print (param)
# print (arg)
# print (typ)
if
(
typ
is
not
None
):
read_params
[
param
]
=
typ
(
arg
)
elif
(
arg
is
True
):
read_params
[
param
]
=
arg
elif
(
arg
is
False
and
opt
in
self
.
default_params
):
read_params
[
param
]
=
arg
break
return
read_params
def
__init__
(
self
):
self
.
admissible_params
=
{}
self
.
help
=
{}
self
.
default_params
=
{}
self
.
admissible_params
[
"study"
]
=
str
self
.
help
[
"study"
]
=
"Specify the study from the BlackDynamite database. This refers to the schemas in PostgreSQL language"
self
.
admissible_params
[
"host"
]
=
str
self
.
help
[
"host"
]
=
"Specify data base server address"
self
.
admissible_params
[
"port"
]
=
int
self
.
help
[
"port"
]
=
"Specify data base server port"
self
.
admissible_params
[
"user"
]
=
str
self
.
help
[
"user"
]
=
"Specify user name to connect to data base server"
self
.
admissible_params
[
"password"
]
=
None
self
.
help
[
"password"
]
=
"Flag to request prompt for typing password"
self
.
admissible_params
[
"BDconf"
]
=
str
self
.
help
[
"BDconf"
]
=
"Path to a BlackDynamite file (*.bd) configuring current optons"
self
.
admissible_params
[
"truerun"
]
=
None
self
.
help
[
"truerun"
]
=
"Set this flag if you want to truly perform the action on base. If not set all action are mainly dryrun"
self
.
default_params
[
"truerun"
]
=
False
self
.
admissible_params
[
"job_constraints"
]
=
[
str
]
self
.
help
[
"job_constraints"
]
=
"This allows to constraint run selections by job properties"
self
.
default_params
[
"job_constraints"
]
=
None
self
.
admissible_params
[
"run_constraints"
]
=
[
str
]
self
.
help
[
"run_constraints"
]
=
"This allows to constraint run selections by run properties"
self
.
default_params
[
"run_constraints"
]
=
None
self
.
admissible_params
[
"list_parameters"
]
=
None
self
.
help
[
"list_parameters"
]
=
"Request to list the possible job/run parameters"
################################################################
def
filterParams
(
sub_list
,
total_list
):
new_list
=
{}
for
p
in
sub_list
:
if
(
p
in
total_list
):
new_list
[
p
]
=
total_list
[
p
]
return
new_list
################################################################
class
RunParser
(
BDParser
):
"""
"""
def
parseBDParameters
(
self
):
params
=
BDParser
.
parseBDParameters
(
self
)
self
.
checkParam
(
'machine_name'
,
params
)
self
.
checkParam
(
'nproc'
,
params
)
self
.
checkParam
(
'run_name'
,
params
)
return
params
def
declareBDParameters
(
self
,
additional_params
=
None
,
default_additional_params
=
None
):
tmp_params
=
self
.
run_params
if
(
additional_params
):
tmp_params
.
update
(
additional_params
)
tmp_default_params
=
self
.
run_default_params
if
(
default_additional_params
):
tmp_default_params
.
update
(
default_additional_params
)
BDParser
.
declareBDParameters
(
self
,
additional_params
=
tmp_params
,
default_additional_params
=
tmp_default_params
)
def
__init__
(
self
):
BDParser
.
__init__
(
self
)
self
.
run_params
=
{}
self
.
run_params
[
"machine_name"
]
=
str
self
.
help
[
"machine_name"
]
=
"Specify the name of the machine where the job is to be launched"
self
.
run_params
[
"nproc"
]
=
int
self
.
help
[
"nproc"
]
=
"Specify the number of processors onto which this run is supposed to be launched"
self
.
run_params
[
"run_name"
]
=
str
self
.
help
[
"run_name"
]
=
"User friendly name given to this run. This is usually helpful to recall a run kind"
self
.
run_default_params
=
{}
self
.
run_default_params
[
"job_constraints"
]
=
None
Event Timeline
Log In to Comment