I created a python script to reconcile all edits on the users versions, then post the reconciled version to a QAQC and then to the sde or dbo default version. After this process the script will proceed to compress analyze and rebuild indexes for the tables in the sde database. This script will be scheduled on windows scheduler to run silently at 2:00am on Thursday and Saturday morning when users are offline.
If there should be any error encountered, the script is to print the errors and also generate a log file containing the errors and store the logfile with date and time in the specified drive location. Since the script was scheduled to run behind scene on windows scheduler, it was difficult for me to see errors when it happens. This was a big issue because users edits weren't reflected on our default database after sometime due to the error encountered by the script, his actually necessitated the need for a functional logfile that will enable me identify the errors.
The initial problem I had was that the script executed very well but could only print the errors on the screen but it generates an empty logfile.
This was corrected by putting in 3 lines at the end of the script to get it to write the printed errors intothe logfile, save it with time and date.
The Initial Script - executes but generates an empty log file
<SPAN class="comment token"># This script will reconcile and post all edits made in child versions of Natural Resources Geodatabase to QA\QC and then to SDE default database,it will go further to compress the database, analyze and rebuild the indexes on the database tables.</SPAN>
<SPAN class="comment token"># Script developed by Irene F. Egbulefu - GIS Analyst ( TNR Dept)</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">import</SPAN> traceback
<SPAN class="comment token"># Database Connection</SPAN>
editDB <SPAN class="operator token">=</SPAN><SPAN class="string token">"Database Connections\\Frogmouth_Natural_ResourcesTC.sde"</SPAN>
<SPAN class="comment token"># Current Day</SPAN>
Day <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%m-%d-%Y"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Current Time</SPAN>
Time <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%I:%M:%S %p"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set workspace</SPAN>
workspace <SPAN class="operator token">=</SPAN> editDB
<SPAN class="comment token"># Set the workspace environment</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> workspace
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Start Time</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Process Started at '</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Day<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Time<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># block new connections to the PublicWorks Geodatabase on Frogmouth Server.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Blocking Connections..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AcceptConnections<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># disconnect all users from the PublicWorks Geodatabase on Frogmouth Server.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Disconnecting Users..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>DisconnectUser<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get a list of all child versions besides QAQC and DEFAULT to pass into the ReconcileVersions tool.</SPAN>
ver1List <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> ver1 <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>ListVersions<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">!=</SPAN> <SPAN class="string token">'TC_USER.QA/QC'</SPAN> <SPAN class="operator token">and</SPAN> ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">!=</SPAN> <SPAN class="string token">'sde.DEFAULT'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Execute the ReconcileVersions tool with QAQC Target Version and do not delete child versions</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Reconcile/post versions to QAQC...."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ReconcileVersions_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL_VERSIONS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TC_USER.QA/QC"</SPAN><SPAN class="punctuation token">,</SPAN> ver1List<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LOCK_ACQUIRED"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ABORT_CONFLICTS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BY_OBJECT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FAVOR_TARGET_VERSION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"KEEP_VERSION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Extract QAQC version from the list of versions to pass to ReconcileVersions tool.</SPAN>
ver2List <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ver2<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> ver2 <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>ListVersions<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> ver2<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'TC_USER.QA/QC'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Execute the ReconcileVersions tool with DEFAULT Target Version and do not delete QAQC version</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Reconcile/post QAQC to DEFAULT..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ReconcileVersions_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL_VERSIONS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"sde.DEFAULT"</SPAN><SPAN class="punctuation token">,</SPAN> ver2List<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LOCK_ACQUIRED"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ABORT_CONFLICTS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BY_OBJECT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FAVOR_TARGET_VERSION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"KEEP_VERSION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Run the compress tool.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Compressing database..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Compress_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># /////////////////////////////////// ANALYZE DATASETS AND CALC STATISTICS /////////////////////////////////////</SPAN>
<SPAN class="comment token"># NOTE: Rebuild indexes can accept a Python list of datasets.</SPAN>
<SPAN class="comment token"># Get a list of all the datasets the user has access to.</SPAN>
<SPAN class="comment token"># First, get all the stand alone tables, feature classes and rasters.</SPAN>
dataList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListTables<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Next, for feature datasets get all of the datasets and featureclasses</SPAN>
<SPAN class="comment token"># from the list and add them to the master list.</SPAN>
<SPAN class="keyword token">for</SPAN> dataset <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Feature"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> dataset<SPAN class="punctuation token">)</SPAN>
dataList <SPAN class="operator token">+=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># reset the workspace</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> workspace
<SPAN class="comment token"># Concatenate all datasets into a list</SPAN>
datasetList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ds <SPAN class="keyword token">for</SPAN> ds <SPAN class="keyword token">in</SPAN> dataList<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"rebuilding indexes"</SPAN>
<SPAN class="comment token"># Execute rebuild indexes</SPAN>
<SPAN class="comment token"># Note: to use the "SYSTEM" option the workspace user must be an administrator.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RebuildIndexes_management<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_SYSTEM"</SPAN><SPAN class="punctuation token">,</SPAN> datasetList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Rebuild Complete'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"analyzing datasets"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AnalyzeDatasets_management<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_SYSTEM"</SPAN><SPAN class="punctuation token">,</SPAN> datasetList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_BASE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_DELTA"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_ARCHIVE"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"analysis complete"</SPAN>
<SPAN class="comment token">#Allow the database to begin accepting connections again</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Set databases to allow connections..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AcceptConnections<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ script initiation, Rec/Post process error handling \\\\\\\\\\\\\\\\\\\\\\\\\\\\\</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'An error occured'</SPAN>
failMsg <SPAN class="operator token">=</SPAN> <SPAN class="string token">'\nSCRIPT FAILURE IN SCRIPT INITIATION OR RECONCILE-POST PROCESS, \n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'Most recent GP messages below.\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN><SPAN class="string token">'\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'\nTraceback messages below.\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> traceback<SPAN class="punctuation token">.</SPAN>format_exc<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>splitlines<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> failMsg
<SPAN class="comment token"># \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ write error log info\\\\\\\\\\\\\\\\\\\\\\\\\\\\\</SPAN>
<SPAN class="comment token"># move to working directory </SPAN>
os<SPAN class="punctuation token">.</SPAN>chdir <SPAN class="punctuation token">(</SPAN>u<SPAN class="string token">'Y:\\TOOLS\\Logs\\'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## define function with variable filename and the format of the timestamp</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">timeStamped</SPAN><SPAN class="punctuation token">(</SPAN>filename<SPAN class="punctuation token">,</SPAN> fmt<SPAN class="operator token">=</SPAN><SPAN class="string token">'%m-%d-%y-%H.%M.%S-{filename}'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN>fmt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>filename<SPAN class="operator token">=</SPAN>filename<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## assign local variable filename and use whatever file name and extension you need</SPAN>
filename <SPAN class="operator token">=</SPAN> timeStamped<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Natural_ResourcesTC_toQC_Log.txt'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## Create the logfile and assign write permission</SPAN>
Open<SPAN class="punctuation token">(</SPAN>filename<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"w"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Final corrected script - executes and generates logfile, prints errors on the screen and also writes error information into the logfile it
<SPAN class="comment token"># This script will reconcile and post all edits made in child versions of Natural Resources Geodatabase to QA\QC and then to SDE default database,it will go further to compress the database, analyze and rebuild the indexes on the database tables</SPAN>
<SPAN class="comment token"># Script developed by Irene F. Egbulefu - GIS Analyst ( TNR Dept)</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">import</SPAN> traceback
<SPAN class="comment token"># Database Connection</SPAN>
editDB <SPAN class="operator token">=</SPAN><SPAN class="string token">"Database Connections\\Frogmouth_Natural_ResourcesTC.sde"</SPAN>
<SPAN class="comment token"># Current Day</SPAN>
Day <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%m-%d-%Y"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Current Time</SPAN>
Time <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%I:%M:%S %p"</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set workspace</SPAN>
workspace <SPAN class="operator token">=</SPAN> editDB
<SPAN class="comment token"># Set the workspace environment</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> workspace
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Start Time</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Process Started at '</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Day<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>Time<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># block new connections to the PublicWorks Geodatabase on Frogmouth Server.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Blocking Connections..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AcceptConnections<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># disconnect all users from the PublicWorks Geodatabase on Frogmouth Server.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Disconnecting Users..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>DisconnectUser<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get a list of all child versions besides QAQC and DEFAULT to pass into the ReconcileVersions tool.</SPAN>
ver1List <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> ver1 <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>ListVersions<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">!=</SPAN> <SPAN class="string token">'TC_USER.QA/QC'</SPAN> <SPAN class="operator token">and</SPAN> ver1<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">!=</SPAN> <SPAN class="string token">'sde.DEFAULT'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Execute the ReconcileVersions tool with QAQC Target Version and do not delete child versions</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Reconcile/post versions to QAQC...."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ReconcileVersions_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL_VERSIONS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TC_USER.QA/QC"</SPAN><SPAN class="punctuation token">,</SPAN> ver1List<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LOCK_ACQUIRED"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ABORT_CONFLICTS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BY_OBJECT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FAVOR_TARGET_VERSION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"KEEP_VERSION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Extract QAQC version from the list of versions to pass to ReconcileVersions tool.</SPAN>
ver2List <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ver2<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> ver2 <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>ListVersions<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> ver2<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'TC_USER.QA/QC'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Execute the ReconcileVersions tool with DEFAULT Target Version and do not delete QAQC version</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Reconcile/post QAQC to DEFAULT..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ReconcileVersions_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL_VERSIONS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"sde.DEFAULT"</SPAN><SPAN class="punctuation token">,</SPAN> ver2List<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LOCK_ACQUIRED"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ABORT_CONFLICTS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BY_OBJECT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FAVOR_TARGET_VERSION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"KEEP_VERSION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Run the compress tool.</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Compressing database..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Compress_management<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># /////////////////////////////////// ANALYZE DATASETS AND CALC STATISTICS /////////////////////////////////////</SPAN>
<SPAN class="comment token"># NOTE: Rebuild indexes can accept a Python list of datasets.</SPAN>
<SPAN class="comment token"># Get a list of all the datasets the user has access to.</SPAN>
<SPAN class="comment token"># First, get all the stand alone tables, feature classes and rasters.</SPAN>
dataList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListTables<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Next, for feature datasets get all of the datasets and featureclasses</SPAN>
<SPAN class="comment token"># from the list and add them to the master list.</SPAN>
<SPAN class="keyword token">for</SPAN> dataset <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Feature"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> dataset<SPAN class="punctuation token">)</SPAN>
dataList <SPAN class="operator token">+=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># reset the workspace</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> workspace
<SPAN class="comment token"># Concatenate all datasets into a list</SPAN>
datasetList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>ds <SPAN class="keyword token">for</SPAN> ds <SPAN class="keyword token">in</SPAN> dataList<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"rebuilding indexes"</SPAN>
<SPAN class="comment token"># Execute rebuild indexes</SPAN>
<SPAN class="comment token"># Note: to use the "SYSTEM" option the workspace user must be an administrator.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RebuildIndexes_management<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_SYSTEM"</SPAN><SPAN class="punctuation token">,</SPAN> datasetList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALL"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Rebuild Complete'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"analyzing datasets"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AnalyzeDatasets_management<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_SYSTEM"</SPAN><SPAN class="punctuation token">,</SPAN> datasetList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_BASE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_DELTA"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ANALYZE_ARCHIVE"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"analysis complete"</SPAN>
<SPAN class="comment token">#Allow the database to begin accepting connections again</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Set databases to allow connections..."</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AcceptConnections<SPAN class="punctuation token">(</SPAN>editDB<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ script initiation, Rec/Post process error handling \\\\\\\\\\\\\\\\\\\\\\\\\\\\\</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'An error occured'</SPAN>
failMsg <SPAN class="operator token">=</SPAN> <SPAN class="string token">'\nSCRIPT FAILURE IN SCRIPT INITIATION OR RECONCILE-POST PROCESS, \n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'Most recent GP messages below.\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN><SPAN class="string token">'\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'\nTraceback messages below.\n'</SPAN>
failMsg <SPAN class="operator token">+=</SPAN> traceback<SPAN class="punctuation token">.</SPAN>format_exc<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>splitlines<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> failMsg
<SPAN class="comment token"># \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ write error log info\\\\\\\\\\\\\\\\\\\\\\\\\\\\\</SPAN>
<SPAN class="comment token"># move to working directory </SPAN>
os<SPAN class="punctuation token">.</SPAN>chdir <SPAN class="punctuation token">(</SPAN>u<SPAN class="string token">'Y:\\TOOLS\\Logs\\'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## define function with variable filename and the format of the timestamp</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">timeStamped</SPAN><SPAN class="punctuation token">(</SPAN>filename<SPAN class="punctuation token">,</SPAN> fmt<SPAN class="operator token">=</SPAN><SPAN class="string token">'%m-%d-%y-%H.%M.%S-{filename}'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN>fmt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>filename<SPAN class="operator token">=</SPAN>filename<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## assign local variable filename and use whatever file name and extension you need</SPAN>
filename <SPAN class="operator token">=</SPAN> timeStamped<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Natural_ResourcesTC_toQC_Log.txt'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## Create the logfile and assign write permission</SPAN>
f <SPAN class="operator token">=</SPAN> Open<SPAN class="punctuation token">(</SPAN>filename<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"w"</SPAN><SPAN class="punctuation token">)</SPAN>
f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>failmsg<SPAN class="punctuation token">)</SPAN>
f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>