Commit f3ce8933 authored by cpomies's avatar cpomies
Browse files

Added comment and modified code for respect python style

parent b05ac70d
......@@ -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 extractFromJson(self, file_format_json):
def extract_from_json(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 extractFromXml(self, config_file):
def extract_from_xml(self, config_file):
self.log.info('Extract slots from %s', config_file)
try:
xmlParse = parse(config_file)
xml_parse = 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 xmlParse.findall("slot")
for el in xml_parse.findall("slot")
if el.attrib.get('disabled', 'false').lower() != 'true')
self.log.info('%s slots from %s', len(slots), config_file)
return slots
def writeFiles(self, slots, output_file):
slot_ids = getIds(slots)
#get_id = lambda slot: get_ids([slot]])[slot]
def write_files(self, slots, output_file):
slot_ids = get_ids(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.writeFiles(slots, output_file)
# Create a file that contain JobParams for each slot
self.write_files(slots, output_file)
self.log.info('End of extraction of all enable slot')
return 0
\ No newline at end of file
return 0
......@@ -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:
xmlParse = ET.parse(slot_id_file)
root = xmlParse.getroot()
xml_parse = ET.parse(slot_id_file)
root = xml_parse.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')
xmlParse = ET.ElementTree(root)
xml_parse = 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)
xmlParse.write(slot_id_file)
xml_parse.write(slot_id_file)
return res
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