Bleroux

Python GPService SDE Connection

Discussion created by Bleroux on Jun 7, 2013
Latest reply on Jul 21, 2015 by Bleroux
I am trying to publish a GPService from a python script. I am having with the service connecting to SDE. I was having trouble so I added the Test Connection section to see what path was being used. It seems like it is not getting the connection info from the SDE file. The script runs fine in ArcMap and I have the SDE file in the same location on the server as it is my dev machine. Any ideas?

"Error executing tool.: C:\arcgisserver\directories\arcgissystem\arcgisinput\GPTools\WildfireUpload.GPServer\extracted\v101\\ArcSDE.dbo.WILDFIREPERIM Table does not exist "

import arcpy, os, zipfile
from arcpy import env

#Setting the env workspace to be an SDE geodatabase
ws = env.workspace = "D:\\agsResources\\DataStoreSDEs\\GIS01(TEST Server).sde"
#Test Connection
if arcpy.Exists(ws + "\\ArcSDE.dbo.WILDFIREPERIM") == False:
    arcpy.AddMessage("Table does not exist")
    arcpy.AddError(ws + "\\ArcSDE.dbo.WILDFIREPERIM Table does not exist")
else:
    arcpy.AddMessage("Table Exists")

infile = arcpy.GetParameterAsText(0)
#startdate = arcpy.GetParameterAsText(1)
outpath, outfileext = os.path.splitext(infile)
filename = outpath.split('\\')[-1]
try:
    # unzip file
    fireZip = zipfile.ZipFile(infile, 'r')
    fireZip.extractall(outpath)
    fireZip.close()
    #arcpy.SetParameterAsText(1,outpath + "\\" + filename + ".shp")
    shpPath = outpath + "\\" + filename + ".shp"
    arcpy.AddMessage("Finished unzipping file.")

# Local variables:
    WildFire_Table_Target = ws + "\\ArcSDE.dbo.WILDFIREPERIM"
    # Create FieldMappings object and load the target dataset
#
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.addTable(WildFire_Table_Target)

    inputfields = [field.name for field in arcpy.ListFields(shpPath) if not field.required]
    for inputfield in inputfields:
        # Iterate through each FieldMap in the FieldMappings
        #
        for i in range(fieldmappings.fieldCount):
            fieldmap = fieldmappings.getFieldMap(i)
            #arcpy.AddMessage(fieldmap.getInputFieldName(0))
            # If the field name from the target dataset matches to a validated input field name
            #
            if fieldmap.getInputFieldName(0) == inputfield.replace("", ""):
                # Add the input field to the FieldMap and replace the old FieldMap with the new
                #
                fieldmap.addInputField(shpPath, inputfield)
                fieldmappings.replaceFieldMap(i, fieldmap)
                break

            
# Process: Append
    arcpy.Append_management(shpPath, WildFire_Table_Target, "NO_TEST", fieldmappings)

except Exception as e:
      print e.message
      arcpy.AddError(e.message)

Outcomes