You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2017/11/08 14:47:30 UTC

[GitHub] cbickel commented on a change in pull request #2938: Convert inline code to attachment in database

cbickel commented on a change in pull request #2938: Convert inline code to attachment in database
URL: https://github.com/apache/incubator-openwhisk/pull/2938#discussion_r149688845
 
 

 ##########
 File path: tools/db/moveCodeToAttachment.py
 ##########
 @@ -0,0 +1,100 @@
+#!/usr/bin/env python
+'''Python script update actions.
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the 'License'); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an 'AS IS' BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+'''
+
+import argparse
+import couchdb.client
+import time
+import base64
+
+def updateJavaAction(db, doc, id):
+    updated = False
+    attachment = db.get_attachment(doc, 'jarfile')
+
+    if attachment != None:
+        print('Updating Java action: "{0}"'.format(id))
+        encodedAttachment = base64.b64encode(attachment.getvalue())
+        db.put_attachment(doc, encodedAttachment, 'codefile', 'text/plain')
+        doc = db.get(id)
+        doc['exec']['code'] = {
+            'attachmentName': 'codefile',
+            'attachmentType': 'text/plain'
+        }
+        if 'jar' in doc['exec']:
+            del doc['exec']['jar']
+        db.save(doc)
+        db.delete_attachment(doc, 'jarfile')
+        updated = True
+
+    return updated
+
+def updateNonJavaAction(db, doc, id):
+    updated = False
+    code = doc['exec']['code']
+
+    if not isinstance(code, dict):
+        print('Updating action: "{0}"'.format(id))
+        db.put_attachment(doc, code, 'codefile', 'text/plain')
+        doc = db.get(id)
+        doc['exec']['code'] = {
+            'attachmentName': 'codefile',
+            'attachmentType': 'text/plain'
+        }
+        db.save(doc)
+        updated = True
+
+    return updated
+
+def main(args):
+    db = couchdb.client.Server(args.dbUrl)[args.dbName]
+    docs = db.view('_all_docs')
+    docCount = len(docs)
+    docIndex = 1
+
+    print('Number of docs: {}'.format(docCount))
+
+    for row in docs:
+        id = row.id
+        doc = db.get(id)
+
+        print('Checking if document {0}/{1} is an action: "{2}"'.format(docIndex, docCount, id))
+
+        if 'exec' in doc and 'code' in doc['exec']:
+            print('Document is an action: "{0}"'.format(id))
+
+            if doc['exec']['kind'] != 'java':
+                updated = updateNonJavaAction(db, doc, id)
+            else:
+                updated = updateJavaAction(db, doc, id)
+
+            if updated:
+                print('Updated action: "{0}"'.format(id))
+                time.sleep(.700)
 
 Review comment:
   Why do we need this sleep here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services