3 from StringIO import StringIO
7 ####################################################################################################################################################################################
9 # Export all active users to file - for 1602+ #
12 # python exportUsers.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ] #
14 # shortest activation (be host = localhost, be port = 8080): #
15 # python exportUsers.py [-f <output file> | --ofile=<output file> ] #
17 ####################################################################################################################################################################################
19 ALL_USERS_SUFFIX = '/sdc2/rest/v1/user/users'
21 def errorAndExit(errorCode, errorDesc):
23 print("status=" + str(errorCode) + ". " + errorDesc)
25 print("status=" + str(errorCode))
28 def getUsers(scheme, beHost, bePort, adminUser):
34 url = scheme + '://' + beHost + ':' + bePort + ALL_USERS_SUFFIX
37 c.setopt(c.WRITEFUNCTION, buffer.write)
38 #c.setopt(c.WRITEFUNCTION, lambda x: None)
39 adminHeader = 'USER_ID: ' + adminUser
40 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
43 c.setopt(pycurl.SSL_VERIFYPEER, 0)
44 c.setopt(pycurl.SSL_VERIFYHOST, 0)
49 #print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
53 body = buffer.getvalue()
59 except Exception as inst:
61 #print type(inst) # the exception instance
62 #print inst.args # arguments stored in .args
63 #print inst # __str__ allows args to be printed directly
71 print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ]'
74 print 'Number of arguments:', len(sys.argv), 'arguments.'
76 adminHeader = 'jh0003'
83 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ofile=","scheme="])
84 except getopt.GetoptError:
86 errorAndExit(2, 'Invalid input')
93 elif opt in ("-i", "--ip"):
95 elif opt in ("-p", "--port"):
97 elif opt in ("-f", "--ofile"):
99 elif opt in ("-s", "--scheme"):
102 print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', output file =',outputfile
104 if ( outputfile == None ):
108 users = getUsers(scheme, beHost, bePort, adminHeader)
112 if ( error != None ):
113 errorAndExit(5, str(error))
118 usersAsJson = json.load(io)
120 writeFile = open(outputfile, 'w')
122 json.dump(usersAsJson, writeFile)
126 print("-------------------------------------------")
127 errorAndExit(0, None)
129 if __name__ == "__main__":