Wiping previous installs plus confirmation

This commit is contained in:
Elias Zeitfogel 2015-12-24 00:05:41 +01:00
parent b206af8f50
commit 645eb5fa51
1 changed files with 81 additions and 63 deletions

View File

@ -5,6 +5,7 @@ import json
import urllib import urllib
import tempfile import tempfile
import tarfile import tarfile
import shutil
from subprocess import Popen from subprocess import Popen
from _winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \ from _winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ
@ -22,94 +23,111 @@ LRESULT = LPARAM
HWND_BROADCAST = 0xFFFF HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A WM_SETTINGCHANGE = 0x1A
def get_confirmation():
while 1:
input = raw_input('Continue? [Yn] ').lower().strip()
if input in ('', 'y'):
break
elif input == 'n':
print
print 'Aborted!'
sys.exit()
def find_location(): def find_location():
install_dir = os.path.join(APPDATA, APP) install_dir = os.path.join(APPDATA, APP)
if os.path.exists(install_dir) and os.path.isdir(install_dir): return install_dir, os.path.join(install_dir, LIB)
return None, None
return install_dir, os.path.join(install_dir, LIB) def wipe_installation(install_dir):
shutil.rmtree(install_dir, ignore_errors=True)
def check_installation(install_dir):
if os.path.exists(install_dir):
print ' Lektor seems to be installed already.'
print ' Continuing will wipe %s' % install_dir
print
get_confirmation()
print
wipe_installation(install_dir)
def fail(message): def fail(message):
print 'Error: %s' % message print 'Error: %s' % message
sys.exit(1) sys.exit(1)
def add_to_path(location): def add_to_path(location):
reg_key = OpenKey(ROOT_KEY, SUB_KEY, 0, KEY_ALL_ACCESS) reg_key = OpenKey(ROOT_KEY, SUB_KEY, 0, KEY_ALL_ACCESS)
try: try:
path_value, _ = QueryValueEx(reg_key, 'Path') path_value, _ = QueryValueEx(reg_key, 'Path')
except WindowsError: except WindowsError:
path_value = '' path_value = ''
paths = path_value.split(';') paths = path_value.split(';')
if location not in paths: if location not in paths:
paths.append(location) paths.append(location)
path_value = ';'.join(paths) path_value = ';'.join(paths)
SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value) SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value)
SendMessage = ctypes.windll.user32.SendMessageW SendMessage = ctypes.windll.user32.SendMessageW
SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID
SendMessage.restype = LRESULT SendMessage.restype = LRESULT
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')
def install(virtualenv_url, virtualenv_filename, install_dir, lib_dir): def install(virtualenv_url, virtualenv_filename, install_dir, lib_dir):
t = tempfile.mkdtemp() t = tempfile.mkdtemp()
with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f: with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f:
download = urllib.urlopen(virtualenv_url) download = urllib.urlopen(virtualenv_url)
f.write(download.read()) f.write(download.read())
download.close() download.close()
with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar: with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar:
tar.extractall(path=t) tar.extractall(path=t)
os.makedirs(install_dir) os.makedirs(install_dir)
os.makedirs(lib_dir) os.makedirs(lib_dir)
Popen(['python', 'virtualenv.py', lib_dir], Popen(['python', 'virtualenv.py', lib_dir],
cwd=os.path.join(t, virtualenv_filename)).wait() cwd=os.path.join(t, virtualenv_filename)).wait()
scripts = os.path.join(lib_dir, 'Scripts')
#just using pip.exe and cwd will still install globally scripts = os.path.join(lib_dir, 'Scripts')
Popen([os.path.join(scripts, 'pip.exe'), Popen([os.path.join(scripts, 'pip.exe'),
'install', '--upgrade', 'Lektor'], 'install', '--upgrade', 'Lektor'],
cwd=scripts).wait() cwd=scripts).wait()
with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file: with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file:
link_file.write('@echo off\n') link_file.write('@echo off\n')
link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*') link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*')
add_to_path(install_dir) add_to_path(install_dir)
def main(): def main():
print 'Welcome to Lektor' print
print print 'Welcome to Lektor'
print 'This script will install Lektor on your computer.' print
print print 'This script will install Lektor on your computer.'
print
install_dir, lib_dir = find_location() install_dir, lib_dir = find_location()
if install_dir == None:
fail('Lektor seems to be installed already.')
print ' Installing at: %s' % install_dir check_installation(install_dir)
while 1:
input = raw_input('Continue? [Yn] ').lower().strip()
if input in ('', 'y'):
break
elif input == 'n':
print 'Aborted!'
sys.exit()
for url in json.load(urllib.urlopen(VENV_URL))['urls']: print ' Installing at: %s' % install_dir
if url['python_version'] == 'source': print
virtualenv_url = url['url'] get_confirmation()
#stripping '.tar.gz'
virtualenv_filename = url['filename'][:-7]
break
else:
fail('Could not find virtualenv')
install(virtualenv_url, virtualenv_filename, install_dir, lib_dir) for url in json.load(urllib.urlopen(VENV_URL))['urls']:
if url['python_version'] == 'source':
virtualenv_url = url['url']
#stripping '.tar.gz'
virtualenv_filename = url['filename'][:-7]
break
else:
fail('Could not find virtualenv')
print install(virtualenv_url, virtualenv_filename, install_dir, lib_dir)
print 'All done!'
print
print 'All done!'
main() main()
"@ "@