mgear-dev / mgear4

mGear v.4.x.x (python 3 ready) https://mgear4.readthedocs.io
MIT License
259 stars 91 forks source link

Customsteps do not work when using a UNC path (Shifter guide settings) #297

Open glendon-mooney opened 9 months ago

glendon-mooney commented 9 months ago

Hello,

We are using mGear-4.0.9. We are using the MGEAR_SHIFTER_CUSTOMSTEP_PATH environment variable to set the default custom step location to a folder on our server. (the value is normalized)

When adding other Custom Steps (located on the same server but different folder), they cannot be run and the "Edit" button does not work. The error message that is shown when pressing the "Edit" button is:

// Error: The step can't be find or does't exists // 

When running the step, we get the following error: custom_step_fail

Issue with running custom steps:

There is a condition in the shifter.guide.GuideSettings.addCustomStep that removes a "/" from the filePath before adding the item in the UI.

        if os.environ.get(MGEAR_SHIFTER_CUSTOMSTEP_KEY, ""):
            filePath = os.path.abspath(filePath)
            baseReplace = os.path.abspath(os.environ.get(
                MGEAR_SHIFTER_CUSTOMSTEP_KEY, ""))
            # backslashes (windows paths) can cause escape characters
            filePath = filePath.replace(baseReplace, "").replace('\\', '/')
            # remove front forward
            if '/' == filePath[0]:
                filePath = filePath[1:]

removed_first_slash

The path put in the item is then used by shifter.guide.HelperSlots.runStep to execute the step. Because the first slash of the UNC path is removed, the os.path.join that is in shifter.guide.HelperSlots.runStep is not able to join the path correctly and the file is not found.

Potential fix for running custom steps in the server location:

We have implemented a workaround which is to change the condition in shifter.guide.GuideSettings.addCustomStep to only remove the "/" if the path does not start with "//".

            # remove front forward
            if '/' == filePath[0] and '//' != filePath[:2]:
                filePath = filePath[1:]

Issue with the "Edit" button:

After modifying the condition in shifter.guide.GuideSettings.addCustomStep we can run custom steps that are outside of the MGEAR_SHIFTER_CUSTOMSTEP_PATH location, but the "Edit" button still throws an error.

This is caused by the os.startfile in shifter.guide.HelperSlots.editFile. The os.startfile can not open the UNC path unless the path has been normalized.

    def editFile(self, widgetList):
        try:
            cs_data = widgetList.selectedItems()[0].text()
            fullpath = self.get_cs_file_fullpath(cs_data)

            if fullpath:
                if sys.platform.startswith('darwin'):
                    subprocess.call(('open', fullpath))
                elif os.name == 'nt':
                    os.startfile(fullpath)

Potential fix "Edit" button:

A potential fix for this could be to normalize the fullPath being returned from shifter.guide.HelperSlots.get_cs_file_fullpath

    def get_cs_file_fullpath(self, cs_data):
        filepath = cs_data.split("|")[-1][1:]
        if os.environ.get(MGEAR_SHIFTER_CUSTOMSTEP_KEY, ""):
            fullpath = os.path.join(
                os.environ.get(
                    MGEAR_SHIFTER_CUSTOMSTEP_KEY, ""), filepath)
        else:
            fullpath = filepath

        return os.path.normpath(fullpath)