Brian O'Connor | e6ff7f0 | 2015-12-02 15:01:12 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Builds synthetic feature file that includes all core, provider, and application |
| 4 | features, so that we can pre-stage all bundles required to run ONOS off-line. |
| 5 | ''' |
| 6 | |
| 7 | import os |
| 8 | import xml.etree.ElementTree as ET |
| 9 | |
| 10 | FEATURE_TAG = '{http://karaf.apache.org/xmlns/features/v1.2.0}feature' |
| 11 | STAGED_REPOS = 'target/staged-repos.xml' |
| 12 | |
| 13 | if 'ONOS_ROOT' in os.environ: |
| 14 | ONOS_ROOT = os.environ['ONOS_ROOT'] |
| 15 | else: |
| 16 | # fallback to working directory if ONOS_ROOT is not set |
| 17 | ONOS_ROOT = os.getcwd() |
| 18 | |
| 19 | def findFeatureFiles(path=ONOS_ROOT): |
| 20 | #only descend into target directories that have pom |
| 21 | for root, dirs, files in os.walk(path): |
| 22 | if 'pom.xml' not in files: |
| 23 | if 'target' in dirs: |
| 24 | #pruning target dir with no pom.xml |
| 25 | dirs.remove('target') |
| 26 | if '/target' in root: |
| 27 | if '/classes/' in root: |
| 28 | #filter out features.xml for maven-plugin |
| 29 | continue |
| 30 | for f in files: |
| 31 | if f.endswith('features.xml'): |
| 32 | yield os.path.join(root, f) |
| 33 | |
| 34 | def featuresFromFile(file): |
| 35 | features = [] |
| 36 | tree = ET.parse(file) |
| 37 | root = tree.getroot() |
| 38 | for feature in root.findall(FEATURE_TAG): |
| 39 | features.append(feature.attrib['name']) |
| 40 | return features |
| 41 | |
| 42 | if __name__ == '__main__': |
| 43 | outputTree = ET.Element('features') |
| 44 | uberFeature = ET.Element('feature', attrib={'name' : 'onos-uber-synthetic'}) |
| 45 | for file in findFeatureFiles(): |
| 46 | features = featuresFromFile(file) |
| 47 | if len(features) > 0: |
| 48 | ET.SubElement(outputTree, 'repository').text = 'file:%s' % file |
| 49 | for feature in features: |
| 50 | ET.SubElement(uberFeature, 'feature').text = feature |
| 51 | outputTree.append(uberFeature) |
| 52 | |
| 53 | outputFile = os.path.join(os.path.dirname(os.path.realpath(__file__)), STAGED_REPOS) |
| 54 | outputDir = os.path.dirname(outputFile) |
| 55 | if not os.path.exists(outputDir): |
| 56 | os.mkdir(outputDir) |
| 57 | ET.ElementTree(outputTree).write(outputFile) |
| 58 | |
| 59 | import sys |
| 60 | if '-d' in sys.argv: |
| 61 | # -------- TODO for debug only -------- |
| 62 | def indent(elem, level=0): |
| 63 | #function borrowed from: http://effbot.org/zone/element-lib.htm#prettyprint |
| 64 | i = "\n" + level*" " |
| 65 | if len(elem): |
| 66 | if not elem.text or not elem.text.strip(): |
| 67 | elem.text = i + " " |
| 68 | if not elem.tail or not elem.tail.strip(): |
| 69 | elem.tail = i |
| 70 | for elem in elem: |
| 71 | indent(elem, level+1) |
| 72 | if not elem.tail or not elem.tail.strip(): |
| 73 | elem.tail = i |
| 74 | else: |
| 75 | if level and (not elem.tail or not elem.tail.strip()): |
| 76 | elem.tail = i |
| 77 | |
| 78 | print 'Writing to file:', outputFile |
| 79 | indent(outputTree) |
| 80 | ET.dump(outputTree) |