suppress data source password dialog

2051
10
05-13-2016 04:35 PM
forestknutsen1
MVP Regular Contributor

I need to re-point all sde raster layers in mxds to a new sde instance (we are moving from sde 9.3.1 to 10.1). Many people have also have ODBC connections within their mxds for tabular data from non-spatial databases. The problem is when I run the script it is constantly prompting me for credentials. I could care less about them for this exercise as I do not need to update the ODBC connection. I would like to just suppress them. Can this be done?   

import arcpy
import os
import time


path_layer = r"***"
osa_sde_path = r"***"
sde_user = "***"
log = open("log.txt", "w")


for dir_path, dir_names, file_names in os.walk(path_layer):
    for file_name in file_names:
        if file_name[-3:] == "mxd":
            ts = os.path.getmtime(os.path.join(dir_path, file_name))
            mod_date = time.strftime('%m/%Y', time.gmtime(ts))
            mod_date_list = mod_date.split("/")
            if int(mod_date_list[1]) > 2012 and int(mod_date_list[0]) > 4:
                mxd = arcpy.mapping.MapDocument(os.path.join(dir_path, file_name))
                layers_list = arcpy.mapping.ListLayers(mxd)
                print "Updating:\t" + os.path.join(dir_path, file_name)


                for layer in layers_list:
                    if layer.supports("DATASOURCE"):
                        data_source = layer.dataSource
                        if data_source.find(sde_user) != -1 and layer.isRasterLayer:
                            print "\tUpdating:\t" + layer.name
                            dot_index = data_source.rfind(".")
                            dataset_name = data_source[dot_index + 1:]
                            print "\t\tNew Path:\t" + os.path.join(osa_sde_path, sde_user + "." + 
                                                                   dataset_name)
                            layer.replaceDataSource(osa_sde_path, "SDE_WORKSPACE", sde_user + "." + 
                                                    dataset_name, False)
                            if layer.isBroken:
                                log.write(os.path.join(dir_path, file_name))
                                print "Data Source Broken"
                mxd.save()
log.close()

Suggestions?

Tags (2)
0 Kudos
10 Replies
DanPatterson_Retired
MVP Esteemed Contributor

certainly not anything in arcpy or arcpy.mapping or Pro's arcpy.ma module

Introduction to arcpy.mp—ArcPy | ArcGIS for Desktop

assuming it is line 33 that is raising the login

0 Kudos
forestknutsen1
MVP Regular Contributor

Line 20 is the problem.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I got waylaid in Pro's help and the only place I saw with the ability to pass a password was here

Updating and fixing data sources—ArcPy | ArcGIS for Desktop

A database connection data source with no joins or relates:

{'connection_info': {'authentication_mode': 'DBMS', 'database': 'gis', 
'db_connection_properties': 'Esri', 'dbclient': 'sqlserver',
 'instance': 'sde:sqlserver:Esri', 'password': '*********', 
'server': 'gis', 'user': 'arcpy', 'version': 'sde.DEFAULT'},
 'dataset': 'gis.arcpy.RangerStations', 'workspace_factory': 'SDE'}

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project_default.mxd") mxd.findAndReplaceWorkspacePaths(r"C:\Project\Connection with password info saved.sde", r"C:\Project\Connection with no password info saved.sde", False) mxd.saveACopy(r"C:\Project\Project_NP.mxd") del mxd

So Forest, you are on your own... another reason I prefer working with local data hope you find something

MichaelVolz
Esteemed Contributor

If you open 1 of the mxds yourself, would you be presented with having to enter credentials for the data connection?

Maybe you would need to first get an inventory of all the connections on the mxds that you will be resourcing where you save this information to a text file or a spreadsheet.  Then if possible you could get the actual credentials for all the connections that you need to make.  Then you could have a python subroutine called where you would associate each user with the appropriate password and then make the connection.  Then you can use this same information to make new user connections in the new database and then save the mxds and your mxds will now be resourced and you have bypassed the script stopping where it asks for credentials.  The big question here is if you would have permission to access all the user's passwords and place them in your script .  You will also want to keep this script in a secure location as it would be listing all of the user's passwords.

0 Kudos
forestknutsen1
MVP Regular Contributor

Michael Volz wrote:

If you open 1 of the mxds yourself, would you be presented with having to enter credentials for the data connection?

Yes, I would.

Michael Volz wrote:

Maybe you would need to first get an inventory of all the connections on the mxds that you will be resourcing where you save this information to a text file or a spreadsheet. Then if possible you could get the actual credentials for all the connections that you need to make. Then you could have a python subroutine called where you would associate each user with the appropriate password and then make the connection. Then you can use this same information to make new user connections in the new database and then save the mxds and your mxds will now be resourced and you have bypassed the script stopping where it asks for credentials. The big question here is if you would have permission to access all the user's passwords and place them in your script . You will also want to keep this script in a secure location as it would be listing all of the user's passwords.

I do not need to update the (non-sde) connections that are causing the Enter Password dialog. They are fine. I just need to update all raster layers in the mxds that point to one sde schema, sde_user, in the code. This part is working, if I just hit cancel on the dialog the code completes and successfully updates the target connections. The problem is that one just has to sit there and hit cancel over and over. Something like 40 times for my test set of mxds. The other option would be to some how pass my credentials to the dialog.

0 Kudos
forestknutsen1
MVP Regular Contributor

Dan Patterson wrote:

So Forest, you are on your own... another reason I prefer working with local data hope you find something

Ya, local is nice. But just not possible in our situation. We would have 200 copies of a feature class littered across the network--none of them the same or up to date.

0 Kudos
WesMiller
Regular Contributor III
0 Kudos
forestknutsen1
MVP Regular Contributor

It is a non-sde connection that is causing the problem. 

0 Kudos
forestknutsen1
MVP Regular Contributor

Any one know how to get a list of only raster layers in a mxd without using arcpy.mapping.ListLayers(mxd) as it looks like it the dialog is occurring when this object asks for info for the non-sde layers?

0 Kudos