How can I extract full sde path from .sde conneciton file for use in arcpy.CreateReplica_management ERROR 000582

2026
1
02-16-2017 11:15 AM
RebeccaStrauch__GISP
MVP Emeritus

I am trying to automate arcpy.CreateReplica_management and eventually the sync/updates.  This will be a one-way replaca to a fgdb, and all the necessary prerequisites are in place. Using Desktop 10.3.1 (eventually 10.5) and current using MS SQL ArcSDE 10.2.1 (still on SQL 2008), but will eventually move/upgrade that to 10.5 too.

When I ran the wizard in the "distributed geodatabase" toolbox, it worked by just using the .sde conneciton file (I wanted to replicate all feature classes), but I didn't get anything in the Results tab to grab a snippet.  Using the .sde connection file only in the Create Replica—Help | ArcGIS for Desktop tool, I would get the "arcgisscripting.ExecuteError: ERROR 000582" .

With suggestions and comments in https://community.esri.com/thread/52766#comment-263419   I reran the tool, dragging the individual feature classes in, vs.just the .sde connection file, and it ran successfully.  Looking at the snippet, I can see that it is wanted a list of the feature classes with the full sde path (including "owner").  something similar to

in_data="'Database Connections\me@master.sde\master.DBO.myDataset\sde_master.DBO.fc1';'Database Connections\me@master.sde\master.DBO.myDataset\sde_master.DBO.fc2';'Database Connections\me@master.sde\master.DBO.myDataset\sde_master.DBO.fc3'"

The "'Database Connections\me@master.sde" portion and the fc1, fc2, fc3, etc. I provide with input or derived arguments and I could also easily provide the "myDataset" info.

Q: How can I extract the bolded part....or at least the   DBO (or SDE or other user) portion?  It seems like I have done this in the past, since the path changes depending on the SDE or ArcGIS version, but I cannot find my code.

Other option is I can test to see if it exists with DBO, if not, test SDE, etc and then build it up, but I was wondering if there is an easier, more reliable way.  I have looked thru many geonet and stackexchange links, and most explain how to buld the .sde file, not how to break it down (or extract the path from sde.

Thanks for any suggestions or code. 

tagging Geodatabase

0 Kudos
1 Reply
RebeccaStrauch__GISP
MVP Emeritus

Summary of workaround

I didn't get any response to this (if there is a way to extract this data, still interested), but I'm going to post my workaround and close this thread. 

I went with looping thru ['DBO', 'SDE'] to see if it exists, and if it did, adding that to my FC list, an formatting it for input to the arcpy.CreateReplica_management command. If it is a username other than one of those, that would need additional code. 

I still need to check to make sure the replica name doesn't already exist, and do a few more tests.  This is a stripped down, more generic version of what I did....and is not tested as a standalone.  However, it may help others so sharing.

import time
import arcpy

# function to see if the masterGDB path is UNC or "local"
def isuncpath(a_path):  
     import os  
     return os.path.splitunc(a_path)[0] != ''

# shows messages for python window or tool
def myMsgs(message):
     arcpy.AddMessage(message)
     print(message)

# Set the necessary product code
arcpy.SetProduct("ArcInfo")

"""
Main function
"""
outputPath = r'd:\tempReplica3'
arcpy.env.workspace = outputPath
arcpy.env.overwriteOutput = False
if not arcpy.Exists(outputPath):
     myMsgs("Folder {0} doesn't exist. Creating...".format(outputPath))
     os.mkdir(outputPath)
else:
     myMsgs("Replica folder location: {0}".format(outputPath))

# local variables using input parameters
replicaFGDBbase = 'ReplDoNotTouch'
replicaFGDB = ("{0}.gdb".format(replicaFGDBbase)) #os.path.basename(updateFGDB)
replicaFGDBpath = os.path.join(outputPath, replicaFGDB)


# ########################################################### #
# this connection assumes the person logged in and running    #
#   this toolset is an Admin for the wcgis_master.sde         #
#   but at a minimum needs read access.                       #
#                                                             #
#   if not, will need to update to connection file that is.   #
#   THIS IS TO AVOID HAVING AN ADMIN PASSWORD hardcoded,      #
#     showing in the results tab, or being distrubuted.       #
#     If the user is logged in and an admin, it               #
#     should not prompt for a password.                       #
#                                                             #
#   If not on server and error pops up on rename, rerun.      #
#                                                             #
#   Also need to have r/w access to the outpath location      #
# #############################################################
scriptPath = sys.path[0]
addinPath = os.path.dirname(scriptPath)

# generic connection file that uses the logging in users dfg creds
masterGDB = os.path.join(addinPath, "conn", "__prod-me@master.sde")
if not arcpy.Exists(masterGDB):
     masterGDB = r'Database Connections\__prod-me@master.sde'
     if arcpy.Exists(masterGDB):
          myMsgs("The source database {0} exists: {1}".format(masterGDB, arcpy.Exists(masterGDB)))
     else:
          myMsgs("ERROR: The source database does not exist. Exiting script.")
          sys.exit()

if not arcpy.Exists(replicaFGDBpath):
     myMsgs("\nCreate NEW replica FGDB {0}...".format(replicaFGDBpath)) 
     arcpy.CreateFileGDB_management (outputPath, replicaFGDBbase)  
else:
     myMsgs("\n{0} exists, continue process...".format(replicaFGDBpath))
     #   may decide to exit here.....don't want to recreate

# if the path is a UNC, need to extract the DB.owner portion, for replica creation

masterGDBname = ("sde_themaster")
masterDataset = 'MasterDC'
checkOwner = ["SDE", "DBO"]  

if isuncpath(masterGDB):  
     lstSdeFDS = os.path.split(masterGDB)   
     lstDbOwnerFDS = lstSdeFDS[1].split(".")
     sdePath =  ('{0}\\{1}.{2}.'.format(masterGDB, lstDbOwnerFDS[0], lstDbOwnerFDS[1]))
     owner = lstDbOwnerFDS[0]
else:  
     sdePath = masterGDB + "\\"
     myMsgs("sdePath: {0}".format(sdePath))  
     for owner in checkOwner:
          tmpPath = ('{0}{1}.{2}.{3}'.format(sdePath, masterGDBname, owner, masterDataset))
          myMsgs("tmpPath: {0}".format(tmpPath))
          if arcpy.Exists(tmpPath):
               myMsgs("{0} is database owner".format(owner))
               datasetPath = ("{0}\\".format(tmpPath))
               break
          else:
               myMsgs("not owner, go to next")


myMsgs("Show me the paths:\n {0}\n {1}".format(sdePath, datasetPath))
preFCPath = ("{0}{1}.{2}".format(datasetPath, masterGDBname, owner))
myMsgs("\nShow me the FC pre-path:\n {0}".format(preFCPath))

masterFCs = ['FC1', 'FC2', 'FC3']

in_data_list = []
for fc in masterFCs:
     fcPath = ("'{0}.{1}'".format(preFCPath, fc))
     unquotedPath = (fcPath.strip("'"))
     #myMsgs("\nShow me the FC path:\n {0}".format(fcPath))

     #myMsgs("{0} exists with quotes {1}".format(fc, arcpy.Exists(fcPath)))
     #myMsgs("{0} exists without quotes {1}".format(fc, arcpy.Exists(fcPath.strip("'"))))
     itemExists = arcpy.Exists(unquotedPath)
     if itemExists:
          myMsgs("{0} exists".format(fc))  #(unquotedPath))
          in_data_list.append(unquotedPath)
     else:
          myMsgs("{0} Does not exist".format(fcPath))


in_data = (";".join(in_data_list))
myMsgs("\n    in_data = {0}".format(in_data))

# source .sde, output one-way fgdb replica name and location
replicaType = "ONE_WAY_REPLICA"
replicaOutGDB = replicaFGDBpath
replicaName = "NewReplicaName"
replicaAccessType = "FULL"  #  "SIMPLE" Full is pulling topology. might use SIMPLE instead
replicaSender = "PARENT_DATA_SENDER"
replicaRecords = "ALL_ROWS"


arcpy.CreateReplica_management(in_data, replicaType, replicaOutGDB, replicaName, replicaAccessType, replicaSender, replicaRecords)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍