-
Notifications
You must be signed in to change notification settings - Fork 34
/
update.py
534 lines (424 loc) · 20.2 KB
/
update.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# Update.py - update hosted feature services by replacing the .SD file
# and calling publishing (with overwrite) to update the feature service
#
import ConfigParser
import ast
import os
import sys
import time
import urllib2
import urllib
import json
import mimetypes
import gzip
from io import BytesIO
import string
import random
from xml.etree import ElementTree as ET
import arcpy
class AGOLHandler(object):
def __init__(self, username, password, serviceName, folderName, proxyDict):
self.headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': ('updatehostedfeatureservice')
}
self.username = username
self.password = password
self.base_url = "https://www.arcgis.com/sharing/rest"
self.proxyDict = proxyDict
self.serviceName = serviceName
self.token = self.getToken(username, password)
self.itemID = self.findItem("Feature Service")
self.SDitemID = self.findItem("Service Definition")
self.folderName = folderName
self.folderID = self.findFolder()
def getToken(self, username, password, exp=60):
referer = "http://www.arcgis.com/"
query_dict = {'username': username,
'password': password,
'expiration': str(exp),
'client': 'referer',
'referer': referer,
'f': 'json'}
token_url = '{}/generateToken'.format(self.base_url)
token_response = self.url_request(token_url, query_dict, 'POST')
if "token" not in token_response:
print(token_response['error'])
sys.exit()
else:
return token_response['token']
def findItem(self, findType):
""" Find the itemID of whats being updated
"""
searchURL = self.base_url + "/search"
query_dict = {'f': 'json',
'token': self.token,
'q': "title:\"" + self.serviceName + "\"AND owner:\"" +
self.username + "\" AND type:\"" + findType + "\""}
jsonResponse = self.url_request(searchURL, query_dict, 'POST')
if jsonResponse['total'] == 0:
print("\nCould not find a service to update. Check the service name in the settings.ini")
sys.exit()
else:
resultList = jsonResponse['results']
for it in resultList:
if it["title"] == self.serviceName:
print("found {} : {}").format(findType, it["id"])
return it["id"]
def findFolder(self, folderName=None):
""" Find the ID of the folder containing the service
"""
if self.folderName == "None":
return ""
findURL = "{}/content/users/{}".format(self.base_url, self.username)
query_dict = {'f': 'json',
'num': 1,
'token': self.token}
jsonResponse = self.url_request(findURL, query_dict, 'POST')
for folder in jsonResponse['folders']:
if folder['title'] == self.folderName:
return folder['id']
print("\nCould not find the specified folder name provided in the settings.ini")
print("-- If your content is in the root folder, change the folder name to 'None'")
sys.exit()
def upload(self, fileName, tags, description):
"""
Overwrite the SD on AGOL with the new SD.
This method uses 3rd party module: requests
"""
updateURL = '{}/content/users/{}/{}/items/{}/update'.format(self.base_url, self.username,
self.folderID, self.SDitemID)
query_dict = {"filename": fileName,
"type": "Service Definition",
"title": self.serviceName,
"tags": tags,
"description": description,
"f": "json",
'multipart': 'true',
"token": self.token}
details = {'filename': fileName}
add_item_res = self.url_request(updateURL, query_dict, "POST", "", details)
itemPartJSON = self._add_part(fileName, add_item_res['id'], "Service Definition")
if "success" in itemPartJSON:
itemPartID = itemPartJSON['id']
commit_response = self.commit(itemPartID)
# valid states: partial | processing | failed | completed
status = 'processing'
while status == 'processing' or status == 'partial':
status = self.item_status(itemPartID)['status']
time.sleep(1.5)
print("updated SD: {}".format(itemPartID))
return True
else:
print("\n.sd file not uploaded. Check the errors and try again.\n")
print(itemPartJSON)
sys.exit()
def _add_part(self, file_to_upload, item_id, upload_type=None):
""" Add the item to the portal in chunks.
"""
def read_in_chunks(file_object, chunk_size=10000000):
"""Generate file chunks of 10MB"""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
url = '{}/content/users/{}/items/{}/addPart'.format(self.base_url, self.username, item_id)
with open(file_to_upload, 'rb') as f:
for part_num, piece in enumerate(read_in_chunks(f), start=1):
title = os.path.basename(file_to_upload)
files = {"file": {"filename": file_to_upload, "content": piece}}
params = {
'f': "json",
'token': self.token,
'partNum': part_num,
'title': title,
'itemType': 'file',
'type': upload_type
}
request_data, request_headers = self.multipart_request(params, files)
resp = self.url_request(url, request_data, "MULTIPART", request_headers)
return resp
def item_status(self, item_id, jobId=None):
""" Gets the status of an item.
Returns:
The item's status. (partial | processing | failed | completed)
"""
url = '{}/content/users/{}/items/{}/status'.format(self.base_url, self.username, item_id)
parameters = {'token': self.token,
'f': 'json'}
if jobId:
parameters['jobId'] = jobId
return self.url_request(url, parameters)
def commit(self, item_id):
""" Commits an item that was uploaded as multipart
"""
url = '{}/content/users/{}/items/{}/commit'.format(self.base_url, self.username, item_id)
parameters = {'token': self.token,
'f': 'json'}
return self.url_request(url, parameters)
def publish(self):
""" Publish the existing SD on AGOL (it will be turned into a Feature Service)
"""
publishURL = '{}/content/users/{}/publish'.format(self.base_url, self.username)
query_dict = {'itemID': self.SDitemID,
'filetype': 'serviceDefinition',
'overwrite': 'true',
'f': 'json',
'token': self.token}
jsonResponse = self.url_request(publishURL, query_dict, 'POST')
try:
if 'jobId' in jsonResponse['services'][0]:
jobID = jsonResponse['services'][0]['jobId']
# valid states: partial | processing | failed | completed
status = 'processing'
print("Checking the status of publish..")
while status == 'processing' or status == 'partial':
status = self.item_status(self.SDitemID, jobID)['status']
print(" {}".format(status))
time.sleep(2)
if status == 'completed':
print("item finished published")
return jsonResponse['services'][0]['serviceItemId']
if status == 'failed':
raise("Status of publishing returned FAILED.")
except Exception as e:
print("Problem trying to check publish status. Might be further errors.")
print("Returned error Python:\n {}".format(e))
print("Message from publish call:\n {}".format(jsonResponse))
print(" -- quit --")
sys.exit()
def enableSharing(self, newItemID, everyone, orgs, groups):
""" Share an item with everyone, the organization and/or groups
"""
shareURL = '{}/content/users/{}/{}/items/{}/share'.format(self.base_url, self.username,
self.folderID, newItemID)
if groups is None:
groups = ''
query_dict = {'f': 'json',
'everyone': everyone,
'org': orgs,
'groups': groups,
'token': self.token}
jsonResponse = self.url_request(shareURL, query_dict, 'POST')
print("successfully shared...{}...".format(jsonResponse['itemId']))
def url_request(self, in_url, request_parameters, request_type='GET',
additional_headers=None, files=None, repeat=0):
"""
Make a request to the portal, provided a portal URL
and request parameters, returns portal response.
Arguments:
in_url -- portal url
request_parameters -- dictionary of request parameters.
request_type -- HTTP verb (default: GET)
additional_headers -- any headers to pass along with the request.
files -- any files to send.
repeat -- repeat the request up to this number of times.
Returns:
dictionary of response from portal instance.
"""
if request_type == 'GET':
req = urllib2.Request('?'.join((in_url, urllib.urlencode(request_parameters))))
elif request_type == 'MULTIPART':
req = urllib2.Request(in_url, request_parameters)
else:
req = urllib2.Request(
in_url, urllib.urlencode(request_parameters), self.headers)
if additional_headers:
for key, value in list(additional_headers.items()):
req.add_header(key, value)
req.add_header('Accept-encoding', 'gzip')
if self.proxyDict:
p = urllib2.ProxyHandler(self.proxyDict)
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(p, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
response = urllib2.urlopen(req)
if response.info().get('Content-Encoding') == 'gzip':
buf = BytesIO(response.read())
with gzip.GzipFile(fileobj=buf) as gzip_file:
response_bytes = gzip_file.read()
else:
response_bytes = response.read()
response_text = response_bytes.decode('UTF-8')
response_json = json.loads(response_text)
if not response_json or "error" in response_json:
rerun = False
if repeat > 0:
repeat -= 1
rerun = True
if rerun:
time.sleep(2)
response_json = self.url_request(
in_url, request_parameters, request_type,
additional_headers, files, repeat)
return response_json
def multipart_request(self, params, files):
""" Uploads files as multipart/form-data. files is a dict and must
contain the required keys "filename" and "content". The "mimetype"
value is optional and if not specified will use mimetypes.guess_type
to determine the type or use type application/octet-stream. params
is a dict containing the parameters to be passed in the HTTP
POST request.
content = open(file_path, "rb").read()
files = {"file": {"filename": "some_file.sd", "content": content}}
params = {"f": "json", "token": token, "type": item_type,
"title": title, "tags": tags, "description": description}
data, headers = multipart_request(params, files)
"""
# Get mix of letters and digits to form boundary.
letters_digits = "".join(string.digits + string.ascii_letters)
boundary = "----WebKitFormBoundary{}".format("".join(random.choice(letters_digits) for i in range(16)))
file_lines = []
# Parse the params and files dicts to build the multipart request.
for name, value in params.iteritems():
file_lines.extend(("--{}".format(boundary),
'Content-Disposition: form-data; name="{}"'.format(name),
"", str(value)))
for name, value in files.items():
if "filename" in value:
filename = value.get("filename")
else:
raise Exception("The filename key is required.")
if "mimetype" in value:
mimetype = value.get("mimetype")
else:
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
if "content" in value:
file_lines.extend(("--{}".format(boundary),
'Content-Disposition: form-data; name="{}"; filename="{}"'.format(name, filename),
"Content-Type: {}".format(mimetype), "",
(value.get("content"))))
else:
raise Exception("The content key is required.")
# Create the end of the form boundary.
file_lines.extend(("--{}--".format(boundary), ""))
request_data = "\r\n".join(file_lines)
request_headers = {"Content-Type": "multipart/form-data; boundary={}".format(boundary),
"Content-Length": str(len(request_data))}
return request_data, request_headers
def makeSD(MXD, serviceName, tempDir, outputSD, maxRecords, tags, summary):
""" create a draft SD and modify the properties to overwrite an existing FS
"""
arcpy.env.overwriteOutput = True
# All paths are built by joining names to the tempPath
SDdraft = os.path.join(tempDir, "tempdraft.sddraft")
newSDdraft = os.path.join(tempDir, "updatedDraft.sddraft")
# Check the MXD for summary and tags, if empty, push them in.
try:
mappingMXD = arcpy.mapping.MapDocument(MXD)
if mappingMXD.tags == "":
mappingMXD.tags = tags
mappingMXD.save()
if mappingMXD.summary == "":
mappingMXD.summary = summary
mappingMXD.save()
except IOError:
print("IOError on save, do you have the MXD open? Summary/tag info not pushed to MXD, publishing may fail.")
arcpy.mapping.CreateMapSDDraft(MXD, SDdraft, serviceName, "MY_HOSTED_SERVICES")
# Read the contents of the original SDDraft into an xml parser
doc = ET.parse(SDdraft)
root_elem = doc.getroot()
if root_elem.tag != "SVCManifest":
raise ValueError("Root tag is incorrect. Is {} a .sddraft file?".format(SDDraft))
# The following 6 code pieces modify the SDDraft from a new MapService
# with caching capabilities to a FeatureService with Query,Create,
# Update,Delete,Uploads,Editing capabilities as well as the ability
# to set the max records on the service.
# The first two lines (commented out) are no longer necessary as the FS
# is now being deleted and re-published, not truly overwritten as is the
# case when publishing from Desktop.
# The last three pieces change Map to Feature Service, disable caching
# and set appropriate capabilities. You can customize the capabilities by
# removing items.
# Note you cannot disable Query from a Feature Service.
# doc.find("./Type").text = "esriServiceDefinitionType_Replacement"
# doc.find("./State").text = "esriSDState_Published"
# Change service type from map service to feature service
for config in doc.findall("./Configurations/SVCConfiguration/TypeName"):
if config.text == "MapServer":
config.text = "FeatureServer"
# Turn off caching
for prop in doc.findall("./Configurations/SVCConfiguration/Definition/" +
"ConfigurationProperties/PropertyArray/" +
"PropertySetProperty"):
if prop.find("Key").text == 'isCached':
prop.find("Value").text = "false"
if prop.find("Key").text == 'maxRecordCount':
prop.find("Value").text = maxRecords
# Turn on feature access capabilities
for prop in doc.findall("./Configurations/SVCConfiguration/Definition/Info/PropertyArray/PropertySetProperty"):
if prop.find("Key").text == 'WebCapabilities':
prop.find("Value").text = "Query,Create,Update,Delete,Uploads,Editing"
# Add the namespaces which get stripped, back into the .SD
root_elem.attrib["xmlns:typens"] = 'http://www.esri.com/schemas/ArcGIS/10.1'
root_elem.attrib["xmlns:xs"] = 'http://www.w3.org/2001/XMLSchema'
# Write the new draft to disk
with open(newSDdraft, 'w') as f:
doc.write(f, 'utf-8')
# Analyze the service
analysis = arcpy.mapping.AnalyzeForSD(newSDdraft)
if analysis['errors'] == {}:
# Stage the service
arcpy.StageService_server(newSDdraft, outputSD)
print("Created {}".format(outputSD))
else:
# If the sddraft analysis contained errors, display them and quit.
print("Errors in analyze: \n {}".format(analysis['errors']))
sys.exit()
if __name__ == "__main__":
#
# start
print("Starting Feature Service publish process")
# Find and gather settings from the ini file
localPath = sys.path[0]
settingsFile = os.path.join(localPath, "settings.ini")
if os.path.isfile(settingsFile):
config = ConfigParser.ConfigParser()
config.read(settingsFile)
else:
print("INI file not found. \nMake sure a valid 'settings.ini' file exists in the same directory as this script.")
sys.exit()
# AGOL Credentials
inputUsername = config.get('AGOL', 'USER')
inputPswd = config.get('AGOL', 'PASS')
# FS values
MXD = config.get('FS_INFO', 'MXD')
serviceName = config.get('FS_INFO', 'SERVICENAME')
folderName = config.get('FS_INFO', 'FOLDERNAME')
tags = config.get('FS_INFO', 'TAGS')
summary = config.get('FS_INFO', 'DESCRIPTION')
maxRecords = config.get('FS_INFO', 'MAXRECORDS')
# Share FS to: everyone, org, groups
shared = config.get('FS_SHARE', 'SHARE')
everyone = config.get('FS_SHARE', 'EVERYONE')
orgs = config.get('FS_SHARE', 'ORG')
groups = config.get('FS_SHARE', 'GROUPS') # Groups are by ID. Multiple groups comma separated
use_prxy = config.get('PROXY', 'USEPROXY')
pxy_srvr = config.get('PROXY', 'SERVER')
pxy_port = config.get('PROXY', 'PORT')
pxy_user = config.get('PROXY', 'USER')
pxy_pass = config.get('PROXY', 'PASS')
proxyDict = {}
if ast.literal_eval(use_prxy):
http_proxy = "http://" + pxy_user + ":" + pxy_pass + "@" + pxy_srvr + ":" + pxy_port
https_proxy = "http://" + pxy_user + ":" + pxy_pass + "@" + pxy_srvr + ":" + pxy_port
ftp_proxy = "http://" + pxy_user + ":" + pxy_pass + "@" + pxy_srvr + ":" + pxy_port
proxyDict = {"http": http_proxy, "https": https_proxy, "ftp": ftp_proxy}
# create a temp directory under the script
tempDir = os.path.join(localPath, "tempDir")
if not os.path.isdir(tempDir):
os.mkdir(tempDir)
finalSD = os.path.join(tempDir, serviceName + ".sd")
# initialize AGOLHandler class
agol = AGOLHandler(inputUsername, inputPswd, serviceName, folderName, proxyDict)
# Turn map document into .SD file for uploading
makeSD(MXD, serviceName, tempDir, finalSD, maxRecords, tags, summary)
# overwrite the existing .SD on arcgis.com
if agol.upload(finalSD, tags, summary):
# publish the sd which was just uploaded
fsID = agol.publish()
# share the item
if ast.literal_eval(shared):
agol.enableSharing(fsID, everyone, orgs, groups)
print("\nfinished.")