Adding onos_stage.py and onos_feature.py for feature generation and onos-package

Change-Id: I67cb1ec9c09a194c41707ff55121dcdc54e3e4ea
diff --git a/buck-tools/BUCK b/buck-tools/BUCK
index 1b8e549..e94358f 100644
--- a/buck-tools/BUCK
+++ b/buck-tools/BUCK
@@ -19,6 +19,19 @@
   visibility = ['PUBLIC'],
 )
 
+python_binary(
+  name = 'onos-stage',
+  main = 'onos_stage.py',
+  deps = [],
+  visibility = ['PUBLIC'],
+)
+
+python_binary(
+  name = 'onos-feature',
+  main = 'onos_feature.py',
+  deps = [],
+  visibility = ['PUBLIC'],
+)
 
 python_binary(
   name = 'pack_war',
diff --git a/buck-tools/onos_feature.py b/buck-tools/onos_feature.py
new file mode 100755
index 0000000..731ce71
--- /dev/null
+++ b/buck-tools/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/buck-tools/onos_stage.py b/buck-tools/onos_stage.py
new file mode 100755
index 0000000..5433cb2
--- /dev/null
+++ b/buck-tools/onos_stage.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+#FIXME Add license
+
+import re
+from zipfile import ZipFile
+
+def stageOnos(output, files=[]):
+    # Note this is not a compressed zip
+    with ZipFile(output, 'a') as output:
+        written_files = set(output.namelist())
+        for file in files:
+            if '.zip' in file:
+                with ZipFile(file, 'r') as zip_part:
+                    for f in zip_part.namelist():
+                        dest = 'apache-karaf-3.0.5/system/' + f
+                        if dest not in written_files:
+                            output.writestr(dest, zip_part.open(f).read())
+                            written_files.add(dest)
+            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 = 'apps/%(name)s/%(name)s.oar' % { 'name': app_name}
+                    output.write(file, dest)
+                    dest = 'apps/%s/app.xml' % app_name
+                    output.writestr(dest, app_xml)
+                    for f in oar.namelist():
+                        if 'm2' in f:
+                            dest = 'apache-karaf-3.0.5/system/' + f[3:]
+                            if dest not in written_files:
+                                output.writestr(dest, oar.open(f).read())
+                                written_files.add(dest)
+            elif 'features.xml' in file:
+                dest = 'apache-karaf-3.0.5/system/org/onosproject/onos-features/1.6.0-SNAPSHOT/'
+                dest += 'onos-features-1.6.0-SNAPSHOT-features.xml'
+                with open(file) as f:
+                    output.writestr(dest, f.read())
+            # filename = file.split('/')[-1]
+            # if mvnCoords == 'APP':
+            #     dest = filename
+            # else:
+            #     groupId, artifactId, version = mvnCoords.split(':')
+            #     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 = 'system/%s/%s/%s/%s' % ( groupId, artifactId, version, filename )
+            # zip.write(file, dest)
+        output.writestr('apps/org.onosproject.drivers/active', '')
+        output.writestr('apps/org.onosproject.openflow-base/active', '')
+        output.writestr('apps/org.onosproject.lldp/active', '')
+        output.writestr('apps/org.onosproject.host/active', '')
+
+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)
+    stageOnos(output, args)