DevPlayer

MS script; Python version check prelaunch of Python module

Oct 3rd, 2011
2,666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.23 KB | None | 0 0
  1. @Echo Off
  2.  
  3. REM  2011-Oct-3
  4.  
  5. REM  This will run a python module if the version is correct.
  6. REM  the Python version is done before any modules are run.
  7.  
  8. REM  Some benefits to this script are:
  9. REM     no files are created; avoids file write permission issues
  10. REM     no environment varibles are created; avoids permission issues
  11. REM     no Python modules are run; avoids syntax exceptions between
  12. REM         Python versions
  13.  
  14. REM Some cons are:
  15. REM     This is a post Window 2000++ script; Win XP and later work.
  16. REM     This is not a unix/linux script although it can be adapted.
  17. REM     It is likely that not all Python interpreters support the
  18. REM     the -V option or push the -V output to stderr;
  19. REM         In which case just remove 2>&1
  20. REM     Assumes the output to be formatted like "Python x.y"
  21. REM     You have to run this script every time to launch your Python
  22. REM         app to benefit from this check.
  23. REM         So therefore you need to include it in your distribution
  24.  
  25. REM  http://www.fpschultze.de/modules/smartfaq/faq.php?faqid=17
  26. REM  Read on 2011-Oct-3
  27.  
  28. REM  Try this at Windows/DOS command prompt to see:
  29. REM     python.exe -V
  30.  
  31. REM  This should send a string like "Python 2.7" without quotation marks,
  32. REM  to stderr (prints to console)
  33.  
  34. REM  If your python.exe version doesn't support the -V option
  35. REM  this batch will still work.
  36.  
  37. REM  This is not Python.  
  38. REM  GENERALLY use " (double qoutes) in Windows DOS scripts/batch files.
  39. REM  Do not use ' (single quotes) unless the DOS command supports it
  40.  
  41. REM  I only had one version of Python.exe on my system.
  42. REM  I don't know what text other versions of "python.exe -V" will return.
  43.  
  44. REM  My version returns "Python 2.7" without the quotes,
  45. REM  even though I have version 2.7.0
  46.  
  47. REM  Perhaps other people with other versions of Python can post what
  48. REM  their version of "python.exe -V" returns textually. I could then
  49. REM  update MS-Windows XP script.
  50.  
  51. REM  Versions of Microsoft Windows OSes after MS-Windows XP Pro
  52. REM  will likely run this script fine.
  53. REM  It is very likely this script can be converted to a linux version.
  54.  
  55. REM alternate: python -c "import sys; print sys.version_info" > pyver.txt
  56.  
  57. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.7" > Nul
  58. If Not ErrorLevel 1 Goto Python27
  59.  
  60. REM  Here is the break down of that DOS line
  61.    REM  "FOR /F", "IN", "DO", "ECHO" are internal DOS commands; ie part of cmd.exe
  62.  
  63.     REM  "tokens=1,2"
  64.     REM     is a paramater for the "FOR /F" DOS command.
  65.     REM     It implicidly means "tokens=1,2 delims= "
  66.  
  67.     REM     "delims= " means break down the string at every space character.
  68.     REM     So a string like "Python 2.7" will be broken down into:
  69.     REM     "Python" and "2.7"
  70.     REM     "delims" means delimiter
  71.  
  72.     REM  "tokens=1,2" %%G
  73.     REM     means grab the "word zero" and grab "word one"
  74.     REM     in the string and put "word zero" (ie token 1) into variable
  75.     REM     %%G and "word one" (ie token 2) into variable %%H.
  76.  
  77.     REM     Where did %%H come from?
  78.     REM     For each token in string, create a script varaible starting
  79.     REM     with %%G as the first varible name.
  80.     REM     This is an implicid feature of "FOR /F";
  81.     REM     max is 26 %%A-%%Z.
  82.  
  83.     REM  IN
  84.     REM     means: in the string returned by the following -thing-
  85.  
  86.     REM  q:\python27\python.exe -V
  87.     REM     tells python.exe to use the -V argument;
  88.     REM     which means print out the Python version
  89.     REM     to stderr and then exit (python.exe)
  90.  
  91.     REM  2>&1
  92.     REM     means to tell the comand to redirect whatever is sent
  93.     REM     to stderr to stdout.
  94.  
  95.     REM     In Lunix you could use just  2>&  I believe
  96.     REM     python.exe -V prints the version to stderr, not stdout.
  97.     REM     So this needs to be redirected to stdout to make it
  98.     REM     a string that can be used by a DOS SCRIPT
  99.  
  100.     REM "python.exe -V 2>&1"
  101.     REM     The double quotation marks are needed
  102.     REM     because there are command parameters seperated by spaces
  103.     REM     and redirection, stderr to stdout, in the command
  104.  
  105.     REM (' ..."python.exe.."  ')
  106.     REM     The parenthesis and single quotes tell the "FOR /F" command
  107.     REM     to execute the string as if the user typed it at the
  108.     REM     command prompt' and from that output of that execution
  109.     REM     put that output into a string usable by the "FOR /F" command.
  110.  
  111.     REM DO
  112.     REM     "DO" is part of the "FOR /F" command
  113.  
  114.     REM ECHO %%H
  115.     REM     write %%H to stdout; not that it is not %%G which would
  116.     REM     normally be the string "Python"
  117.  
  118.     REM | find "2.7" > Nul
  119.     REM     | == pipe
  120.     REM     find "2.7"
  121.     REM        is a DOS command that will search for a string "2.7"
  122.     REM        note the double quotes, no single quotes
  123.     REM        so pipe a string into find and compare to "2.7"
  124.     REM        and MOST importantly if it matches return with an
  125.     REM        Errorlevel of 1 if there is a match
  126.     REM    > Nul
  127.     REM        and send the search results, ie the output (not exit code)
  128.     REM        to the nul device instead of stderr or stdout.
  129.     REM        The user doesn't need to see that.
  130.    
  131.     REM If Not ErrorLevel 1 Goto Python27
  132.     REM    means if "find"'s exit code is 1 then jump to the script
  133.     REM    section labeld Python27.  
  134.     REM    A DOS lable is aline that starts with :<text>
  135.  
  136. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.4" > Nul
  137. If Not ErrorLevel 1 Goto Python24
  138.  
  139. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.5" > Nul
  140. If Not ErrorLevel 1 Goto Python25
  141.  
  142. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.6" > Nul
  143. If Not ErrorLevel 1 Goto Python26
  144.  
  145. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.7" > Nul
  146. If Not ErrorLevel 1 Goto Python27
  147.  
  148. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.8" > Nul
  149. If Not ErrorLevel 1 Goto Python28
  150.  
  151. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "2.9" > Nul
  152. If Not ErrorLevel 1 Goto Python29
  153.  
  154. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.0" > Nul
  155. If Not ErrorLevel 1 Goto Python30
  156.  
  157. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.1" > Nul
  158. If Not ErrorLevel 1 Goto Python31
  159.  
  160. FOR /F "tokens=1,2" %%G IN ('"python.exe -V 2>&1"') DO ECHO %%H | find "3.2" > Nul
  161. If Not ErrorLevel 1 Goto Python32
  162.  
  163. Goto :EOF
  164.  
  165. REM  ------------------------------------
  166. ::Python 2.4 code
  167.  :Python24
  168.  echo FOUND 2.4
  169.  python.exe my_python_app24.py
  170.  goto :EOF
  171.  
  172. ::Python 2.5 code
  173.  :Python25
  174.  echo FOUND 2.5
  175.  python.exe my_python_app25.py
  176.  goto :EOF
  177.  
  178. ::Python 2.6 code
  179.  :Python26
  180.  echo FOUND 2.6
  181.  python.exe my_python_app26.py
  182.  goto :EOF
  183.  
  184. ::Python 2.7 code
  185.  :Python27
  186.  echo FOUND 2.7
  187.  python.exe my_python_app27.py
  188.  goto :EOF
  189.  
  190. :: Python 2.8
  191.  :Python28
  192.  echo Python 2.8 found
  193.  python.exe my_python_app28.py
  194.  goto :EOF
  195.  
  196. :: Python 2.9; doesn't exist yet
  197.  :Python29
  198.  echo Python 2.9 found
  199.  python.exe my_python_app29.py
  200.  goto :EOF
  201.  
  202. :: Python 3.0
  203.  :Python30
  204.  echo Python 3.0 found
  205.  python.exe my_python_app30.py
  206.  goto :EOF
  207.  
  208. :: Python 3.1
  209.  :Python31
  210.  echo Python 3.1 found
  211.  python.exe my_python_app31.py
  212.  goto :EOF
  213.  
  214. :: Python 3.2
  215.  :Python32
  216.  echo Python 3.2 found
  217.  python.exe my_python_app32.py
  218.  goto :EOF
  219.  
  220. ::EOF
  221. :EOF
  222.  
  223.  
Advertisement
Add Comment
Please, Sign In to add comment