Advertisement
here2share

MS Script; Python Version Check Module Prelaunch

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