4 from toscaparser.tosca_template import ToscaTemplate
6 from lcm.pub.utils.toscaparser.dataentityext import DataEntityExt
9 class BaseInfoModel(object):
10 def __init__(self, path, params):
13 def buildToscaTemplate(self, path, params):
16 file_name = self._check_download_file(path)
17 valid_params = self._validate_input_params(file_name, params)
18 return self._create_tosca_template(file_name, valid_params)
20 if file_name != None and file_name != path and os.path.exists(file_name):
26 def _validate_input_params(self, path, params):
28 if params and len(params) > 0:
29 tmp = self._create_tosca_template(path, None)
30 for key,value in params.items():
31 if hasattr(tmp, 'inputs') and len(tmp.inputs) > 0:
32 for input_def in tmp.inputs:
33 if (input_def.name == key):
34 valid_params[key] = DataEntityExt.validate_datatype(input_def.type, value)
38 def _create_tosca_template(self, file_name, valid_params):
41 tosca_tpl = ToscaTemplate(file_name, valid_params)
42 print "-----------------------------"
43 print '\n'.join(['%s:%s' % item for item in tosca_tpl.__dict__.items()])
44 print "-----------------------------"
48 # if tosca_tpl != None and hasattr(tosca_tpl, "temp_dir") and os.path.exists(tosca_tpl.temp_dir):
50 # shutil.rmtree(tosca_tpl.temp_dir)
51 # except Exception, e:
53 # # if tosca_tpl != None and tosca_tpl.temp_dir != None and os.path.exists(tosca_tpl.temp_dir):
55 # # shutil.rmtree(tosca_tpl.temp_dir)
56 # # except Exception, e:
59 def _check_download_file(self, path):
60 if (path.startswith("ftp") or path.startswith("sftp")):
61 return self.downloadFileFromFtpServer(path)
62 elif (path.startswith("http")):
63 return self.download_file_from_httpserver(path)
66 def download_file_from_httpserver(self, path):
67 path = path.encode("utf-8")
68 tmps = str.split(path, '/')
69 localFileName = tmps[len(tmps) - 1]
70 urllib.urlretrieve(path, localFileName)
73 def downloadFileFromFtpServer(self, path):
74 path = path.encode("utf-8")
75 tmp = str.split(path, '://')
77 tmp = str.split(tmp[1], ':')
80 tmp = str.split(tmp[1], '@')
82 index = tmp[1].index('/')
83 hostIp = tmp[1][0:index]
84 remoteFileName = tmp[1][index:len(tmp[1])]
85 if protocol.lower() == 'ftp':
92 userPwd = str.split(tmp[1], '@')[0]
93 hostIp = str.split(tmp[1], '@')[1]
94 index = tmp[2].index('/')
95 hostPort = tmp[2][0:index]
96 remoteFileName = tmp[2][index:len(tmp[2])]
98 localFileName = str.split(remoteFileName, '/')
99 localFileName = localFileName[len(localFileName) - 1]
101 if protocol.lower() == 'sftp':
102 self.sftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
104 self.ftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)