tiny improvements

This commit is contained in:
Ionuț Ciocîrlan 2020-03-20 19:19:55 +02:00
parent 4a1f13b0c8
commit 0f73184173
1 changed files with 36 additions and 27 deletions

View File

@ -1,46 +1,55 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import math import math
import os import os
import shutil import shutil
import sys import sys
import tempfile import tempfile
from subprocess import call from subprocess import call
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
_is_win = sys.platform == 'win32' _IS_WIN = sys.platform == 'win32'
if not _is_win: if not _IS_WIN:
sys.stdin = open('/dev/tty', 'r') sys.stdin = open('/dev/tty', 'r')
if sys.version_info[0] == 2: if sys.version_info.major == 2:
input = raw_input input = raw_input
prompt = os.environ.get('LEKTOR_SILENT') is None _silent = os.environ.get('LEKTOR_SILENT')
_PROMPT = (
_silent is None
or _silent.lower() in ('', '0', 'off', 'false')
)
def get_confirmation(): def get_confirmation():
if prompt is False: if _PROMPT is False:
return return
while True: while True:
user_input = input('Continue? [Yn] ').lower().strip() user_input = input('Continue? [Yn] ').lower().strip()
if user_input in ('', 'y'): if user_input in ('', 'y'):
print('') print()
return return
if user_input == 'n': if user_input == 'n':
print('') print()
print('Aborted!') print('Aborted!')
sys.exit() sys.exit()
def fail(message): def fail(message):
print('Error: %s' % message) print('Error: %s' % message, file=sys.stderr)
sys.exit(1) sys.exit(1)
def multiprint(*lines): def multiprint(*lines, **kwargs):
for line in lines: for line in lines:
print(line) print(line, **kwargs)
class Installer(object): class Installer(object):
@ -121,7 +130,7 @@ class Installer(object):
pass pass
else: else:
venv = EnvBuilder(with_pip=True, venv = EnvBuilder(with_pip=True,
symlinks=False if _is_win else True) symlinks=False if _IS_WIN else True)
venv.create(target_dir) venv.create(target_dir)
created = True created = True
@ -147,11 +156,6 @@ class Installer(object):
@classmethod @classmethod
def fetch_virtualenv(cls): def fetch_virtualenv(cls):
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
fname = os.path.basename(cls.VIRTUALENV_URL) fname = os.path.basename(cls.VIRTUALENV_URL)
root, ext = os.path.splitext(fname) root, ext = os.path.splitext(fname)
@ -165,20 +169,22 @@ class Installer(object):
@staticmethod @staticmethod
def deletion_error(func, path, excinfo): def deletion_error(func, path, excinfo):
print('Problem deleting {}'.format(path)) multiprint('Problem deleting {}'.format(path),
print('Please try and delete {} manually'.format(path)) 'Please try and delete {} manually'.format(path),
print('Aborted!') 'Aborted!',
file=sys.stderr)
sys.exit(1) sys.exit(1)
_home = os.environ.get('HOME') _HOME = os.environ.get('HOME')
class PosixInstaller(Installer): class PosixInstaller(Installer):
# prefer system bin dirs
KNOWN_BIN_PATHS = ['/usr/local/bin', '/opt/local/bin'] KNOWN_BIN_PATHS = ['/usr/local/bin', '/opt/local/bin']
if _home: # this is always true, but it needs not blow up on windows if _HOME: # true on *nix, but we need it to prevent blowing up on windows
KNOWN_BIN_PATHS.extend([ KNOWN_BIN_PATHS.extend([
os.path.join(_home, '.bin'), os.path.join(_HOME, '.bin'),
os.path.join(_home, '.local', 'bin'), os.path.join(_HOME, '.local', 'bin'),
]) ])
def prompt_installation(self): def prompt_installation(self):
@ -201,6 +207,8 @@ class PosixInstaller(Installer):
and derives the lib dir from it. and derives the lib dir from it.
""" """
# look for writable directories in the user's $PATH
# (that are not sbin)
paths = [ paths = [
item for item in os.environ['PATH'].split(':') item for item in os.environ['PATH'].split(':')
if not item.endswith('/sbin') and os.access(item, os.W_OK) if not item.endswith('/sbin') and os.access(item, os.W_OK)
@ -210,6 +218,8 @@ class PosixInstaller(Installer):
fail('None of the items in $PATH are writable. Run with ' fail('None of the items in $PATH are writable. Run with '
'sudo or add a $PATH item that you have access to.') 'sudo or add a $PATH item that you have access to.')
# ... and prioritize them according to KNOWN_BIN_PATHS.
# this makes sure we perform a system install when possible.
def _sorter(path): def _sorter(path):
try: try:
return self.KNOWN_BIN_PATHS.index(path) return self.KNOWN_BIN_PATHS.index(path)
@ -255,7 +265,6 @@ class PosixInstaller(Installer):
return os.path.join(self.lib_dir, 'bin', 'pip') return os.path.join(self.lib_dir, 'bin', 'pip')
def install_lektor(self): def install_lektor(self):
import pdb
super(PosixInstaller, self).install_lektor() super(PosixInstaller, self).install_lektor()
bin = os.path.join(self.lib_dir, 'bin', 'lektor') bin = os.path.join(self.lib_dir, 'bin', 'lektor')
@ -301,7 +310,7 @@ class WindowsInstaller(Installer):
with open(link, 'w') as link_file: with open(link, 'w') as link_file:
link_file.write('@echo off\n') link_file.write('@echo off\n')
link_file.write('"%s" %%*' % exe) link_file.write('"{}" %*'.format(exe))
self.add_to_path(self.install_dir) self.add_to_path(self.install_dir)
@ -360,7 +369,7 @@ class Progress(object):
if self.started: if self.started:
out.write("\b" * 4) out.write("\b" * 4)
out.write("%3d" % progress + "%") out.write("{:3d}%".format(progress))
out.flush() out.flush()
self.started = True self.started = True
@ -375,7 +384,7 @@ class Progress(object):
self.finish() self.finish()
install = WindowsInstaller if _is_win \ install = WindowsInstaller if _IS_WIN \
else PosixInstaller else PosixInstaller