cosmetics (blackify)

This commit is contained in:
Ionuț Ciocîrlan 2020-03-20 19:31:53 +02:00
parent 0f73184173
commit 72c5850f78
1 changed files with 98 additions and 79 deletions

View File

@ -13,55 +13,60 @@ except ImportError:
from urllib import urlretrieve 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.major == 2: if sys.version_info.major == 2:
input = raw_input input = raw_input
_silent = os.environ.get('LEKTOR_SILENT') _silent = os.environ.get("LEKTOR_SILENT")
_PROMPT = ( _PROMPT = (
_silent is None _silent is None
or _silent.lower() in ('', '0', 'off', 'false') 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, file=sys.stderr) print("Error: %s" % message, file=sys.stderr)
sys.exit(1) sys.exit(1)
def multiprint(*lines, **kwargs): def multiprint(*lines, **kwargs):
for line in lines: for line in lines:
print(line, **kwargs) print(line, **kwargs)
class Installer(object): class Installer(object):
APP_NAME = 'lektor' APP_NAME = "lektor"
VIRTUALENV_URL = 'https://bootstrap.pypa.io/virtualenv.pyz' VIRTUALENV_URL = "https://bootstrap.pypa.io/virtualenv.pyz"
def __init__(self): def __init__(self):
multiprint('', multiprint(
'Welcome to Lektor', "",
'', "Welcome to Lektor",
'This script will install Lektor on your computer.', "",
'') "This script will install Lektor on your computer.",
"",
)
self.compute_location() self.compute_location()
@ -77,8 +82,10 @@ class Installer(object):
self.create_virtualenv() self.create_virtualenv()
self.install_lektor() self.install_lektor()
multiprint('', multiprint(
'All done!') "",
"All done!",
)
def compute_location(self): def compute_location(self):
# this method must set self.lib_dir # this method must set self.lib_dir
@ -100,7 +107,7 @@ class Installer(object):
self.mkvirtualenv(self.lib_dir) self.mkvirtualenv(self.lib_dir)
def install_lektor(self): def install_lektor(self):
call([self.get_pip(), 'install', '--upgrade', 'Lektor']) call([self.get_pip(), "install", "--upgrade", "Lektor"])
def get_pip(self): def get_pip(self):
raise NotImplementedError() raise NotImplementedError()
@ -142,7 +149,7 @@ class Installer(object):
venv_exec = which("virtualenv") venv_exec = which("virtualenv")
if venv_exec: if venv_exec:
retval = call([venv_exec, '-p', sys.executable, target_dir]) retval = call([venv_exec, "-p", sys.executable, target_dir])
if retval: if retval:
sys.exit(1) sys.exit(1)
created = True created = True
@ -159,7 +166,7 @@ class Installer(object):
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)
zipapp = tempfile.mktemp(prefix=root+"-", suffix=ext) zipapp = tempfile.mktemp(prefix=root + "-", suffix=ext)
with Progress() as hook: with Progress() as hook:
sys.stdout.write("Downloading virtualenv: ") sys.stdout.write("Downloading virtualenv: ")
@ -169,37 +176,43 @@ class Installer(object):
@staticmethod @staticmethod
def deletion_error(func, path, excinfo): def deletion_error(func, path, excinfo):
multiprint('Problem deleting {}'.format(path), multiprint(
'Please try and delete {} manually'.format(path), "Problem deleting {}".format(path),
'Aborted!', "Please try and delete {} manually".format(path),
file=sys.stderr) "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 # prefer system bin dirs
KNOWN_BIN_PATHS = ['/usr/local/bin', '/opt/local/bin'] KNOWN_BIN_PATHS = ["/usr/local/bin", "/opt/local/bin"]
if _HOME: # true on *nix, but we need it to prevent blowing 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):
multiprint('Installing at:', multiprint(
' bin: %s' % self.bin_dir, "Installing at:",
' app: %s' % self.lib_dir, " bin: %s" % self.bin_dir,
'') " app: %s" % self.lib_dir,
"",
)
def prompt_wipe(self): def prompt_wipe(self):
multiprint('Lektor seems to be installed already.', multiprint(
'Continuing will delete:', "Lektor seems to be installed already.",
' %s' % self.lib_dir, "Continuing will delete:",
'and remove this symlink:', " %s" % self.lib_dir,
' %s' % self.symlink_path, "and remove this symlink:",
'') " %s" % self.symlink_path,
"",
)
def compute_location(self): def compute_location(self):
""" """
@ -210,13 +223,16 @@ class PosixInstaller(Installer):
# look for writable directories in the user's $PATH # look for writable directories in the user's $PATH
# (that are not sbin) # (that are not sbin)
paths = [ paths = [
item for item in os.environ['PATH'].split(':') item
if not item.endswith('/sbin') and os.access(item, os.W_OK) for item in os.environ["PATH"].split(":")
if not item.endswith("/sbin") and os.access(item, os.W_OK)
] ]
if not paths: if not paths:
fail('None of the items in $PATH are writable. Run with ' fail(
'sudo or add a $PATH item that you have access to.') "None of the items in $PATH are writable. Run with "
"sudo or add a $PATH item that you have access to."
)
# ... and prioritize them according to KNOWN_BIN_PATHS. # ... and prioritize them according to KNOWN_BIN_PATHS.
# this makes sure we perform a system install when possible. # this makes sure we perform a system install when possible.
@ -224,25 +240,24 @@ class PosixInstaller(Installer):
try: try:
return self.KNOWN_BIN_PATHS.index(path) return self.KNOWN_BIN_PATHS.index(path)
except ValueError: except ValueError:
return float('inf') return float("inf")
paths.sort(key=_sorter) paths.sort(key=_sorter)
lib_dir = None lib_dir = None
home = os.environ['HOME']
for path in paths: for path in paths:
if path.startswith(home): if path.startswith(_HOME):
lib_dir = os.path.join(home, '.local', 'lib', self.APP_NAME) lib_dir = os.path.join(_HOME, ".local", "lib", self.APP_NAME)
break break
if path.endswith('/bin'): if path.endswith("/bin"):
parent = os.path.dirname(path) parent = os.path.dirname(path)
lib_dir = os.path.join(parent, 'lib', self.APP_NAME) lib_dir = os.path.join(parent, "lib", self.APP_NAME)
break break
if lib_dir is None: if lib_dir is None:
fail('Could not determine installation location for Lektor.') fail("Could not determine installation location for Lektor.")
self.bin_dir = path self.bin_dir = path
self.lib_dir = lib_dir self.lib_dir = lib_dir
@ -262,32 +277,36 @@ class PosixInstaller(Installer):
shutil.rmtree(self.lib_dir, onerror=self.deletion_error) shutil.rmtree(self.lib_dir, onerror=self.deletion_error)
def get_pip(self): def get_pip(self):
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):
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")
os.symlink(bin, self.symlink_path) os.symlink(bin, self.symlink_path)
class WindowsInstaller(Installer): class WindowsInstaller(Installer):
APP_NAME = 'lektor-cli' # backwards-compatibility with previous installer APP_NAME = "lektor-cli" # backwards-compatibility with previous installer
LIB_DIR = 'lib' LIB_DIR = "lib"
def prompt_installation(self): def prompt_installation(self):
multiprint('Installing at:', multiprint(
' %s' % self.install_dir, "Installing at:",
'') " %s" % self.install_dir,
"",
)
def prompt_wipe(self): def prompt_wipe(self):
multiprint('Lektor seems to be installed already.', multiprint(
'Continuing will delete:', "Lektor seems to be installed already.",
' %s' % self.install_dir, "Continuing will delete:",
'') " %s" % self.install_dir,
"",
)
def compute_location(self): def compute_location(self):
install_dir = os.path.join(os.environ['LocalAppData'], self.APP_NAME) install_dir = os.path.join(os.environ["LocalAppData"], self.APP_NAME)
lib_dir = os.path.join(install_dir, self.LIB_DIR) lib_dir = os.path.join(install_dir, self.LIB_DIR)
self.install_dir = install_dir self.install_dir = install_dir
@ -300,16 +319,16 @@ class WindowsInstaller(Installer):
shutil.rmtree(self.install_dir, onerror=self.deletion_error) shutil.rmtree(self.install_dir, onerror=self.deletion_error)
def get_pip(self): def get_pip(self):
return os.path.join(self.lib_dir, 'Scripts', 'pip.exe') return os.path.join(self.lib_dir, "Scripts", "pip.exe")
def install_lektor(self): def install_lektor(self):
super(WindowsInstaller, self).install_lektor() super(WindowsInstaller, self).install_lektor()
exe = os.path.join(self.lib_dir, 'Scripts', 'lektor.exe') exe = os.path.join(self.lib_dir, "Scripts", "lektor.exe")
link = os.path.join(self.install_dir, 'lektor.cmd') link = os.path.join(self.install_dir, "lektor.cmd")
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('"{}" %*'.format(exe)) link_file.write('"{}" %*'.format(exe))
self.add_to_path(self.install_dir) self.add_to_path(self.install_dir)
@ -321,7 +340,7 @@ class WindowsInstaller(Installer):
OpenKey, CloseKey, QueryValueEx, SetValueEx, OpenKey, CloseKey, QueryValueEx, SetValueEx,
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ, HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ,
) )
except ImportError: # py2 except ImportError: # py2
from _winreg import ( from _winreg import (
OpenKey, CloseKey, QueryValueEx, SetValueEx, OpenKey, CloseKey, QueryValueEx, SetValueEx,
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ, HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_EXPAND_SZ,
@ -332,23 +351,23 @@ class WindowsInstaller(Installer):
HWND_BROADCAST = 0xFFFF HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A WM_SETTINGCHANGE = 0x1A
reg_key = OpenKey(HKEY_CURRENT_USER, 'Environment', 0, KEY_ALL_ACCESS) reg_key = OpenKey(HKEY_CURRENT_USER, "Environment", 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 = LPARAM SendMessage.restype = LPARAM
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment') SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u"Environment")
class Progress(object): class Progress(object):
@ -388,5 +407,5 @@ install = WindowsInstaller if _IS_WIN \
else PosixInstaller else PosixInstaller
if __name__ == '__main__': if __name__ == "__main__":
install() install()