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
---------------------------------------------------------------------------------------------------------------------------------------------------------------
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> xml<SPAN class="punctuation token">.</SPAN>dom<SPAN class="punctuation token">.</SPAN>minidom <SPAN class="keyword token">as</SPAN> DOM
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> traceback
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Setting the workspace to your Vector.sde connection..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># User must enter connection to 'Vector' SDE geodatabase ##"C:\Users\jsnyder\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\Vector.sde"</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Specify the folder in which the shapefiles that are necessary will be created, set other variables</SPAN>
wrkspc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Navigate to the workspace that contains or should contain parcels shapefile ##r"F:\ETL\04_Exports\Output\CRW"</SPAN>
parcel_shp <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\parcels.shp"</SPAN> <SPAN class="comment token">##"F:\ETL\04_Exports\Output\CRW\parcels.shp"</SPAN>
parcel_view <SPAN class="operator token">=</SPAN> <SPAN class="string token">"vector.dbo.PARCEL_VIEW"</SPAN>
tags <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Spotsylvania, Virginia, VA, CRW'</SPAN>
summary <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Map Service is to be used within Spotsylvania County's CRW application"</SPAN>
service <SPAN class="operator token">=</SPAN> <SPAN class="string token">'CRW'</SPAN>
server <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"GIS Servers\arcgis on gis.spotsylvania.va.us (publisher)"</SPAN>
mapDoc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Navigate to the location of the CRW MXD file that will be used to publish the map service ##arcpy.mapping.MapDocument(r"F:\MXDs\CRW_MapService\CRW_MapService.mxd")</SPAN>
sddraft <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\CRW_MapService.sddraft"</SPAN> <SPAN class="comment token">##r"X:\ETL\04_Exports\Output\CRW\CRW_MapService.sddraft"</SPAN>
sd <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\CRW_MapService.sd"</SPAN> <SPAN class="comment token">##r"X:\ETL\04_Exports\Output\CRW\CRW_MapService.sd"</SPAN>
<SPAN class="comment token"># If any shapefiles exist in the folder currently, delete them.</SPAN>
<SPAN class="keyword token">if</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>parcel_shp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Renaming the old 'parcels' shapefile to 'parcels_old'"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Rename_management<SPAN class="punctuation token">(</SPAN>parcel_shp<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\parcels_old"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Creating new 'parcels' shapefile..."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>FeatureClassToFeatureClass_conversion<SPAN class="punctuation token">(</SPAN>parcel_view<SPAN class="punctuation token">,</SPAN> wrkspc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"parcels"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>FeatureClassToFeatureClass_conversion<SPAN class="punctuation token">(</SPAN>parcel_view<SPAN class="punctuation token">,</SPAN> wrkspc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"parcels"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Dropping 'OID' field from new shapefile..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Delete 'ESRI_OID' field from new shapefile</SPAN>
dropField <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"ESRI_OID"</SPAN><SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>DeleteField_management<SPAN class="punctuation token">(</SPAN>parcel_shp<SPAN class="punctuation token">,</SPAN> dropField<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Setting parameters for the CRW Map Service. During this process, the script will specify the the service definition type as a replacement..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Create the service definition draft for the CRW Map Service</SPAN>
analysis <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>CreateMapSDDraft<SPAN class="punctuation token">(</SPAN>mapDoc<SPAN class="punctuation token">,</SPAN> sddraft<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CRW'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ARCGIS_SERVER'</SPAN><SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> summary<SPAN class="punctuation token">,</SPAN> tags<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set service type to be 'esriServiceDefinitionType_Replacement' to serve as a replacement for the existing service</SPAN>
newType <SPAN class="operator token">=</SPAN> <SPAN class="string token">'esriServiceDefinitionType_Replacement'</SPAN>
xml <SPAN class="operator token">=</SPAN> sddraft
doc <SPAN class="operator token">=</SPAN> DOM<SPAN class="punctuation token">.</SPAN>parse<SPAN class="punctuation token">(</SPAN>xml<SPAN class="punctuation token">)</SPAN>
descriptions <SPAN class="operator token">=</SPAN> doc<SPAN class="punctuation token">.</SPAN>getElementsByTagName<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Type'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> desc <SPAN class="keyword token">in</SPAN> descriptions<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> desc<SPAN class="punctuation token">.</SPAN>parentNode<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'SVCManifest'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> desc<SPAN class="punctuation token">.</SPAN>hasChildNodes<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
desc<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> newType
outXml <SPAN class="operator token">=</SPAN> xml
f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>outXml<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN>
doc<SPAN class="punctuation token">.</SPAN>writexml<SPAN class="punctuation token">(</SPAN> f <SPAN class="punctuation token">)</SPAN>
f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># After analyzing the service definition draft, if there are no errors, stage and upload the service</SPAN>
<SPAN class="keyword token">if</SPAN> analysis<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"There were noe errors...the service should now publish."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Execute Stage Service</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>StageService_server<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">,</SPAN> sd<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Upload the service</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>UploadServiceDefinition_server<SPAN class="punctuation token">(</SPAN>sd<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"There seem to have been some errors with some of the parameters that have been specified. Please see the messages below."</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>analysis<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The tool ran successfully. Please make sure that the CRW Map Service has successfully been uploaded."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
tb <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>exc_info<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
tbinfo <SPAN class="operator token">=</SPAN> traceback<SPAN class="punctuation token">.</SPAN>format_tb<SPAN class="punctuation token">(</SPAN>tb<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
pymsg <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PYTHON ERRORS: \nTraceback info:\n"</SPAN> <SPAN class="operator token">+</SPAN> tbinfo <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\nError Info:\n"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>exc_type<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">":"</SPAN> <SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>exc_value<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>pymsg<SPAN class="punctuation token">)</SPAN>
msgs <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ArcPy ERRORS: \n"</SPAN> <SPAN class="operator token">+</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>msgs<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> pymsg <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN>
<SPAN class="keyword token">print</SPAN> msgs
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>