-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "[execute_process] emulate_tty configurable and defaul…
- Loading branch information
Showing
3 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2019 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Script that demonstrates disabling tty emulation. | ||
This is most significant for python processes which, without tty | ||
emulation, will be buffered by default and have various other | ||
capabilities disabled." | ||
""" | ||
|
||
import os | ||
import sys | ||
from typing import cast | ||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) # noqa | ||
|
||
import launch | ||
|
||
|
||
def generate_launch_description(): | ||
ld = launch.LaunchDescription() | ||
|
||
# Disable tty emulation (on by default). | ||
ld.add_action(launch.actions.SetLaunchConfiguration('emulate_tty', 'false')) | ||
|
||
# Wire up stdout from processes | ||
def on_output(event: launch.Event) -> None: | ||
for line in event.text.decode().splitlines(): | ||
print('[{}] {}'.format( | ||
cast(launch.events.process.ProcessIO, event).process_name, line)) | ||
|
||
ld.add_action(launch.actions.RegisterEventHandler(launch.event_handlers.OnProcessIO( | ||
on_stdout=on_output, | ||
))) | ||
|
||
# Execute | ||
ld.add_action(launch.actions.ExecuteProcess( | ||
cmd=[sys.executable, './counter.py'] | ||
)) | ||
return ld | ||
|
||
|
||
if __name__ == '__main__': | ||
# ls = LaunchService(argv=argv, debug=True) # Use this instead to get more debug messages. | ||
ls = launch.LaunchService(argv=sys.argv[1:]) | ||
ls.include_launch_description(generate_launch_description()) | ||
sys.exit(ls.run()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2019 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests for emulate_tty configuration of ExecuteProcess actions.""" | ||
|
||
import platform | ||
import sys | ||
|
||
import launch | ||
import pytest | ||
|
||
|
||
class OnExit(object): | ||
|
||
def __init__(self): | ||
self.returncode = None | ||
|
||
def handle(self, event, context): | ||
self.returncode = event.returncode | ||
|
||
|
||
def tty_expected_unless_windows(): | ||
return 1 if platform.system() != 'Windows' else 0 | ||
|
||
|
||
@pytest.mark.parametrize('test_input,expected', [ | ||
# use the default defined by ExecuteProcess | ||
(None, tty_expected_unless_windows()), | ||
# redundantly override the default via LaunchConfiguration | ||
('true', tty_expected_unless_windows()), | ||
# override the default via LaunchConfiguration | ||
('false', 0) | ||
]) | ||
def test_emulate_tty(test_input, expected): | ||
on_exit = OnExit() | ||
ld = launch.LaunchDescription() | ||
ld.add_action(launch.actions.ExecuteProcess( | ||
cmd=[sys.executable, | ||
'-c', | ||
'import sys; sys.exit(sys.stdout.isatty())' | ||
] | ||
) | ||
) | ||
if test_input is not None: | ||
ld.add_action( | ||
launch.actions.SetLaunchConfiguration( | ||
'emulate_tty', | ||
test_input | ||
) | ||
) | ||
ld.add_action( | ||
launch.actions.RegisterEventHandler( | ||
launch.event_handlers.OnProcessExit(on_exit=on_exit.handle) | ||
) | ||
) | ||
ls = launch.LaunchService() | ||
ls.include_launch_description(ld) | ||
ls.run() | ||
assert on_exit.returncode == expected |