lektor-website/assets/install.ps1

174 lines
4.7 KiB
PowerShell
Raw Normal View History

2015-12-19 15:03:40 +01:00
$InstallScript = @"
2015-12-19 14:52:17 +01:00
import os
import sys
import json
import tempfile
import tarfile
2016-02-10 19:25:33 +01:00
import shutil
2015-12-19 14:52:17 +01:00
from subprocess import Popen
try: # py3
from urllib.request import urlopen
from winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ
except ImportError: # py2
from urllib import urlopen
from _winreg import OpenKey, CloseKey, QueryValueEx, SetValueEx, \
2015-12-19 14:52:17 +01:00
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ
2015-12-19 14:52:17 +01:00
import ctypes
from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM, LPVOID
2016-02-10 19:25:33 +01:00
2015-12-19 14:52:17 +01:00
2015-12-19 15:03:40 +01:00
VENV_URL = 'https://pypi.python.org/pypi/virtualenv/json'
2015-12-19 14:52:17 +01:00
APPDATA = os.environ['LocalAppData']
APP = 'lektor-cli'
LIB = 'lib'
ROOT_KEY = HKEY_CURRENT_USER
SUB_KEY = 'Environment'
LRESULT = LPARAM
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A
PY2 = sys.version_info[0] == 2
if PY2:
input = raw_input
2016-02-10 19:25:33 +01:00
def get_confirmation():
while 1:
user_input = input('Continue? [Yn] ').lower().strip()
if user_input in ('', 'y'):
2016-02-10 19:25:33 +01:00
break
elif user_input == 'n':
print()
print('Aborted!')
2016-02-10 19:25:33 +01:00
sys.exit()
def find_location():
2016-02-10 19:25:33 +01:00
install_dir = os.path.join(APPDATA, APP)
return install_dir, os.path.join(install_dir, LIB)
2015-12-19 14:52:17 +01:00
2016-02-09 22:36:22 +01:00
def deletion_error(func, path, excinfo):
print('Problem deleting {}'.format(path))
print('Please try and delete {} manually'.format(path))
print('Aborted!')
2016-02-10 19:25:33 +01:00
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()
2016-02-10 19:25:33 +01:00
get_confirmation()
print()
2016-02-10 19:25:33 +01:00
wipe_installation(install_dir)
2016-02-09 22:36:22 +01:00
2015-12-19 14:52:17 +01:00
def fail(message):
print('Error: %s' % message)
2016-02-10 19:25:33 +01:00
sys.exit(1)
2015-12-19 14:52:17 +01:00
def add_to_path(location):
2016-02-10 19:25:33 +01:00
reg_key = OpenKey(ROOT_KEY, SUB_KEY, 0, KEY_ALL_ACCESS)
2015-12-19 14:52:17 +01:00
2016-02-10 19:25:33 +01:00
try:
path_value, _ = QueryValueEx(reg_key, 'Path')
except WindowsError:
path_value = ''
2015-12-19 14:52:17 +01:00
2016-02-10 19:25:33 +01:00
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)
2015-12-19 14:52:17 +01:00
2016-02-10 19:25:33 +01:00
SendMessage = ctypes.windll.user32.SendMessageW
SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID
SendMessage.restype = LRESULT
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')
2015-12-19 14:52:17 +01:00
2018-12-22 19:55:53 +01:00
def _fetch_virtualenv():
for url in json.load(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')
2016-02-10 19:25:33 +01:00
t = tempfile.mkdtemp()
with open(os.path.join(t, 'virtualenv.tar.gz'), 'wb') as f:
download = urlopen(virtualenv_url)
2016-02-10 19:25:33 +01:00
f.write(download.read())
download.close()
with tarfile.open(os.path.join(t, 'virtualenv.tar.gz'), 'r:gz') as tar:
tar.extractall(path=t)
2018-12-22 19:55:53 +01:00
return os.path.join(t, virtualenv_filename)
def install_virtualenv(target_dir):
# recent python versions include virtualenv
cmd = [sys.executable, '-m', 'venv', target_dir]
try:
import venv
except ImportError:
venv_dir = _fetch_virtualenv()
venv_file = os.path.join(venv_dir, 'virtualenv.py')
# in recent versions "virtualenv.py" moved to the "src" subdirectory
if not os.path.exists(venv_file):
venv_file = os.path.join(venv_dir, 'src', 'virtualenv.py')
cmd = [sys.executable, venv_file, target_dir]
Popen(cmd).wait()
def install(install_dir, lib_dir):
2016-02-10 19:25:33 +01:00
os.makedirs(install_dir)
os.makedirs(lib_dir)
2018-12-22 19:55:53 +01:00
install_virtualenv(lib_dir)
2016-02-10 19:25:33 +01:00
scripts = os.path.join(lib_dir, 'Scripts')
Popen([os.path.join(scripts, 'pip.exe'),
'install', '--upgrade', 'Lektor'],
2015-12-19 14:52:17 +01:00
cwd=scripts).wait()
2016-02-10 19:25:33 +01:00
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') + '\"' + ' %*')
2015-12-19 14:52:17 +01:00
2016-02-10 19:25:33 +01:00
add_to_path(install_dir)
2015-12-19 14:52:17 +01:00
def main():
print()
print('Welcome to Lektor')
print()
print('This script will install Lektor on your computer.')
print()
2016-02-10 19:25:33 +01:00
install_dir, lib_dir = find_location()
check_installation(install_dir)
print(' Installing at:')
print(' %s' % install_dir)
print()
2016-02-10 19:25:33 +01:00
get_confirmation()
2018-12-22 19:55:53 +01:00
install(install_dir, lib_dir)
2016-02-10 19:25:33 +01:00
print()
print('All done!')
2015-12-19 14:52:17 +01:00
main()
2015-12-19 15:03:40 +01:00
"@
if (Get-Command python) { python -c $InstallScript } else { "To use this script you need to have Python installed"; exit }