Debian Codegraph @

tests/ansible/lib/modules/test_echo_module.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2012, Michael DeHaan <[email protected]>
5 # (c) 2016, Toshio Kuratomi <[email protected]>
6 # (c) 2020, Steven Robertson <[email protected]>
7 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12 import os
13 import stat
14 import platform
15 import subprocess
16 import sys
17
18 from ansible.module_utils.basic import AnsibleModule
19
20
21 # trace_realpath() and _join_tracepath() adapated from stdlib posixpath.py
22 # https://github.com/python/cpython/blob/v3.12.6/Lib/posixpath.py#L423-L492
23 # Copyright (c) 2001 - 2023 Python Software Foundation
24 # Copyright (c) 2024 Alex Willmer <[email protected]>
25 # License: Python Software Foundation License Version 2
26
27 def trace_realpath(filename, strict=False):
28 """
29 Return the canonical path of the specified filename, and a trace of
30 the route taken, eliminating any symbolic links encountered in the path.
31 """
32 path, trace, ok = _join_tracepath(filename[:0], filename, strict, seen={}, trace=[])
33 return os.path.abspath(path), trace
34
35
36 def _join_tracepath(path, rest, strict, seen, trace):
37 """
38 Join two paths, normalizing and eliminating any symbolic links encountered
39 in the second path.
40 """
41 trace.append(rest)
42 if isinstance(path, bytes):
43 sep = b'/'
44 curdir = b'.'
45 pardir = b'..'
46 else:
47 sep = '/'
48 curdir = '.'
49 pardir = '..'
50
51 if os.path.isabs(rest):
52 rest = rest[1:]
53 path = sep
54
55 while rest:
56 name, _, rest = rest.partition(sep)
57 if not name or name == curdir:
58 # current dir
59 continue
60 if name == pardir:
61 # parent dir
62 if path:
63 path, name = os.path.split(path)
64 if name == pardir:
65 path = os.path.join(path, pardir, pardir)
66 else:
67 path = pardir
68 continue
69 newpath = os.path.join(path, name)
70 try:
71 st = os.lstat(newpath)
72 except OSError:
73 if strict:
74 raise
75 is_link = False
76 else:
77 is_link = stat.S_ISLNK(st.st_mode)
78 if not is_link:
79 path = newpath
80 continue
81 # Resolve the symbolic link
82 if newpath in seen:
83 # Already seen this path
84 path = seen[newpath]
85 if path is not None:
86 # use cached value
87 continue
88 # The symlink is not resolved, so we must have a symlink loop.
89 if strict:
90 # Raise OSError(errno.ELOOP)
91 os.stat(newpath)
92 else:
93 # Return already resolved part + rest of the path unchanged.
94 return os.path.join(newpath, rest), trace, False
95 seen[newpath] = None # not resolved symlink
96 path, trace, ok = _join_tracepath(path, os.readlink(newpath), strict, seen, trace)
97 if not ok:
98 return os.path.join(path, rest), False
99 seen[newpath] = path # resolved symlink
100
101 return path, trace, True
102
103
104 def main():
105 module = AnsibleModule(argument_spec=dict(
106 facts_copy=dict(type=dict, default={}),
107 facts_to_override=dict(type=dict, default={})
108 ))
109
110 # revert the Mitogen OSX tweak since discover_interpreter() doesn't return this info
111 # NB This must be synced with mitogen.parent.Connection.get_boot_command()
112 platform_release_major = int(platform.release().partition('.')[0])
113 if sys.modules.get('mitogen') and sys.platform == 'darwin':
114 if platform_release_major < 19 and sys.executable == '/usr/bin/python2.7':
115 sys.executable = '/usr/bin/python'
116 if platform_release_major in (20, 21) and sys.version_info[:2] == (2, 7):
117 # only for tests to check version of running interpreter -- Mac 10.15+ changed python2
118 # so it looks like it's /usr/bin/python but actually it's /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
119 sys.executable = "/usr/bin/python"
120
121 facts_copy = module.params['facts_copy']
122
123 discovered_interpreter_python = facts_copy['discovered_interpreter_python']
124 d_i_p_realpath, d_i_p_trace = trace_realpath(discovered_interpreter_python)
125 d_i_p_proc = subprocess.Popen(
126 [discovered_interpreter_python, '-c', 'import sys; print(sys.executable)'],
127 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
128
129 )
130 d_i_p_stdout, d_i_p_stderr = d_i_p_proc.communicate()
131
132 sys_exec_realpath, sys_exec_trace = trace_realpath(sys.executable)
133
134 result = {
135 'changed': False,
136 'ansible_facts': module.params['facts_to_override'],
137 'discovered_and_running_samefile': os.path.samefile(
138 os.path.realpath(discovered_interpreter_python),
139 os.path.realpath(sys.executable),
140 ),
141 'discovered_python': {
142 'as_seen': discovered_interpreter_python,
143 'resolved': d_i_p_realpath,
144 'trace': [os.path.abspath(p) for p in d_i_p_trace],
145 'sys': {
146 'executable': {
147 'as_seen': d_i_p_stdout.decode('ascii').rstrip('\n'),
148 'proc': {
149 'stderr': d_i_p_stderr.decode('ascii'),
150 'returncode': d_i_p_proc.returncode,
151 },
152 },
153 },
154 },
155 'running_python': {
156 'platform': {
157 'release': {
158 'major': platform_release_major,
159 },
160 },
161 'sys': {
162 'executable': {
163 'as_seen': sys.executable,
164 'resolved': sys_exec_realpath,
165 'trace': [os.path.abspath(p) for p in sys_exec_trace],
166 },
167 'platform': sys.platform,
168 'version_info': {
169 'major': sys.version_info[0],
170 'minor': sys.version_info[1],
171 },
172 },
173 },
174 }
175
176 module.exit_json(**result)
177
178
179 if __name__ == '__main__':
180 main()