wmarcell

Using a list of mxd locations with arcpy mapping

Discussion created by wmarcell on Mar 15, 2012
I am having an issue with a script I am writing for data management.

The idea of the script is to use AGSSOM to crawl a gis server and get the mxd/msd file locations for all the hosted services.  I would then feed these file locations into arcpy mapping to get information such as the layer name, the layers location in SDE, and all of the fields that are available for the Feature Class in SDE.  The script itself works fine if I feed the info in manually.  However if i generate a list of mxd locations and then use a for loop on the list to feed arcpy mapping it does not work for any mxd.

I have a feeling that the function arcpy.mapping.MapDocument(mxd) does not like the way that the file locations are being handled from a for loop list.  I have tried many different ways to change the syntax to no avail.  The code below is in the middle of me testing things out.  I would like to use the list that I am generating (MXDList) but I can of course use the csv that i created to pull the file locations.  I was just wondering if anyone has seen this or knows how to handle it.  Perhaps I am just missing something obvious or am taking the completely wrong approach.

I ran a couple of commands at the completion that may help with debug.  I think that the way the list is generating strings (single quotes) may not be condusive to the way arcpy mapping is expecting the input (generally single quotes or prefaced with r).  Any feedback or advice is greatly appreciated as I think this will be a very good tool for managing data and may be expanded for rebuilding services or upgrading servers.

Output
>>> mxd
'C:/arcgisserver/arcgisinput/ZoomtoGP/Easements_Centroids.mxd'
>>> print mxd
C:/arcgisserver/arcgisinput/ZoomtoGP/Easements_Centroids.mxd
>>> str(mxd)
'C:/arcgisserver/arcgisinput/ZoomtoGP/Easements_Centroids.mxd'
>>> print str(mxd)
C:/arcgisserver/arcgisinput/ZoomtoGP/Easements_Centroids.mxd

My mess of code... Sorry in advance!

# Import arcpy module
import arcpy, subprocess, os, re
outfile = open(r'WebServiceLocations.csv', 'w')
#Get list of map services currently running
stream = os.popen('C:\scripts\ServiceInfo\AGSSOMv10.0\AGSSOM.exe -list MapServer' )
mapservices = stream.readlines()
MXDList  = []


for item in mapservices:
    itemSplit = item.split(',')
    if len(itemSplit) > 1:
        stream2 = os.popen('C:\scripts\ServiceInfo\AGSSOMv10.0\AGSSOM.exe -describe ' + itemSplit[0])
        descMapServ = stream2.readlines()
        for desc in descMapServ:
            desc = desc.strip()
            if desc.find("FilePath") > -1:
                desc = desc.replace("FilePath: ", "")
##                print itemSplit[0]
##                print desc
                try:
                    outfile.write('"' + itemSplit[0] + '","' + desc + '"\n')
                    MXDList.append(desc)
                except:
                    outfile.write('"' + itemSplit[0] + '","' + "No Found Path" + '"\n')
                    print itemSplit[0] + "Error"
                del desc
print MXDList
outfile.close()

if os.path.exists("MXDdescription.txt"):
    os.remove("MXDdescription.txt")
MXDList = [x.replace('.msd', '.mxd') for x in MXDList]
MXDList = [x.replace('\\',  '/') for x in MXDList]
print MXDList
print str(MXDList)

    
MXDdescribe = open("MXDdescription.txt", "a")

for mxd in MXDList:
    print mxd
    MXDdescribe.write(mxd + '\n')
##    mxd = '"%s"' % mxd
##    mxd = "r" + mxd
##    print mxd
##    print mxd[1:-1]
    try:
        print 'r"' + mxd + '"'
        inMxd =arcpy.mapping.MapDocument('r"' + mxd + '"')
        print inMxd
        for df in arcpy.mapping.ListDataFrames(inMxd):
            for lyr in df:
##                MXDdescribe.write("  " + lyr + '\n')
##                print lyr
                if lyr.supports("WORKSPACEPATH"):
                    MXDdescribe.write("  " + lyr + " - " + lyr.workspacePath + '\n')
##                    print lyr.workspacePath 
                if lyr.supports("SERVICEPROPERTIES"):
                    if lyr.serviceProperties["ServiceType"]=="SDE":
                        servProp = lyr.serviceProperties
##                        print "    " + lyr.longName + " - " + servProp.get('Server') + "/" + lyr.datasetName 
                        lyrtbl = lyr.name + "_tbl"
                        arcpy.MakeTableView_management(lyr, lyrtbl)
                        fields = arcpy.ListFields(lyrtbl)
                        fieldnames = [f.name for f in fields]
##                        print "        " + str(fieldnames)
                        arcpy.Delete_management(lyrtbl)
    except:
        MXDdescribe.write ("Check and rename msd to match mxd!!!" + '\n')
##MXDList = [x.replace('.msd', '.mxd') for x in MXDList]
##print MXDList

MXDdescribe.close()

Outcomes