Hello,
I have written a script that should take a 'Table View' from an SDE Geodatabase, and export it to Shapefile format for use as source data for a layer in a specific MXD file. The script should then take this MXD file and upload it to ArcGIS Server as a map service. This map service must overwrite an existing service (this is a weekly process). I was wondering if anyone could take a look at my code, before I actually run it, to see if there is anything in there that could be a potential issue. I really appreciate any help whatsoever. Code is posted below. Thanks so much.
-Jacob
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import arcpy
import xml.dom.minidom as DOM
import os
import sys
import traceback
arcpy.AddMessage("Setting the workspace to your Vector.sde connection...")
arcpy.env.workspace = arcpy.GetParameterAsText(0)
try:
wrkspc = arcpy.GetParameterAsText(1)
parcel_shp = str(wrkspc) + "\parcels.shp"
parcel_view = "vector.dbo.PARCEL_VIEW"
tags = 'Spotsylvania, Virginia, VA, CRW'
summary = "Map Service is to be used within Spotsylvania County's CRW application"
service = 'CRW'
server = r"GIS Servers\arcgis on gis.spotsylvania.va.us (publisher)"
mapDoc = arcpy.GetParameterAsText(2)
sddraft = str(wrkspc) + "\CRW_MapService.sddraft"
sd = str(wrkspc) + "\CRW_MapService.sd"
if os.path.exists(parcel_shp):
arcpy.AddMessage("Renaming the old 'parcels' shapefile to 'parcels_old'")
arcpy.Rename_management(parcel_shp, str(wrkspc) + "\parcels_old")
arcpy.AddMessage("Creating new 'parcels' shapefile...")
arcpy.FeatureClassToFeatureClass_conversion(parcel_view, wrkspc, "parcels")
else:
arcpy.FeatureClassToFeatureClass_conversion(parcel_view, wrkspc, "parcels")
arcpy.AddMessage("Dropping 'OID' field from new shapefile...")
dropField = ["ESRI_OID"]
arcpy.DeleteField_management(parcel_shp, dropField)
arcpy.AddMessage("Setting parameters for the CRW Map Service. During this process, the script will specify the the service definition type as a replacement...")
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, 'CRW', 'ARCGIS_SERVER', server, True, None, summary, tags)
newType = 'esriServiceDefinitionType_Replacement'
xml = sddraft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
if desc.parentNode.tagName == 'SVCManifest':
if desc.hasChildNodes():
desc.firstChild.data = newType
outXml = xml
f = open(outXml, 'w')
doc.writexml( f )
f.close()
if analysis['errors'] == {}:
arcpy.AddMessage("There were noe errors...the service should now publish.")
arcpy.StageService_server(sddraft, sd)
arcpy.UploadServiceDefinition_server(sd, server)
else:
arcpy.AddError("There seem to have been some errors with some of the parameters that have been specified. Please see the messages below.")
arcpy.AddError(str(analysis['errors']))
arcpy.AddWarning("The tool ran successfully. Please make sure that the CRW Map Service has successfully been uploaded.")
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS: \nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_type) + ":" +str(sys.exc_value) + "\n"
arcpy.AddError(pymsg)
msgs = "ArcPy ERRORS: \n" +arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
print pymsg + "\n"
print msgs