|
| 1 | +import glob |
| 2 | +import os |
| 3 | +from sys import platform |
| 4 | + |
| 5 | +def win_del_old_inst(): |
| 6 | + if not platform.startswith('win'): |
| 7 | + return False |
| 8 | + |
| 9 | + path = os.getenv('PROGRAMFILES') |
| 10 | + |
| 11 | + out = [] |
| 12 | + pdirs = [f for f in glob.glob(path + "**/Python*", recursive=False)] |
| 13 | + |
| 14 | + for f in pdirs: |
| 15 | + # print('Searching in "' + f + '"') |
| 16 | + exe = os.path.join(f, 'Scripts', 'pyradio.exe') |
| 17 | + # # print(" EXE: {0} - {1}".format(exe, 'exists' if os.path.exists(exe) else 'not found')) |
| 18 | + if os.path.exists(exe): |
| 19 | + # print(' Found "' + exe + '"') |
| 20 | + out.append('DEL "' + exe + '"\n') |
| 21 | + site_packages = os.path.join(f, 'Lib', 'site-packages') |
| 22 | + if os.path.exists(site_packages): |
| 23 | + # # print('site-packages: "{}"'.format(site_packages)) |
| 24 | + pydir = [f for f in glob.glob(site_packages + "**/pyradio*.egg", recursive=False)] |
| 25 | + for n in pydir: |
| 26 | + # print(' Found "' + n + '"') |
| 27 | + out.append('RD /Q /S "' + n + '"\n') |
| 28 | + # print(pydir) |
| 29 | + # print('') |
| 30 | + if out: |
| 31 | + TARGET = os.path.join(os.getenv('USERPROFILE'), 'tmp-pyradio') |
| 32 | + if not os.path.exists(TARGET): |
| 33 | + os.makedirs(TARGET) |
| 34 | + |
| 35 | + msg = '''@ECHO OFF |
| 36 | +SETLOCAL EnableDelayedExpansion |
| 37 | +
|
| 38 | +IF EXIST DEV (SET NO_DEV=0) ELSE (SET NO_DEV=1) |
| 39 | +
|
| 40 | +::net file to test privileges, 1>NUL redirects output, 2>NUL redirects errors |
| 41 | +:: https://gist.github.com/neremin/3a4020c172053d837ab37945d81986f6 |
| 42 | +:: https://stackoverflow.com/questions/13212033/get-windows-version-in-a-batch-file |
| 43 | +net session >nul 2>&1 |
| 44 | +IF "%errorlevel%" == "0" ( GOTO START ) ELSE ( |
| 45 | + FOR /f "tokens=4-5 delims=. " %%i in ('ver') do SET VERSION=%%i.%%j |
| 46 | + IF "%version%" == "6.1" ( GOTO win7exit ) |
| 47 | + IF "%version%" == "6.0" ( GOTO win7exit ) |
| 48 | + GOTO getPrivileges |
| 49 | +) |
| 50 | +
|
| 51 | +:win7exit |
| 52 | +ECHO. |
| 53 | +ECHO Error: You must have Administrative permissions to run this script. |
| 54 | +ECHO Please start cmd "As Administrator". |
| 55 | +ECHO. |
| 56 | +ECHO If that does not work, ask the system administrator to |
| 57 | +ECHO install PyRadio for you. |
| 58 | +ECHO. |
| 59 | +GOTO endnopause |
| 60 | +
|
| 61 | +:getPrivileges |
| 62 | +IF "%version%" == "6.1" ( GOTO win7exit ) |
| 63 | +IF "%version%" == "6.0" ( GOTO win7exit ) |
| 64 | +
|
| 65 | +IF '%1'=='ELEV' ( GOTO START ) ELSE ( ECHO Running elevated in a different window) |
| 66 | +
|
| 67 | +SET "batchPath=%~f0" |
| 68 | +SET "batchArgs=ELEV" |
| 69 | +
|
| 70 | +::Add quotes to the batch path, IF needed |
| 71 | +SET "script=%0" |
| 72 | +SET script=%script:"=% |
| 73 | +IF '%0'=='!script!' ( GOTO PathQuotesDone ) |
| 74 | + SET "batchPath=""%batchPath%""" |
| 75 | +:PathQuotesDone |
| 76 | +
|
| 77 | +::Add quotes to the arguments, IF needed. |
| 78 | +:ArgLoop |
| 79 | +IF '%1'=='' ( GOTO EndArgLoop ) ELSE ( GOTO AddArg ) |
| 80 | + :AddArg |
| 81 | + SET "arg=%1" |
| 82 | + SET arg=%arg:"=% |
| 83 | + IF '%1'=='!arg!' ( GOTO NoQuotes ) |
| 84 | + SET "batchArgs=%batchArgs% "%1"" |
| 85 | + GOTO QuotesDone |
| 86 | + :NoQuotes |
| 87 | + SET "batchArgs=%batchArgs% %1" |
| 88 | + :QuotesDone |
| 89 | + SHIFT |
| 90 | + GOTO ArgLoop |
| 91 | +:EndArgLoop |
| 92 | +
|
| 93 | +::Create and run the vb script to elevate the batch file |
| 94 | +ECHO SET UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" |
| 95 | +ECHO UAC.ShellExecute "cmd", "/c ""!batchPath! !batchArgs!""", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" |
| 96 | +"%temp%\OEgetPrivileges.vbs" |
| 97 | +EXIT /B |
| 98 | +
|
| 99 | +:START |
| 100 | +::Remove the elevation tag and SET the correct working directory |
| 101 | +IF '%1'=='ELEV' ( SHIFT /1 ) |
| 102 | +CD /d %~dp0 |
| 103 | +
|
| 104 | +::Do your adminy thing here... |
| 105 | +
|
| 106 | +:: RD /Q /S %USERPROFILE%\tmp-pyradio |
| 107 | +:: MKDIR %USERPROFILE%\tmp-pyradio |
| 108 | +:: START %USERPROFILE%\tmp-pyradio |
| 109 | +
|
| 110 | +
|
| 111 | +''' |
| 112 | + TARGET_FILE = os.path.join(TARGET, 'RunMe.bat') |
| 113 | + with open(TARGET_FILE, 'w') as ofile: |
| 114 | + ofile.write(msg) |
| 115 | + ofile.write('\nECHO Looking for old style installation files\n') |
| 116 | + for n in out: |
| 117 | + ofile.write('ECHO found ' + n.replace('RD /Q /S ', '').replace('DEL ', '').replace('\n', '') + ' - removing it...\n') |
| 118 | + # enable this one to not delete any files |
| 119 | + # ofile.write(':: ' + n) |
| 120 | + # enable this one to actually delete files |
| 121 | + ofile.write(n) |
| 122 | + ofile.write('ECHO.\n') |
| 123 | + ofile.write('ECHO Old style installation files removed...\n') |
| 124 | + ofile.write('ECHO.\n') |
| 125 | + ofile.write('PAUSE\n') |
| 126 | + |
| 127 | + os.startfile(TARGET_FILE) |
| 128 | + return True |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + if not win_del_old_inst(): |
| 132 | + print('This is a Windows only program!') |
| 133 | + |
0 commit comments