Ensure a kitty executable is on PATH even when running from source

This commit is contained in:
Kovid Goyal 2018-01-05 14:56:01 +05:30
parent 973352a98d
commit 88e9c21a3b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 43 additions and 19 deletions

View File

@ -4,8 +4,6 @@
import sys
first_arg = '' if len(sys.argv) < 2 else sys.argv[1]
def icat(args):
from kitty.icat import main
@ -17,16 +15,22 @@ def list_fonts(args):
main(args)
if first_arg in ('icat', '+icat'):
def main():
first_arg = '' if len(sys.argv) < 2 else sys.argv[1]
if first_arg in ('icat', '+icat'):
icat(sys.argv[1:])
elif first_arg in ('list-fonts', '+list-fonts'):
elif first_arg in ('list-fonts', '+list-fonts'):
list_fonts(sys.argv[1:])
elif first_arg == '+' and len(sys.argv) > 2:
elif first_arg == '+' and len(sys.argv) > 2:
q = sys.argv[2]
if q == 'icat':
icat(sys.argv[2:])
elif q == 'list-fonts':
list_fonts(sys.argv[2:])
else:
else:
from kitty.main import main
main()
if __name__ == '__main__':
main()

View File

@ -109,12 +109,19 @@ def main():
locale.setlocale(locale.LC_ALL, '')
except Exception:
print('Failed to set locale with no LANG, ignoring', file=sys.stderr)
# Ensure kitty is in PATH
rpath = getattr(sys, 'bundle_exe_dir', None)
if rpath:
# Ensure kitty bin directory is in PATH
items = frozenset(os.environ['PATH'].split(os.pathsep))
if rpath not in items:
if not rpath:
for candidate in items:
if os.access(os.path.join(candidate, 'kitty'), os.X_OK):
break
else:
rpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'launcher')
if rpath and rpath not in items:
os.environ['PATH'] += os.pathsep + rpath
if os.environ.pop('KITTY_LAUNCHED_BY_LAUNCH_SERVICES',
None) == '1' and getattr(sys, 'frozen', True):
os.chdir(os.path.expanduser('~'))

13
launcher/kitty Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import sys
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, base)
with open(os.path.join(base, '__main__.py')) as f:
src = f.read()
code = compile(src, f.name, 'exec')
exec(code, {'__name__': '__main__'})