import Python scripts into bazel

Change-Id: Ide26371f903356aae829d3235fe5d4bf789c1151
diff --git a/tools/build/bazel/BUILD b/tools/build/bazel/BUILD
index e69de29..e513517 100644
--- a/tools/build/bazel/BUILD
+++ b/tools/build/bazel/BUILD
@@ -0,0 +1,27 @@
+py_binary(
+    name = "onos_app_writer",
+    srcs = [ "onos_app.py" ],
+    main = "onos_app.py",
+    visibility = ["//visibility:public"],
+)
+
+py_binary(
+    name = "onos-app-oar",
+    srcs = [ "onos_oar.py" ],
+    main = "onos_oar.py",
+    visibility = ["//visibility:public"],
+)
+
+py_binary(
+  name = "onos-stage",
+  srcs = [ "onos_stage.py" ],
+  main = "onos_stage.py",
+  visibility = ["//visibility:public"],
+)
+
+py_binary(
+  name = 'onos-feature',
+  srcs = [ "onos_feature.py" ],
+  main = 'onos_feature.py',
+  visibility = ["//visibility:public"],
+)
diff --git a/tools/build/bazel/onos_app.py b/tools/build/bazel/onos_app.py
new file mode 100755
index 0000000..bbb053c
--- /dev/null
+++ b/tools/build/bazel/onos_app.py
@@ -0,0 +1,219 @@
+#!/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'
+SECURITY = '''\
+    <security>
+%s
+    </security>\n'''
+APP_FOOTER = '</app>'
+
+NON_OSGI_TAG = 'NON-OSGI'
+
+def mvnUrl(bundle):
+    #mvn-uri := 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ]
+    parts = bundle.split(':')
+    prefix = 'mvn:'
+    suffix = ''
+    if len(parts) > 3:
+        parts.insert(2, parts.pop()) # move version to the 3rd position
+    if len(parts) >= 5:
+        # check classifier for special non-OSGi tag
+        i = parts[4].find(NON_OSGI_TAG)
+        if i == 0:
+            prefix = 'wrap:' + prefix
+            suffix = '$Bundle-SymbolicName=%s.%s&amp;Bundle-Version=%s' % tuple(parts[0:3])
+            if len(parts[4]) == len(NON_OSGI_TAG):
+                parts.pop() # pop off empty classifier
+                if parts[3].lower() == 'jar':
+                    parts.pop() # pop off default extension: jar
+            else:
+                parts[4] = parts[4][len(NON_OSGI_TAG):]
+    return prefix + '/'.join(parts) + suffix
+
+def generateFeatureFile(feature_repo_name,
+                        features = [],
+                        **kwargs):
+    values = {
+        'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]),
+    }
+
+    output = FEATURES_HEADER % values
+
+    for feature in features:
+        output += feature
+
+    output += FEATURES_FOOTER
+    return output
+
+def generateFeature(feature_name,
+                    version,
+                    title,
+                    features = [],
+                    bundles = [],
+                    **kwargs):
+    values = {
+        'feature_name' : feature_name,
+        'version' : version,
+        'title' : title,
+    }
+
+    output = FEATURE_HEADER % values
+
+    if features:
+        for feature in features:
+            output += EXISTING_FEATURE % feature
+
+    if bundles:
+        for bundle in bundles:
+            output += BUNDLE % mvnUrl(bundle)
+
+    output += FEATURE_FOOTER
+    return output
+
+
+def generateAppFile(app_name,
+                    origin,
+                    version,
+                    title,
+                    category,
+                    url,
+                    feature_repo_name,
+                    feature_name,
+                    description = None,
+                    apps = [],
+                    artifacts = [],
+                    security= None,
+                    **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)
+
+    if security is not None:
+        output += SECURITY % security
+
+    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('-D', '--desc',     dest='desc',         help='Application description')
+    parser.add_option('-s', '--security', dest='security',     help='Application security')
+
+    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")
+    parser.add_option("-E", "--write-feature", dest="write_feature", action="store_true")
+
+    (options, args) = parser.parse_args()
+
+    values = {}
+    if options.feature_coords and options.version and options.title:
+        parts = options.feature_coords.split(':')
+        values['feature_name'] = parts[1] if len(parts) > 1 else parts[0]
+        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
+
+    bundles = []
+    if options.included_bundles:
+        bundles += options.included_bundles
+    if options.excluded_bundles:
+        bundles += options.excluded_bundles
+    if options.desc:
+        values['description'] = options.desc
+
+    feature = generateFeature(bundles=bundles,
+                              features=options.features,
+                              **values)
+
+    if options.write_feature:
+        print feature
+
+    if options.write_features:
+        print generateFeatureFile(features=[ feature ],
+                                  **values)
+
+    if options.write_app:
+        print generateAppFile(artifacts=options.included_bundles,
+                              apps=options.apps,
+                              security=options.security,
+                              **values)
\ No newline at end of file
diff --git a/tools/build/bazel/onos_feature.py b/tools/build/bazel/onos_feature.py
new file mode 100755
index 0000000..731ce71
--- /dev/null
+++ b/tools/build/bazel/onos_feature.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+from zipfile import ZipFile
+
+def generateOar(output, files=[]):
+    # Note this is not a compressed zip
+    with ZipFile(output, 'w') as zip:
+        for file, mvnCoords in files:
+            filename = file.split('/')[-1]
+            if mvnCoords == 'NONE':
+                dest = filename
+            else:
+                parts = mvnCoords.split(':')
+                if len(parts) > 3:
+                    parts.insert(2, parts.pop()) # move version to the 3rd position
+                groupId, artifactId, version = parts[0:3]
+                groupId = groupId.replace('.', '/')
+                extension = filename.split('.')[-1]
+                if extension == 'jar':
+                    filename = '%s-%s.jar' % ( artifactId, version )
+                elif 'features.xml' in filename:
+                    filename = '%s-%s-features.xml' % ( artifactId, version )
+                dest = '%s/%s/%s/%s' % ( groupId, artifactId, version, filename )
+            zip.write(file, dest)
+
+if __name__ == '__main__':
+    import sys
+
+    if len(sys.argv) < 2:
+        print 'USAGE'
+        sys.exit(1)
+
+    output = sys.argv[1]
+    args = sys.argv[2:]
+
+    if len(args) % 2 != 0:
+        print 'There must be an even number of args: file mvn_coords'
+        sys.exit(2)
+
+    files = zip(*[iter(args)]*2)
+    generateOar(output, files)
diff --git a/tools/build/bazel/onos_oar.py b/tools/build/bazel/onos_oar.py
new file mode 100755
index 0000000..30f70f5
--- /dev/null
+++ b/tools/build/bazel/onos_oar.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+from zipfile import ZipFile
+
+def generateOar(output, files=[]):
+    # Note this is not a compressed zip
+    with ZipFile(output, 'w') as zip:
+        for file, mvnCoords in files:
+            filename = file.split('/')[-1]
+            if mvnCoords == 'NONE':
+                dest = filename
+            else:
+                parts = mvnCoords.split(':')
+                if len(parts) > 3:
+                    parts.insert(2, parts.pop()) # move version to the 3rd position
+                groupId, artifactId, version = parts[0:3]
+                groupId = groupId.replace('.', '/')
+                extension = filename.split('.')[-1]
+                if extension == 'jar':
+                    filename = '%s-%s.jar' % ( artifactId, version )
+                elif 'features.xml' in filename:
+                    filename = '%s-%s-features.xml' % ( artifactId, version )
+                dest = 'm2/%s/%s/%s/%s' % ( groupId, artifactId, version, filename )
+            zip.write(file, dest)
+
+if __name__ == '__main__':
+    import sys
+
+    if len(sys.argv) < 2:
+        print 'USAGE'
+        sys.exit(1)
+
+    output = sys.argv[1]
+    args = sys.argv[2:]
+
+    if len(args) % 2 != 0:
+        print 'There must be an even number of args: file mvn_coords'
+        sys.exit(2)
+
+    files = zip(*[iter(args)]*2)
+    generateOar(output, files)
diff --git a/tools/build/bazel/onos_stage.py b/tools/build/bazel/onos_stage.py
new file mode 100755
index 0000000..8991dfa
--- /dev/null
+++ b/tools/build/bazel/onos_stage.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+import re
+import os
+from zipfile import ZipFile
+from tarfile import TarFile, TarInfo
+import tarfile
+import time
+from cStringIO import StringIO
+import subprocess
+
+
+written_files = set()
+now = time.time()
+
+def addFile(tar, dest, file, file_size):
+    if dest not in written_files:
+        info = TarInfo(dest)
+        info.size = file_size
+        info.mtime = now
+        info.mode = 0777
+        tar.addfile(info, fileobj=file)
+        written_files.add(dest)
+
+def addString(tar, dest, string):
+    if dest not in written_files:
+        print dest, string
+        info = TarInfo(dest)
+        info.size = len(string)
+        info.mtime = now
+        info.mode = 0777
+        file = StringIO(string)
+        tar.addfile(info, fileobj=file)
+        file.close()
+        written_files.add(dest)
+
+def getHash():
+    p = subprocess.Popen('git rev-parse --verify HEAD --short', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
+    (output, err) = p.communicate()
+    return output if p.wait() == 0 else '0000000000'
+
+def stageOnos(output, version, files=[]):
+    base = 'onos-%s/' % version
+
+    runtimeVersion = version
+    if version.endswith('-SNAPSHOT'):
+        runtimeVersion = version.replace('-SNAPSHOT', '.%s' % getHash())
+
+    # Note this is not a compressed zip
+    with tarfile.open(output, 'w:gz') as output:
+        for file in files:
+            if '.zip' in file:
+                with ZipFile(file, 'r') as zip_part:
+                    for f in zip_part.infolist():
+                        dest = f.filename
+                        if base not in dest:
+                            dest = base + 'apache-karaf-3.0.8/system/' + f.filename
+                        addFile(output, dest, zip_part.open(f), f.file_size)
+            elif '.oar' in file:
+                with ZipFile(file, 'r') as oar:
+                    app_xml = oar.open('app.xml').read()
+                    app_name = re.search('name="([^"]+)"', app_xml).group(1)
+                    dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name}
+                    addFile(output, dest, open(file), os.stat(file).st_size)
+                    dest = base + 'apps/%s/app.xml' % app_name
+                    addString(output, dest, app_xml)
+                    for f in oar.infolist():
+                        filename = f.filename
+                        if 'm2' in filename:
+                            dest = base + 'apache-karaf-3.0.8/system/' + filename[3:]
+                            if dest not in written_files:
+                                addFile(output, dest, oar.open(f), f.file_size)
+                                written_files.add(dest)
+            elif 'features.xml' in file:
+                dest = base + 'apache-karaf-3.0.8/system/org/onosproject/onos-features/%s/' % version
+                dest += 'onos-features-%s-features.xml' % version
+                with open(file) as f:
+                    addFile(output, dest, f, os.stat(file).st_size)
+        addString(output, base + 'apps/org.onosproject.drivers/active', '')
+        addString(output, base + 'VERSION', runtimeVersion)
+
+if __name__ == '__main__':
+    import sys
+
+    if len(sys.argv) < 3:
+        print 'USAGE' #FIXME
+        sys.exit(1)
+
+    output = sys.argv[1]
+    version = sys.argv[2]
+    args = sys.argv[3:]
+
+    stageOnos(output, version, args)