Skip to content

Commit

Permalink
Update try_import to be aware of what package it's running against (#…
Browse files Browse the repository at this point in the history
…17850)

* update try_import script to understand context
  • Loading branch information
scbedd authored Apr 6, 2021
1 parent 0761479 commit 1e3683d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion eng/tox/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ deps =
commands =
- {envbindir}/python -m pip uninstall aiohttp --yes
{envbindir}/python {toxinidir}/../../../eng/tox/create_package_and_install.py -d {envtmpdir} -p {toxinidir} -w {envtmpdir}
{envbindir}/python {toxinidir}/../../../eng/tox/try_import.py aiohttp
{envbindir}/python {toxinidir}/../../../eng/tox/try_import.py aiohttp -p {toxinidir}
{envbindir}/python -m pip freeze
pytest \
{[testenv]default_pytest_params} \
Expand Down
25 changes: 21 additions & 4 deletions eng/tox/try_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# This script is used to verify that packages are not importable. This is especially useful when
# running checks without the presence of "optional" packages like aiohttp.
# This script is used to verify that packages are not importable. This is especially useful when
# running checks without the presence of "optional" packages like aiohttp.


import argparse
import logging
import os
import sys

root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
common_task_path = os.path.abspath(os.path.join(root_dir, "scripts", "devops_tasks"))
sys.path.append(common_task_path)
from common_tasks import parse_setup, get_name_from_specifier

logging.getLogger().setLevel(logging.INFO)

Expand All @@ -27,13 +34,23 @@
help="The set of packages that we shouldn't be able to import.",
)

parser.add_argument(
"-p",
"--path-to-setup",
dest="target_setup",
help="The path to the setup.py (not including the file) for the package that we are running try_import alongside. The key here is that if a package on our 'check' list actually requires something, we will not fail the check.",
)

args = parser.parse_args()

print(args.imports)
acceptable_to_import = []
if args.target_setup:
_, _, _, reqs = parse_setup(args.target_setup)
acceptable_to_import = [get_name_from_specifier(req).strip() for req in reqs]

importable_packages = []

for ns in list(args.imports):
for ns in [ns for ns in list(args.imports) if ns not in acceptable_to_import]:
try:
logging.info("Ensuring that namespace {} is not importable.".format(ns))
exec("import {}".format(ns))
Expand Down
4 changes: 4 additions & 0 deletions scripts/devops_tasks/common_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def parse_setup_requires(setup_path):
return python_requires


def get_name_from_specifier(version):
return re.split(r'[><=]', version)[0]


def filter_for_compatibility(package_set):
collected_packages = []
v = sys.version_info
Expand Down

0 comments on commit 1e3683d

Please sign in to comment.