Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | #FIXME Add license |
| 3 | |
| 4 | ##### Templates for features.xml |
| 5 | FEATURES_HEADER = '''\ |
| 6 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 7 | <features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" |
| 8 | name="%(feature_repo_name)s"> |
| 9 | ''' |
| 10 | FEATURE_HEADER= '''\ |
| 11 | <feature name="%(feature_name)s" version="%(version)s" |
| 12 | description="%(title)s"> |
| 13 | ''' |
| 14 | EXISTING_FEATURE = ' <feature>%s</feature>\n' |
| 15 | BUNDLE = ' <bundle>%s</bundle>\n' |
| 16 | FEATURE_FOOTER = ' </feature>\n' |
| 17 | FEATURES_FOOTER = '</features>' |
| 18 | |
| 19 | ##### Templates for app.xml |
| 20 | APP_HEADER = '''\ |
| 21 | <?xml version="1.0" encoding="UTF-8"?> |
| 22 | <app name="%(app_name)s" origin="%(origin)s" version="%(version)s" |
| 23 | title="%(title)s" category="%(category)s" url="%(url)s" |
| 24 | featuresRepo="%(feature_repo_name)s" |
| 25 | features="%(feature_name)s" apps="%(apps)s"> |
| 26 | <description>%(description)s</description> |
| 27 | ''' |
| 28 | ARTIFACT = ' <artifact>%s</artifact>\n' |
| 29 | APP_FOOTER = '</app>' |
| 30 | |
| 31 | |
| 32 | def mvnUrl(bundle): |
| 33 | return 'mvn:' + bundle.replace(':', '/') |
| 34 | |
| 35 | def generateFeatureFile(feature_name, |
| 36 | version, |
| 37 | title, |
| 38 | feature_repo_name, |
| 39 | features = [], |
| 40 | bundles = [], |
| 41 | **kwargs): |
| 42 | values = { |
| 43 | 'feature_name' : feature_name, |
| 44 | 'version' : version, |
| 45 | 'title' : title, |
| 46 | 'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]), |
| 47 | } |
| 48 | |
| 49 | output = FEATURES_HEADER % values + FEATURE_HEADER % values |
| 50 | |
| 51 | for feature in features: |
| 52 | output += EXISTING_FEATURE % feature |
| 53 | |
| 54 | for bundle in bundles: |
| 55 | output += BUNDLE % mvnUrl(bundle) |
| 56 | |
| 57 | output += FEATURE_FOOTER + FEATURES_FOOTER |
| 58 | return output |
| 59 | |
| 60 | def generateAppFile(app_name, |
| 61 | origin, |
| 62 | version, |
| 63 | title, |
| 64 | category, |
| 65 | url, |
| 66 | feature_repo_name, |
| 67 | feature_name, |
| 68 | description = None, |
| 69 | apps = [], |
| 70 | artifacts = [], |
| 71 | **kwargs): |
| 72 | values = { |
| 73 | 'app_name' : app_name, |
| 74 | 'origin' : origin, |
| 75 | 'version' : version, |
| 76 | 'title' : title, |
| 77 | 'category' : category, |
| 78 | 'url' : url, |
| 79 | 'feature_repo_name' : mvnUrl(feature_repo_name) + '/xml/features', |
| 80 | 'feature_name' : feature_name, |
| 81 | } |
| 82 | |
| 83 | values['description'] = description if description else title |
| 84 | values['apps'] = ','.join(apps) if apps else '' |
| 85 | |
| 86 | output = APP_HEADER % values |
| 87 | |
| 88 | for artifact in artifacts: |
| 89 | output += ARTIFACT % mvnUrl(artifact) |
| 90 | |
| 91 | output += APP_FOOTER |
| 92 | return output |
| 93 | |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | import sys, optparse |
| 97 | |
| 98 | parser = optparse.OptionParser() |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 99 | parser.add_option("-n", "--name", dest="feature_coords", help="Feature MVN Coords") |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 100 | parser.add_option("-a", "--app", dest="app_name", help="App Name") |
| 101 | parser.add_option("-o", "--origin", dest="origin", help="Origin") |
| 102 | parser.add_option("-c", "--category", dest="category", help="Category") |
| 103 | parser.add_option("-u", "--url", dest="url", help="URL") |
| 104 | parser.add_option("-v", "--version", dest="version", help="Version") |
| 105 | parser.add_option("-t", "--title", dest="title", help="Title") |
| 106 | parser.add_option("-r", "--repo", dest="repo_name", help="Repo Name") |
| 107 | |
| 108 | parser.add_option('-b', '--bundle', |
| 109 | action="append", dest='included_bundles', |
| 110 | metavar="BUNDLE", help='Included Bundle (multiple allowed)') |
| 111 | parser.add_option('-e', '--excluded-bundle', |
| 112 | action="append", dest='excluded_bundles', |
| 113 | metavar="BUNDLE", help='Excluded Bundle (multiple allowed)') |
| 114 | parser.add_option('-f', '--feature', |
| 115 | action="append", dest='features', |
| 116 | metavar="FEATURE", help='Existing Feature (multiple allowed)') |
| 117 | parser.add_option('-d', '--apps', |
| 118 | action="append", dest='apps', |
| 119 | metavar="FEATURE", help='Required App (multiple allowed)') |
| 120 | |
| 121 | parser.add_option("-A", "--write-app", dest="write_app", action="store_true") |
| 122 | parser.add_option("-F", "--write-features", dest="write_features", action="store_true") |
| 123 | |
| 124 | (options, args) = parser.parse_args() |
| 125 | |
| 126 | values = {} |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 127 | if options.feature_coords and options.version and options.title: |
| 128 | values['feature_name'] = options.feature_coords.split(':')[1] |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 129 | values['version'] = options.version |
| 130 | values['title'] = options.title |
| 131 | else: |
| 132 | sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n') |
| 133 | sys.stderr.flush() |
| 134 | sys.exit(1) |
| 135 | |
| 136 | if options.app_name and options.origin and options.category and options.url: |
| 137 | values['app_name'] = options.app_name |
| 138 | values['origin'] = options.origin |
| 139 | values['category'] = options.category |
| 140 | values['url'] = options.url |
| 141 | elif options.write_app: |
| 142 | sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n') |
| 143 | sys.stderr.flush() |
| 144 | sys.exit(1) |
| 145 | |
| 146 | values['feature_repo_name'] = options.repo_name if options.repo_name \ |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 147 | else options.feature_coords |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 148 | |
| 149 | if options.write_features: |
| 150 | bundles = [] |
| 151 | if options.included_bundles: |
| 152 | bundles += options.included_bundles |
| 153 | if options.excluded_bundles: |
| 154 | bundles += options.excluded_bundles |
| 155 | print generateFeatureFile(bundles=bundles, |
| 156 | features=options.features, |
| 157 | **values) |
| 158 | if options.write_app: |
| 159 | print generateAppFile(artifacts=options.included_bundles, |
| 160 | apps=options.apps, |
| 161 | **values) |