From a622b7fe17a94698c1d242772d5d1b1d48761a29 Mon Sep 17 00:00:00 2001 From: eschcam Date: Mon, 19 Jun 2023 17:39:10 +0100 Subject: Migrate Python 2 files to Python 3 Issue-ID: SDC-4498 Signed-off-by: eschcam Change-Id: I6b55fcd799371dd209898607c90d5c348e242c36 --- .../main/resources/scripts/python/duplicates.py | 60 ++-- .../scripts/python/duplicatesAndRemove.py | 238 ++++++++-------- .../src/main/resources/scripts/python/graphSize.py | 83 +++--- .../resources/scripts/python/user/exportUsers.py | 161 +++++------ .../resources/scripts/python/user/importUsers.py | 304 ++++++++++----------- .../inputfiles/call_home.py | 8 +- .../inputfiles/check_availability.py | 8 +- .../inputfiles/register_status.py | 30 +- .../inputfiles/wait_for_resources.py | 12 +- .../action_library_client/action_library_client.py | 59 ++-- openecomp-be/tools/build/scripts/parse-json.py | 14 +- openecomp-be/tools/migration/1607_to_1610.py | 192 ++++++------- 12 files changed, 599 insertions(+), 570 deletions(-) diff --git a/asdctool/src/main/resources/scripts/python/duplicates.py b/asdctool/src/main/resources/scripts/python/duplicates.py index be60b05909..7228677734 100644 --- a/asdctool/src/main/resources/scripts/python/duplicates.py +++ b/asdctool/src/main/resources/scripts/python/duplicates.py @@ -1,47 +1,49 @@ import json import sys -dict = {} -dupliacteUid = {} -#debugFlag = True -debugFlag = False +dict = {} +dupliacteUid = {} +# debugFlag = True +debugFlag = False + def debug(str1, str2=""): 'print only if debug enabled' - if (debugFlag == True): print str1, str2 + if debugFlag: + print(str1, str2) -print 'Number of arguments:', len(sys.argv), 'arguments.' +print('Number of arguments:', len(sys.argv), 'arguments.') with open(sys.argv[1]) as json_file: json_data = json.load(json_file) for x in json_data['vertices']: - uid = None - nodeLabel=x.get('nodeLabel') - debug(nodeLabel) - if ( nodeLabel == 'user' ): - uid = x['userId'] - elif ( nodeLabel == 'tag' ): - uid = x['name'] - elif ( nodeLabel == None ): - pass - elif ( nodeLabel == 'lockNode' ): - uid = x.get('uid') - else: uid = x['uid'] - - debug(nodeLabel, uid) - - existId = dict.get(uid) - if (existId == None): - dict[uid] = x.get('_id') - else: - dupliacteUid[uid] = existId - - print dupliacteUid + uid = None + nodeLabel = x.get('nodeLabel') + debug(nodeLabel) + if nodeLabel == 'user': + uid = x['userId'] + elif nodeLabel == 'tag': + uid = x['name'] + elif nodeLabel is None: + pass + elif nodeLabel == 'lockNode': + uid = x.get('uid') + else: + uid = x['uid'] + + debug(nodeLabel, uid) + + existId = dict.get(uid) + if existId == None: + dict[uid] = x.get('_id') + else: + dupliacteUid[uid] = existId + + print(dupliacteUid) # with open('data.txt', 'w') as outfile: # json.dump(json_data, outfile) - # print x['uid'] diff --git a/asdctool/src/main/resources/scripts/python/duplicatesAndRemove.py b/asdctool/src/main/resources/scripts/python/duplicatesAndRemove.py index a4bd35dd2b..086ab926ba 100644 --- a/asdctool/src/main/resources/scripts/python/duplicatesAndRemove.py +++ b/asdctool/src/main/resources/scripts/python/duplicatesAndRemove.py @@ -1,136 +1,144 @@ +import getopt import json -import sys, getopt +import sys from collections import OrderedDict -dict = {} -dupliacteUid = {} -#debugFlag = True -debugFlag = False +dict = {} +dupliacteUid = {} +# debugFlag = True +debugFlag = False + def join_strings(lst): concat = "" for string in lst: - if (string != None): - if (type(string) == int): - string = str(string) - concat += (string + " ") + if string is None: + if type(string) == int: + string = str(string) + concat += (string + " ") return concat + def debug(desc, *args): - 'print only if debug enabled' - if (debugFlag == True): - print desc, join_strings(args) + 'print only if debug enabled' + if debugFlag: + print(desc, join_strings(args)) + def log(desc, arg): - 'print log info' - print desc, arg + 'print log info' + print(desc, arg) + def getUid(vertex): - uid = None - nodeLabel=vertex.get('nodeLabel') - debug(nodeLabel) - if ( nodeLabel == 'user' ): - uid = vertex['userId'] - elif ( nodeLabel == 'tag' ): - uid = vertex['name'] - elif ( nodeLabel == None ): - pass - elif ( nodeLabel == 'lockNode' ): - uid = vertex.get('uid') - else: uid = vertex['uid'] - - debug(nodeLabel, uid) - - return uid + uid = None + nodeLabel = vertex.get('nodeLabel') + debug(nodeLabel) + if nodeLabel == 'user': + uid = vertex['userId'] + elif nodeLabel == 'tag': + uid = vertex['name'] + elif nodeLabel is None: + pass + elif nodeLabel == 'lockNode': + uid = vertex.get('uid') + else: + uid = vertex['uid'] + + debug(nodeLabel, uid) + + return uid + def generateFile(inputFile, outputFile): - - with open(inputFile) as json_file: - dupliacteUid = {} - json_data = json.load(json_file) - for x in json_data['vertices']: - uid = getUid(x) - - existId = dict.get(uid) - if (existId == None): - dict[uid] = x.get('_id') - else: - dupliacteUid[uid] = existId - - log("duplicate ids", dupliacteUid) - - json_data_vertices = json_data['vertices'] - log("number of vertices is", len(json_data_vertices)) - - ids = {} - deleteIndexes = [] - - for i in xrange(len(json_data_vertices)): - #print "****** ", i, " *************" - #print json_data_vertices[i] - id = json_data_vertices[i]["_id"] - uid = getUid(json_data_vertices[i]) - isDuplicateId = dupliacteUid.get(uid) - if (isDuplicateId != None): - debug("uid to id pair", uid if uid != None else 'None', id) - value = ids.get(uid) - if (value == None): - list = [id,] - ids[uid] = list - else: - value.append(id) - deleteIndexes.append(id) - - log("ids", ids) - log("deleteIndexes", deleteIndexes) - log("deleteIndexes size", len(deleteIndexes)) - - filter_vertex = [ x for x in json_data_vertices if x.get('_id') not in deleteIndexes ] - json_data['vertices'] = filter_vertex - - log("number of vertexes after filter", len(filter_vertex)) - - json_data_edges = json_data['edges'] - - log("number of edges", len(json_data_edges)) - - filter_edge = [ x for x in json_data_edges if x['_outV'] not in (deleteIndexes) and x['_inV'] not in (deleteIndexes) ] - json_data['edges'] = filter_edge - - log("number of edges after filter", len(json_data['edges'])) - - json_data = OrderedDict(sorted(json_data.items(), key=lambda t: t[0], reverse=True)) - - with open(outputFile, 'w') as outfile: - #json.dump(json_data, outfile) - json.dump(json_data, outfile) - log("output file is", outputFile); + with open(inputFile) as json_file: + dupliacteUid = {} + json_data = json.load(json_file) + for x in json_data['vertices']: + uid = getUid(x) + + existId = dict.get(uid) + if existId is None: + dict[uid] = x.get('_id') + else: + dupliacteUid[uid] = existId + + log("duplicate ids", dupliacteUid) + + json_data_vertices = json_data['vertices'] + log("number of vertices is", len(json_data_vertices)) + + ids = {} + deleteIndexes = [] + + for i in xrange(len(json_data_vertices)): + # print "****** ", i, " *************" + # print json_data_vertices[i] + id = json_data_vertices[i]["_id"] + uid = getUid(json_data_vertices[i]) + isDuplicateId = dupliacteUid.get(uid) + if isDuplicateId is not None: + debug("uid to id pair", uid if uid != None else 'None', id) + value = ids.get(uid) + if value is None: + list = [id, ] + ids[uid] = list + else: + value.append(id) + deleteIndexes.append(id) + + log("ids", ids) + log("deleteIndexes", deleteIndexes) + log("deleteIndexes size", len(deleteIndexes)) + + filter_vertex = [x for x in json_data_vertices if x.get('_id') not in deleteIndexes] + json_data['vertices'] = filter_vertex + + log("number of vertexes after filter", len(filter_vertex)) + + json_data_edges = json_data['edges'] + + log("number of edges", len(json_data_edges)) + + filter_edge = [x for x in json_data_edges if + x['_outV'] not in deleteIndexes and x['_inV'] not in deleteIndexes] + json_data['edges'] = filter_edge + + log("number of edges after filter", len(json_data['edges'])) + + json_data = OrderedDict(sorted(json_data.items(), key=lambda t: t[0], reverse=True)) + + with open(outputFile, 'w') as outfile: + # json.dump(json_data, outfile) + json.dump(json_data, outfile) + log("output file is", outputFile) + def main(argv): - print 'Number of arguments:', len(sys.argv), 'arguments.' - inputfile = None - outputfile = '' - try: - opts, args = getopt.getopt(argv,"h:i:o:",["ifile=","ofile="]) - except getopt.GetoptError: - print sys.argv[0], '-i ' - sys.exit(2) - for opt, arg in opts: - if opt == '-h': - print sys.argv[0], '-i ' - sys.exit(3) - elif opt in ("-i", "--ifile"): - inputfile = arg - - if ( inputfile == None ): - print sys.argv[0] ,'-i ' - sys.exit(3) - - print 'Input file is "', inputfile - generateFile(inputfile, inputfile + '.noduplicates') - + print('Number of arguments:', len(sys.argv), 'arguments.') + inputfile = None + outputfile = '' + try: + opts, args = getopt.getopt(argv, "h:i:o:", ["ifile=", "ofile="]) + except getopt.GetoptError: + print(sys.argv[0], '-i ') + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print(sys.argv[0], '-i ') + sys.exit(3) + elif opt in ("-i", "--ifile"): + inputfile = arg + + if (inputfile == None): + print(sys.argv[0], '-i ') + sys.exit(3) + + print('Input file is "', inputfile) + generateFile(inputfile, inputfile + '.noduplicates') + if __name__ == "__main__": - main(sys.argv[1:]) - + main(sys.argv[1:]) + # print x['uid'] diff --git a/asdctool/src/main/resources/scripts/python/graphSize.py b/asdctool/src/main/resources/scripts/python/graphSize.py index 6c36ec6844..236da92ecd 100644 --- a/asdctool/src/main/resources/scripts/python/graphSize.py +++ b/asdctool/src/main/resources/scripts/python/graphSize.py @@ -1,56 +1,59 @@ import json import sys, getopt -dict = {} -dupliacteUid = {} -#debugFlag = True -debugFlag = False +dict = {} +dupliacteUid = {} +# debugFlag = True +debugFlag = False + def debug(desc, *args): - 'print only if debug enabled' - if (debugFlag == True): - print desc, join_strings(args) + 'print only if debug enabled' + if debugFlag: + print(desc, join_strings(args)) + def log(desc, arg): - 'print log info' - print desc, arg + 'print log info' + print(desc, arg) + def graphSize(inputFile): - - with open(inputFile) as json_file: - json_data = json.load(json_file) + with open(inputFile) as json_file: + json_data = json.load(json_file) - json_data_vertices = json_data['vertices'] - log("number of vertices is", len(json_data_vertices)) + json_data_vertices = json_data['vertices'] + log("number of vertices is", len(json_data_vertices)) + + json_data_edges = json_data['edges'] + log("number of edges is", len(json_data_edges)) - json_data_edges = json_data['edges'] - log("number of edges is", len(json_data_edges)) def main(argv): - print 'Number of arguments:', len(sys.argv), 'arguments.' - inputfile = None - outputfile = '' - try: - opts, args = getopt.getopt(argv,"h:i:o:",["ifile=","ofile="]) - except getopt.GetoptError: - print sys.argv[0], '-i ' - sys.exit(2) - for opt, arg in opts: - if opt == '-h': - print sys.argv[0], '-i ' - sys.exit(3) - elif opt in ("-i", "--ifile"): - inputfile = arg - - if ( inputfile == None ): - print sys.argv[0], '-i ' - sys.exit(3) - - print 'Input file is ', inputfile - graphSize(inputfile) - + print('Number of arguments:', len(sys.argv), 'arguments.') + inputfile = None + outputfile = '' + try: + opts, args = getopt.getopt(argv, "h:i:o:", ["ifile=", "ofile="]) + except getopt.GetoptError: + print(sys.argv[0], '-i ') + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print(sys.argv[0], '-i ') + sys.exit(3) + elif opt in ("-i", "--ifile"): + inputfile = arg + + if (inputfile == None): + print(sys.argv[0], '-i ') + sys.exit(3) + + print('Input file is ', inputfile) + graphSize(inputfile) + if __name__ == "__main__": - main(sys.argv[1:]) - + main(sys.argv[1:]) + # print x['uid'] diff --git a/asdctool/src/main/resources/scripts/python/user/exportUsers.py b/asdctool/src/main/resources/scripts/python/user/exportUsers.py index ed7515cc3e..058ad926c8 100644 --- a/asdctool/src/main/resources/scripts/python/user/exportUsers.py +++ b/asdctool/src/main/resources/scripts/python/user/exportUsers.py @@ -3,7 +3,6 @@ import sys, getopt from StringIO import StringIO import json - #################################################################################################################################################################################### # # # Export all active users to file - for 1602+ # @@ -18,113 +17,117 @@ import json ALL_USERS_SUFFIX = '/sdc2/rest/v1/user/users' + def errorAndExit(errorCode, errorDesc): - if ( errorCode > 0 ): - print("status=" + str(errorCode) + ". " + errorDesc) - else: - print("status=" + str(errorCode)) - sys.exit(errorCode) + if errorCode > 0: + print("status=" + str(errorCode) + ". " + errorDesc) + else: + print("status=" + str(errorCode)) + sys.exit(errorCode) + def getUsers(scheme, beHost, bePort, adminUser): + try: + buffer = StringIO() + c = pycurl.Curl() - try: - buffer = StringIO() - c = pycurl.Curl() + url = scheme + '://' + beHost + ':' + bePort + ALL_USERS_SUFFIX + print(url) + c.setopt(c.URL, url) + c.setopt(c.WRITEFUNCTION, buffer.write) + # c.setopt(c.WRITEFUNCTION, lambda x: None) + adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) - url = scheme + '://' + beHost + ':' + bePort + ALL_USERS_SUFFIX - print(url) - c.setopt(c.URL, url) - c.setopt(c.WRITEFUNCTION, buffer.write) - #c.setopt(c.WRITEFUNCTION, lambda x: None) - adminHeader = 'USER_ID: ' + adminUser - c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) + if scheme == 'https': + c.setopt(pycurl.SSL_VERIFYPEER, 0) + c.setopt(pycurl.SSL_VERIFYHOST, 0) - if scheme == 'https': - c.setopt(pycurl.SSL_VERIFYPEER, 0) - c.setopt(pycurl.SSL_VERIFYHOST, 0) + res = c.perform() + # print(res) - res = c.perform() - #print(res) + # print('Status: %d' % c.getinfo(c.RESPONSE_CODE)) - #print('Status: %d' % c.getinfo(c.RESPONSE_CODE)) + c.close() - c.close() + body = buffer.getvalue() - body = buffer.getvalue() + # print(body) - #print(body) + return body, None - return (body, None) + except Exception as inst: + print(inst) + # print type(inst) # the exception instance + # print inst.args # arguments stored in .args + # print inst # __str__ allows args to be printed directly + # x, y = inst.args + # print 'x =', x - except Exception as inst: - print inst - #print type(inst) # the exception instance - #print inst.args # arguments stored in .args - #print inst # __str__ allows args to be printed directly - #x, y = inst.args - #print 'x =', x - - return (None, inst) + return None, inst def usage(): - print sys.argv[0], '[optional -s | --scheme=, default http] [-i | --ip=] [-p | --port= ] [-f | --ofile= ]' + print(sys.argv[0], + '[optional -s | --scheme=, default http] [-i | --ip=] [-p | --port= ] [-f | --ofile= ]') + def main(argv): - print 'Number of arguments:', len(sys.argv), 'arguments.' + print('Number of arguments:', len(sys.argv), 'arguments.') + + adminHeader = 'jh0003' + beHost = 'localhost' + bePort = '8080' + outputfile = None + scheme = 'http' + + try: + opts, args = getopt.getopt(argv, "i:p:f:h:s:", ["ip=", "port=", "ofile=", "scheme="]) + except getopt.GetoptError: + usage() + errorAndExit(2, 'Invalid input') - adminHeader = 'jh0003' - beHost = 'localhost' - bePort = '8080' - outputfile = None - scheme = 'http' + for opt, arg in opts: + # print opt, arg + if opt == '-h': + usage() + sys.exit(3) + elif opt in ("-i", "--ip"): + beHost = arg + elif opt in ("-p", "--port"): + bePort = arg + elif opt in ("-f", "--ofile"): + outputfile = arg + elif opt in ("-s", "--scheme"): + scheme = arg - try: - opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ofile=","scheme="]) - except getopt.GetoptError: - usage() - errorAndExit(2, 'Invalid input') - - for opt, arg in opts: - #print opt, arg - if opt == '-h': - usage() - sys.exit(3) - elif opt in ("-i", "--ip"): - beHost = arg - elif opt in ("-p", "--port"): - bePort = arg - elif opt in ("-f", "--ofile"): - outputfile = arg - elif opt in ("-s", "--scheme"): - scheme = arg + print('scheme =', scheme, ', be host =', beHost, ', be port =', bePort, ', output file =', outputfile) - print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', output file =',outputfile + if outputfile == None: + usage() + sys.exit(3) - if ( outputfile == None ): - usage() - sys.exit(3) + users = getUsers(scheme, beHost, bePort, adminHeader) + error = users[1] + body = users[0] - users = getUsers(scheme, beHost, bePort, adminHeader) - error = users[1] - body = users[0] + if error is not None: + errorAndExit(5, str(error)) - if ( error != None ): - errorAndExit(5, str(error)) + # print body - #print body + io = StringIO(body) + usersAsJson = json.load(io) - io = StringIO(body) - usersAsJson = json.load(io) + writeFile = open(outputfile, 'w') - writeFile = open(outputfile, 'w') + json.dump(usersAsJson, writeFile) - json.dump(usersAsJson, writeFile) + writeFile.close() - writeFile.close() + print("-------------------------------------------") + errorAndExit(0, None) - print("-------------------------------------------") - errorAndExit(0, None) if __name__ == "__main__": - main(sys.argv[1:]) + main(sys.argv[1:]) diff --git a/asdctool/src/main/resources/scripts/python/user/importUsers.py b/asdctool/src/main/resources/scripts/python/user/importUsers.py index 82ddec5139..c15ecb7b7a 100644 --- a/asdctool/src/main/resources/scripts/python/user/importUsers.py +++ b/asdctool/src/main/resources/scripts/python/user/importUsers.py @@ -4,6 +4,7 @@ from StringIO import StringIO import json import copy + ##################################################################################################################################################################################### # # # Import all users from a given file # @@ -18,210 +19,209 @@ import copy def importUsers(scheme, beHost, bePort, users, adminUser): - - result = [] - - for user in users: - - #print("Going to add user " + user['userId']) - - getRes = getUser(scheme, beHost, bePort, user) - userId = getRes[0] - error = getRes[1] - #print error - if ( error != None and error == 404 ): - res = createUser(scheme, beHost, bePort, user ,adminUser) - result.append(res) - else: - if ( error == 200 ): - curResult = (userId, 409) - result.append(curResult) - else: - result.append(getRes) - - return result + result = [] + + for user in users: + + # print("Going to add user " + user['userId']) + + getRes = getUser(scheme, beHost, bePort, user) + userId = getRes[0] + error = getRes[1] + # print error + if error is not None and error == 404: + res = createUser(scheme, beHost, bePort, user, adminUser) + result.append(res) + else: + if error == 200: + curResult = (userId, 409) + result.append(curResult) + else: + result.append(getRes) + + return result def convertUsersToCreationObject(users): + cloneUsers = copy.deepcopy(users) + for user in cloneUsers: + # print user + if user.get('fullName') is not None: + del user['fullName'] + # user['userId'] = user['userId'] + '1' + # print user - cloneUsers = copy.deepcopy(users) - for user in cloneUsers: - #print user - if (user.get('fullName') != None): - del user['fullName'] - #user['userId'] = user['userId'] + '1' - #print user + return cloneUsers - return cloneUsers def getUser(scheme, beHost, bePort, user): + userId = user['userId'] + try: + buffer = StringIO() + c = pycurl.Curl() + + # print type(userId) + url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId) + c.setopt(c.URL, url) - userId = user['userId'] - try: - buffer = StringIO() - c = pycurl.Curl() + # adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json']) + c.setopt(c.WRITEFUNCTION, lambda x: None) - #print type(userId) - url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user/' + str(userId) - c.setopt(c.URL, url) + if scheme == 'https': + c.setopt(pycurl.SSL_VERIFYPEER, 0) + c.setopt(pycurl.SSL_VERIFYHOST, 0) - #adminHeader = 'USER_ID: ' + adminUser - c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json']) - c.setopt(c.WRITEFUNCTION, lambda x: None) + res = c.perform() - if scheme == 'https': - c.setopt(pycurl.SSL_VERIFYPEER, 0) - c.setopt(pycurl.SSL_VERIFYHOST, 0) + # print("Before get response code") + httpRes = c.getinfo(c.RESPONSE_CODE) + # print("After get response code") + responseCode = c.getinfo(c.RESPONSE_CODE) - res = c.perform() - - #print("Before get response code") - httpRes = c.getinfo(c.RESPONSE_CODE) - #print("After get response code") - responseCode = c.getinfo(c.RESPONSE_CODE) - - #print('Status: ' + str(responseCode)) + # print('Status: ' + str(responseCode)) - c.close() + c.close() - return (userId, httpRes) + return userId, httpRes - except Exception as inst: - print(inst) - return (userId, None) + except Exception as inst: + print(inst) + return userId, None - def createUser(scheme, beHost, bePort, user, adminUser): + userId = user['userId'] + try: + buffer = StringIO() + c = pycurl.Curl() - userId = user['userId'] - try: - buffer = StringIO() - c = pycurl.Curl() + url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user' + c.setopt(c.URL, url) + c.setopt(c.POST, 1) - url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/user' - c.setopt(c.URL, url) - c.setopt(c.POST, 1) + adminHeader = 'USER_ID: ' + adminUser + c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) - adminHeader = 'USER_ID: ' + adminUser - c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) + data = json.dumps(user) + c.setopt(c.POSTFIELDS, data) - data = json.dumps(user) - c.setopt(c.POSTFIELDS, data) + c.setopt(c.WRITEFUNCTION, lambda x: None) - c.setopt(c.WRITEFUNCTION, lambda x: None) + if scheme == 'https': + c.setopt(pycurl.SSL_VERIFYPEER, 0) + c.setopt(pycurl.SSL_VERIFYHOST, 0) - if scheme == 'https': - c.setopt(pycurl.SSL_VERIFYPEER, 0) - c.setopt(pycurl.SSL_VERIFYHOST, 0) + # print("before perform") + res = c.perform() + # print(res) - #print("before perform") - res = c.perform() - #print(res) - - #print("Before get response code") - httpRes = c.getinfo(c.RESPONSE_CODE) - #print("After get response code") - responseCode = c.getinfo(c.RESPONSE_CODE) - - #print('Status: ' + str(responseCode)) + # print("Before get response code") + httpRes = c.getinfo(c.RESPONSE_CODE) + # print("After get response code") + responseCode = c.getinfo(c.RESPONSE_CODE) - c.close() + # print('Status: ' + str(responseCode)) - return (userId, httpRes) + c.close() - except Exception as inst: - print(inst) - return (userId, None) + return userId, httpRes + + except Exception as inst: + print(inst) + return userId, None def errorAndExit(errorCode, errorDesc): - if ( errorCode > 0 ): - print("status=" + str(errorCode) + ". " + errorDesc) - else: - print("status=" + str(errorCode)) - sys.exit(errorCode) - + if errorCode > 0: + print("status=" + str(errorCode) + ". " + errorDesc) + else: + print("status=" + str(errorCode)) + sys.exit(errorCode) + + def usage(): - print sys.argv[0], '[optional -s | --scheme=, default http] [-i | --ip=] [-p | --port= ] [-f | --ifile= ]' + print(sys.argv[0], + '[optional -s | --scheme=, default http] [-i | --ip=] [-p | --port= ] [-f | --ifile= ]') + def main(argv): - print 'Number of arguments:', len(sys.argv), 'arguments.' + print('Number of arguments:', len(sys.argv), 'arguments.') + + beHost = 'localhost' + bePort = '8080' + inputfile = None + scheme = 'http' + adminUser = 'jh0003' + + try: + opts, args = getopt.getopt(argv, "i:p:f:h:s:", ["ip=", "port=", "ifile=", "scheme="]) + except getopt.GetoptError: + usage() + errorAndExit(2, 'Invalid input') - beHost = 'localhost' - bePort = '8080' - inputfile = None - scheme = 'http' - adminUser = 'jh0003' + for opt, arg in opts: + # print opt, arg + if opt == '-h': + usage() + sys.exit(3) + elif opt in ("-i", "--ip"): + beHost = arg + elif opt in ("-p", "--port"): + bePort = arg + elif opt in ("-f", "--ifile"): + inputfile = arg + elif opt in ("-s", "--scheme"): + scheme = arg - try: - opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ifile=","scheme="]) - except getopt.GetoptError: - usage() - errorAndExit(2, 'Invalid input') - - for opt, arg in opts: - #print opt, arg - if opt == '-h': - usage() - sys.exit(3) - elif opt in ("-i", "--ip"): - beHost = arg - elif opt in ("-p", "--port"): - bePort = arg - elif opt in ("-f", "--ifile"): - inputfile = arg - elif opt in ("-s", "--scheme"): - scheme = arg + print('scheme =', scheme, ', be host =', beHost, ', be port =', bePort, ', users file =', inputfile) - print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', users file =',inputfile - - if ( inputfile == None ): - usage() - sys.exit(3) + if inputfile == None: + usage() + sys.exit(3) - print 'Input file is ', inputfile + print('Input file is ', inputfile) - usersFile = open(inputfile) + usersFile = open(inputfile) - json_data = json.load(usersFile) + json_data = json.load(usersFile) - #print json_data + # print json_data - cloneUsers = convertUsersToCreationObject(json_data) + cloneUsers = convertUsersToCreationObject(json_data) - activeUsers = filter(lambda x: x['status'] == 'ACTIVE', cloneUsers) + activeUsers = filter(lambda x: x['status'] == 'ACTIVE', cloneUsers) - #print activeUsers + # print activeUsers - resultTable = importUsers(scheme, beHost, bePort, activeUsers, adminUser) + resultTable = importUsers(scheme, beHost, bePort, activeUsers, adminUser) - g = lambda x: x[1] != 201 and x[1] != 409 + g = lambda x: x[1] != 201 and x[1] != 409 - result = filter(g, resultTable) + result = filter(g, resultTable) - if ( len(result) > 0 ): - #print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result))) - errorAndExit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0],result))) + if (len(result) > 0): + # print("ERROR: Failed to load the users " + ', '.join(map(lambda x: x[0],result))) + errorAndExit(3, "Failed to load the users " + ', '.join(map(lambda x: x[0], result))) - g = lambda x: x[1] == 409 - result = filter(g, resultTable) + g = lambda x: x[1] == 409 + result = filter(g, resultTable) - print("-------------------------------------------") - print("Existing users: " + ', '.join(map(lambda x: x[0],result))) + print("-------------------------------------------") + print("Existing users: " + ', '.join(map(lambda x: x[0], result))) - result = filter(lambda x: x[1] == 201, resultTable) - if ( len(result) == 0 ): - print("-------------------------------------------") - print("No NEW user was loaded. All users are already exist") - print("-------------------------------------------") - else: - print("-------------------------------------------") - print("Loaded users: " + ', '.join(map(lambda x: x[0],result))) - print("-------------------------------------------") + result = filter(lambda x: x[1] == 201, resultTable) + if len(result) == 0: + print("-------------------------------------------") + print("No NEW user was loaded. All users are already exist") + print("-------------------------------------------") + else: + print("-------------------------------------------") + print("Loaded users: " + ', '.join(map(lambda x: x[0], result))) + print("-------------------------------------------") - errorAndExit(0, None) + errorAndExit(0, None) if __name__ == "__main__": - main(sys.argv[1:]) + main(sys.argv[1:]) diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/call_home.py b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/call_home.py index 54f2a69c09..0b120983eb 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/call_home.py +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/call_home.py @@ -6,7 +6,6 @@ from socket import getfqdn from sys import platform from time import sleep - PARSER = argparse.ArgumentParser() PARSER.add_argument("manager_ip", help="The IPv4 Address where one can read the MaveriQConductor.") PARSER.add_argument("--mockupfile", type=str, help="The path of the json mockupfile to use.") @@ -43,7 +42,7 @@ def check_availability(): is_connected = False while is_connected is False: try: - if requests.get(URL_AVAIL, headers={'Connection': 'close'}).status_code is 200: + if requests.get(URL_AVAIL, headers={'Connection': 'close'}).status_code == 200: is_connected = True sleep(2) except requests.exceptions.ConnectionError: @@ -90,8 +89,9 @@ def main(): # Perform any replacement needed. json_data = multiple_replace(regex_dict, json_data) - print json_data + print(json_data) check_availability() return post_request(p_json_data=json_data, p_headers=HEADERS) -print main() + +print(main()) diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/check_availability.py b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/check_availability.py index e93b372f15..09098b23d0 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/check_availability.py +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/check_availability.py @@ -15,20 +15,20 @@ PARSER.add_argument("ScribeIP", type=str, help="The Stage the application is currently in") ARGS = PARSER.parse_args() -print "Begining check availability check!" +print("Beginning check availability check!") isConnected = False while isConnected is False: try: sys.stdout.write('.') sleep(2) if requests.get(conductor_url.format(ARGS.ScribeIP), - headers={'Connection': 'close'}).status_code is 200 and requests.get( + headers={'Connection': 'close'}).status_code == 200 and requests.get( check_api.format(ARGS.ScribeIP, headers={'Connection': 'close'}), auth=requests.auth.HTTPBasicAuth(check_user, - check_password)).status_code is 200: + check_password)).status_code == 200: isConnected = True except requests.exceptions.ConnectionError as e: sleep(2) -print 'Conductor and Scribe are ready!' +print('Conductor and Scribe are ready!') diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/register_status.py b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/register_status.py index 98adafaf2f..55be22daf5 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/register_status.py +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/register_status.py @@ -12,7 +12,6 @@ from socket import getfqdn import sys from time import time - parser = argparse.ArgumentParser() parser.add_argument("scribe_ip", type=str, help="The IP where the Scribe can be reached.") @@ -88,22 +87,24 @@ def build_add_json(): ADD_COMPONENT_BODY["OAM_IP"] = OAM_DIRECT_IP for interface in netifaces.interfaces(): - ADD_COMPONENT_BODY["machineNetworkInterfaces"].append({"name": interface, "value": netifaces.ifaddresses(interface)[2][0]['addr']}) + ADD_COMPONENT_BODY["machineNetworkInterfaces"].append( + {"name": interface, "value": netifaces.ifaddresses(interface)[2][0]['addr']}) - if PROBE_ID is not "": - ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\ - CLUSTER_NAME + '_' + MACHINE_NAME + '_' + PROBE_ID + if PROBE_ID != "": + ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' + \ + CLUSTER_NAME + '_' + MACHINE_NAME + '_' + PROBE_ID else: - ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\ - CLUSTER_NAME + '_' + MACHINE_NAME + ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' + \ + CLUSTER_NAME + '_' + MACHINE_NAME return ADD_COMPONENT_BODY def send_postage(p_url, p_url_user, p_url_password, p_json_data): json_header = {'Content-type': 'application/json'} - request = requests.post(p_url, json.dumps(p_json_data), json_header, auth=requests.auth.HTTPBasicAuth(p_url_user, p_url_password)) - print request.status_code - if (request.status_code != 200): + request = requests.post(p_url, json.dumps(p_json_data), json_header, + auth=requests.auth.HTTPBasicAuth(p_url_user, p_url_password)) + print(request.status_code) + if request.status_code != 200: sys.exit(1) return request.status_code @@ -118,9 +119,10 @@ def post_add_machine(): read_metadata() return send_postage(add_machine_ip, user, password, build_add_json()) + if args.stage is None and args.status is None and args.description is None: - print "adding machine" - print post_add_machine() + print("adding machine") + print(post_add_machine()) else: - print "logging health" - print post_health() + print("logging health") + print(post_health()) diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/wait_for_resources.py b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/wait_for_resources.py index 5d960d659e..5abf961f75 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/wait_for_resources.py +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/resources/mock/services/heattotosca/securityrulestoportconnection/securityRulesToPortGetResource/inputfiles/wait_for_resources.py @@ -16,9 +16,9 @@ def parse_json_file(json_path): def check_network_interfaces(): for interface in netifaces.interfaces(): - if(sys.platform != 'win32' or netifaces.ifaddresses(interface)[-1000][0]['addr'] != '00:00:00:00:00:00:00:e0'): + if sys.platform != 'win32' or netifaces.ifaddresses(interface)[-1000][0]['addr'] != '00:00:00:00:00:00:00:e0': while 2 not in netifaces.ifaddresses(interface).keys() and 23 not in netifaces.ifaddresses(interface).keys(): - print "Still waiting for interface:", interface + print("Still waiting for interface:", interface) time.sleep(TIME_INTERVAL) @@ -29,7 +29,7 @@ def check_connectivity(): ping_str = "ping -n 1 " while os.system(ping_str + component_ip) != 0: - print "No connectivity to", component_ip, "waiting", TIME_INTERVAL, "seconds" + print("No connectivity to", component_ip, "waiting", TIME_INTERVAL, "seconds") time.sleep(TIME_INTERVAL) @@ -51,7 +51,7 @@ def check_cinder_mounts(): cinder_attached = os.popen("wmic diskdrive get DeviceID | find /i \"PHYSICALDRIVE\" | find /V \"0\" /C").read() while (int(cinder_attached) < cinder_count) and (cinder_count != 0): - print "Missing a cinder mount, waiting", TIME_INTERVAL, "seconds" + print("Missing a cinder mount, waiting", TIME_INTERVAL, "seconds") time.sleep(TIME_INTERVAL) if sys.platform.startswith('linux'): @@ -62,7 +62,7 @@ def check_cinder_mounts(): "wmic diskdrive get DeviceID | find /i \"PHYSICALDRIVE\" | find /V \"0\" /C").read() if int(cinder_attached) == cinder_count: - print "All cinder are attached and ready to be formatted and mounted" + print("All cinder are attached and ready to be formatted and mounted") def main(): @@ -72,7 +72,7 @@ def main(): if component_ip is not None: check_connectivity() - print "All resources are ready" + print("All resources are ready") if __name__ == "__main__": diff --git a/openecomp-be/tools/build/scripts/action_library_client/action_library_client.py b/openecomp-be/tools/build/scripts/action_library_client/action_library_client.py index 7f513afe86..0537128665 100644 --- a/openecomp-be/tools/build/scripts/action_library_client/action_library_client.py +++ b/openecomp-be/tools/build/scripts/action_library_client/action_library_client.py @@ -147,6 +147,7 @@ class FinalizeStatus(object): class ArgsDict(dict): """A dict which makes attributes accessible as properties.""" + def __getattr__(self, attr): return self[attr] @@ -221,33 +222,33 @@ class ArgumentParser(object): def usage(): """Print usage message.""" print("" + - "Usage: action_library_client.py [--help] [--url ] [--in ]\n" + - " [--out ] [--config ]\n" + - " [--log ] [--uuid ]\n" + - " [--curl] [--dryrun] [--verbose] [--version]\n" + - " [--list | --create | --update= | --delete |\n" + - " --checkout | --undocheckout | --checkin | --submit]\n" + - "\n" + - "Optional arguments:\n" + - " --help Show this help message and exit\n" + - " --url REST endpoint URL\n" + - " --in Path to JSON input file (else STDIN)\n" + - " --out Path to JSON output file (else STDOUT or logfile)\n" + - " --config Path to configuration file\n" + - " --log Path to logfile (else STDOUT)\n" + - " --uuid Action UUID, (=='actionInvariantUUID')\n" + - " --curl Use curl transport impl\n" + - " --dryrun Describe what will happen, execute nothing\n" + - " --verbose Verbose diagnostic output\n" + - " --version Print script version and exit\n" + - " --list List actions\n" + - " --create Create new action (requires --in)\n" + - " --update Update existing action (requires --uuid, --in)\n" + - " --delete Delete existing action (requires --uuid)\n" + - " --checkout Create minor version candidate (requires --uuid)\n" + - " --undocheckout Discard minor version candidate (requires --uuid)\n" + - " --checkin Create minor version from candidate (requires --uuid)\n" + - " --submit Create next major version (requires --uuid)") + "Usage: action_library_client.py [--help] [--url ] [--in ]\n" + + " [--out ] [--config ]\n" + + " [--log ] [--uuid ]\n" + + " [--curl] [--dryrun] [--verbose] [--version]\n" + + " [--list | --create | --update= | --delete |\n" + + " --checkout | --undocheckout | --checkin | --submit]\n" + + "\n" + + "Optional arguments:\n" + + " --help Show this help message and exit\n" + + " --url REST endpoint URL\n" + + " --in Path to JSON input file (else STDIN)\n" + + " --out Path to JSON output file (else STDOUT or logfile)\n" + + " --config Path to configuration file\n" + + " --log Path to logfile (else STDOUT)\n" + + " --uuid Action UUID, (=='actionInvariantUUID')\n" + + " --curl Use curl transport impl\n" + + " --dryrun Describe what will happen, execute nothing\n" + + " --verbose Verbose diagnostic output\n" + + " --version Print script version and exit\n" + + " --list List actions\n" + + " --create Create new action (requires --in)\n" + + " --update Update existing action (requires --uuid, --in)\n" + + " --delete Delete existing action (requires --uuid)\n" + + " --checkout Create minor version candidate (requires --uuid)\n" + + " --undocheckout Discard minor version candidate (requires --uuid)\n" + + " --checkin Create minor version from candidate (requires --uuid)\n" + + " --submit Create next major version (requires --uuid)") ############################################################################### @@ -533,7 +534,7 @@ class CURLRESTClient(IRESTClient): """Debug curl command, for diags and dryrun.""" buf = "" for token in cmd: - if token is "curl" or token.startswith("-"): + if token == "curl" or token.startswith("-"): buf = "{0}{1} ".format(buf, token) else: buf = "{0}\"{1}\" ".format(buf, token) @@ -556,7 +557,7 @@ class CURLRESTClient(IRESTClient): try: separator = output.index("\r\n\r\n{") self.logger.debug("HTTP preamble:\n{0}".format(output[:separator])) - json_body = json.loads(output[(separator+4):]) + json_body = json.loads(output[(separator + 4):]) self.log_json_response(method, json_body) return json_body except ValueError: diff --git a/openecomp-be/tools/build/scripts/parse-json.py b/openecomp-be/tools/build/scripts/parse-json.py index 46537ceac5..83c70aac01 100644 --- a/openecomp-be/tools/build/scripts/parse-json.py +++ b/openecomp-be/tools/build/scripts/parse-json.py @@ -41,17 +41,19 @@ from collections import OrderedDict def readJsonFile(file, type): - with open(file, 'r') as f: - data = json.load(f, object_pairs_hook=OrderedDict) - return data[type] + with open(file, 'r') as f: + data = json.load(f, object_pairs_hook=OrderedDict) + return data[type] + def printJsonTypeEntries(jsonData): - for i in jsonData.keys(): - print jsonData[i] + ';' + for i in jsonData.keys(): + print(jsonData[i] + ';') def usage(): - print 'parseJsonFile.py [-f & -t & -t >> Error: Credentials required (username/password)\n' - printHelp() - return - - else: - creds = argv[1].split('/') - username = creds[0] - password = creds[1] # not used - - try: - cmdIp=argv[2] - host=argv[3] - except IndexError: - host=DEFAULT_HOST - print "Going to use user defined values" + if argv[0].lower() == 'h' or argv[0].lower() == '-h': + printHelp() + return + + if argv[0] == '-ip': + host = argv[1] + else: + if argv[0].lower() == '-a' and '/' not in argv[1]: + print('\n>>> Error: Credentials required (username/password)\n') + printHelp() + return + + else: + creds = argv[1].split('/') + username = creds[0] + password = creds[1] # not used + + try: + cmdIp = argv[2] + host = argv[3] + except IndexError: + host = DEFAULT_HOST + print("Going to use user defined values") Service.server(host) - webHandler=WebHandler(host=host, port=DEFAULT_PORT) + webHandler = WebHandler(host=host, port=DEFAULT_PORT) response, headers = webHandler.rest(url=VSP_LIST_PATH, method='GET', data=None, userId=username) jResult = json.loads(response) jSrvices = jResult["results"] - reportFileName = 'upgradereport.csv' #datetime.now() + reportFileName = 'upgradereport.csv' # datetime.now() reportFile = open(reportFileName, 'w') reportFile.write(Service.header()) @@ -69,85 +70,91 @@ def main(argv): else: lockingUser = jService["lockingUser"] - service = Service(serviceName=serviceName, vspId=vspId, vendorName=vendorName, lockingUser=lockingUser ) - print service - # Will try to GET the service + service = Service(serviceName=serviceName, vspId=vspId, vendorName=vendorName, lockingUser=lockingUser) + print(service) + # Will try to GET the service res = service.Get() if res == 500: - serviceMigration(service, status, username) + serviceMigration(service, status, username) else: - print "Service {0} was tested and does not need a migration".format(serviceName) + print("Service {0} was tested and does not need a migration".format(serviceName)) reportFile.write(service.line()) reportFile.close() def serviceMigration(service, serviceStatus, username): - print "Service {0} was tested and it needs a migration".format(service.serviceName) - print "Service {0} - Migration start" - if serviceStatus == "Locked": - print "Service {0} is locked - forcing checkin".format(service.serviceName) - service.Checkin() - print "Doing new checkout" - service.Checkout(username) - - zipName = service.DownloadHeat() - if not zipName: - print "no heat found" - service.uploadStatus = "no heat found" - else: - uploadResponse = service.UploadHeat(zipName) - uploadResults = json.loads(uploadResponse) - if uploadResults['status'] == 'Success' and uploadResults['errors'].__len__() == 0: - service.uploadStatus = "Heat uploaded successfully" - else: - service.uploadStatus = "Heat uploaded with errors" - print "Doing new checkin" + print("Service {0} was tested and it needs a migration".format(service.serviceName)) + print("Service {0} - Migration start") + if serviceStatus == "Locked": + print("Service {0} is locked - forcing checkin".format(service.serviceName)) service.Checkin() + print("Doing new checkout") + service.Checkout(username) + + zipName = service.DownloadHeat() + if not zipName: + print("no heat found") + service.uploadStatus = "no heat found" + else: + uploadResponse = service.UploadHeat(zipName) + uploadResults = json.loads(uploadResponse) + if uploadResults['status'] == 'Success' and uploadResults['errors'].__len__() == 0: + service.uploadStatus = "Heat uploaded successfully" + else: + service.uploadStatus = "Heat uploaded with errors" + print("Doing new checkin") + service.Checkin() - print "Service {0} - Migration end" + print("Service {0} - Migration end") -def printHelp(): +def printHelp(): print("Upgrade script Help:") print("==================================") print("1607_to_1610 -h --> get help") print("1607_to_1610 -a / [-ip {ip}]") print("Example: 1607_to_1610 -a root/secret") + class Service(object): - def __init__(self, serviceName, vspId ,vendorName, lockingUser): + def __init__(self, serviceName, vspId, vendorName, lockingUser): self.serviceName = serviceName self.vspId = vspId self.vendorName = vendorName self.lockingUser = lockingUser - self.webHandler = WebHandler(host=Service.serveraddress, port=DEFAULT_PORT) # Schema? + self.webHandler = WebHandler(host=Service.serveraddress, port=DEFAULT_PORT) # Schema? self.uploadStatus = "not started" def __repr__(self): - return 'Name: {0}, Id: {1}, Vendor: {2}, locked by: {3}, status {4}'.format(self.serviceName, self.vspId ,self.vendorName, self.lockingUser, self.uploadStatus) + return 'Name: {0}, Id: {1}, Vendor: {2}, locked by: {3}, status {4}'.format(self.serviceName, self.vspId, + self.vendorName, self.lockingUser, + self.uploadStatus) + @classmethod def header(cls): return 'Name,Id,Vendor,locked-by,status\n' @classmethod def server(cls, address): - cls.serveraddress=address + cls.serveraddress = address def line(self): - return '{0},{1},{2},{3},{4}\n'.format(self.serviceName, self.vspId ,self.vendorName, self.lockingUser, self.uploadStatus) + return '{0},{1},{2},{3},{4}\n'.format(self.serviceName, self.vspId, self.vendorName, self.lockingUser, + self.uploadStatus) def Checkout(self, userId): # /v1.0/vendor-software-products/{vspId}/actions - urlpath=VSP_ACTIONS_PATH.format(vspId=self.vspId) - response, headers = self.webHandler.rest( url=urlpath, method='PUT', data={"action": "Checkout"}, userId=userId) - self.lockingUser=userId #we will later use this user to checkin + urlpath = VSP_ACTIONS_PATH.format(vspId=self.vspId) + response, headers = self.webHandler.rest(url=urlpath, method='PUT', data={"action": "Checkout"}, userId=userId) + self.lockingUser = userId # we will later use this user to checkin return response def Checkin(self): # /v1.0/vendor-software-products/{vspId}/actions urlpath = VSP_ACTIONS_PATH.format(vspId=self.vspId) - response, headers = self.webHandler.rest(url=urlpath, method='PUT', data={"action": "Checkin"}, userId=self.lockingUser) + response, headers = self.webHandler.rest(url=urlpath, method='PUT', data={"action": "Checkin"}, + userId=self.lockingUser) return response def Get(self): @@ -156,38 +163,39 @@ class Service(object): try: response, headers = self.webHandler.rest(url=urlpath, method='GET', data=None, userId=self.lockingUser) except HttpError as e: - print e.message + print(e.message) response = e.status return response def UploadHeat(self, zipName): - #/v1.0/vendor-software-products/{vspId}/upload - urlpath = VSP_UPLOAD_PATH.format(vspId=self.vspId) - try: - fields = [] - with open(zipName, 'rb') as fin: - buffer = fin.read() - fin.close() - files = [('upload', 'heatfile.zip', buffer)] - response = self.webHandler.post_multipart('HTTP', urlpath, fields, files, self.lockingUser) - - return response - finally: - print "done upload" + # /v1.0/vendor-software-products/{vspId}/upload + urlpath = VSP_UPLOAD_PATH.format(vspId=self.vspId) + try: + fields = [] + with open(zipName, 'rb') as fin: + buffer = fin.read() + fin.close() + files = [('upload', 'heatfile.zip', buffer)] + response = self.webHandler.post_multipart('HTTP', urlpath, fields, files, self.lockingUser) + + return response + finally: + print("done upload") def DownloadHeat(self): - urlpath=VSP_DOWNLOAD_PATH.format(vspId=self.vspId) + urlpath = VSP_DOWNLOAD_PATH.format(vspId=self.vspId) try: - response, headers = self.webHandler.rest(url=urlpath, method='Get', data=None, userId=self.lockingUser, accept='application/octet-stream') + response, headers = self.webHandler.rest(url=urlpath, method='Get', data=None, userId=self.lockingUser, + accept='application/octet-stream') except HttpError as e: if e.status == 404: return "" for (key, value) in headers: if key.lower() == "content-disposition": - file_name = value[value.index('=')+1:] + file_name = value[value.index('=') + 1:] break - heatsDir= os.path.join(os.path.dirname(__file__), 'heats') + heatsDir = os.path.join(os.path.dirname(__file__), 'heats') if not os.path.exists(heatsDir): os.makedirs(heatsDir) file_name = os.path.join(heatsDir, file_name) @@ -198,7 +206,6 @@ class Service(object): return file_name - class WebHandler(object): def __init__(self, host, port): self.host = host @@ -208,13 +215,13 @@ class WebHandler(object): connection = httplib.HTTPConnection(host=self.host, port=self.port) try: - headers = {'Content-Type':content_type ,'Accept':accept} + headers = {'Content-Type': content_type, 'Accept': accept} headers['USER_ID'] = userId connection.request(method=method, headers=headers, body=json.dumps(data), url=url) response = connection.getresponse() if response.status not in range(200, 300): - raise HttpError(status= response.status, message=response.reason) + raise HttpError(status=response.status, message=response.reason) return response.read(), response.getheaders() finally: @@ -241,7 +248,7 @@ class WebHandler(object): h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() - print errcode, errmsg, headers + print(errcode, errmsg, headers) return h.file.read() def encode_multipart_form_data(self, fields, files): @@ -268,14 +275,15 @@ class WebHandler(object): def get_content_type(self, filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' + class HttpError(Exception): def __init__(self, status, message): self.status = status - self.message=message + self.message = message + def __str__(self): return repr(self.value, self.message) + if __name__ == "__main__": main(sys.argv[1:]) - - -- cgit 1.2.3-korg