Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Colas Pomies
LbNightlyTools
Commits
f3ce8933
Commit
f3ce8933
authored
Apr 22, 2015
by
cpomies
Browse files
Added comment and modified code for respect python style
parent
b05ac70d
Changes
2
Hide whitespace changes
Inline
Side-by-side
python/LbNightlyTools/EnabledSlots.py
View file @
f3ce8933
...
...
@@ -12,7 +12,6 @@
Simple script to extract slot who need to be compile
Create one file for each slot. Each file contains parameters for the next job.
Now we only have the slot name in parameter in files
'''
__author__
=
'Colas Pomies <colas.pomies@cern.ch>'
...
...
@@ -21,81 +20,83 @@ import json
from
xml.etree.ElementTree
import
parse
from
LbNightlyTools.Utils
import
JobParams
from
os.path
import
splitext
,
basename
from
LbNightlyTools.SlotBuildId
import
getIds
from
LbNightlyTools.SlotBuildId
import
get_ids
import
LbUtils.Script
class
Script
(
LbUtils
.
Script
.
PlainScript
):
'''
TODO : Explain the script
Script to create one file for all enable slots or for slots in parameters
This file contain the slot name and the slot build id
The slot build id is extract with the function get_ids
'''
__usage__
=
'%prog [options] <
format_
output_file.txt> '
__usage__
=
'%prog [options] <output_file.txt>
[<slot1> <slot2> ...]
'
__version__
=
''
def
extract
F
rom
J
son
(
self
,
file_format_json
):
def
extract
_f
rom
_j
son
(
self
,
file_format_json
):
self
.
log
.
info
(
'Extract slots from %s files'
,
file_format_json
)
slots
=
set
()
#get all json files for slot configuration
#
get all json files for slot configuration
files
=
glob
.
glob
(
file_format_json
)
for
file_name
in
files
:
try
:
with
open
(
file_name
)
as
data_file
:
data
=
json
.
load
(
data_file
)
#check if slot is not disable
if
(
not
'disabled'
in
data
)
or
data
[
'disabled'
]
==
False
:
#extract attribute slot if exist
#
check if slot is not disable
if
(
'disabled'
not
in
data
)
or
data
[
'disabled'
]
is
False
:
#
extract attribute slot if exist
if
'slot'
in
data
:
slot_name
=
data
[
'slot'
]
# if not extract slot name from filename
else
:
slot_name
=
splitext
(
basename
(
file_name
))[
0
]
slots
.
add
(
slot_name
)
self
.
log
.
debug
(
'Add %s to the slot list from %s'
,
slot_name
,
file_name
)
self
.
log
.
debug
(
'Add %s to the slot list from %s'
,
slot_name
,
file_name
)
except
:
self
.
log
.
warning
(
'Can''t find or open %s'
,
file_name
)
self
.
log
.
info
(
'%s slots from %s'
,
len
(
slots
),
file_format_json
)
return
slots
def
extract
F
rom
X
ml
(
self
,
config_file
):
def
extract
_f
rom
_x
ml
(
self
,
config_file
):
self
.
log
.
info
(
'Extract slots from %s'
,
config_file
)
try
:
xml
P
arse
=
parse
(
config_file
)
xml
_p
arse
=
parse
(
config_file
)
except
:
self
.
log
.
warning
(
'Can''t find or open %s'
,
config_file
)
return
set
()
#Extract all slots name from configuration who doesn't have [attribute disabled to true
# Extract all slots name from xml configuration file
# Extract slots with no attribute disabled or set to False
self
.
log
.
debug
(
'Get slot from %s'
,
config_file
)
slots
=
set
(
el
.
get
(
'name'
)
for
el
in
xml
P
arse
.
findall
(
"slot"
)
for
el
in
xml
_p
arse
.
findall
(
"slot"
)
if
el
.
attrib
.
get
(
'disabled'
,
'false'
).
lower
()
!=
'true'
)
self
.
log
.
info
(
'%s slots from %s'
,
len
(
slots
),
config_file
)
return
slots
def
write
F
iles
(
self
,
slots
,
output_file
):
slot_ids
=
get
I
ds
(
slots
)
#get_id = lambda slot: get_ids([slot]])[slot]
def
write
_f
iles
(
self
,
slots
,
output_file
):
slot_ids
=
get
_i
ds
(
slots
)
#
get_id = lambda slot: get_ids([slot]])[slot]
for
slot
in
slots
:
output_file_name
=
output_file
.
format
(
slot
)
open
(
output_file_name
,
'w'
).
write
(
str
(
JobParams
(
slot
=
slot
,
slot_build_id
=
slot_ids
[
slot
]))
+
'
\n
'
)
open
(
output_file_name
,
'w'
)
\
.
write
(
str
(
JobParams
(
slot
=
slot
,
slot_build_id
=
slot_ids
[
slot
]
))
+
'
\n
'
)
self
.
log
.
debug
(
'%s written'
,
output_file_name
)
self
.
log
.
info
(
'%s slots to start'
,
len
(
slots
))
def
main
(
self
):
self
.
log
.
info
(
'Starting extraction of all enable slot'
)
if
len
(
self
.
args
)
<
1
:
self
.
parser
.
error
(
'wrong number of arguments'
)
...
...
@@ -103,11 +104,13 @@ class Script(LbUtils.Script.PlainScript):
slots
=
self
.
args
[
1
:]
if
not
slots
:
slots
=
self
.
extractFromJson
(
'configs/lhcb-*.json'
)
|
self
.
extractFromXml
(
'configs/configuration.xml'
)
self
.
log
.
info
(
'Starting extraction of all enable slot'
)
slots
=
self
.
extract_from_json
(
'configs/lhcb-*.json'
)
|
\
self
.
extract_from_xml
(
'configs/configuration.xml'
)
#Create a file that contain JobParams for each slot
self
.
write
F
iles
(
slots
,
output_file
)
#
Create a file that contain JobParams for each slot
self
.
write
_f
iles
(
slots
,
output_file
)
self
.
log
.
info
(
'End of extraction of all enable slot'
)
return
0
\ No newline at end of file
return
0
python/LbNightlyTools/SlotBuildId.py
View file @
f3ce8933
...
...
@@ -19,6 +19,7 @@ import os
import
sys
import
xml.etree.ElementTree
as
ET
def
indent
(
elem
,
level
=
0
):
i
=
"
\n
"
+
level
*
" "
if
len
(
elem
):
...
...
@@ -34,9 +35,12 @@ def indent(elem, level=0):
if
level
and
(
not
elem
.
tail
or
not
elem
.
tail
.
strip
()):
elem
.
tail
=
i
def
getIds
(
slots
):
slot_id_dir
=
os
.
path
.
join
(
os
.
environ
[
'JENKINS_HOME'
],
'nightlies'
,
os
.
environ
[
'flavour'
])
#slot_id_dir = 'configs'
def
get_ids
(
slots
):
slot_id_dir
=
os
.
path
.
join
(
os
.
environ
[
'JENKINS_HOME'
],
'nightlies'
,
os
.
environ
[
'flavour'
])
# slot_id_dir = 'configs'
slot_id_file
=
os
.
path
.
join
(
slot_id_dir
,
'slot_id.xml'
)
if
not
os
.
path
.
exists
(
slot_id_dir
):
...
...
@@ -45,8 +49,8 @@ def getIds(slots):
if
os
.
path
.
isfile
(
slot_id_file
):
try
:
xml
P
arse
=
ET
.
parse
(
slot_id_file
)
root
=
xml
P
arse
.
getroot
()
xml
_p
arse
=
ET
.
parse
(
slot_id_file
)
root
=
xml
_p
arse
.
getroot
()
except
:
logging
.
error
(
'Can''t find or open %s'
,
slot_id_file
)
...
...
@@ -55,7 +59,7 @@ def getIds(slots):
os
.
open
(
slot_id_file
,
os
.
O_CREAT
,
0644
)
logging
.
info
(
'File %s created'
,
slot_id_file
)
root
=
ET
.
Element
(
'slot_id'
)
xml
P
arse
=
ET
.
ElementTree
(
root
)
xml
_p
arse
=
ET
.
ElementTree
(
root
)
res
=
{}
add_slot
=
False
...
...
@@ -67,7 +71,7 @@ def getIds(slots):
if
slot
is
not
None
:
slot_id
=
slot
.
get
(
'last_id'
)
if
not
slot_id
:
logging
.
error
(
'No
attribute
current_id on the slot %s'
,
slot_name
)
logging
.
error
(
'No current_id on the slot %s'
,
slot_name
)
sys
.
exit
(
2
)
slot_id
=
int
(
slot_id
)
+
1
slot
.
set
(
'last_id'
,
str
(
slot_id
))
...
...
@@ -85,6 +89,6 @@ def getIds(slots):
if
add_slot
:
indent
(
root
)
xml
P
arse
.
write
(
slot_id_file
)
xml
_p
arse
.
write
(
slot_id_file
)
return
res
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment