Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Omnibus tooling to support Python checks defined by a pyproject.toml file #10694

Merged
merged 5 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions omnibus/config/software/datadog-agent-integrations-py2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@
manifest = JSON.parse(File.read(manifest_file_path))
manifest['supported_os'].include?(os) || next

setup_file_path = "#{check_dir}/setup.py"
File.file?(setup_file_path) || next
File.file?("#{check_dir}/setup.py") || File.file?("#{check_dir}/pyproject.toml") || next
# Check if it supports Python 2.
support = `inv agent.check-supports-python-version #{setup_file_path} 2`
support = `inv agent.check-supports-python-version #{check_dir} 2`
if support == "False"
log.info(log_key) { "Skipping '#{check}' since it does not support Python 2." }
next
Expand Down
5 changes: 2 additions & 3 deletions omnibus/config/software/datadog-agent-integrations-py3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,9 @@
manifest = JSON.parse(File.read(manifest_file_path))
manifest['supported_os'].include?(os) || next

setup_file_path = "#{check_dir}/setup.py"
File.file?(setup_file_path) || next
File.file?("#{check_dir}/setup.py") || File.file?("#{check_dir}/pyproject.toml") || next
# Check if it supports Python 3.
support = `inv agent.check-supports-python-version #{setup_file_path} 3`
support = `inv agent.check-supports-python-version #{check_dir} 3`
if support == "False"
log.info(log_key) { "Skipping '#{check}' since it does not support Python 3." }
next
Expand Down
45 changes: 36 additions & 9 deletions tasks/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,23 +627,50 @@ def omnibus_manifest(


@task
def check_supports_python_version(_, filename, python):
def check_supports_python_version(_, check_dir, python):
"""
Check if a setup.py file states support for a given major Python version.
Check if a Python project states support for a given major Python version.
"""
import toml
from packaging.specifiers import SpecifierSet

if python not in ['2', '3']:
raise Exit("invalid Python version", code=2)

with open(filename, 'r') as f:
tree = ast.parse(f.read(), filename=filename)
project_file = os.path.join(check_dir, 'pyproject.toml')
setup_file = os.path.join(check_dir, 'setup.py')
if os.path.isfile(project_file):
with open(project_file, 'r') as f:
data = toml.loads(f.read())

prefix = f'Programming Language :: Python :: {python}'
for node in ast.walk(tree):
if isinstance(node, ast.keyword) and node.arg == "classifiers":
classifiers = ast.literal_eval(node.value)
print(any(cls.startswith(prefix) for cls in classifiers), end="")
project_metadata = data['project']
if 'requires-python' not in project_metadata:
print('True', end='')
return

specifier = SpecifierSet(project_metadata['requires-python'])
# It might be e.g. `>=3.8` which would not immediatelly contain `3`
for minor_version in range(100):
if specifier.contains(f'{python}.{minor_version}'):
print('True', end='')
return
else:
print('False', end='')
elif os.path.isfile(setup_file):
with open(setup_file, 'r') as f:
tree = ast.parse(f.read(), filename=setup_file)

prefix = f'Programming Language :: Python :: {python}'
for node in ast.walk(tree):
if isinstance(node, ast.keyword) and node.arg == 'classifiers':
classifiers = ast.literal_eval(node.value)
print(any(cls.startswith(prefix) for cls in classifiers), end='')
return
else:
print('False', end='')
else:
raise Exit('not a Python project', code=1)


@task
def clean(ctx):
Expand Down