From: Hyokeun Jeon Date: Tue, 27 Dec 2016 02:07:43 +0000 (+0900) Subject: Use xml parser instead of string operation for git-ref-mapping X-Git-Tag: submit/trunk/20190927.012743~574^2~4^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F45%2F107145%2F2;p=services%2Fjenkins-scripts.git Use xml parser instead of string operation for git-ref-mapping Change-Id: Ie83b4139aa7613e9aaa39174a5a6c0805dc883b9 --- diff --git a/job_ref_snapshot_info_update.py b/job_ref_snapshot_info_update.py index af8da72..1b081ad 100644 --- a/job_ref_snapshot_info_update.py +++ b/job_ref_snapshot_info_update.py @@ -19,17 +19,26 @@ import sys from common.buildtrigger import trigger_info from common.git import Git from common.mapping import git_ref_map +import xml.etree.cElementTree as ElementTree +from lxml import etree -TEMPLATE = """ """ +XML_DECLARATION = """""" def main(): """The main body""" # parameters - fields = trigger_info(os.getenv('TRIGGER_INFO')) - profile = fields['profile'] - base_project = fields['base_project'] - release_version = fields['release_id'].split('_')[-1] + if os.getenv('profile') is not None and os.getenv('release_id') is not None: + base_project = os.getenv('profile') + release_id = os.getenv('release_id') + release_version = release_id.split('_')[-1] + print 'Manual trigger: %s, %s' % (base_project, release_id) + else: + fields = trigger_info(os.getenv('TRIGGER_INFO')) + profile = fields['profile'] + base_project = fields['base_project'] + release_id = fields['release_id'] + release_version = release_id.split('_')[-1] prjdir = os.path.join(os.getenv('WORKSPACE'), os.path.basename(os.getenv('REF_MAPPING_PRJ'))) mapping_file = '{0}/git-ref-mapping.xml'.format(prjdir) @@ -47,27 +56,32 @@ def main(): need_update = False with open(mapping_file) as mf: lines = mf.readlines() - for idx,line in enumerate(lines): - for x in found: - old_item = TEMPLATE % (x[0], x[1], x[2]) - new_item = TEMPLATE % (x[0], x[1], ':'.join([':'.join(x[2].split(':')[:-1]), release_version])) - if old_item == new_item: - continue - if line.rstrip() == old_item: - lines[idx] = line.replace(old_item, new_item) - need_update = True - print ' Updating to %s for %s / %s' % (release_version, x[1], x[0]) + parser = etree.XMLParser(remove_comments=False) + tree = etree.fromstring(''.join(lines)) + + for dft in tree.getiterator('path'): + for item in dft.getiterator(): + if item.get('OBS_project') == base_project: + orgn_prj = item.get('OBS_project') + orgn_brch = item.get('name') + old_ref = item.get('OBS_staging_project') + new_ref = ':'.join(old_ref.split(':')[:-1]) + ':' + release_version + if old_ref != new_ref: + need_update = True + print ' Replace [%s][%s] %s -> %s' % (orgn_prj, orgn_brch, old_ref, new_ref) + item.set('OBS_staging_project', new_ref) + + new_xml_string = XML_DECLARATION + '\n' + etree.tostring(tree) # update git repository if need_update: with open(mapping_file, 'w') as mf: - for line in lines: - mf.write(line) + mf.write(new_xml_string) mygit = Git(prjdir) mygit.add_files('*', True) - mygit.commit_staged('Updating %s => %s' % (base_project, fields['release_id'])) + mygit.commit_staged('Updating %s => %s' % (base_project, release_id)) mygit.push(repo = 'origin', src = 'master') - print "\"Title\": \"%s\"" % (fields['release_id']) + print "\"Title\": \"%s\"" % (release_id) else: print 'No updates for git-ref-mapping.'