Debian Codegraph @

test/integration/targets/module_utils_common.respawn/library/respawnme.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2021, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 import os
9 import sys
10
11 from ansible.module_utils.basic import AnsibleModule
12 from ansible.module_utils.common.respawn import respawn_module, has_respawned
13
14
15 def main():
16 mod = AnsibleModule(argument_spec=dict(
17 mode=dict(required=True, choices=['multi_respawn', 'no_respawn', 'respawn'])
18 ))
19
20 # just return info about what interpreter we're currently running under
21 if mod.params['mode'] == 'no_respawn':
22 mod.exit_json(interpreter_path=sys.executable)
23 elif mod.params['mode'] == 'respawn':
24 if not has_respawned():
25 new_interpreter = os.path.join(mod.tmpdir, 'anotherpython')
26 os.symlink(sys.executable, new_interpreter)
27 respawn_module(interpreter_path=new_interpreter)
28
29 # respawn should always exit internally- if we continue executing here, it's a bug
30 raise Exception('FAIL, should never reach this line')
31 else:
32 # return the current interpreter, as well as a signal that we created a different one
33 mod.exit_json(created_interpreter=sys.executable, interpreter_path=sys.executable)
34 elif mod.params['mode'] == 'multi_respawn':
35 # blindly respawn with the current interpreter, the second time should bomb
36 respawn_module(sys.executable)
37
38 # shouldn't be any way for us to fall through, but just in case, that's also a bug
39 raise Exception('FAIL, should never reach this code')
40
41
42 if __name__ == '__main__':
43 main()