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 | |
Brian O'Connor | bd6305a | 2016-04-25 14:28:12 -0700 | [diff] [blame] | 31 | NON_OSGI_TAG = 'NON-OSGI' |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 32 | |
| 33 | def mvnUrl(bundle): |
Brian O'Connor | bd6305a | 2016-04-25 14:28:12 -0700 | [diff] [blame] | 34 | #mvn-uri := 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ] |
| 35 | parts = bundle.split(':') |
| 36 | prefix = 'mvn:' |
| 37 | suffix = '' |
| 38 | if len(parts) > 3: |
| 39 | parts.insert(2, parts.pop()) # move version to the 3rd position |
| 40 | if len(parts) >= 5: |
| 41 | # check classifier for special non-OSGi tag |
| 42 | i = parts[4].find(NON_OSGI_TAG) |
| 43 | if i == 0: |
| 44 | prefix = 'wrap:' + prefix |
| 45 | suffix = '$Bundle-SymbolicName=%s.%s&Bundle-Version=%s' % tuple(parts[0:3]) |
| 46 | if len(parts[4]) == len(NON_OSGI_TAG): |
| 47 | parts.pop() # pop off empty classifier |
| 48 | if parts[3].lower() == 'jar': |
| 49 | parts.pop() # pop off default extension: jar |
| 50 | else: |
| 51 | parts[4] = parts[4][len(NON_OSGI_TAG):] |
| 52 | return prefix + '/'.join(parts) + suffix |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 53 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 54 | def generateFeatureFile(feature_repo_name, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 55 | features = [], |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 56 | **kwargs): |
| 57 | values = { |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 58 | 'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]), |
| 59 | } |
| 60 | |
| 61 | output = FEATURES_HEADER % values |
| 62 | |
| 63 | for feature in features: |
| 64 | output += feature |
| 65 | |
| 66 | output += FEATURES_FOOTER |
| 67 | return output |
| 68 | |
| 69 | def generateFeature(feature_name, |
| 70 | version, |
| 71 | title, |
| 72 | features = [], |
| 73 | bundles = [], |
| 74 | **kwargs): |
| 75 | values = { |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 76 | 'feature_name' : feature_name, |
| 77 | 'version' : version, |
| 78 | 'title' : title, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 81 | output = FEATURE_HEADER % values |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 82 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 83 | if features: |
| 84 | for feature in features: |
| 85 | output += EXISTING_FEATURE % feature |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 86 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 87 | if bundles: |
| 88 | for bundle in bundles: |
| 89 | output += BUNDLE % mvnUrl(bundle) |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 90 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 91 | output += FEATURE_FOOTER |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 92 | return output |
| 93 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 94 | |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 95 | def generateAppFile(app_name, |
| 96 | origin, |
| 97 | version, |
| 98 | title, |
| 99 | category, |
| 100 | url, |
| 101 | feature_repo_name, |
| 102 | feature_name, |
| 103 | description = None, |
| 104 | apps = [], |
| 105 | artifacts = [], |
| 106 | **kwargs): |
| 107 | values = { |
| 108 | 'app_name' : app_name, |
| 109 | 'origin' : origin, |
| 110 | 'version' : version, |
| 111 | 'title' : title, |
| 112 | 'category' : category, |
| 113 | 'url' : url, |
| 114 | 'feature_repo_name' : mvnUrl(feature_repo_name) + '/xml/features', |
| 115 | 'feature_name' : feature_name, |
| 116 | } |
| 117 | |
| 118 | values['description'] = description if description else title |
| 119 | values['apps'] = ','.join(apps) if apps else '' |
| 120 | |
| 121 | output = APP_HEADER % values |
| 122 | |
| 123 | for artifact in artifacts: |
| 124 | output += ARTIFACT % mvnUrl(artifact) |
| 125 | |
| 126 | output += APP_FOOTER |
| 127 | return output |
| 128 | |
| 129 | |
| 130 | if __name__ == '__main__': |
| 131 | import sys, optparse |
| 132 | |
| 133 | parser = optparse.OptionParser() |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 134 | 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] | 135 | parser.add_option("-a", "--app", dest="app_name", help="App Name") |
| 136 | parser.add_option("-o", "--origin", dest="origin", help="Origin") |
| 137 | parser.add_option("-c", "--category", dest="category", help="Category") |
| 138 | parser.add_option("-u", "--url", dest="url", help="URL") |
| 139 | parser.add_option("-v", "--version", dest="version", help="Version") |
| 140 | parser.add_option("-t", "--title", dest="title", help="Title") |
| 141 | parser.add_option("-r", "--repo", dest="repo_name", help="Repo Name") |
Thomas Vachuska | 7c45e28 | 2017-08-23 17:55:53 -0700 | [diff] [blame] | 142 | parser.add_option('-D', '--desc', dest='desc', help='Application description') |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 143 | |
| 144 | parser.add_option('-b', '--bundle', |
| 145 | action="append", dest='included_bundles', |
| 146 | metavar="BUNDLE", help='Included Bundle (multiple allowed)') |
| 147 | parser.add_option('-e', '--excluded-bundle', |
| 148 | action="append", dest='excluded_bundles', |
| 149 | metavar="BUNDLE", help='Excluded Bundle (multiple allowed)') |
| 150 | parser.add_option('-f', '--feature', |
| 151 | action="append", dest='features', |
| 152 | metavar="FEATURE", help='Existing Feature (multiple allowed)') |
| 153 | parser.add_option('-d', '--apps', |
| 154 | action="append", dest='apps', |
| 155 | metavar="FEATURE", help='Required App (multiple allowed)') |
| 156 | |
| 157 | parser.add_option("-A", "--write-app", dest="write_app", action="store_true") |
| 158 | parser.add_option("-F", "--write-features", dest="write_features", action="store_true") |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 159 | parser.add_option("-E", "--write-feature", dest="write_feature", action="store_true") |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 160 | |
| 161 | (options, args) = parser.parse_args() |
| 162 | |
| 163 | values = {} |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 164 | if options.feature_coords and options.version and options.title: |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 165 | parts = options.feature_coords.split(':') |
| 166 | values['feature_name'] = parts[1] if len(parts) > 1 else parts[0] |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 167 | values['version'] = options.version |
| 168 | values['title'] = options.title |
| 169 | else: |
| 170 | sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n') |
| 171 | sys.stderr.flush() |
| 172 | sys.exit(1) |
| 173 | |
| 174 | if options.app_name and options.origin and options.category and options.url: |
| 175 | values['app_name'] = options.app_name |
| 176 | values['origin'] = options.origin |
| 177 | values['category'] = options.category |
| 178 | values['url'] = options.url |
| 179 | elif options.write_app: |
| 180 | sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n') |
| 181 | sys.stderr.flush() |
| 182 | sys.exit(1) |
| 183 | |
| 184 | values['feature_repo_name'] = options.repo_name if options.repo_name \ |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame] | 185 | else options.feature_coords |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 186 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 187 | bundles = [] |
| 188 | if options.included_bundles: |
| 189 | bundles += options.included_bundles |
| 190 | if options.excluded_bundles: |
| 191 | bundles += options.excluded_bundles |
Thomas Vachuska | 7c45e28 | 2017-08-23 17:55:53 -0700 | [diff] [blame] | 192 | if options.desc: |
| 193 | values['description'] = options.desc |
| 194 | |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 195 | feature = generateFeature(bundles=bundles, |
| 196 | features=options.features, |
| 197 | **values) |
| 198 | |
| 199 | if options.write_feature: |
| 200 | print feature |
| 201 | |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 202 | if options.write_features: |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 203 | print generateFeatureFile(features=[ feature ], |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 204 | **values) |
Brian O'Connor | fc7f5fc | 2016-04-29 17:04:06 -0700 | [diff] [blame] | 205 | |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 206 | if options.write_app: |
| 207 | print generateAppFile(artifacts=options.included_bundles, |
| 208 | apps=options.apps, |
| 209 | **values) |