Ensure kitty is on the PATH when running from kitty.app

This commit is contained in:
Kovid Goyal 2017-12-12 21:20:02 +05:30
parent a962a28b36
commit 2e570e21a0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 0 deletions

View File

@ -111,6 +111,12 @@ def main():
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
except Exception: except Exception:
print('Failed to set locale with no LANG, ignoring', file=sys.stderr) print('Failed to set locale with no LANG, ignoring', file=sys.stderr)
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:
os.environ['PATH'] += os.pathsep + rpath
if os.environ.pop('KITTY_LAUNCHED_BY_LAUNCH_SERVICES', if os.environ.pop('KITTY_LAUNCHED_BY_LAUNCH_SERVICES',
None) == '1' and getattr(sys, 'frozen', True): None) == '1' and getattr(sys, 'frozen', True):
os.chdir(os.path.expanduser('~')) os.chdir(os.path.expanduser('~'))

View File

@ -37,6 +37,7 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
int ret = 1; int ret = 1;
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL); wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; } if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; }
PyObject *ed = PyUnicode_FromWideChar(exe_dir, -1);
wchar_t stdlib[PATH_MAX+1] = {0}; wchar_t stdlib[PATH_MAX+1] = {0};
num = swprintf(stdlib, PATH_MAX, L"%ls/../Resources/Python/lib/python%s:%ls/../Resources/Python/lib/python%s/lib-dynload", exe_dir, PYVER, exe_dir, PYVER); num = swprintf(stdlib, PATH_MAX, L"%ls/../Resources/Python/lib/python%s:%ls/../Resources/Python/lib/python%s/lib-dynload", exe_dir, PYVER, exe_dir, PYVER);
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python stdlib\n"); return 1; } if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python stdlib\n"); return 1; }
@ -47,6 +48,7 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
Py_Initialize(); Py_Initialize();
PySys_SetArgvEx(argc - 1, argv + 1, 0); PySys_SetArgvEx(argc - 1, argv + 1, 0);
PySys_SetObject("frozen", Py_True); // dont care if this fails PySys_SetObject("frozen", Py_True); // dont care if this fails
if (ed) { PySys_SetObject("bundle_exe_dir", ed); Py_CLEAR(ed); }
PyObject *kitty = PyUnicode_FromWideChar(stdlib, -1); PyObject *kitty = PyUnicode_FromWideChar(stdlib, -1);
if (kitty == NULL) { fprintf(stderr, "Failed to allocate python kitty lib object\n"); goto end; } if (kitty == NULL) { fprintf(stderr, "Failed to allocate python kitty lib object\n"); goto end; }
PyObject *runpy = PyImport_ImportModule("runpy"); PyObject *runpy = PyImport_ImportModule("runpy");