Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo Off
- REM Note from here2share: I have not tried this as of yet though it
- REM does appear to be very safe. Just keeping this as for a future
- REM reference
- REM DevPlayer@gmail.com
- REM 2011-Oct-3
- REM This will run a python module if the version is correct.
- REM the Python version is done before any modules are run.
- REM Some benefits to this script are:
- REM no files are created; avoids file write permission issues
- REM no environment varibles are created; avoids permission issues
- REM no Python modules are run; avoids syntax exceptions between
- REM Python versions
- REM Some cons are:
- REM This is a post Window 2000++ script; Win XP and later work.
- REM This is not a unix/linux script although it can be adapted.
- REM It is likely that not all Python interpreters support the
- REM the -V option or push the -V output to stderr;
- REM In which case just remove 2>&1
- REM Assumes the output to be formatted like "Python x.y"
- REM You have to run this script every time to launch your Python
- REM app to benefit from this check.
- REM So therefore you need to include it in your distribution
- REM http://www.fpschultze.de/modules/smartfaq/faq.php?faqid=17
- REM Read on 2011-Oct-3
- REM Try this at Windows/DOS command prompt to see:
- REM python.exe -V
- REM This should send a string like "Python 2.7" without quotation marks,
- REM to stderr (prints to console)
- REM If your python.exe version doesn't support the -V option
- REM this batch will still work.
- REM This is not Python.
- REM GENERALLY use " (double qoutes) in Windows DOS scripts/batch files.
- REM Do not use ' (single quotes) unless the DOS command supports it
- REM I only had one version of Python.exe on my system.
- REM I don't know what text other versions of "python.exe -V" will return.
- REM My version returns "Python 2.7" without the quotes,
- REM even though I have version 2.7.0
- REM Perhaps other people with other versions of Python can post what
- REM their version of "python.exe -V" returns textually. I could then
- REM update MS-Windows XP script.
- REM Versions of Microsoft Windows OSes after MS-Windows XP Pro
- REM will likely run this script fine.
- REM It is very likely this script can be converted to a linux version.
- REM alternate: python -c "import sys; print sys.version_info" > pyver.txt
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.7" > Nul
- If Not ErrorLevel 1 Goto Python27
- REM Here is the break down of that DOS line
- REM "FOR /F", "IN", "DO", "ECHO" are internal DOS commands; ie part of cmd.exe
- REM "tokens=1,2"
- REM is a paramater for the "FOR /F" DOS command.
- REM It implicidly means "tokens=1,2 delims= "
- REM "delims= " means break down the string at every space character.
- REM So a string like "Python 2.7" will be broken down into:
- REM "Python" and "2.7"
- REM "delims" means delimiter
- REM "tokens=1,2" %%G
- REM means grab the "word zero" and grab "word one"
- REM in the string and put "word zero" (ie token 1) into variable
- REM %%G and "word one" (ie token 2) into variable %%H.
- REM Where did %%H come from?
- REM For each token in string, create a script varaible starting
- REM with %%G as the first varible name.
- REM This is an implicid feature of "FOR /F";
- REM max is 26 %%A-%%Z.
- REM IN
- REM means: in the string returned by the following -thing-
- REM q:\python27\python.exe -V
- REM tells python.exe to use the -V argument;
- REM which means print out the Python version
- REM to stderr and then exit (python.exe)
- REM 2>&1
- REM means to tell the comand to redirect whatever is sent
- REM to stderr to stdout.
- REM In Lunix you could use just 2>& I believe
- REM python.exe -V prints the version to stderr, not stdout.
- REM So this needs to be redirected to stdout to make it
- REM a string that can be used by a DOS SCRIPT
- REM "python.exe -V 2>&1"
- REM The double quotation marks are needed
- REM because there are command parameters seperated by spaces
- REM and redirection, stderr to stdout, in the command
- REM (' ..."python.exe.." ')
- REM The parenthesis and single quotes tell the "FOR /F" command
- REM to execute the string as if the user typed it at the
- REM command prompt' and from that output of that execution
- REM put that output into a string usable by the "FOR /F" command.
- REM DO
- REM "DO" is part of the "FOR /F" command
- REM ECHO %%H
- REM write %%H to stdout; not that it is not %%G which would
- REM normally be the string "Python"
- REM | find "2.7" > Nul
- REM | == pipe
- REM find "2.7"
- REM is a DOS command that will search for a string "2.7"
- REM note the double quotes, no single quotes
- REM so pipe a string into find and compare to "2.7"
- REM and MOST importantly if it matches return with an
- REM Errorlevel of 1 if there is a match
- REM > Nul
- REM and send the search results, ie the output (not exit code)
- REM to the nul device instead of stderr or stdout.
- REM The user doesn't need to see that.
- REM If Not ErrorLevel 1 Goto Python27
- REM means if "find"'s exit code is 1 then jump to the script
- REM section labeld Python27.
- REM A DOS lable is aline that starts with :<text>
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.4" > Nul
- If Not ErrorLevel 1 Goto Python24
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.5" > Nul
- If Not ErrorLevel 1 Goto Python25
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.6" > Nul
- If Not ErrorLevel 1 Goto Python26
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.7" > Nul
- If Not ErrorLevel 1 Goto Python27
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.8" > Nul
- If Not ErrorLevel 1 Goto Python28
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.9" > Nul
- If Not ErrorLevel 1 Goto Python29
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.0" > Nul
- If Not ErrorLevel 1 Goto Python30
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.1" > Nul
- If Not ErrorLevel 1 Goto Python31
- FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.2" > Nul
- If Not ErrorLevel 1 Goto Python32
- Goto :EOF
- REM ------------------------------------
- ::Python 2.4 code
- :Python24
- echo FOUND 2.4
- python.exe my_python_app24.py
- goto :EOF
- ::Python 2.5 code
- :Python25
- echo FOUND 2.5
- python.exe my_python_app25.py
- goto :EOF
- ::Python 2.6 code
- :Python26
- echo FOUND 2.6
- python.exe my_python_app26.py
- goto :EOF
- ::Python 2.7 code
- :Python27
- echo FOUND 2.7
- python.exe my_python_app27.py
- goto :EOF
- :: Python 2.8
- :Python28
- echo Python 2.8 found
- python.exe my_python_app28.py
- goto :EOF
- :: Python 2.9; doesn't exist yet
- :Python29
- echo Python 2.9 found
- python.exe my_python_app29.py
- goto :EOF
- :: Python 3.0
- :Python30
- echo Python 3.0 found
- python.exe my_python_app30.py
- goto :EOF
- :: Python 3.1
- :Python31
- echo Python 3.1 found
- python.exe my_python_app31.py
- goto :EOF
- :: Python 3.2
- :Python32
- echo Python 3.2 found
- python.exe my_python_app32.py
- goto :EOF
- ::EOF
- :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement