Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions scripts/launch-app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def launch(identifier, terminal='foot'):
import gi
gi.require_version('Gio', '2.0')
gi.require_version('GioUnix', '2.0')
from gi.repository import Gio, GioUnix
from gi.repository import Gio, GioUnix, GLib
if os.environ.get('MODESTY_PREVIEW') == '1': raise ValueError('Launching is disabled in preview')
env = clean_environment()
os.environ.clear(); os.environ.update(env)
Expand All @@ -19,7 +19,6 @@ def launch(identifier, terminal='foot'):
# desktop entry with an explicit terminal prefix, preserving Exec syntax.
if terminal not in ('foot', 'kitty', 'alacritty', 'wezterm'): raise ValueError('Unsupported terminal')
if not shutil.which(terminal): raise ValueError(terminal+' is not installed. Choose another terminal in Settings.')
from gi.repository import GLib
keyfile = GLib.KeyFile()
keyfile.load_from_file(entry.get_filename(), GLib.KeyFileFlags.NONE)
prefix = {'foot':'foot -e ', 'kitty':'kitty ', 'alacritty':'alacritty -e ', 'wezterm':'wezterm start -- '}[terminal]
Expand All @@ -28,7 +27,11 @@ def launch(identifier, terminal='foot'):
keyfile.set_boolean('Desktop Entry','DBusActivatable',False)
entry = GioUnix.DesktopAppInfo.new_from_keyfile(keyfile)
context = Gio.AppLaunchContext()
if not entry.launch([], context): raise RuntimeError('The application could not be started')
# App output must not retain or corrupt the launcher's JSON reply pipes.
with open(os.devnull, 'r+b', buffering=0) as streams:
if not entry.launch_uris_as_manager_with_fds([], context, GLib.SpawnFlags.SEARCH_PATH,
None, None, None, None, streams.fileno(), streams.fileno(), streams.fileno()):
raise RuntimeError('The application could not be started')
return {'ok': True, 'id': identifier}

def close_app(identifier):
Expand Down
27 changes: 27 additions & 0 deletions tests/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,30 @@
print('PASS desktop launch', 'terminal' if is_terminal else 'GUI', 'quoted args, field codes, working directory, clean environment')
bad=subprocess.run(['python3','scripts/launch-app.py','modesty-definitely-missing'],capture_output=True,text=True);assert bad.returncode and not json.loads(bad.stdout)['ok']
print('PASS missing desktop entry reports error')
release=p/'release';result=p/'stdio.json'
noisy=p/'noisy app.py'
noisy.write_text('''import os,time,sys
from pathlib import Path
release=Path(os.environ['LAUNCH_RELEASE'])
deadline=time.monotonic()+10
while not release.exists() and time.monotonic()<deadline: time.sleep(.01)
print('application stdout',flush=True)
print('application stderr',file=sys.stderr,flush=True)
Path(os.environ['RESULT']).write_text('completed')
''')
(apps/'modesty-launch-stdio.desktop').write_text('[Desktop Entry]\nType=Application\nName=Launch stdio test\nExec=/usr/bin/python3 "'+str(noisy)+'"\nTerminal=false\n')
env=dict(os.environ,XDG_DATA_HOME=str(p),RESULT=str(result),LAUNCH_RELEASE=str(release))
helper=subprocess.Popen(['python3','scripts/launch-app.py','modesty-launch-stdio'],env=env,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
try:
try: response,errors=helper.communicate(timeout=2)
except subprocess.TimeoutExpired:
raise AssertionError('Launched app retains launcher reply pipes; helper cannot finish independently') from None
assert helper.returncode==0 and json.loads(response)['ok'] and not errors,(response,errors)
finally:
release.touch()
helper.communicate(timeout=5)
for _ in range(50):
if result.exists():break
time.sleep(.05)
assert result.read_text()=='completed','Application could not write after helper exit'
print('PASS application streams are independent of launcher JSON and helper lifetime')