Home > OS >  Executable Scriptspython.EXE not found for pre-commit (lose slashes)
Executable Scriptspython.EXE not found for pre-commit (lose slashes)

Time:07-28

We created a pre-commit hook for our project. But when we ran that pre-commit on Windows, we got an error:

Check copyright notices...................................................................Failed
- hook id: check-copyright
- exit code: 1

Executable `C:Userstore.cachepre-commitrepoq2jv0lxhpy_env-python3.9Scriptspython.EXE` not found 

So, pre-commit lose slashes for Windows machine. How to fix it? In pre-commit script we have #!/usr/bin/env python shebang, could it be a problem with that? For Unix-systems everything is ok

.pre-commit-config.yaml

- repo: https://github.com/espressif/check-copyright/
  rev: v1.0.0
  hooks:
    - id: check-copyright
      args: ['--ignore', 'tools/ci/check_copyright_ignore.txt', '--config', 'tools/ci/check_copyright_config.yaml']

.pre-commit-hooks.yaml:

- id: check-copyright
  name: Check copyright notices
  entry: check_copyright.py --verbose --replace
  language: python
  files: \.(py|c|h|cpp|hpp|ld|s|S)$
  require_serial: true

Full repository: https://github.com/espressif/check-copyright

CodePudding user response:

the hook you are using is packaged poorly -- it doesn't properly support windows installations

their usage of scripts generates a file in the bin directory with a (bogus) shebang:

C:\Users\Anthony\workspace\check-copyright>head -1 venv\Scripts\check_copyright.py
#!C:\Users\Anthony\workspace\check-copyright\venv\Scripts\python.exe

the supported way to make platform-agnostic entrypoints is to utilize console_scripts:

the easiest fix is to submit a pull request which adjusts their usage of scripts to instead utilize console_scripts:

diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml
index dd2ebf1..9ed932b 100644
--- a/.pre-commit-hooks.yaml
    b/.pre-commit-hooks.yaml
@@ -1,6  1,6 @@
 - id: check-copyright
   name: Check copyright notices
-  entry: check_copyright.py --verbose --replace
   entry: check-copyright --verbose --replace
   language: python
   files: \.(py|c|h|cpp|hpp|ld|s|S)$
   require_serial: true
\ No newline at end of file
diff --git a/setup.py b/setup.py
index 97b5cc6..c5c01bb 100644
--- a/setup.py
    b/setup.py
@@ -33,5  33,6 @@ setuptools.setup(
     url=URL,
     install_requires=REQUIRES,
     py_modules=['check_copyright'],
-    scripts=['check_copyright.py']
     scripts=['check_copyright.py'],
     entry_points={'console_scripts': ['check-copyright=check_copyright:main']},
 )

disclaimer: I created pre-commit

CodePudding user response:

Don't forget that in Python \ is an escape character. For Windows paths, you'd need to double them up with a second \. Depending on if you're using Python 2 or Python 3, you can use the following approaches to make your file path OS agnostic.

os.path.join()

Prior to Python 3.4, you would use os.path.join() to create an agnostic path. It is a little awkward to use however, since you need to issue a new call for each directory in your path. For example, if I want to have a path to the folder source_code/my_clones/projA, I would have to use the following code:

clone_path = os.path.join("source_code", "my_clones")
proj_path = os.path.join(clone_path, "projA")

As you can see, this is not particularly nice and is very cumbersome for large file paths.

Pathlib

The Pathlib library was introduced in Python 3. Its handling of paths is much better. Simply specify the UNIX-style path and the library will handle the OS-specific path conversions for you. Taking a look at our previous example, source_code/my_clones/projA, you could write:

proj_path = Path("source_code/my_clones/projA") 

Most file operations will handle the conversion for you when using pathlib.

  • Related