#------------------------------------------------------------------------------- # Name: Search Directories and ID MXD Layer Sources # Purpose: This script runs through a mothership directory searching out MXDs # # Author: John Spence, Spatial Data Administrator, City of Bellevue # # Created: November 17, 2020 # Modified: # Modification Purpose: # # # #------------------------------------------------------------------------------- # 888888888888888888888888888888888888888888888888888888888888888888888888888888 # ------------------------------- Configuration -------------------------------- # Set the data store location for all your MXDs below. # ShareLevel options include PUBLIC or PRIVATE. # ShareOrg options include SHARE_ORGANIZATION or NO_SHARE_ORGANIZATION # ShareGroups options are to place the name of a group in there. # # ------------------------------- Dependencies --------------------------------- # # # 888888888888888888888888888888888888888888888888888888888888888888888888888888 # # Data Store Location Where MXDs Reside data_store_path_MXD = r"\\fileserver\data\GISPublished\Production" # # Where the Output will reside from the search inCsv = r"\\fileserver\data\GISPublished\Production" # # File Name Pre-Fix for the Output fileprefix = 'Production_' # # ------------------------------------------------------------------------------ # DO NOT UPDATE BELOW THIS LINE OR RISK DOOM AND DISPAIR! Have a nice day! # ------------------------------------------------------------------------------ # Import Python libraries import arcpy, os, sys, datetime, csv import xml.dom.minidom as DOM import pandas as pd #import xlsxwriter import collections from os import listdir from os.path import isfile, join arcpy.env.overwriteOutput = False def dirWalk(): for dirpath, dirnames, filenames in os.walk(data_store_path_MXD): if 'System' in dirnames: dirnames.remove('System') if 'Utilities' in dirnames: dirnames.remove('Utilities') if '.site' in dirnames: dirnames.remove('.site') if 'SampleWorldCities.MapServer' in dirnames: dirnames.remove('SampleWorldCities.MapServer') for filenames in [f for f in filenames if f.endswith(".mxd")]: print ("Target found: " + os.path.join(dirpath,filenames)) data_path_MXD = dirpath mxdfile = filenames findMXD (data_path_MXD, mxdfile, inCsv, inXLS) return def findMXD(data_path_MXD, mxdfile, inCsv, inXLS): mxd_service_name = mxdfile name = mxd_service_name.replace('.mxd', '') currentDT = datetime.datetime.now() print ("Sources Task Started: " + str(currentDT)) print ("Source name: " + name) doc = data_path_MXD + '\\' + mxdfile print (doc) mxdInfo(doc, mxd_service_name, inCsv, inXLS, data_path_MXD) currentDT = datetime.datetime.now() print ("Sources Task Completed: " + str(currentDT) + "\n") return def mxdInfo(doc, mxd_service_name, inCsv, inXLS, data_path_MXD): layer = [] mxd = arcpy.mapping.MapDocument(doc) a = collections.OrderedDict() for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("SERVICEPROPERTIES"): servProp = lyr.serviceProperties lName = lyr.name try: ldquery = lyr.definitionQuery except: ldquery = 'None' dbName = servProp.get('Database', 'N/A') serviceName = servProp.get('Service', 'N/A') serverName = serviceName.split(':') userName = servProp.get('UserName', 'N/A') workspace = lyr.supports('WORKSPACEPATH') try: dq_layer = 'True' dq = lyr.supports('DEFINITIONQUERY') except: dq_layer = 'False' dq = 'N/A' mxdpath = data_path_MXD else: print "SERVICEPROPERTIES not supported on " + lyr.name continue if lyr.supports("DATASOURCE"): dSource = str(lyr.dataSource) fcName = dSource.split('.sde\\') print "Layer Name: " + lyr.name try: print "Feature Class: " + fcName[1] except Exception as e: print "Feature Class: Unknown issue" print e else: print "DATASOURCE not supported on " + lyr.name continue ## a["MXD File Name"] = mxd_service_name ## a["Layer Name"] = lName ## a["Server"] = serverName[2] ## a["Database"] = dbName ## a["Feature Class"] = fcName[1] ## a["User Name"] = userName ## a["Definition Query"] = ldquery ## a["Workspace"] = workspace ## a["MXD Path"] = mxdpath ## layer.append(a) ## ## df_l = pd.DataFrame(layer) ## writer = pd.ExcelWriter (inXLS, engine='xlsxwriter') ## df_l.to_excel (writer, sheet_name='MXD Sources') ##writer.save() #write out to csv if not os.path.isfile(inCsv): csvFile = open(inCsv, 'wb') try: writer = csv.writer(csvFile) writer.writerow(('MXD', 'Layer Name', 'Server', 'Database', 'Feature Class', 'User Name', 'Definition Query', 'Workspace', 'MXD Path', 'Test 1', 'Test 2')) writer.writerow((mxd_service_name, lName, serverName[2], dbName, fcName[1], userName, ldquery, workspace, mxdpath, dq_layer, dq)) except: print "error writing first row of csv" else: csvFile = open(inCsv, 'ab') try: writer = csv.writer(csvFile) writer.writerow((mxd_service_name, lName, serverName[2], dbName, fcName[1], userName, ldquery, workspace, mxdpath, dq_layer, dq)) except Exception as e: print "error writing to csv" print e del mxd return #------------------------------------------------------------------------------- # # # MAIN SCRIPT # # #------------------------------------------------------------------------------- inXLS = inCsv + '\\' + fileprefix + 'MXDSources_' + str(datetime.datetime.now().date()) + '.xlsx' inCsv = inCsv + '\\' + fileprefix + 'MXDSources_' + str(datetime.datetime.now().date()) + '.csv' dirWalk()