-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathpep723.py
More file actions
46 lines (35 loc) · 1.52 KB
/
pep723.py
File metadata and controls
46 lines (35 loc) · 1.52 KB
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
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
import importlib.metadata
import os
import re
import pytest
def has_package_requirements_or_skip(example):
example_name = os.path.basename(example)
with open(example, encoding="utf-8") as f:
content = f.read()
# The canonical regex as defined in PEP 723
pep723 = re.search(r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$", content)
if not pep723:
raise ValueError(f"PEP 723 metadata not found in {example_name}")
metadata = {}
for line in pep723.group("content").splitlines():
line = line.lstrip("# ").rstrip()
if not line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
metadata[key] = value
if "dependencies" not in metadata:
raise ValueError(f"PEP 723 dependencies not found in {example_name}")
missing_dependencies = []
dependencies = eval(metadata["dependencies"]) # noqa: S307
for dependency in dependencies:
name = re.match("[a-zA-Z0-9_-]+", dependency)
try:
importlib.metadata.distribution(name.group(0))
except importlib.metadata.PackageNotFoundError:
missing_dependencies.append(name.string)
if missing_dependencies:
pytest.skip(f"Skipping {example} due to missing package requirement: {', '.join(missing_dependencies)}")