-
Notifications
You must be signed in to change notification settings - Fork 44
/
python.py
157 lines (129 loc) · 5.41 KB
/
python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2016-2019 Dirk Thomas
# Licensed under the Apache License, Version 2.0
import warnings
from colcon_core.package_identification import logger
from colcon_core.package_identification \
import PackageIdentificationExtensionPoint
from colcon_core.plugin_system import satisfies_version
class PythonPackageIdentification(PackageIdentificationExtensionPoint):
"""
Identify Python packages with `setup.cfg` files.
Only packages which pass no arguments (or only a ``cmdclass``) to the
``setup()`` function in their ``setup.py`` file are being considered.
"""
def __init__(self): # noqa: D107
super().__init__()
satisfies_version(
PackageIdentificationExtensionPoint.EXTENSION_POINT_VERSION,
'^1.0')
def identify(self, desc): # noqa: D102
if desc.type is not None and desc.type != 'python':
return
setup_py = desc.path / 'setup.py'
if not setup_py.is_file():
return
setup_cfg = desc.path / 'setup.cfg'
if not setup_cfg.is_file():
return
if not is_reading_cfg_sufficient(setup_py):
logger.debug(
f"Python package in '{desc.path}' passes arguments to the "
'setup() function which requires a different identification '
f"extension than '{self.PACKAGE_IDENTIFICATION_NAME}'")
return
config = get_configuration(setup_cfg)
name = config.get('metadata', {}).get('name')
if not name:
return
desc.type = 'python'
if desc.name is not None and desc.name != name:
msg = 'Package name already set to different value'
logger.error(msg)
raise RuntimeError(msg)
desc.name = name
def is_reading_cfg_sufficient(setup_py):
"""
Check the content of the setup.py file.
If the ``setup()`` function is called with no arguments or only a
``cmdclass`` it is sufficient to only read the content of the ``setup.cfg``
file.
:param setup_py: The path of the setup.py file
:returns: The flag if reading the setup.cfg file is sufficient
:rtype: bool
"""
setup_py_content = setup_py.read_text()
# the setup function must be called with no arguments
# or only a ``cmdclass``to be considered by this extension otherwise
# only reading the content of the setup.cfg file isn't sufficient
return 'setup()' in setup_py_content or \
'setup(cmdclass=cmdclass)' in setup_py_content
def get_configuration(setup_cfg):
"""
Read the setup.cfg file.
:param setup_cfg: The path of the setup.cfg file
:returns: The configuration data
:rtype: dict
"""
try:
# import locally to allow other functions in this module to be usable
try:
from setuptools.config.setupcfg import read_configuration
except ImportError:
from setuptools.config import read_configuration
except ImportError as e:
try:
from importlib.metadata import distribution
except ImportError:
from importlib_metadata import distribution
from packaging.version import Version
try:
setuptools_version = distribution('setuptools').version
except ModuleNotFoundError:
setuptools_version = '0'
minimum_version = '30.3.0'
if Version(setuptools_version) < Version(minimum_version):
e.msg += ', ' \
"'setuptools' needs to be at least version " \
f'{minimum_version}, if a newer version is not available ' \
"from the package manager use 'pip3 install -U setuptools' " \
'to update to the latest version'
raise
return read_configuration(str(setup_cfg))
def extract_dependencies(options):
"""
Get the dependencies of the package.
This function has been depreated, use
``colcon_core.package_augmentation.python.extract_dependencies()``
instead.
:param options: The dictionary from the options section of the setup.cfg
file
:returns: The dependencies
:rtype: dict(string, set(DependencyDescriptor))
"""
warnings.warn(
"'colcon_core.package_identification.python.extract_dependencies()' "
'has been deprecated, use '
"'colcon_core.package_augmentation.python.extract_dependencies()' "
'instead', stacklevel=2)
from colcon_core.package_augmentation.python import \
extract_dependencies as function
return function(options)
def create_dependency_descriptor(requirement_string):
"""
Create a DependencyDescriptor from a PEP440 compliant string.
See https://www.python.org/dev/peps/pep-0440/#version-specifiers
This function has been depreated, use
``colcon_core.package_augmentation.python.create_dependency_descriptor()``
instead.
:param str requirement_string: a PEP440 compliant requirement string
:return: A descriptor with version constraints from the requirement string
:rtype: DependencyDescriptor
"""
warnings.warn(
"'colcon_core.package_identification.python."
"create_dependency_descriptor()' has been deprecated, use "
"'colcon_core.package_augmentation.python."
"create_dependency_descriptor()' instead", stacklevel=2)
from colcon_core.package_augmentation.python import \
create_dependency_descriptor as function
return function(requirement_string)