TransWikia.com

IO error on bulk MXD publishing to ArcGIS Server using Python

Geographic Information Systems Asked on December 6, 2020

I have a folder of MXDs and I want to publish all of them in batch to a single ArcGIS Server. I’ve had success using Python on a single MXD, but when I add a loop into the code so that I can do multiple services at once, I run into errors that I’m unable to troubleshoot. I found the code below on ArcGIS Enterprise site and tried to modify it for batch processing.

import arcpy
import os

wrkspc = 'C:/Project/BULK/'
#mxd = arcpy.mapping.MapDocument('BULK_Test.mxd')

con = 'AGSconnectionfile.ags'

for (wrkspc, dirs, files) in os.walk(wrkspc):
    for fl in files:
        if fl.lower().endswith(".mxd"):
            
            mxd = arcpy.mapping.MapDocument(os.path.join(wrkspc, fl))
            # Provide other service details
            service = mxd
            sddraft = 'C:/Project/BULK/' + str(mxd) + '.sddraft'
            sd = 'C:/Project/BULK/' + str(mxd) + '.sd'
            summary = 'BULK_Test'
            tags = 'BULK_Test'

            # Create service definition draft
            arcpy.mapping.CreateMapSDDraft(mxd, sddraft, mxd, 'ARCGIS_SERVER', con, True, "Project", summary, tags)

            # Analyze the service definition draft
            analysis = arcpy.mapping.AnalyzeForSD(sddraft)

            # Print errors, warnings, and messages returned from the analysis
            print "The following information was returned during analysis of the MXD:"
            for key in ('messages', 'warnings', 'errors'):
                print '----' + key.upper() + '---'
                vars = analysis[key]
                for ((message, code), layerlist) in vars.iteritems():
                    print '    ', message, ' (CODE %i)' % code
                    print '       applies to:',
                    for layer in layerlist:
                        print layer.name,
                    print

            # Stage and upload the service if the sddraft analysis did not contain errors
            if analysis['errors'] == {}:
                # Execute StageService. This creates the service definition.
                arcpy.StageService_server(sddraft, sd)

                # Execute UploadServiceDefinition. This uploads the service definition and publishes the service.
                arcpy.UploadServiceDefinition_server(sd, con)
                print "Service successfully published"
            else: 
                print "Service could not be published because errors were found during analysis."

            print arcpy.GetMessages()

I’ve tried as a standalone script and from within ArcMap 10.7.1. The error:

File "C:ProjectBULKPublishAGSBulk.py", line 29, in <module>
IOError: Operation on file C:Users...Temp{67337BAB-1DDF-4A39-9FB4-B7C8EF8597B0}<geoprocessing Map object object at 0x036534A0>.sddraft failed. Unknown error.

I am doing this with an active connection to the ArcGIS Server (10.8) in ArcCatalog.

One Answer

I made a couple of other changes which suit my needs:

import arcpy
import xml.dom.minidom as DOM

Workspace = r'C:ProjectBULKMXDs'
arcpy.env.workspace = Workspace
#connection workspace - this is my default location
con_wrkspc = r'C:UsersUSERNAMEAppDataRoamingESRIDesktop10.6ArcCatalog'
con = con_wrkspc + '' + 'arcgis_connection.ags'

for file in arcpy.ListFiles("*.mxd"):
    print ("Processing: "+ file)
    mxd_path = Workspace + '' + file
    mxd = arcpy.mapping.MapDocument(mxd_path)
    # Provide location for drafts
    service = file.strip('.mxd')
    sddraft = 'C:tempdrafts' + '' + str(service) + '.sddraft'
    sd = 'C:tempdrafts' + ''+ str(service) + '.sd'
    #summary option provided you have previously stored this
    summary = mxd.summary
    #summery = 'Bulk'
    tags = 'Bulk upload'

    # Create service definition draft
    #Syntax
    # CreateMapSDDraft (map_document, out_sddraft, service_name, {server_type}, {connection_file_path}, {copy_data_to_server}, {folder_name}, {summary}, {tags})
    arcpy.mapping.CreateMapSDDraft(mxd,sddraft,service,'FROM_CONNECTION_FILE',con,"FALSE","CYP",summary,tags)

    # change service parameters - I don't want Kml enabled
    doc = DOM.parse(sddraft)
    services___ = doc.getElementsByTagName('TypeName')
    for service__ in services___:
        if service__.firstChild.data == 'KmlServer':
            service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'false'

    # output to a new sddraft
    New_sddraft = 'C:tempdrafts' + '' + str(service) + '_new.sddraft'
    f = open(New_sddraft, 'w')
    doc.writexml( f )
    f.close() 

    # Analyze the service definition draft
    analysis = arcpy.mapping.AnalyzeForSD(New_sddraft)

    # Print errors, warnings, and messages returned from the analysis
    print "The following information was returned during analysis of the MXD:"
    for key in ('messages', 'warnings', 'errors'):
        print '----' + key.upper() + '---'
        vars = analysis[key]
        for ((message, code), layerlist) in vars.iteritems():
            print '    ', message, ' (CODE %i)' % code
            print '       applies to:',
            for layer in layerlist:
                print layer.name,
            print
    # Stage and upload the service if the sddraft analysis did not contain error
    if analysis['errors'] == {}:
        # Execute StageService. This creates the service definition.

        arcpy.StageService_server(New_sddraft, sd)
        # Execute UploadServiceDefinition. This uploads the service definition and publishes the service.

        arcpy.UploadServiceDefinition_server(sd, con)
        print "Service successfully published!"
    else: 
        print "Service could not be published because errors were found during analysis."
        pass
    print "arcpy messages......"
    print arcpy.GetMessages()

Answered by wunderkind on December 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP