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