Daniel Harding wrote:
Jonathan Hartley wrote:

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.

I would make one improvement on what you have, and simply pass the code
to python using the -c command line option.  This removes the need for
an additional file.  pylint.bat then becomes:


     @echo off
     python -c "import sys; from pylint import lint; lint.Run(sys.argv[1:])" %*

After looking at things some more, I think I'm actually going to
contradict myself and recommend Jonathan's approach of having two
separate files.

I was looking at the other .bat files that get installed by pylint to
see about also converting them to use the same mechanism as I had
proposed for pylint.bat.  In doing so, I noticed two things.  First,
pylint-gui.bat for Windows has different code than the pylint-gui script
used on non-Windows platforms.  The latter has a try/except block to
report an error message in the case that Tkinter is not installed.  It
seems to me that it would be useful to include the same check on Windows,
which leads to my second observation:  when using the '-c' option from a
batch file, it is not possible to use complex statements like
try/except, because they cannot be written on a single line.

So, in view of avoiding code duplication between Windows batch files and
non-Windows scripts, and to allow full usage of Python syntax, I propose the
following solution:  on Windows, install the scripts used for other
platforms along with the batch files, and change each batch file to
simply execute the corresponding script with Python.

Using a slight modification of Jonathan's code, each batch file would
become:

    @echo off
    python "%~dpn0" %*

which says call python passing as a single parameter the drive, path,
and file name, without extension, of parameter 0, where parameter 0 is
the name of the batch file, followed by any parameters which were passed
on the command line.

I have attached a patch which makes these modifications to the batch
files, and also changes setup.py so that the additional scripts are
installed on Windows.

If anyone sees any issues with this approach or experiences problems
when testing it, please let me know.

Daniel Harding
diff -r 2672ec7e3228 bin/epylint.bat
--- a/bin/epylint.bat   Fri Dec 18 14:41:44 2009 +0100
+++ b/bin/epylint.bat   Tue Jan 26 21:01:52 2010 +0000
@@ -1,16 +1,5 @@
 @echo off
-rem = """-*-Python-*- script
-rem -------------------- DOS section --------------------
-rem You could set PYTHONPATH or TK environment variables here
-python -x "%~f0" %*
-goto exit
- 
-"""
-# -------------------- Python section --------------------
-from pylint import epylint
-epylint.Run()
- 
-
-DosExitLabel = """
-:exit
-rem """
+rem Use python to execute the python script having the same name as this batch
+rem file, but without any extension, located in the same directory as this
+rem batch file
+python "%~dpn0" %*
diff -r 2672ec7e3228 bin/pylint-gui.bat
--- a/bin/pylint-gui.bat        Fri Dec 18 14:41:44 2009 +0100
+++ b/bin/pylint-gui.bat        Tue Jan 26 21:01:52 2010 +0000
@@ -1,20 +1,5 @@
-...@echo off
-rem = """-*-Python-*- script
-...@echo off
-rem -------------------- DOS section --------------------
-rem You could set PYTHONPATH or TK environment variables here
-python -x "%~f0" %*
-goto exit
- 
-"""
-# -------------------- Python section --------------------
-import sys
-from pylint import gui 
-gui.Run(sys.argv[1:])
- 
-
-DosExitLabel = """
-:exit
-rem """
-
-
+...@echo off
+rem Use python to execute the python script having the same name as this batch
+rem file, but without any extension, located in the same directory as this
+rem batch file
+python "%~dpn0" %*
diff -r 2672ec7e3228 bin/pylint.bat
--- a/bin/pylint.bat    Fri Dec 18 14:41:44 2009 +0100
+++ b/bin/pylint.bat    Tue Jan 26 21:01:52 2010 +0000
@@ -1,20 +1,5 @@
-...@echo off
-rem = """-*-Python-*- script
-rem -------------------- DOS section --------------------
-rem You could set PYTHONPATH or TK environment variables here
-python -x "%~f0" %*
-goto exit
- 
-"""
-# -------------------- Python section --------------------
-import sys
-from pylint import lint
-lint.Run(sys.argv[1:])
- 
-
-DosExitLabel = """
-:exit
-exit(ERRORLEVEL)
-rem """
-
-
+...@echo off
+rem Use python to execute the python script having the same name as this batch
+rem file, but without any extension, located in the same directory as this
+rem batch file
+python "%~dpn0" %*
diff -r 2672ec7e3228 bin/pyreverse.bat
--- a/bin/pyreverse.bat Fri Dec 18 14:41:44 2009 +0100
+++ b/bin/pyreverse.bat Tue Jan 26 21:01:52 2010 +0000
@@ -1,18 +1,5 @@
 @echo off
-rem = """-*-Python-*- script
-...@echo off
-rem -------------------- DOS section --------------------
-rem You could set PYTHONPATH or TK environment variables here
-python -x "%~f0" %*
-goto exit
-
-"""
-# -------------------- Python section --------------------
-import sys
-from pylint.pyreverse import main
-main.Run(sys.argv[1:])
-
-
-DosExitLabel = """
-:exit
-rem """
+rem Use python to execute the python script having the same name as this batch
+rem file, but without any extension, located in the same directory as this
+rem batch file
+python "%~dpn0" %*
diff -r 2672ec7e3228 bin/symilar.bat
--- a/bin/symilar.bat   Fri Dec 18 14:41:44 2009 +0100
+++ b/bin/symilar.bat   Tue Jan 26 21:01:52 2010 +0000
@@ -1,20 +1,5 @@
-...@echo off
-rem = """-*-Python-*- script
-...@echo off
-rem -------------------- DOS section --------------------
-rem You could set PYTHONPATH or TK environment variables here
-python -x "%~f0" %*
-goto exit
- 
-"""
-# -------------------- Python section --------------------
-import sys
-from pylint.checkers import similar
-similar.run()
- 
-
-DosExitLabel = """
-:exit
-rem """
-
-
+...@echo off
+rem Use python to execute the python script having the same name as this batch
+rem file, but without any extension, located in the same directory as this
+rem batch file
+python "%~dpn0" %*
diff -r 2672ec7e3228 setup.py
--- a/setup.py  Fri Dec 18 14:41:44 2009 +0100
+++ b/setup.py  Tue Jan 26 21:01:52 2010 +0000
@@ -63,7 +63,8 @@
     """
     from distutils import util
     if util.get_platform()[:3] == 'win':
-        scripts_ = [script + '.bat' for script in linux_scripts]
+        scripts_ = linux_scripts + [script + '.bat'
+                for script in linux_scripts]
     else:
         scripts_ = linux_scripts
     return scripts_
_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to