Select to view content in your preferred language

Reconcile and Post script for ArcGIS10 / ArcGIS server 10

3863
7
03-16-2011 02:50 PM
moeodsi
Emerging Contributor
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
Tags (2)
0 Kudos
7 Replies
moeodsi
Emerging Contributor
More Details:

To use this script, setup the variables for your SDE connection and make sure parent/default names are entered correctly.

This script runs through the version names twice, the first run is a Reconcile-Post, the second run (if no conflicts detected) is a Reconcile-Only.  The reason for the Reconcile-Only is it ensures that all versions can see each other's edits.

This script prompts for user input.  Hit enter to process all versions,  type list to see a "list" of all version names, type a version name to Rec/Post only one version or type "end" to stop the script.
0 Kudos
LISWaukesha_Co
Occasional Contributor
Thanks Moe this works great!
0 Kudos
JasonScheirer
Esri Alum
HIGH FIVE!
0 Kudos
JoseGonzalez
Esri Contributor
works great!... thank you Moe!
0 Kudos
LukeBehling
Frequent Contributor
Thank you Moe - very good!
0 Kudos
JoshuaThompson2
Deactivated User
Hi Moe,

I reviewed your reconcile-post python script and would love to use it for my workflow, but unfortunately I do not know much about python scripting.  I was wondering if you could help me sometime get the script to run properly.  I have tried plugging in my information into the script template, but kept getting syntax errors.  I have three versions in my SDE geodatabase which include:  Maintenance, QC, and DEFAULT.  My typical workflow is to first reconcile �??Maintenance�?� (child) and post to �??QC�?� (parent).  The second step is to reconcile �??QC�?� and post to �??DEFAULT�?�.   Thanks.

Josh
0 Kudos
moeodsi
Emerging Contributor
Hi Moe,

I reviewed your reconcile-post python script and would love to use it for my workflow, but unfortunately I do not know much about python scripting.  I was wondering if you could help me sometime get the script to run properly.  I have tried plugging in my information into the script template, but kept getting syntax errors.  I have three versions in my SDE geodatabase which include:  Maintenance, QC, and DEFAULT.  My typical workflow is to first reconcile �??Maintenance�?� (child) and post to �??QC�?� (parent).  The second step is to reconcile �??QC�?� and post to �??DEFAULT�?�.   Thanks.

Josh


Be sure to enter in "QC" for parent instead of "parent".  You do not have to type in the children's name as the script automatically grabs those from your SDE DB.  If you're still having issues, you can provide some of the syntax errors that you get and hopefully we can assist you a little bit better.   Good luck.

Moe
0 Kudos