ed92ca8b3d592785c5903ac8c604eabe952f79dc
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importCommon.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6
7 ###############################################################################################################
8 #
9 #
10 ###############################################################################################################
11
12 debugFlag = True
13
14
15 def join_strings(lst):
16     concat = ""
17     for string in lst:
18         if string is not None:
19             if type(string) == int:
20                 string = str(string)
21             concat += (string + " ")
22     return concat
23
24
25 def debug(desc, *args):
26     if debugFlag:
27         print desc, join_strings(args)
28
29
30 def log(desc, arg=None):
31     print desc, arg
32
33
34 def error_and_exit(error_code, error_desc):
35     if error_code > 0:
36         print "status={0}. {1}".format(error_code, '' if error_desc is None else error_desc)
37     else:
38         print "status={0}".format(error_code)
39     sys.exit(error_code)
40
41
42 def print_name_and_return_code(name, code):
43     print "{0:30} | {1:6}".format(name, code)
44
45
46 def print_frame_line():
47     print "----------------------------------------"
48
49
50 def parse_cmd_line_params(argv):
51     print 'Number of arguments:', len(sys.argv), 'arguments.'
52
53     be_host = 'localhost'
54     be_port = '8080'
55     admin_user = 'jh0003'
56     scheme = 'http'
57
58     try:
59         opts, args = getopt.getopt(argv, "i:p:u:h:s:", ["ip=", "port=", "user=", "scheme="])
60     except getopt.GetoptError:
61         usage()
62         error_and_exit(2, 'Invalid input')
63
64     for opt, arg in opts:
65         # print opt, arg
66         if opt == '-h':
67             usage()
68             sys.exit(3)
69         elif opt in ("-i", "--ip"):
70             be_host = arg
71         elif opt in ("-p", "--port"):
72             be_port = arg
73         elif opt in ("-u", "--user"):
74             admin_user = arg
75         elif opt in ("-s", "--scheme"):
76             scheme = arg
77
78     print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user
79
80     if be_host is None:
81         usage()
82         sys.exit(3)
83     return scheme, be_host, be_port, admin_user
84
85
86 def usage():
87     print sys.argv[
88         0], '[optional -s <scheme> | --scheme=<scheme>, default http ] [-i <be host> | --ip=<be host>] [-p <be port> ' \
89             '| --port=<be port> ] [-u <user userId> | --user=<user userId> ] '