Select to view content in your preferred language

File will not copy in a Python Toolbox

467
2
11-06-2013 11:28 AM
JohnDye
Deactivated User
I've developed a lot of custom tools in Python Toolboxes for my end users. From time to time, those tools are updated or refined and I end up having to go to each user's system and update the Python Toolbox for them. So to alieviate myself from that requirement, I put a little custom logic into the updateParameters function of the each tool that compares the user's Python toolbox (.pyt file) against the version of the Python toolbox I've published to a shared drive, using the 'filecmp.cmp()' method.

If the comparison returns false, then there is a difference between the two files and the the tool raises an error on all tool parameters telling the user that an update is required.

So that means I needed an automated update tool for those Python Toolboxes. So within our maintenance toolbox, I wrote a simple logic update tool. The only thing it is supposed to do is go out to the shared drive, grab the .pyt and copy it back to the user's local 'My Toolboxes' folder, overwriting the existing file if neccesary.

Problem is, when I put the source code into the the PyScripter Interpeter, it works fine. If I run it in the ArcGIS Python Window, it works fine. When it is run in the toolbox however, it does not copy the 'SourceFile' to the 'Destination' but the copy function always returns without error as if it did.

def execute(self, parameters, messages):
        # If parameter0 (Update Location Analytics Toolbox) has been checked by the user
        if parameters[0].value == True:
            ## User has opted to update the Location Analytics Toolbox
            
            # Aquire the Published Logic File to synchronize with
            arcpy.AddMessage("Acquiring Published Location Analytics Source Logic...")
            SourceFile = r"\\cof.ds.capitalone.com\root\SDS_SDS6\36051\GIS_Management\Toolboxes\LocationAnalytics\Location Analytics.pyt"
            
            # Validate that the Destination exists
            arcpy.AddMessage("Validating Destination...")
            Destination = r"C:\Users\\" + str(User) + "\\AppData\Roaming\ESRI\\" + str(DesktopVersion) + "\\ArcToolbox\My Toolboxes"
            if arcpy.Exists(Destination) == True:
                
                # Copy the file
                arcpy.AddMessage("Copying " + str(SourceFile) + " to " + str(Destination) + "...")
                shutil.copy2(SourceFile, Destination)
            else:
                arcpy.AddError("Could not locate " + str(Destination) + " on local machine.")

Note: The 'User' parameter is a string returned from the 'getpass.getuser()' method provided in the 'isLicensed' function of the toolbox and is declared global.
The 'DesktopVersion' parameter is a string returned from the 'isLicensed' function of the toolbox, which simply looks at the user's installed program folder to determine which version of ArcGIS is installed and populates the parameter with 'Desktop10.2' for example, if that is the highest version installed. It is also declared global.


I've tried using different copy functions as well, including 'shutil.copy()', 'shutil.copy2()' and 'arcpy.Copy_management', of course slightly altering the 'SourceFile' and 'Destination' parameters as each method requires.

In every instance, I get the same result. Works fine in the interpreter, works fine in the Python Window, acts as if it succeeded when run as a Python tool but does not actually copy the 'SourceFile' to the 'Destination'.

What gives?
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Esri Alum
Just a hunch, but is the tool overwriting its own toolbox? Is the copy routine to replace the .pyt in the same .pyt it's updating? File might be locked in that case.
0 Kudos
JohnDye
Deactivated User
It's a good hunch, but no. They are completely separate toolboxes, thus completely separate .pyt files.
0 Kudos