moecpgis

Reconcile and Post script for ArcGIS10 / ArcGIS server 10

Discussion created by moecpgis on Mar 16, 2011
Latest reply on Sep 12, 2012 by moecpgis
Hi all,  my first post on here thought i'd share my Reconcile and Post script that I made in python for my organization.  This script is designed to fully Rec/Post all of the versions that edit multiple datasets in SDE to "parent" or directly to "Default".

The way we have it setup is we run this script to Rec/Post child versions to "parent" then if all goes well, we run another script that does "parent" to "default"

This works wonders along with the GDBT tool to visually see the effect of this script.

## Reconcile and Post Script v5.2 Created Feb 16, 2011. By Mo E @ CPGIS of University of Memphis
## This script will automatically create an SDE connection file.
## By default, the SDE connection file will go into C:\temp
## This Script can:
##  - Reconcile and Post all versions OR just one version.
##  - Populate a list of all version names.



# Set the necessary product code & import modules
import arcinfo, arcpy, os



print "\nPost CHILDREN to PARENT Script"

## general set-up variables

try:
    ## SDE connection settings
    folderName = r"C:\\temp"        ## output path to SDE connection file
    fileName = "Py_Def_test.sde"         ## SDE file name (something.sde)
    serverName = "your.sde.server"  ## server address
    serviceName = "0000"            ## service number
    databaseName = ""               ## optional
    authType = "DATABASE_AUTH"
    username = "user"
    password = "pass"
    saveUserInfo = "SAVE_USERNAME"
    saveVersionInfo = "SAVE_VERSION"
    

    DefaultV = "SDE.DEFAULT"
    parent = "parent"  
    
    ## check if connection to SDE exists
    con_check = os.path.isfile(folderName + '\\' + fileName)
    if con_check is False:
        
    ## Create SDE connection file in C:temp
        arcpy.CreateArcSDEConnectionFile_management (folderName, fileName, serverName, serviceName, databaseName, authType, username, password, saveUserInfo, DefaultV, saveVersionInfo)
        print ">First time run, SDE File Created<\n"

except Exception as e:
    ## show error
    print "Cannot connect to SDE for the following Reason(s):"
    print e.message  ## error code

## Set workspace
arcpy.env.workspace = folderName + '\\' + fileName
SDE_DEFAULT = arcpy.env.workspace

print ("Connected to: %s\n" % fileName)

## Get list of versions and remove Non-Applicaple ones from the list.  We only need child versions.
try:
    versionList = arcpy.ListVersions(SDE_DEFAULT)
    versionList.remove (DefaultV)
    versionList.remove (parent)
except Exception as e:
    print "WARNING!"
    print "Error: " + e.message
    print "Check your parent/default names in the set-up!!"
    print ""
    

## Exit operation
def exit():
    try:
        raise SystemExit()
    except:
        print "RP Script terminated by user"

        
## Reconcile-Post operation
def Post(version):
    arcpy.ReconcileVersion_management(SDE_DEFAULT, version, parent, "BY_OBJECT", "FAVOR_TARGET_VERSION", "NO_LOCK_AQUIRED", "ABORT_CONFLICTS", "POST")
    return version

## Reconcile-Only operation
def Rec(version):
    arcpy.ReconcileVersion_management(SDE_DEFAULT, version, parent, "BY_OBJECT", "FAVOR_TARGET_VERSION", "LOCK_AQUIRED", "ABORT_CONFLICTS", "NO_POST")
    return version

## counters
count = 0
E = 0
## define user input function and script execution algorithem
def user_command():
    ## Take user input
    print "Enter 'version' name below,\n Type 'list' to see all versions in SDE,\n  Type 'end' to Exit"
    input = raw_input('   OR Press Enter to process all:> ')
    print ""
    input_version = input.strip()
    if input == "":
        for version in versionList:

            
## Begin Post ALL ##
            try:
                    print "Posting: " + version
                    Post(version)
                    print "     Done."
                    global count
                    count +=1
            ## Handle Conflict Error
            except Exception as e:
                    print "   Error.   A conflict detected under the version " + version
                    global E
                    E +=1
                    # print e.message + "\n"   ## uncomment this line to get error Codes ##
                    # arcpy.AddError(e.message)  ## this is an optional line that could be used in the future ##
## End Post ALL ##

        if E > 0:
            print ""
            print count , " Items processed successfully."
            print E , " Conflict(s) occured.\n"
            print "Did not complete 'Post' operation, Please resolve conflict(s) and re-run script "
        else:
            if E == 0:
                print ""
                print count , " Items processed successfully."
                print E , " Conflict(s) occured.\n"
                ## re-count
                count = 0
                E = 0
                ## Rec versions if no conflicts detected
                print "Now posting...\n"
                for version in versionList:
                    
## Begin Rec ALL ##
                            try:
                                    print "Reconciling: " + version
                                    Rec(version)
                                    print "     Done."
                                    count +=1
                            ## Handle Conflict Error
                            except Exception as e:
                                    print "   Error.   A conflict detected under the version " + version
                                    E +=1
                                    ## print e.message + "\n"   ## uncomment this line to get the actual error Codes ##
                                    
                print ""
                print count , " Items processed successfully."
                print E , " Conflict(s) occured.\n"
                if E == 0:
                    print "Reconcile and Post for All versions completed successfully."
                else:
                    print "Reconcile and Post for All versions completed with conflicts(s)"
## End Rec ALL ##

    else:
            b = 0
            ## List versions command 
            if input == "list":
                    print "\nHere is a list of child versions in SDE: \n"
                    for version in versionList:
                            b = (b+1)
                            print b, "-" , version
                    print ""
                    ## Re-prompt input from user
                    user_command()
            else:


## Post and Rec a specific version name
                    for version in versionList:
                            if input_version == version:
                                    try:
                                        ## Post single version
                                            print "\n  Posting version " + version
                                            Post(version)
                                            print "     Done."
                                            count +=1
                                    except Exception as e:
                                            print "   Error.\n   A conflict detected under the version " + version + "\n"
                                            E +=1

                                    if E > 0:
                                        print ""
                                        print count , " Items processed successfully."
                                        print E , " Conflict(s) occured.\n"
                                        print "Did not complete 'Post' operation, Please resolve conflict(s) and re-run script "
                                    #else:
                                    if E == 0:
                                        print ""
                                        print count , " Items processed successfully."
                                        print E , " Conflict(s) occured.\n"
                                        ## re-count
                                        count = 0
                                        E = 0
                                        ## Reconcile version if no conflicts detected
                                        try:
                                            print "\n  Reconciling version " + version
                                            Rec(version)
                                            print "     Done."
                                            print "\nReconcile and Post for " + version + " completed successfully."
                                            count +=1
                                        except Exception as e:
                                            print "   Error.\n   A conflict detected under the version " + version + "\n"
                                            E +=1
                                    break
                    else:
                        if input == "end":
                            exit()
                        else:
                            ## Unknown command, assumed to be a mis-spelled version name?
                            print "HIGH FIVE!"
                            print "\nThe version " + input_version + " does not exist!\n"
                            ## Re-prompt input from user
                            user_command()
 
## Initial Execution, prompt first input from user:
user_command()

## End of Script

## Created by Mo E
## Senior GIS Tech at CPGIS of University of Memphis

Outcomes