I am trying to use the Try Except statement and I was wondering if I set it up correctly because I get an error when I run the script. I'm not sure if the error is due to the try Except or bad scripting on my part.
The script checks for a connection to NEO_1. If the connection exists then it checks all the versions name. This is where I seem to get an error. Since the user already exists the break should stop the script before prompting an error?
Empty Workspace Cache
Done
Connections Exists: NEO_1
Searching for User Credentials..
Windows Credentials: EGAS\jnmiller
Checking SDE for existing Credentials..
Executing: CreateVersion "Database Connections\NEO_1.sde" sde.DEFAULT jnmiller PROTECTED
Start Time: Wed Feb 21 11:38:41 2018
ERROR 001148: Cannot create a version with the specified name.
Version already exists [jnmiller][STATE_ID = 99689]
Failed to execute (CreateVersion).
Failed at Wed Feb 21 11:38:42 2018 (Elapsed Time: 0.72 seconds)
# Name: SDE_CreateVersion
# Description: Creates a new SDE version
import arcpy, time, smtplib, os
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "Database Connections/NEO_1.sde"
connection = arcpy.env.workspace
# Set local variables
sdeConnection = "Database Connections\NEO_1.sde"
parentVersion = "sde.DEFAULT"
versionName = os.environ.get( "USERNAME" )
fullPath = os.path.join("Windows Credentials: EGAS", versionName)
userVersion = os.path.join("Version Currently Exists: ",versionName)
createVersion = os.path.join("Version Created: ",versionName)
# Clear Workspace Cache
print("Empty Workspace Cache")
arcpy.ClearWorkspaceCache_management()
print("Done")
# Database Connection: NEO_1 = True
if arcpy.Exists(connection):
versionFound = False
try:
for version in arcpy.da.ListVersions(sdeConnection):
if version.name.split(".")[0] == versionName:
print(userVersion)
versionFound = True
break
# Execute CreateVersion
if not versionFound:
arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
print(createVersion)
except arcpy.ExecuteError:
print(arcpy.GetMessages())
# Database Connection: NEO_1 = False
if not arcpy.Exists(connection):
print("Creating Database Connection: NEO_1")
arcpy.CreateDatabaseConnection_management(out_folder_path="Database Connections",
out_name="NEO_1.sde",
database_platform="SQL_SERVER",
instance="lan-svr-sql",
account_authentication="OPERATING_SYSTEM_AUTH",
database="NEO_1",
version_type="TRANSACTIONAL",
version="sde.DEFAULT")
arcpy.env.workspace = r"Database Connections\NEO_1.sde"
if arcpy.Exists(connection):
versionFound = False
try:
for version in arcpy.da.ListVersions(sdeConnection):
if version.name.split(".")[0] == versionName:
print(existingVersion)
versionFound = True
break
#Execute CreateVersion
if not versionFound:
arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
print(createVersion)
except arcpy.ExecuteError:
print(arcpy.GetMessages())
I think you need to dedent the code block starting at line 72. For every version in the ListVersions, the second if statement will get executed within the for loop. And, since you're already specifying that versionFound = False (64), the CreateVersion will try to execute.
try:
for version in arcpy.da.ListVersions(sdeConnection):
if version.name.split(".")[0] == versionName:
print(existingVersion)
versionFound = True
break
#Execute CreateVersion
if not versionFound:
arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
print(createVersion)
except arcpy.ExecuteError:
print(arcpy.GetMessages())
Is there a reason why the CreateVersion_management keeps running if it's outside the for loop? Inside the for loop if the users version exists it's supposed to skip. And since the version exist it's supposed to set versionFound = True. Once versionFound = True the CreateVersion_management shouldn't run.
ERROR 001148: Cannot create a version with the specified name.
Version already exists [jnmiller][STATE_ID = 99940]
Failed to execute (CreateVersion).
Failed at Wed Feb 21 12:36:53 2018 (Elapsed Time: 0.68 seconds)
try:
for version in arcpy.da.ListVersions(sdeConnection):
if version.name.split(".")[0] == versionName:
print(userVersion)
versionFound = True
break
# Execute CreateVersion
if not versionFound:
arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
print(createVersion)
except arcpy.ExecuteError:
print(arcpy.GetMessages())
I might be missing something, but in your full script in the OP I don't see where versionName has been assigned a value. I don't see where userVersion has been assigned a value either.
The versionName variable is in line 15. It's supposed to be getting the users windows credentials. Sorry for the confusion with the userVersion variable. I changed the name with existingVersion on my script and I didn't think to change it before replying. I'll update the first post.
I would try printing each version name while looping over them, just to make sure all the expected versions are getting iterated over.
print "Target: {0}".format(versionName)
for version in arcpy.da.ListVersions(sdeConnection):
print "Found - {0}".format(version.name.split(".")[0])
if version.name.split(".")[0] == versionName:
print(userVersion)
versionFound = True
break
Here's the results:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================ RESTART: C:\Users\jnmiller\Desktop\test 3.py ================
Empty Workspace Cache
Done
Target: jnmiller
Found - "EGAS\JMEESE"
Found - "EGAS\SCRAWFORD"
Found - "EGAS\MWETZEL"
Found - DBO
Found - "EGAS\CCOON"
Found - sde
Executing: CreateVersion "Database Connections\NEO_1.sde" sde.DEFAULT jnmiller PROTECTED
Start Time: Wed Feb 21 13:36:42 2018
ERROR 001148: Cannot create a version with the specified name.
Version already exists [jnmiller][STATE_ID = 100173]
Failed to execute (CreateVersion).
Failed at Wed Feb 21 13:36:43 2018 (Elapsed Time: 0.68 seconds)
>>>
So it looks like 'version.name.split(".")[0]` is returning the [Owner] name and not the [Name]. That's strange... What happens it you tried printing the entire version.name?
print "Target: {0}".format(versionName)
for version in arcpy.da.ListVersions(sdeConnection):
print "Found - {0}".format(version.name)
## if version.name.split(".")[0] == versionName:
## print(userVersion)
## versionFound = True
## break
When I hash out those line it seems to create EGAS\JNMILLER when I run the script the first time. If I run the script again it generates the same error.
First run:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================ RESTART: C:\Users\jnmiller\Desktop\test 3.py ================
Empty Workspace Cache
Done
Target: jnmiller
Found - "EGAS\JMEESE"
Found - "EGAS\SCRAWFORD"
Found - "EGAS\MWETZEL"
Found - sde
Found - DBO
Found - "EGAS\CCOON"
Version Created: \jnmiller
>>>
Second run:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================ RESTART: C:\Users\jnmiller\Desktop\test 3.py ================
Empty Workspace Cache
Done
Target: jnmiller
Found - "EGAS\JMEESE"
Found - "EGAS\SCRAWFORD"
Found - "EGAS\MWETZEL"
Found - sde
Found - "EGAS\JNMILLER"
Found - DBO
Found - "EGAS\CCOON"
Executing: CreateVersion "Database Connections\NEO_1.sde" sde.DEFAULT jnmiller PROTECTED
Start Time: Wed Feb 21 14:12:56 2018
ERROR 001148: Cannot create a version with the specified name.
Version already exists [jnmiller][STATE_ID = 100173]
Failed to execute (CreateVersion).
Failed at Wed Feb 21 14:12:57 2018 (Elapsed Time: 0.80 seconds)
>>>