This pre-commit hook is designed to enhance the portability of Python code by flagging instances of hard-coded path separators in string literals that are likely to represent file paths. It suggests the use of os.path.join
or os.sep
for better cross-platform compatibility.
Different operating systems use different characters as path separators (/
for Unix/Linux, and \\
for Windows). Using hard-coded path separators can therefore cause issues when your code is run on different operating systems.
By checking for hard-coded path separators at commit time, this pre-commit hook helps to prevent potential file path issues before they enter your codebase.
- First, ensure that you have pre-commit installed. If not, you can install it using pip:
pip install pre-commit
- Add the following to your
.pre-commit-config.yaml
file:
- repo: https://github.com/AdnanEkici/pre-commit-platform-safe-path-enforcer
rev: v0.0.2
hooks:
- id: pre-commit-platform-safe-path-enforcer
name: 'Check Path Separator'
entry: hooks/check_paths.py
language: script
types: [ python ]
- Run pre-commit:
pre-commit run --all-files
If the hook finds any hard-coded path separators in your Python files, it will output a message and fail, preventing the commit. You can then fix the issue before retrying the commit.
If there's a particular line of code where you want to intentionally use a hard-coded path separator, you can suppress the warning from this pre-commit hook by appending # noqa
at the end of the line. This tells the hook to ignore this line.
For example:
path = 'path/to/folder' # noqa
Contributions to this pre-commit hook are welcome. If you find a bug or have a suggestion for improvement, please open an issue or pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
In the next version (0.0.3) of my Pre-commit Safe Path Enforcer hook, I am planning to introduce the following features and improvements:
-
Performance optimization: I will be optimizing the script to reduce the overall time it takes to check larger codebases.
-
Expanded language support: I plan to extend our hook to support more programming languages beyond Python.
-
Exclude List Improvement: I am working on making the
exclude
feature more intuitive and robust, making it easier to specify files or folders that you want the hook to skip. -
Customization options: I am planning to introduce more customization options so that users can better tailor the tool to their needs.
-
Fix: Currently hook detects URL's. It will be fixed.
Stay tuned for these updates!