-
Notifications
You must be signed in to change notification settings - Fork 10
/
Publishservice.py
300 lines (239 loc) · 11.1 KB
/
Publishservice.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#coding:cp936
import os
import arcpy
import time
import sys
import getpass
__author__ = 'jiangmb'
from arcpy import mapping
import xml.dom.minidom as DOM
import os
import tempfile
import argparse
class CreateSddraft:
def CreateSddraft(self,mapDocPath,con,serviceName,copy_data_to_server=True,folder=None):
"""
:param mapDocPath: mxd path
:param con: arcgis server connection file
:param serviceName: service name
:param clusterName: cluster name
:param folder: folder to contain the publishing service
:return: the file path of the sddraft
"""
mapDoc=mapping.MapDocument(mapDocPath)
sddraft=mapDocPath.replace(".mxd",".sddraft")
result= mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER', con, copy_data_to_server, folder)
return sddraft
def setTheClusterName(self,xml,clusterName):# the new description
doc = DOM.parse(xml)
# find the Item Information Description element
doc.getElementsByTagName('Cluster')[0].childNodes[0].nodeValue=clusterName
# output to a new sddraft
outXml =xml
f = open(outXml, 'w')
doc.writexml( f )
f.close()
return outXml
class CreateContectionFile(object):
def __init__(self):
self.__filePath = None
self.__loginDict = None
def CreateContectionFile(self):
"""
wrkspc: store the ags file
loginDict: dictionary stored login information
"""
# con = 'http://localhost:6080/arcgis/admin'
try:
server_url = "http://{}:{}/arcgis/admin".format(self.__loginDict['server'],self.__loginDict['port'])
connection_file_path = str(self.__filePath) #
use_arcgis_desktop_staging_folder = False
if os.path.exists(connection_file_path):
os.remove(connection_file_path)
out_name = os.path.basename(connection_file_path)
path = os.path.split(self.filePath)[0]
result = mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES",
path,
out_name,
server_url,
"ARCGIS_SERVER",
use_arcgis_desktop_staging_folder,
path,
self.__loginDict['userName'],
self.__loginDict['passWord'],
"SAVE_USERNAME"
)
print ("++++++++INFO:�����ļ������ɹ�++++++++")
return connection_file_path
except Exception as msg:
print (msg)
#
@property
def filePath(self):
return self.__filePath
@filePath.setter
def filePath(self, value):
self.__filePath = value
@property
def loginInfo(self):
return self.__loginDict
@loginInfo.setter
def loginInfo(self, value):
self.__loginDict = value
class CreateSddraft:
def CreateSddraft(self,mapDocPath,con,serviceName,copy_data_to_server=True,folder=None):
"""
:param mapDocPath: mxd path
:param con: arcgis server connection file
:param serviceName: service name
:param clusterName: cluster name
:param folder: folder to contain the publishing service
:return: the file path of the sddraft
"""
mapDoc=mapping.MapDocument(mapDocPath)
sddraft=mapDocPath.replace(".mxd",".sddraft")
result= mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER', con, copy_data_to_server, folder)
return sddraft
def setTheClusterName(self,xml,clusterName):# the new description
doc = DOM.parse(xml)
# find the Item Information Description element
doc.getElementsByTagName('Cluster')[0].childNodes[0].nodeValue=clusterName
# output to a new sddraft
outXml =xml
f = open(outXml, 'w')
doc.writexml( f )
f.close()
return outXml
class publishServices:
def checkfileValidation(self,mxdLists):
print ("++++++++INFO:��ʼ����ĵ�����Ч��++++++++")
file_to_be_published=[]
for file in mxdLists:
mxd=mapping.MapDocument(file)
brknlist=mapping.ListBrokenDataSources(mxd)
if not len(brknlist)==0:
print ("++++++++ERROR:��ͼ�ĵ�,"+os.path.split(file)[1]+"������������++++++++")
else:
file_to_be_published.append(file)
print ("++++++++INFO:��ͼ�ĵ���Ч�Լ�����++++++")
return file_to_be_published
def publishServices(self,mxdLists,con,clusterName='default',copy_data_to_server=True,folder=None):
for file in self.checkfileValidation(mxdLists):
###tmp:
serviceslist=[]
serviceName=os.path.splitext(os.path.split(file)[1])[0]
print ("++++++++INFO:����_"+serviceName+"��ʼ�����������ļ�++++++++")
clsCreateSddraft=CreateSddraft()
sddraft=clsCreateSddraft.CreateSddraft(file,con,serviceName,copy_data_to_server,folder)
print ("++++++++INFO:��ʼ��������:"+serviceName+"++++++++")
analysis = arcpy.mapping.AnalyzeForSD(sddraft)
dirName=os.path.split(file)[0]
if analysis['errors'] == {}:
print ("++++++++WARNING:�����ڴ�������������ʾ��Ϣ����Щ���ݿ��ܻ�Ӱ���������+++++++")
print (analysis['warnings'])
if(not self.checkWarnings(analysis['warnings'])):
try:
sd=dirName+"\\"+serviceName+".sd"
if(os.path.exists(sd)):
os.remove(sd)
arcpy.StageService_server(sddraft, sd)
print ("++++++++INFO:����:"+serviceName+"����ɹ�+++++++")
arcpy.UploadServiceDefinition_server(sd, con,in_cluster=clusterName)
print ("++++++++INFO:����:"+str(serviceName)+"�����ɹ�++++++")
os.remove(sd)
####ֹͣ����
except Exception as msg:
print (msg)
else:
print ("++++++++WARNING:ǿ�ҽ��飬�˳���ǰ����ȥע������Դ���粻�˳���6s���������+++")
# time.sleep(10)
try:
sd=dirName+"\\"+serviceName+".sd"
if(os.path.exists(sd)):
os.remove(sd)
arcpy.StageService_server(sddraft, sd)
print ("++++++++INFO:����ɹ�++++++++")
arcpy.UploadServiceDefinition_server(sd, con,in_cluster=clusterName)
print ("++++++++INFO:"+serviceName+"�����ɹ�+++++++")
os.remove(sd)
except Exception as msg:
print (msg)
else:
print ('++++++++ERROR:�������´���:'+analysis['errors']+'++++++++')
#������˳�����̨
time.sleep(5)
sys.exit(1)
def checkWarnings(self,warnings):
for warning in warnings:
if warning[1]==24011:
print ("++++++++��ǰ����λ��û��ע�ᣬ���ݻ´������������,�������̻�Ӱ�췢���ٶ�+++++++")
return True
return False
def GetMxFileList(self,filePath):
#�ж��ļ����Ƿ����
if not os.path.exists(filePath):
print ("++++++++ERROR:�ļ��в�����+++++++")
sys.exit(1)
#��ȡ�ļ����е�����mxd�ļ�
list=[]
for root,dirname, files in os.walk(filePath):
for file in files:
if os.path.splitext(file)[1]=='.mxd':
mxdfile=os.path.join(root,file)
list.append(mxdfile)
if list==[]:
print ("++++++++INFO:�ڵ�ǰĿ¼�²�������Ч��mxd�ļ�++++++++")
time.sleep(5)
sys.exit(1)
return list
def GetInfo():
server = raw_input("������GIS Server IP:")
userName=raw_input("������վ�����Ա�û���:")
passWord=getpass.getpass("������վ�����Ա����:")
port=raw_input("������˿ں�(6080)��")
logDict={'server':server,
'userName':userName,
'passWord':passWord,
'port':port}
contionfile=os.path.join(tempfile.mkdtemp(),'server.ags')
#���ô��������ļ��IJ���
instace=CreateContectionFile()
instace.filePath=contionfile
instace.loginInfo=logDict
instace.CreateContectionFile()
if(os.path.isfile(contionfile)==False):
print ("++++++++ERROR:��������ʧ��++++++++")
time.sleep(5)
sys.exit(1)
#����mxd�ļ����ļ���e
mxdDir=raw_input('������mxd�����ļ���:')
clsPublishservice=publishServices()
fileList=clsPublishservice.GetMxFileList(mxdDir)
servic_dir=raw_input("��ָ��������������Ŀ¼��Ĭ��Ϊroot��ʹ��Ĭ��ֱֵ�ӻس�:")
if len(servic_dir)==0:
servic_dir==None
clusterName=raw_input("��ָ����������Ⱥ��Ĭ��Ϊcluster����û�м�Ⱥ��������ֱ�ӻس�:")
if len(clusterName)==0:
clusterName='default'
clsPublishservice=publishServices()
clsPublishservice.publishServices(fileList,contionfile,clusterName,copy_data_to_server=False,folder=servic_dir)
if __name__=='__main__':
GetInfo()
"""logDict = {'server': 'localhost',
'userName': "arcgis",
'passWord': "Super123",
'port':'6080'}
dd = CreateContectionFile()
dd.loginInfo = logDict
path =os.path.join(tempfile.mkdtemp(),'server.ags')
print path
dd.filePath = path
dd.CreateContectionFile()
clsPublishservice=publishServices()
#get
file=r"d:\workspace\New folder\10"
fileList=clsPublishservice.GetMxFileList(file)
clusterName='default'
servic_dir=''
clsPublishservice.publishServices(fileList,path,clusterName,copy_data_to_server=False,folder=servic_dir)
"""