Skip to content

Conversation

@StephenMaturrin
Copy link

@StephenMaturrin StephenMaturrin commented Aug 13, 2025

📝 Description

Improved path utilities for anomaly detection.
Enhanced versioned directory creation, snake/case conversion, and output filename generation.
🛠️ Fixes compatibility issues when using anomalib on Windows (especially with symbolic links and path handling).

✨ Changes

Select what type of change your PR is:

  • 🐞 Bug fix (non-breaking change which fixes an issue)

@StephenMaturrin StephenMaturrin force-pushed the fix/utils/windows-path-support branch 3 times, most recently from b1a2c24 to 7052e9d Compare August 13, 2025 13:07
@rajeshgangireddy rajeshgangireddy self-requested a review August 14, 2025 11:30
Copy link
Contributor

@rajeshgangireddy rajeshgangireddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for the contribution.
I will test this PR soon. I currently do not have access to windows.

In the meantime, I have a question.
Do you think it might be better to create a shortcut (.lnk) in windows instead of copying the whole directory when there are no admin privileges.

@StephenMaturrin StephenMaturrin force-pushed the fix/utils/windows-path-support branch from 7052e9d to 56dca8b Compare August 14, 2025 15:18
Signed-off-by: Miguel Angel SOLINAS <miguelangel.solinas@se.com>
@StephenMaturrin StephenMaturrin force-pushed the fix/utils/windows-path-support branch from 56dca8b to f63b7aa Compare August 14, 2025 15:46
Copy link
Contributor

@rajeshgangireddy rajeshgangireddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes. Looks good to me.
Could you please fix the pre-commit issues. Then we are good to go.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes Windows compatibility issues in the path utilities module by adding Windows-specific handling for directory junctions and symbolic links. The changes address problems with path operations on Windows systems, particularly around versioned directory creation and link management.

  • Adds Windows junction detection and safe removal utilities
  • Implements Windows-specific latest directory creation using junctions
  • Updates create_versioned_dir to use platform-specific link handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 84 to 85
subprocess.run( # noqa: S603
["cmd", "/c", "mklink", "/J", str(tmp), str(target.resolve())], # noqa: S607
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the noqa comments acknowledge the security scanner warnings, using subprocess with shell commands can be risky. Consider validating that both tmp and target.resolve() paths don't contain shell metacharacters or use subprocess.run with shell=False (which is already the case here) and ensure paths are properly sanitized.

Copilot uses AI. Check for mistakes.
@ashwinvaidya17 ashwinvaidya17 added this to the v2.2.0 milestone Sep 8, 2025
rajeshgangireddy and others added 3 commits September 8, 2025 19:46
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com>
…g directory junctions on Windows. Yet to be tested.
@rajeshgangireddy rajeshgangireddy marked this pull request as draft September 10, 2025 14:12
@rajeshgangireddy
Copy link
Contributor

I added some changes to avoid subprocess. But I guess the issue might remain that a user might need admin priv. I will test this on a windows machine later this week. I will mark this as draft till then so we don't merge it.

@rajeshgangireddy
Copy link
Contributor

I tested the changes - it looks like there is no simple way to add shortcut paths in windows without a shell command (if we use pythonic way, we need admin privileges).
I looks like we might have to use shell commands after all - we need to validate the paths properly

@rajeshgangireddy rajeshgangireddy marked this pull request as ready for review September 15, 2025 20:08
@rajeshgangireddy
Copy link
Contributor

I tested on windows with these changes - This works. I added some extra checks to make sure the paths are validated before running shell commands.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +134 to +146
result = subprocess.run( # noqa: S603
[ # noqa: S607
"cmd",
"/c",
"mklink",
"/J",
str(tmp),
str(target.resolve()),
],
capture_output=True,
text=True,
check=False,
)
Copy link

Copilot AI Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using subprocess with shell commands poses security risks. While path validation is performed, consider using the Windows API directly through ctypes or a dedicated library for creating junctions to eliminate subprocess usage entirely.

Copilot uses AI. Check for mistakes.
@ashwinvaidya17 ashwinvaidya17 modified the milestones: v2.2.0, v2.3.0 Sep 17, 2025
@rajeshgangireddy
Copy link
Contributor

While the current solution works on windows, I would like to have one where we do not make use of shell commands if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants