kitty/update-on-ubuntu
Luflosi 2b095f720e
Use "with suppress()" to suppress python exceptions
Using
```Python
with suppress(OSError):
    os.remove('somefile.tmp')
```
instead of
```Python
try:
    os.remove('somefile.tmp')
except OSError:
    pass
```
makes the code more compact and more readable IMO.

This pattern was recommended by Raymond Hettinger, a Python Core
Developer in his talk "Transforming Code into Beautiful, Idiomatic Python" at https://www.youtube.com/watch?v=OSGv2VnC0go. The transcript is available at https://github.com/JeffPaine/beautiful_idiomatic_python
2019-06-03 12:27:43 +02:00

56 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
from contextlib import suppress
if False:
tarball = sys.argv[-1]
dest = os.path.expanduser('~/.local/kitty.app')
shutil.rmtree(dest, ignore_errors=True)
os.makedirs(dest)
os.chdir(dest)
dest = os.path.expanduser('~/.local/bin/kitty')
with suppress(EnvironmentError):
os.remove(dest)
with suppress(EnvironmentError):
os.makedirs(os.path.dirname(dest))
subprocess.check_call(['tar', 'xJf', tarball])
os.symlink(os.path.abspath('bin/kitty'), dest)
print('kitty installed to ~/.local/kitty.app')
# EOF_REMOTE
HOST = 'ubuntu'
base = os.path.dirname(os.path.abspath(__file__))
if True:
sys.path.insert(0, base)
from kitty.constants import str_version
tarball = f'kitty-{str_version}-x86_64.txz'
def run(what):
ret = subprocess.run(shlex.split(what))
if ret.returncode != 0:
raise SystemExit(ret.returncode)
script = open(__file__, 'rb').read().decode('utf-8')
script = script[:script.find('# EOF_REMOTE')].replace('if False:', 'if True:', 1)
os.chdir(os.path.expanduser('~/work/build-kitty'))
with tempfile.NamedTemporaryFile(prefix='install-tarball-', suffix='.py') as f:
run('./linux 64 kitty --debug-build --compression-level=1')
f.write(script.encode('utf-8'))
f.flush()
run(f'scp build/linux/64/sw/dist/{tarball} {f.name} {HOST}:/tmp')
run(f'ssh {HOST} python /tmp/{os.path.basename(f.name)} /tmp/{tarball}')