Error when trying to publish geoprocessing service

Discussion created by ionarawilson on Sep 3, 2013
Latest reply on Sep 5, 2013 by khibma-esristaff
I am trying to publish a geoprocessing script from a script tool in python. The code runs great in ArcGIS but I am getting error messages when trying to publish the geoprocessing service.


#Import modules
import os, sys, arcpy, traceback, arcgisscripting

#Set Map Document
mxd = arcpy.mapping.MapDocument("Current")

#Set Overwrite Option
arcpy.env.overwriteOutput = True

gp = arcgisscripting.create(10.1)


    sCursor = arcpy.SearchCursor("Stewardship")
    for row in sCursor:
        if row.SequenceNumber:

            arcpy.AddError("You can't run the tool in previous records. Please select your last digitized record and run the tool")

        if not row.SequenceNumber:

        #Sets parameters (attributes)
            Status = gp.GetParameterAsText(0)
            Office =  gp.GetParameterAsText(1)
            Forester = gp.GetParameterAsText(2)
            DateStart = gp.GetParameterAsText(3)
            PlanLength = gp.GetParameter(4)
            PlanEQIP = gp.GetParameter(5)

        # Create domains
        # This is to send the code to the database but keep the description for the user input
            StatusDomain = {'Completed': 'C', 'Pending': 'P'}
            StatusCode = StatusDomain[Status]

            officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC''}
            officeCode= officeDomain[Office]
            foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope' }

            foresterCode = foresterDomain[Forester]
            UnderservedDomain = {'Yes': '1', 'No': '0'}
            UnderservedCode = UnderservedDomain[Underserved]

    del row, sCursor

# This is to send the data input to the database
    with arcpy.da.UpdateCursor("Stewardship", ("Status", "Office", "Forester", "DateStart","PlanLength", "PlanEQIP", "RecipientLast", "RecipientFirst",
     "MailAddress", "City" , "State", "ZipCode", "Phone", "Email", "Underserved")) as rows:
        # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
        for row in rows:
            row[0] = StatusCode
            row[1] = officeCode
            row[2] = foresterCode
    del row, rows
# This is to save only the year to the FFY field from the DateStart field
    with arcpy.da.UpdateCursor("Stewardship", ("FFY")) as rows:
        Datestarstr1 = str(DateStart)

        if len(Datestarstr1) == 9:
            # if for example 6/28/2013 or 10/4/2013
                yearonly = DateStart[5:9]

        # if for example 6/8/2013
        if len(Datestarstr1) == 8:
                yearonly = DateStart[4:8]
        if len(Datestarstr1) > 9:
             # if for example 10/10/2013
                yearonly = DateStart[6:10]
        for row in rows:
            row[0] = yearonly


# This is to create centroids of the stewardship boundaries to select the county that contains the centroid (stewardship), to be used to create the PlanID

    Countieslayer = 'Counties FL'  # this is a string that represents the Counties feature layer
    Stewardshiplayer = 'Stewardship FL'  # this is a string that represents the Stewardship feature layer
    Stewardshipcentroidslayer = 'Stewardship centroid FL'  # this is a string that represents the Stewardship feature layer
    if arcpy.Exists(Countieslayer):
        # make feature layer
    arcpy.MakeFeatureLayer_management("Counties", "Countieslayer")

    if arcpy.Exists(Stewardshiplayer):
        # make feature layer
    arcpy.MakeFeatureLayer_management("Stewardship", "Stewardshiplayer")

    # I don't need to save the output to a folder, i can create a variable to hold a name and then create the layer after creating centroids
    outstewardshipcentroids = "StewardshipCentroids2"

    # Create centroid
    arcpy.FeatureToPoint_management("Stewardshiplayer",  outstewardshipcentroids,"INSIDE")

    if arcpy.Exists(Stewardshipcentroidslayer):
        # make feature layer
    arcpy.MakeFeatureLayer_management(outstewardshipcentroids, "Stewardshipcentroidslayer")

    arcpy.SelectLayerByLocation_management("Countieslayer", "CONTAINS", "Stewardshipcentroidslayer", "", "NEW_SELECTION")

    countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]
    urows = arcpy.da.UpdateCursor("StewardshipLayer", "County")
    for urow in urows:
        urow[0] = countycode

    del urows, urow

# To add 1 to the year if month starts in October

    import time
    from datetime import datetime, date
    #sim_date_counter = 1

    with arcpy.da.UpdateCursor("Stewardshiplayer", ("FFY")) as rows:
      for row in rows:
        #get the value of the date

        FFY = row[0]
        FFYnumber = int(FFY)
        #convert to string
        DateStartstr = str(DateStart)
        # Get only the month
        # To get only the first two characters:
        DateStart2 = DateStartstr[0:2]
        if "/" in DateStart2:
                    # if for example 6/28/2013
                DateStart3 = DateStart[0:1]
                FFYnumbercount = FFYnumber
        if "/" not in DateStart2:
             # if for example 10/10/2013
            DateStart4 = int(DateStart2)
            FFYnumbercount = FFYnumber + 1

        row[0] = str(FFYnumbercount)


# First we need to search by object id to it, it can be the highest, since there is only one selected
    #maxValue3 = arcpy.SearchCursor("Stewardshiplayer", "", "", "", "OBJECTID D").next().getValue("OBJECTID")

    trows = arcpy.SearchCursor("Stewardshiplayer")

    for row in trows:

               FFYtxt = row.getValue("FFY")
               Countytxt = row.getValue("County")
    del trows, row

# we list the layer to create a layer feature without a selection (because it is selected by the user)
    lyr = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument('CURRENT'), 'Stewardship')[0]

    arcpy.MakeFeatureLayer_management(lyr.dataSource, "TMP")
# To get the total ammount of records, not only the selected
    result = int(arcpy.GetCount_management("TMP").getOutput(0))

# write a query to get the county and year using the FFY and County of the object id of the Stewardship layer (the one that is selected)

    query = "FFY" +"=" + "'"+ str(FFYtxt)+"'" + "AND " + "County" +"=" + "'"+ str(Countytxt)+"'"
    # select all the records that have the FFYcounty the same as the FFYCounty of the hightest ObjectID
    arcpy.SelectLayerByAttribute_management("TMP",  "NEW_SELECTION", query )
    result = int(arcpy.GetCount_management("TMP").getOutput(0))

# if there are more than 1 records with the same ffy and county
    # select only the record witht the highest sequencenumber (sort first, then select)
    if  result > 1:
        maxValue4 = arcpy.SearchCursor("TMP", "", "", "", "SequenceNumber D").next().getValue("SequenceNumber")

        query = "SequenceNumber = "
        arcpy.SelectLayerByAttribute_management("TMP",  "SUBSET_SELECTION", "SequenceNumber = " + str(maxValue4))
        result = int(arcpy.GetCount_management("TMP").getOutput(0))

    rows = arcpy.SearchCursor("TMP")

    for row in rows:

               Seqnum = row.SequenceNumber

    del rows, row

# If the sequence number is equal or higher than 1 it means there are other records with the same FFY and County, so we add 1 to the number

    rows = arcpy.UpdateCursor("Stewardshiplayer")
# we make sure the Sequence number gets updated with the variable that is counting the sequence number
    for row in rows:
        row.Sequencenumber = Seqnum
    del rows, row

    with arcpy.da.UpdateCursor("Stewardshiplayer", ("Sequencenumber")) as rows:

        if Seqnum >= 1:
                arcpy.AddMessage("Other records with the same FFY and County")
                vsn = Seqnum + 1
                arcpy.AddMessage("No othe records with the same FFY and County")
                vsn = 1
        for row in rows:

        #get the value of the date

           row[0] = vsn


# we Create the Plan ID concatenating the FFY, the county and the Sequence number, adding zeros in front of the sequence number if necessary to make the lenght always 3
    with arcpy.da.UpdateCursor("Stewardship", ("PlanID")) as rowsffycounty2:
        for row in rowsffycounty2:
            row[0] = str(FFYtxt) + "-" + str(Countytxt) + "-" + str(vsn).zfill(3)



    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"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"


    print msgs
    print pymsg

    print arcpy.GetMessages(1)