Advertisement
DevPlayer

MS script; Python version check prelaunch of Python module

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