Skip to content

Commit

Permalink
Scripts for generating target definition file from template with exam…
Browse files Browse the repository at this point in the history
…ple template.
dxmaxwell committed Dec 7, 2015
1 parent a1d710a commit c55db29
Showing 3 changed files with 190 additions and 0 deletions.
26 changes: 26 additions & 0 deletions build/cs-studio.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="cs-studio" sequenceNumber="1">
<!-- This target definition template is meant to be expanded by the p2target.py utility. -->
<locations>
<location includeAllPlatforms="true" includeConfigurePhase="true" includeMode="slicer" includeSource="true" type="InstallableUnit">
<!-- Include all features from the CS Studio 4.1 Update Site. -->
<repository location="http://download.controlsystemstudio.org/updates/4.1"/>
</location>
<location includeAllPlatforms="true" includeConfigurePhase="true" includeMode="slicer" includeSource="true" type="InstallableUnit">
<!-- Include only the latest version of these features from the Eclipse 4.4 Update Site -->
<unit id="org.eclipse.platform.sdk"/>
<unit id="org.eclipse.jdt.feature.group" />
<repository location="http://download.eclipse.org/eclipse/updates/4.4"/>
</location>
<location includeAllPlatforms="true" includeConfigurePhase="true" includeMode="slicer" includeSource="true" type="InstallableUnit">
<!-- Include only the latest version of these features from the Eclipse Luna Update Site -->
<unit id="org.eclipse.gef.sdk.feature.group" />
<repository location="http://download.eclipse.org/releases/luna"/>
</location>
<location includeAllPlatforms="true" includeConfigurePhase="true" includeMode="slicer" includeSource="true" type="InstallableUnit">
<!-- Include all features from Orbit Update Site. -->
<repository location="http://download.eclipse.org/tools/orbit/downloads/drops/R20140114142710/repository"/>
</location>
</locations>
</target>
61 changes: 61 additions & 0 deletions build/p2list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python2

# NOTE: This script requires location of the Eclipse executable
# to be specified by the environment variable ECLIPSE_BIN.

import sys, os, os.path, re, time, subprocess
from StringIO import StringIO

DESCRIPTION="List the Installable Units contained in the specified P2 repository."

if "ECLIPSE_BIN" not in os.environ:
sys.stderr.write("Eclipse application not found, set ECLIPSE_BIN environment variable\n")
sys.exit(1)

eclipse = os.environ["ECLIPSE_BIN"]

if not os.path.isfile(eclipse):
sys.stderr.write("Eclipse application not found, executable not found: " + eclipse + "\n");
sys.exit(1)


def p2list(repo, query=None):

iu_pattern = re.compile(r"{id=(.*),version=(.*)}")

cmd_args = [ eclipse, "-nosplash", "-application", "org.eclipse.equinox.p2.director", "-repository", repo, "-lf", '{id=${id},version=${version}}', "-list" ]

if query is not None:
cmd_args.append(query)

sys.stderr.write("Querying repository: " + repo + ": ")
start = time.time()
try:
output = subprocess.check_output(cmd_args)
except subprocess.CalledProcessError as e:
sys.stderr.write("Error (%s)\n" % (e.returncode,))
sys.stderr.write(e.output)
sys.exit(1)

sys.stderr.write("Done (%.2fs)\n" % ((time.time() - start),))

ius = []
for line in StringIO(output):
result = iu_pattern.match(line)
if result is not None:
ius.append(result.group(1,2))

return ius


if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description=DESCRIPTION)
parser.add_argument("repository", help="Eclipse P2 repository (http: or file: URL)" )
parser.add_argument("query", nargs='?', help="Optional P2 query expression (e.g. 'Q:group')")

args = parser.parse_args(sys.argv[1:])

ius = p2list(args.repository, args.query)
for iu in ius:
sys.stdout.write(iu[0]+'/'+iu[1]+'\n')
103 changes: 103 additions & 0 deletions build/p2target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/python2

# NOTE: This script requires location of the Eclipse executable
# to be specified by the environment variable ECLIPSE_BIN.

import sys, p2list

from argparse import FileType
from argparse import ArgumentParser

from xml.etree import ElementTree

DESCRIPTION="""Expand the specified target definition template as follows:
All 'location' elements must contains a valid 'repository' element with a
'location' attribute. If a 'location' element does NOT contain 'unit'
elements then the P2 repository is queried for all features using
the expression 'Q:group'. The 'location' element is then filled with 'unit'
elements for the latest version of each feature contained in the repository.
If a 'location' element contains one or more 'unit' elements then the P2
repository is queried for all installable units (IUs). The 'unit' elements
must specify an 'id' attribute, but the 'version' attribute is optional.
If the specified IU is found in the repository then the 'version' attribute
is either completed or verified.
"""

parser = ArgumentParser(description=DESCRIPTION)
parser.add_argument("template", type=FileType('r'), help="Location of target definition template")
parser.add_argument("target", type=FileType('w'), help="Destination of target definition (default: stdout)", nargs='?', default=sys.stdout)

args = parser.parse_args(sys.argv[1:])


tree = ElementTree.parse(args.template)
if tree is None:
sys.stderr.write("Error parsing file: invalid XML file: " + str(template) + "\n")
sys.exit(1)

locations = tree.getroot().find("locations")
if locations is None:
sys.stderr.write("Error parsing file: missing 'locations' element\n")
sys.exit(1)

for location in locations.findall("location"):
repository = location.find("repository")
if repository is None:
sys.stderr.write("Error parsing file: missing 'repository' element\n")
sys.exit(1)
url = repository.get("location")
if url is None:
sys.stderr.write("Error parsing file: missing 'location' attribute\n")
sys.exit(1)

units = location.findall("unit")
if len(units) == 0:
elm = None
for iu in p2list.p2list(url, "Q:group"):
if elm is None or iu[0] != elm.get('id'):
elm = ElementTree.Element("unit")
elm.set('id', iu[0])
elm.set('version', iu[1])
elm.tail = '\n'
location.append(elm)
else:
elm.set('id', iu[0])
elm.set('version', iu[1])
else:
ius = p2list.p2list(url)
for unit in units:
id = unit.get('id')
if id is None:
sys.stderr.write("Error parsing file: 'unit' element missing 'id' attribute\n")
sys.exit(1)
ver = unit.get('version')
if ver is None:
location.remove(unit)
elm = None
for iu in ius:
if iu[0] == id:
if elm is None or iu[0] != elm.get('id'):
elm = ElementTree.Element("unit")
elm.set('id', iu[0])
elm.set('version', iu[1])
elm.tail = '\n'
location.append(elm)
else:
elm.set('id', iu[0])
elm.set('version', iu[1])
break
else:
sys.stderr.write("Error verifying file: IU '%s' not found in repository\n" % (id,))
sys.exit(1)
else:
for iu in ius:
if iu[0] == id and iu[1] == ver:
break
else:
sys.stderr.write("Error verifying file: IU '%s' with version '%s' not found in repository\n" % (id,ver))
sys.exit(1)


tree.write(args.target, encoding="UTF-8", xml_declaration=True)

0 comments on commit c55db29

Please sign in to comment.