From 645eb5fa519c88dd6d51162fbceeef9a5de1e4d3 Mon Sep 17 00:00:00 2001 From: Elias Zeitfogel Date: Thu, 24 Dec 2015 00:05:41 +0100 Subject: [PATCH 1/3] Wiping previous installs plus confirmation --- assets/install.ps1 | 144 +++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 63 deletions(-) diff --git a/assets/install.ps1 b/assets/install.ps1 index 15715997..240409b0 100644 --- a/assets/install.ps1 +++ b/assets/install.ps1 @@ -5,6 +5,7 @@ import json import urllib import tempfile import tarfile +import shutil from subprocess import Popen from _winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \ HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ @@ -22,94 +23,111 @@ LRESULT = LPARAM HWND_BROADCAST = 0xFFFF 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(): - install_dir = os.path.join(APPDATA, APP) - if os.path.exists(install_dir) and os.path.isdir(install_dir): - return None, None - return install_dir, os.path.join(install_dir, LIB) + install_dir = os.path.join(APPDATA, APP) + 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): - print 'Error: %s' % message - sys.exit(1) + print 'Error: %s' % message + sys.exit(1) 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: - path_value, _ = QueryValueEx(reg_key, 'Path') - except WindowsError: - path_value = '' + try: + path_value, _ = QueryValueEx(reg_key, 'Path') + except WindowsError: + path_value = '' - paths = path_value.split(';') - if location not in paths: - paths.append(location) - path_value = ';'.join(paths) - SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value) + paths = path_value.split(';') + if location not in paths: + paths.append(location) + path_value = ';'.join(paths) + SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value) - SendMessage = ctypes.windll.user32.SendMessageW - SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID - SendMessage.restype = LRESULT - SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') + SendMessage = ctypes.windll.user32.SendMessageW + SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID + SendMessage.restype = LRESULT + SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') def install(virtualenv_url, virtualenv_filename, install_dir, lib_dir): - t = tempfile.mkdtemp() - with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f: - download = urllib.urlopen(virtualenv_url) - f.write(download.read()) - download.close() - with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar: - tar.extractall(path=t) + t = tempfile.mkdtemp() + with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f: + download = urllib.urlopen(virtualenv_url) + f.write(download.read()) + download.close() + with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar: + tar.extractall(path=t) - os.makedirs(install_dir) - os.makedirs(lib_dir) + os.makedirs(install_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() - scripts = os.path.join(lib_dir, 'Scripts') - #just using pip.exe and cwd will still install globally - Popen([os.path.join(scripts, 'pip.exe'), + + scripts = os.path.join(lib_dir, 'Scripts') + Popen([os.path.join(scripts, 'pip.exe'), 'install', '--upgrade', 'Lektor'], cwd=scripts).wait() - with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file: - link_file.write('@echo off\n') - link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*') + with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file: + link_file.write('@echo off\n') + link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*') - add_to_path(install_dir) + add_to_path(install_dir) def main(): - print 'Welcome to Lektor' - print - print 'This script will install Lektor on your computer.' - print + print + print 'Welcome to Lektor' + print + print 'This script will install Lektor on your computer.' + print - install_dir, lib_dir = find_location() - if install_dir == None: - fail('Lektor seems to be installed already.') + install_dir, lib_dir = find_location() - print ' Installing at: %s' % install_dir - while 1: - input = raw_input('Continue? [Yn] ').lower().strip() - if input in ('', 'y'): - break - elif input == 'n': - print 'Aborted!' - sys.exit() + check_installation(install_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 ' Installing at: %s' % install_dir + print + get_confirmation() - 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 - print 'All done!' + install(virtualenv_url, virtualenv_filename, install_dir, lib_dir) + + print + print 'All done!' main() "@ From 6e8ffd93293ceaa3224071aa0efc456be8182543 Mon Sep 17 00:00:00 2001 From: Elias Zeitfogel Date: Wed, 10 Feb 2016 19:25:33 +0100 Subject: [PATCH 2/3] Same dialog is unix installation --- assets/install.ps1 | 169 +++++++++++++++++++++++---------------------- 1 file changed, 85 insertions(+), 84 deletions(-) diff --git a/assets/install.ps1 b/assets/install.ps1 index bad93a77..ddb65977 100644 --- a/assets/install.ps1 +++ b/assets/install.ps1 @@ -5,12 +5,13 @@ import json import urllib import tempfile import tarfile +import shutil from subprocess import Popen from _winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \ HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ import ctypes from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM, LPVOID -import shutil + VENV_URL = 'https://pypi.python.org/pypi/virtualenv/json' APPDATA = os.environ['LocalAppData'] @@ -23,118 +24,118 @@ HWND_BROADCAST = 0xFFFF WM_SETTINGCHANGE = 0x1A -def get_location(): - install_dir = os.path.join(APPDATA, APP) - return install_dir +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(): - install_dir = get_location() - if os.path.exists(install_dir) and os.path.isdir(install_dir): - return None, None - return install_dir, os.path.join(install_dir, LIB) + install_dir = os.path.join(APPDATA, APP) + return install_dir, os.path.join(install_dir, LIB) def deletion_error(func, path, excinfo): - print 'Problem deleting {}'.format(path) - print 'Please try and delete {} manually'.format(path) - print 'Aborted!' - sys.exit() + print 'Problem deleting {}'.format(path) + print 'Please try and delete {} manually'.format(path) + print 'Aborted!' + sys.exit() + +def wipe_installation(install_dir): + shutil.rmtree(install_dir, onerror=deletion_error) + +def check_installation(install_dir): + if os.path.exists(install_dir): + print ' Lektor seems to be installed already.' + print ' Continuing will delete: + print ' %s' % install_dir + print + get_confirmation() + print + wipe_installation(install_dir) def fail(message): - print 'Error: %s' % message - sys.exit(1) + print 'Error: %s' % message + sys.exit(1) 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: - path_value, _ = QueryValueEx(reg_key, 'Path') - except WindowsError: - path_value = '' + try: + path_value, _ = QueryValueEx(reg_key, 'Path') + except WindowsError: + path_value = '' - paths = path_value.split(';') - if location not in paths: - paths.append(location) - path_value = ';'.join(paths) - SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value) + paths = path_value.split(';') + if location not in paths: + paths.append(location) + path_value = ';'.join(paths) + SetValueEx(reg_key, 'Path', 0, REG_EXPAND_SZ, path_value) - SendMessage = ctypes.windll.user32.SendMessageW - SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID - SendMessage.restype = LRESULT - SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') + SendMessage = ctypes.windll.user32.SendMessageW + SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID + SendMessage.restype = LRESULT + SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') def install(virtualenv_url, virtualenv_filename, install_dir, lib_dir): - t = tempfile.mkdtemp() - with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f: - download = urllib.urlopen(virtualenv_url) - f.write(download.read()) - download.close() - with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar: - tar.extractall(path=t) + t = tempfile.mkdtemp() + with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f: + download = urllib.urlopen(virtualenv_url) + f.write(download.read()) + download.close() + with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar: + tar.extractall(path=t) - os.makedirs(install_dir) - os.makedirs(lib_dir) + os.makedirs(install_dir) + os.makedirs(lib_dir) - Popen(['python', 'virtualenv.py', lib_dir], + Popen([sys.executable, 'virtualenv.py', lib_dir], 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 - Popen([os.path.join(scripts, 'pip.exe'), + + scripts = os.path.join(lib_dir, 'Scripts') + Popen([os.path.join(scripts, 'pip.exe'), 'install', '--upgrade', 'Lektor'], cwd=scripts).wait() - with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file: - link_file.write('@echo off\n') - link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*') + with open(os.path.join(install_dir, 'lektor.cmd'), 'w') as link_file: + link_file.write('@echo off\n') + link_file.write('\"' + os.path.join(scripts, 'lektor.exe') + '\"' + ' %*') - add_to_path(install_dir) + add_to_path(install_dir) def main(): - print 'Welcome to Lektor' - print - print 'This script will install Lektor on your computer.' - print + print + print 'Welcome to Lektor' + print + print 'This script will install Lektor on your computer.' + print - install_dir, lib_dir = find_location() - if install_dir == None: - print ' Lektor seems to be already installed at:' - print ' {}'.format(get_location()) - while 1: - input = raw_input( - 'Delete existing and reinstall? [Yn]' - ).lower().strip() + install_dir, lib_dir = find_location() - if input in ('', 'y'): - shutil.rmtree(get_location(), onerror=deletion_error) - break - elif input == 'n': - print 'Aborted!' - sys.exit() + check_installation(install_dir) - install_dir, lib_dir = find_location() + print ' Installing at:' + print ' %s' % install_dir + print + get_confirmation() - print ' Installing at: %s' % 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']: + if url['python_version'] == 'source': + virtualenv_url = url['url'] + #stripping '.tar.gz' + virtualenv_filename = url['filename'][:-7] + break + else: + fail('Could not find virtualenv') - 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') + install(virtualenv_url, virtualenv_filename, install_dir, lib_dir) - install(virtualenv_url, virtualenv_filename, install_dir, lib_dir) - - print - print 'All done!' + print + print 'All done!' main() "@ From 718ea1b6b0d9caf86fe1e19a1c9eb35ee157fe1f Mon Sep 17 00:00:00 2001 From: Elias Zeitfogel Date: Wed, 10 Feb 2016 19:31:53 +0100 Subject: [PATCH 3/3] Fix prints --- assets/install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/install.ps1 b/assets/install.ps1 index ddb65977..af2e115e 100644 --- a/assets/install.ps1 +++ b/assets/install.ps1 @@ -50,7 +50,7 @@ def wipe_installation(install_dir): def check_installation(install_dir): if os.path.exists(install_dir): print ' Lektor seems to be installed already.' - print ' Continuing will delete: + print ' Continuing will delete:' print ' %s' % install_dir print get_confirmation()