# Name: ExportMapServerCacheTiles.py
# Description: The following stand-alone script demonstrates how to export
# cache from a map layer in ArcGIS Enterprise portal,
# as CACHE_DATASET in COMPACT_V2 storage format and MERGE tiles
# using an AREA_OF_INTEREST to TARGET_CACHE_PATH
# which is accessible to server instances
# Example: This sample script updates map cache tiles.
import arcpy
from arcpy import env
import os, sys, time, datetime, traceback, string
import traceback, sys
# Set environment settings
env.workspace = "C:/data"
# Sign in to ArcGIS Enterprise
myPortal= "https://MyPortal.domain.com/portalwebadaptor"
arcpy.SignInToPortal(myPortal, "siteadminValue", "siteadminPassword")
myServer="https://Myserver.domain.com/serverwebadaptor"
serviceName= "SampleWorldCities"
serviceType= "MapServer"
myPortalServiceURL = (myServer + "/" + "rest/services" +"/" + serviceName + "/"
+ serviceType)
# Stand alone ArcGIS Server
##target_server_connection = r"C:\share\python3\arcgis on MyServer.ags"
##serviceName= "MyServiceName"
##serviceType= ".MapServer"
##myPortalServiceURL = (target_server_connection + "\\" + serviceName + serviceType)
print (myPortalServiceURL)
#variables for reporting
currentTime = datetime.datetime.now()
arg1 = currentTime.strftime("%H-%M")
arg2 = currentTime.strftime("%Y-%m-%d %H:%M")
file = r'C:\test\report_%s.txt'% arg1
# List of input variables for map or image service
targetCachePath = "C:/data/temp"
exportCacheType = "CACHE_DATASET"
copyDataFromServer = "DO_NOT_COPY"
storageFormat = "COMPACT_V2"
scales = [500000,250000,125000,64000]
numOfCachingServiceInstances = "2"
areaOfInterest = "C:/data/101/Portland/Metro.shp"
exportExtents = ""
overwriteTiles = "MERGE"
ready_to_serve_format = "NON_READY_TO_SERVE_FORMAT"
# Print results of the script to a report
report = open(file,'w')
try:
result = arcpy.server.ExportMapServerCache(myPortalServiceURL, targetCachePath,
exportCacheType,
copyDataFromServer,
storageFormat, scales,
numOfCachingServiceInstances,
areaOfInterest, exportExtents,
overwriteTiles,
ready_to_serve_format)
while result.status < 4:
time.sleep(0.2)
resultValue = result.getMessages()
report.write ("completed " + str(resultValue))
print ("Exported cache successfully for " + serviceName )
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)
report.write(str(e))
report.close()