Debian Codegraph @

lib/ansible/module_utils/facts/system/python.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import sys
19 import typing as t
20
21 from ansible.module_utils.facts.collector import BaseFactCollector
22
23 try:
24 # Check if we have SSLContext support
25 from ssl import create_default_context, SSLContext
26 del create_default_context
27 del SSLContext
28 HAS_SSLCONTEXT = True
29 except ImportError:
30 HAS_SSLCONTEXT = False
31
32
33 class PythonFactCollector(BaseFactCollector):
34 name = 'python'
35 _fact_ids = set() # type: t.Set[str]
36
37 def collect(self, module=None, collected_facts=None):
38 python_facts = {}
39 python_facts['python'] = {
40 'version': {
41 'major': sys.version_info[0],
42 'minor': sys.version_info[1],
43 'micro': sys.version_info[2],
44 'releaselevel': sys.version_info[3],
45 'serial': sys.version_info[4]
46 },
47 'version_info': list(sys.version_info),
48 'executable': sys.executable,
49 'has_sslcontext': HAS_SSLCONTEXT
50 }
51
52 try:
53 python_facts['python']['type'] = sys.subversion[0]
54 except AttributeError:
55 try:
56 python_facts['python']['type'] = sys.implementation.name
57 except AttributeError:
58 python_facts['python']['type'] = None
59
60 return python_facts