Select to view content in your preferred language

Attaching a python script in a custom tool to live in a custom toolbox,

3166
4
01-27-2013 09:23 AM
JamalNUMAN
Legendary Contributor
Attaching a python script in a custom tool to live in a custom toolbox,

I couldn�??t manage how the script below (http://resources.arcgis.com/en/help/main/10.1/index.html#//003n000000v7000000) can be attached in a custom tool in a custom toolbox such that the end user just needs to enter the connection of the database to run the entire script.

-----------------
import arcpy, time, smtplib

# set the workspace
arcpy.env.workspace = 'Database Connections/admin.sde'

# set a variable for the workspace
workspace = arcpy.env.workspace

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/admin.sde")

# get a list of usernames of users currently connected and make email addresses
emailList = [u.Name + "@yourcompany.com" for user in arcpy.ListUsers("Database Connections/admin.sde")]

# take the email list and use it to send an email to connected users.
SERVER = "mailserver.yourcompany.com"
FROM = "SDE Admin <python@yourcompany.com>"
TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/admin.sde', False)

# wait 15 minutes
time.sleep(900)

#disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/admin.sde', "ALL")

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/admin.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")

# Run the compress tool.
arcpy.Compress_management('Database Connections/admin.sde')

#Allow the database to begin accepting connections again
arcpy.AcceptConnections('Database Connections/admin.sde', True)

#Get a list of datasets owned by the admin user

# Get the user name for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(arcpy.env.workspace).connectionProperties.user

# Get a list of all the datasets the user has access to.
# First, get all the stand alone tables, feature classes and rasters.
dataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')

# Next, for feature datasets get all of the featureclasses
# from the list and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
    dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

# pass in the list of datasets owned by the admin to the rebuild indexes and analyze datasets tools
# Note: to use the "SYSTEM" option the user must be an administrator.
arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")

----------------

How such custom tool can be developed to live in a toolbox?


Thank you

Best

Jamal
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
Tags (2)
4 Replies
T__WayneWhitley
Frequent Contributor
Probably what you want is this:

Scheduling a Python script to run at prescribed times
Desktop » Geoprocessing » Executing tools
http://resources.arcgis.com/en/help/main/10.1/index.html#//002100000038000000


It probably doesn't make sense, not practical sense, to make this a script tool - unless you mean you only want a part of this as a tool.  At the very beginning of the webhelp documentation, the purpose of the design of this script is posted:

"This topic discusses the process an administrator might go through to run a scheduled nightly reconciliation of versions."


At any rate, this is a rather long and intensive administrator task that:

a)  You wouldn't likely want users' to launch, particularly during the day (which would effectively take the database offline for a lengthy period of time.

b)  You would very likely need to monitor the log and need time as an administrator to fix any hiccups that are bound to occur.

c)  There are so many input parameters in this script, users would think they are filling out a job application....just setting it up for the run, unless you set it up in some kind of default mode, will be a headache for most any user...doubtful an administrator would even use it.

Hope that helps, and by the way - I have written overnight scheduled tasks before and they work very well.  This one you have chosen to tackle is likely the most complexly powerful one I have ever seen....and it is critical to get it right, particularly if you have many users.

Good luck!  It is very interesting though.

-Wayne
0 Kudos
JamalNUMAN
Legendary Contributor
Probably what you want is this:

Scheduling a Python script to run at prescribed times
Desktop » Geoprocessing » Executing tools
http://resources.arcgis.com/en/help/main/10.1/index.html#//002100000038000000


It probably doesn't make sense, not practical sense, to make this a script tool - unless you mean you only want a part of this as a tool.  At the very beginning of the webhelp documentation, the purpose of the design of this script is posted:

"This topic discusses the process an administrator might go through to run a scheduled nightly reconciliation of versions."


At any rate, this is a rather long and intensive administrator task that:

a)  You wouldn't likely want users' to launch, particularly during the day (which would effectively take the database offline for a lengthy period of time.

b)  You would very likely need to monitor the log and need time as an administrator to fix any hiccups that are bound to occur.

c)  There are so many input parameters in this script, users would think they are filling out a job application....just setting it up for the run, unless you set it up in some kind of default mode, will be a headache for most any user...doubtful an administrator would even use it.

Hope that helps, and by the way - I have written overnight scheduled tasks before and they work very well.  This one you have chosen to tackle is likely the most complexly powerful one I have ever seen....and it is critical to get it right, particularly if you have many users.

Good luck!  It is very interesting though.

-Wayne


Many thanks Wayne for the time and effort and the distinct help,

I do totally agree with you. You have perfectly described the best workflow for the script.


My aim from this tool is just to test the behavior of the script by SIMPLE TOOL (accommodated in a toolbox).

Attached is a script tool developed for the same purpose. But it doesn�??t do the �??disconnect users�?� part of the work. All what the user needs to do is to provide the data connection and then the script will run smoothly.

[ATTACH=CONFIG]21114[/ATTACH], [ATTACH=CONFIG]21115[/ATTACH], [ATTACH=CONFIG]21116[/ATTACH]


Best

Jamal
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
CarlSunderman
Occasional Contributor
I don't see in your script where you are disconnecting the users

arcpy.DisconnectUser("Database Connections/admin@sde.sde", "ALL")
0 Kudos
JamalNUMAN
Legendary Contributor
I don't see in your script where you are disconnecting the users

arcpy.DisconnectUser("Database Connections/admin@sde.sde", "ALL")


Many thanks Carl for the answer. Sorry for the confusion

1. Below is the code that I need to have a SCRIPT TOOL (in a toolbox) for (As I don�??t have sufficient skills to do so):

-------------------------------------------------------------------------
import arcpy, time, smtplib

# set the workspace
arcpy.env.workspace = 'Database Connections/admin.sde'

# set a variable for the workspace
workspace = arcpy.env.workspace

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/admin.sde")

# get a list of usernames of users currently connected and make email addresses
emailList = [u.Name + "@yourcompany.com" for user in arcpy.ListUsers("Database Connections/admin.sde")]

# take the email list and use it to send an email to connected users.
SERVER = "mailserver.yourcompany.com"
FROM = "SDE Admin <python@yourcompany.com>"
TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/admin.sde', False)

# wait 15 minutes
time.sleep(900)

#disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/admin.sde', "ALL")

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/admin.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")

# Run the compress tool.
arcpy.Compress_management('Database Connections/admin.sde')

#Allow the database to begin accepting connections again
arcpy.AcceptConnections('Database Connections/admin.sde', True)

#Get a list of datasets owned by the admin user

# Get the user name for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(arcpy.env.workspace).connectionProperties.user

# Get a list of all the datasets the user has access to.
# First, get all the stand alone tables, feature classes and rasters.
dataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')

# Next, for feature datasets get all of the featureclasses
# from the list and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

# pass in the list of datasets owned by the admin to the rebuild indexes and analyze datasets tools
# Note: to use the "SYSTEM" option the user must be an administrator.
arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
--------------------------------------

2. Here is the code that has already a script tool (attached to my previous post)

---------------------------
# Name: GDBMaintenance.py
# Description: Rebuilds indexes and statistics
# on geodatabase feature classes
# using an existing .sde file.
# Author: Esri
# Import system modules
import arcpy, os
import sys, traceback
  
# set the workspace environment using the data owner account  D:\Practical2_Editing\Data\Sa_A.sde
path=arcpy.GetParameterAsText(0)
arcpy.env.workspace = path
                     ###########################################################

#Gather all the FeatureClasses and Tables from the Workspace
datasets = arcpy.ListTables() + arcpy.ListFeatureClasses()

#Save geodatabase path to use for concatonating feature dataset names
path = arcpy.env.workspace

#Determine if any were found
for fd in arcpy.arcpy.ListDatasets("","Feature"):
    arcpy.env.workspace = path + "\\" + fd
    #Gather the FeatureClasses from the Current Dataset
    datasets += arcpy.ListFeatureClasses()
arcpy.env.workspace = path
#Rebuild and analyze all datasets
arcpy.RebuildIndexes_management(arcpy.env.workspace,"NO_SYSTEM",datasets,"ALL")
print arcpy.GetMessages()

arcpy.AnalyzeDatasets_management(arcpy.env.workspace,"NO_SYSTEM",datasets,"ANALYZE_BASE","ANALYZE_DELTA","ANALYZE_ARCHIVE")
print arcpy.GetMessages()

#Rebuild and analyze all system tables and compress the Geodatabase
# set the workspace environment using the Geodatabase administrative account
arcpy.env.workspace = path
                      ######################################################

arcpy.RebuildIndexes_management(arcpy.env.workspace,"SYSTEM","","ALL")
print arcpy.GetMessages()
arcpy.AnalyzeDatasets_management(arcpy.env.workspace,"SYSTEM","","ANALYZE_BASE","ANALYZE_DELTA","ANALYZE_ARCHIVE")
print arcpy.GetMessages()
arcpy.Compress_management(arcpy.env.workspace)
print arcpy.GetMessages()

print "Processing complete"

-------------------------------

Best

Jamal
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos