-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-wsdl-v2.py
109 lines (86 loc) · 3.02 KB
/
export-wsdl-v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python
import urllib2
import xml.etree.ElementTree
import sys, getopt
import os
import itertools
schemaUrls = []
schemaFilenames = []
dirName = ''
def urlGet(url):
try:
urlContent = urllib2.urlopen(url)
except:
print 'Error opening URL:' + url
sys.exit(2)
return urlContent.read()
def getSubSchemaUrl(url):
subSchemaContent = urlGet(url)
subSchemaTree = ''
try:
subSchemaTree = xml.etree.ElementTree.fromstring(subSchemaContent)
except:
print 'Failed to parse response from:' + url + ' as XML'
# print subSchemaContentString
subSchemaRoot = subSchemaTree
importElements = subSchemaRoot.findall(".//*[@schemaLocation]")
schemaUrls.append([url,subSchemaContent])
for child in importElements:
subSchemaUrl = child.attrib['schemaLocation']
print 'Found schema reference in: ' + url + ' to: ' + subSchemaUrl
getSubSchemaUrl(subSchemaUrl)
def saveSchemas():
global schemaFilenames
global dirName
for url,filename,content in schemaFilenames:
# fileContent = urlGet(url)
print 'Transforming and saving ' + url + ' to local file: ' + filename
for in_url,in_filename,_ in schemaFilenames:
content = content.replace(in_url,in_filename)
# fileTree = content
# fileRoot = fileTree.getroot()
#
# for in_url,in_filename,_ in schemaFilenames:
# xpathValue = ".//*[@schemaLocation=\"" + in_url + "\"]"
# importElement = fileTree.find(xpathValue)
# if importElement is not None:
# importElement.set('schemaLocation',in_filename)
schemaFile = open(dirName + '/' + filename, 'w')
schemaFile.write(content)
schemaFile.close
# content.write(open(dirName + '/' + filename, 'wb'))
def main(argv):
wsdlUrl = ''
global schemaUrls
global schemaFilenames
global dirName
print 'Starting...'
try:
opts, args = getopt.getopt(argv,"u:")
except getopt.GetoptError:
print 'usage: export-wsdl.py -u <wsdlUrl>'
sys.exit(2)
if len(opts) == 0:
print 'usage: export-wsdl.py -u <wsdlUrl>'
sys.exit(2)
wsdlUrl = opts[0][1]
wsdlName = wsdlUrl.split("/")[-1].split("?")[0]
dirName = wsdlName
print 'Exporting schema definitions from: ' + wsdlUrl
if not os.path.exists(wsdlName):
print 'Creating new project directory: ' + wsdlName
os.makedirs(dirName)
getSubSchemaUrl(wsdlUrl)
schemaUrls.sort()
schemaUrls = list(schemaUrls for schemaUrls,_ in itertools.groupby(schemaUrls))
for index,schemas in enumerate(schemaUrls):
if '?wsdl' in schemas[0]:
schemaName = wsdlName + '.wsdl'
else:
schemaName = wsdlName + '_' + str(index) + '.xsd'
schemaFilenames.append([schemas[0],schemaName,schemas[1]])
# schemaFilenames.append([wsdlUrl, wsdlName + '/' + wsdlName + '.wsdl'])
saveSchemas()
print 'Done!'
if __name__ == "__main__":
main(sys.argv[1:])