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 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
enhancements:
- |
Update Omnibus tooling to support Python checks defined by
a ``pyproject.toml`` file.
ofek marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ docker-squash==1.0.9
requests==2.27.1
PyYAML==6.0
toml==0.10.2
packaging==21.3
40 changes: 31 additions & 9 deletions tasks/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import sys
from distutils.dir_util import copy_tree

import toml
from invoke import task
from invoke.exceptions import Exit, ParseError
from packaging.specifiers import SpecifierSet

from .build_tags import filter_incompatible_tags, get_build_tags, get_default_build_tags
from .docker import pull_base_images
Expand Down Expand Up @@ -627,23 +629,43 @@ 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.
"""
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:
return
ofek marked this conversation as resolved.
Show resolved Hide resolved

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}'):
break
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:
raise Exit("not a Python project", code=1)


@task
def clean(ctx):
Expand Down