In an effort to improve my Python skills, I am trying to build code to automate the Compress and Analyze commands for our SQL 2008 R2 SDE database (code below). The Compress code works as does the Analyze, save for one exception. The Analyze function (and the remaining Python code) quits if it encounters a feature class or table that is not registered.
Is there a way in Python to determine if a feature class or table is registered so that I can exempt\skip unregistered items? Failing that, what would be the best way to 'on failure, resume next' so my code doesn't just quit.
# Date: Jan, 2014
# Version: ArcGIS 10.0
# This script is designed to compress an SDE Geodatabase and then
# loop through and analyze the statistics ofeach feature dataset, feature class and table.
# Code is designed to log the entire process to a text file
# Created by Mike Long - ml56067@gmail.com - 7/16/09
# Edited by HGil - 1/2014
# Import system, Geoprocessing, time modules
import sys, string, os, arcgisscripting, time, smtplib
# Set the date.
Date = time.strftime("%m-%d-%Y", time.localtime())
# Set the time.
Time = time.strftime("%I:%M:%S %p", time.localtime())
# Create the Geoprocessor object
gp = arcgisscripting.create()
gp.CheckProduct("ArcEditor") #Checks the license level.
gp.SetProduct("arceditor") #Sets the license level.
print "Process started at " + str(Date) + " " + str(Time) + "." + "\n"
# Set up the log file.
LogFile = file('C:\\GIS\\DB_LogFiles\\SDE_GISPROD-' + Date + '.txt', 'w') #Creates a log file with current date.
output = open('C:\\GIS\\DB_LogFiles\\SDE_GISPROD-' + Date + '.txt', 'w') #Path to log file.
output.write(str("Process started at " + str(Date) + " " + str(Time) + "." + "\n")) #Write the start time to the log file.
# Load required toolboxes
gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")
#----------------------------
try:
# Compress the database
print "Begining Compress..." + "\n"
gp.toolbox = "management"
gp.compress("C:\\...<datasource path>...\\ArcCatalog\\TEST_PROD.sde")
print gp.GetMessages() + "\n"
output.write(gp.GetMessages()+ "\n")
except:
print gp.GetMessages() + "\n"
output.write(gp.GetMessages()+ "\n")
#----------------------------
analyzeworkspace = "C:\\...<datasource path>...\\Desktop10.0\\ArcCatalog\\TEST_PROD.sde"
try:
# Loop through and Analyze all feature datasets
gp.Workspace = analyzeworkspace
FCList = gp.ListFeatureClasses ("*", "all")
FC = FCList.Next()
while FC:
gp.Analyze_management(FC, "BUSINESS;FEATURE;ADDS;DELETES")
print gp.GetMessages() + "\n"
output.write(gp.GetMessages() + "\n")
FC = FCList.Next()
# Loop through and analyze all the Feature classes
gp.Workspace = analyzeworkspace
FDList = gp.ListDatasets ("*", "all")
FD = FDList.Next()
while FD:
gp.Analyze_management(FD, "BUSINESS;FEATURE;ADDS;DELETES")
print gp.GetMessages() + "\n"
output.write(gp.GetMessages() + "\n")
FD = FDList.Next()
# Loop through and analyze all the Tables.
# The if-else statements set the script to skip over Multi-Versioned views.
TBList = gp.ListTables ("*", "all")
TB = TBList.Next()
while TB:
if '_MV' in TB:
print "Skipping Multi-Versioned View"
output.write("Skipping Multi-Versioned View " + TB + "\n")
else:
gp.Analyze_management(TB, "BUSINESS;FEATURE;ADDS;DELETES")
print gp.GetMessages() + "\n"
output.write(gp.GetMessages() + "\n")
TB = TBList.Next()
except:
print gp.GetMessages()
output.write(gp.GetMessages())
#----------------------------
# Sets the Date & Time since the script started.
Date = time.strftime("%m-%d-%Y", time.localtime())# Set the date.
Time = time.strftime("%I:%M:%S %p", time.localtime()) # Set the time.
output.write(str("Process completed at " + str(Date) + " " + str(Time) + "." + "\n")) # Write the start time to the log file.
output.close() # Closes the log file.
print "Process completed at " + str(Date) + " " + str(Time) + "."