For reasons I can't explain, the scripts I describe below work for me. However my foolish intellect insists that perhaps pylint-script.py should actually read:

    import sys
    from pylint import lint
    sys.exit(lint.Run(sys.argv[1:]))

(ie. explicitly calling sys.exit)

Jonathan Hartley      Made of meat.      http://tartley.com
tart...@tartley.com   +44 7737 062 225   twitter/skype: tartley



On 21/01/2010 17:41, Jonathan Hartley wrote:
Hi,

I didn't see anyone follow up on Daniel Harding's assertion that his adapted pylint.bat fix could be improved to "detect and remove the --exitcode parameter from any position in the argument list." Has anyone done any work on this?

I'm no batch file expert, but I just discovered an alternative solution which may be a better because it doesn't require introducing a new command-line switch.


SUMMARY

Replace the current pylint.bat with:

    @echo off
    python "%~dp0%~n0-script.py" %*

This just executes the file pylint-script.py, in the same directory. This is a new file:

    import sys
    from pylint import lint
    lint.Run(sys.argv[1:])

The result will execute pylint without ever exiting the current shell, and will always propagate the exit value from lint.Run() back to the invoker as a process exit value. As a bonus, despite introducing an extra .py file, this seems a much tidier and more understandable mechanism than the existing pylint.bat.

Am I overlooking something? Thoughts and criticism much appreciated.


DETAILS if you are really interested

The key is that a .bat file *will* return a process exit value to its invoker, even if the bat file was not invoked as a child process - it returns the exit value of the last command in the .bat script. In the existing .bat file, this is always zero because of the 'rem' command which is the last thing executed by the .bat interpreter. Hence the need for the 'exit' command to that was recently introduced, to override this zero exit value.

In simpler batch files (ie. ones that aren't also valid python files), this problem can be worked around as follows:

    python somecommand-script.py
    set EXITVAL=%ERRORLEVEL%

    call any-other-commands.bat
    set any-variables-etc

    python -c "import sys; sys.exit(%EXITVAL%)"

This way, the final command in the bat file has an exit value which is the saved exit value from the first python command. However, in pylint.bat, I can't find a way to do this - the last line of this file always has to be [rem """].

So I decided to try a solution which replaced pylint.bat with a straight .bat file and a separate .py file.


    Jonathan
Jonathan Hartley      Made of meat.http://tartley.com
tart...@tartley.com    +44 7737 062 225   twitter/skype: tartley
On 30/12/2009 13:23, Jonathan Hartley wrote:
Daniel's pylint.bat works fine for me, WindowsXP, Python 2.6.4.

Jonathan Hartley      Made of meat.http://tartley.com
tart...@tartley.com    +44 7737 062 225   twitter/skype: tartley

On 29/12/2009 22:00, Daniel Harding wrote:
Pierre Rouleau wrote:

Unfortunately, the pylint.bat file as submitted by Daniel does not completely work (at least on XP SP3, and probably other versions as well). The problem lies in the fact that SHIFT does not remove the --exitcode from %* expansion but removes it from "%~f0", the reverse from what is wanted in the original intent of the code.

Thanks for catching that - I wasn't aware of that.

Another problem with the submitted batch is the fact that endlocal is
never called (but selocal is called twice).

The second setlocal was accidentally left from an earlier version of the file. However, endlocal is not needed, because an implicit endlocal is performed by Windows when the batch file finishes for every setlocal that is called during the batch file.

I created a new version of pylint.bat that solves the mentioned problems. It has a limitation of being able to support only 8 parameters when the --exitcode parameter is specified. I wish Ièd found a better way to handle the arguments but, as I said, SHIFT does not impact %* expansion. If anyone has a better idea, let me know.

I was able to find a technique on Stack Overflow to get an arbitrary number of arguments:

http://stackoverflow.com/questions/761615/is-there-a-way-to-indicate-the-last-n-parameters-in-a-batch-file

I adapted the code shown there to implement removing the --exitcode parameter and passing all remaining arguments to python. Currently the code I have written only detects the --exitcode parameter if it is the first argument, but it would be easy to modify the code to detect and remove the --exitcode parameter from any position in the argument list.

I have attached my latest revision of pylint.bat.  Feedback is welcome.

Cheers,

Daniel


_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects


_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects
_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to