File size: 11,647 Bytes
e98c0d7 | 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | import glob
import io
import json
import os.path
import re
import configparser
import setuptools
import pip._internal.req.req_file
from pip._internal.network.session import PipSession
from pip._internal.req.constructors import (
install_req_from_line,
install_req_from_parsed_requirement,
)
from packaging.requirements import InvalidRequirement, Requirement
# TODO: Replace 3p package `tomli` with 3.11's new stdlib `tomllib` once we
# drop support for Python 3.10.
import tomli
# Inspired by pips internal check:
# https://github.com/pypa/pip/blob/0bb3ac87f5bb149bd75cceac000844128b574385/src/pip/_internal/req/req_file.py#L35
COMMENT_RE = re.compile(r'(^|\s+)#.*$')
def parse_pep621_pep735_dependencies(pyproject_path):
with open(pyproject_path, "rb") as file:
project_toml = tomli.load(file)
def version_from_req(specifier_set):
if (len(specifier_set) == 1 and
next(iter(specifier_set)).operator in {"==", "==="}):
return next(iter(specifier_set)).version
def parse_requirement(entry, pyproject_path, requirement_type=None):
try:
req = Requirement(entry)
except InvalidRequirement as e:
print(json.dumps({"error": repr(e)}))
exit(1)
else:
data = {
"name": req.name,
"version": version_from_req(req.specifier),
"markers": str(req.marker) or None,
"file": pyproject_path,
"requirement": str(req.specifier),
"extras": sorted(list(req.extras)),
"requirement_type": requirement_type,
}
return data
def parse_toml_section_pep621_dependencies(
pyproject_path, dependencies, requirement_type=None
):
requirement_packages = []
for dependency in dependencies:
parsed_dependency = parse_requirement(
dependency, pyproject_path, requirement_type
)
requirement_packages.append(parsed_dependency)
return requirement_packages
def parse_toml_section_pep735_dependencies(
pyproject_path,
dependency_groups,
group_name,
visited=None,
):
requirement_packages = []
visited = visited or set()
if group_name in visited:
return requirement_packages
visited.add(group_name)
dependencies = dependency_groups.get(group_name, [])
for entry in dependencies:
# Handle direct requirement
if isinstance(entry, str):
parsed_dependency = parse_requirement(
entry, pyproject_path, group_name
)
requirement_packages.append(parsed_dependency)
# Handle include-group directive
elif isinstance(entry, dict) and "include-group" in entry:
included_group = entry["include-group"]
requirement_packages.extend(
parse_toml_section_pep735_dependencies(
pyproject_path,
dependency_groups,
included_group,
visited
)
)
return requirement_packages
dependencies = []
if 'project' in project_toml:
project_section = project_toml['project']
if 'dependencies' in project_section:
dependencies_toml = project_section['dependencies']
runtime_dependencies = parse_toml_section_pep621_dependencies(
pyproject_path,
dependencies_toml,
"dependencies"
)
dependencies.extend(runtime_dependencies)
if 'optional-dependencies' in project_section:
optional_dependencies_toml = project_section[
'optional-dependencies'
]
for group in optional_dependencies_toml:
group_dependencies = parse_toml_section_pep621_dependencies(
pyproject_path,
optional_dependencies_toml[group],
group
)
dependencies.extend(group_dependencies)
if 'dependency-groups' in project_toml:
dependency_groups = project_toml['dependency-groups']
for group_name in dependency_groups:
group_dependencies = parse_toml_section_pep735_dependencies(
pyproject_path, dependency_groups, group_name
)
dependencies.extend(group_dependencies)
if 'build-system' in project_toml:
build_system_section = project_toml['build-system']
if 'requires' in build_system_section:
build_system_dependencies = parse_toml_section_pep621_dependencies(
pyproject_path,
build_system_section['requires'],
"build-system.requires"
)
dependencies.extend(build_system_dependencies)
return json.dumps({"result": dependencies})
def parse_requirements(directory):
# Parse the requirements.txt
requirement_packages = []
requirement_files = glob.glob(os.path.join(directory, '*.txt')) \
+ glob.glob(os.path.join(directory, '**', '*.txt'))
pip_compile_files = glob.glob(os.path.join(directory, '*.in')) \
+ glob.glob(os.path.join(directory, '**', '*.in'))
def version_from_install_req(install_req):
if install_req.is_pinned:
return next(iter(install_req.specifier)).version
for reqs_file in requirement_files + pip_compile_files:
try:
requirements = pip._internal.req.req_file.parse_requirements(
reqs_file,
session=PipSession()
)
for parsed_req in requirements:
install_req = install_req_from_parsed_requirement(parsed_req)
if install_req.req is None:
continue
# Ignore file: requirements
if install_req.link is not None and install_req.link.is_file:
continue
pattern = r"-[cr] (.*) \(line \d+\)"
abs_path = re.search(pattern, install_req.comes_from).group(1)
# Ignore dependencies from remote constraint files
if not os.path.isfile(abs_path):
continue
rel_path = os.path.relpath(abs_path, directory)
requirement_packages.append({
"name": install_req.req.name,
"version": version_from_install_req(install_req),
"markers": str(install_req.markers) or None,
"file": rel_path,
"requirement": str(install_req.specifier) or None,
"extras": sorted(list(install_req.extras))
})
except Exception as e:
print(json.dumps({"error": repr(e)}))
exit(1)
return json.dumps({"result": requirement_packages})
def parse_setup(directory):
def version_from_install_req(install_req):
if install_req.is_pinned:
return next(iter(install_req.specifier)).version
def parse_requirement(req, req_type, filename):
install_req = install_req_from_line(req)
if install_req.original_link:
return
setup_packages.append(
{
"name": install_req.req.name,
"version": version_from_install_req(install_req),
"markers": str(install_req.markers) or None,
"file": filename,
"requirement": str(install_req.specifier) or None,
"requirement_type": req_type,
"extras": sorted(list(install_req.extras)),
}
)
def parse_requirements(requires, req_type, filename):
for req in requires:
req = COMMENT_RE.sub('', req)
req = req.strip()
parse_requirement(req, req_type, filename)
# Parse the setup.py and setup.cfg
setup_py = "setup.py"
setup_py_path = os.path.join(directory, setup_py)
setup_cfg = "setup.cfg"
setup_cfg_path = os.path.join(directory, setup_cfg)
setup_packages = []
if os.path.isfile(setup_py_path):
def setup(*args, **kwargs):
for arg in ["setup_requires", "install_requires", "tests_require"]:
requires = kwargs.get(arg, [])
parse_requirements(requires, arg, setup_py)
extras_require_dict = kwargs.get("extras_require", {})
for key, value in extras_require_dict.items():
parse_requirements(
value, "extras_require:{}".format(key), setup_py
)
setuptools.setup = setup
def noop(*args, **kwargs):
pass
def fake_parse(*args, **kwargs):
return []
global fake_open
def fake_open(*args, **kwargs):
content = (
"VERSION = ('0', '0', '1+dependabot')\n"
"__version__ = '0.0.1+dependabot'\n"
"__author__ = 'someone'\n"
"__title__ = 'something'\n"
"__description__ = 'something'\n"
"__author_email__ = 'something'\n"
"__license__ = 'something'\n"
"__url__ = 'something'\n"
)
return io.StringIO(content)
content = open(setup_py_path, "r").read()
# Remove `print`, `open`, `log` and import statements
content = re.sub(r"print\s*\(", "noop(", content)
content = re.sub(r"log\s*(\.\w+)*\(", "noop(", content)
content = re.sub(r"\b(\w+\.)*(open|file)\s*\(", "fake_open(", content)
content = content.replace("parse_requirements(", "fake_parse(")
version_re = re.compile(r"^.*import.*__version__.*$", re.MULTILINE)
content = re.sub(version_re, "", content)
# Set variables likely to be imported
__version__ = "0.0.1+dependabot"
__author__ = "someone"
__title__ = "something"
__description__ = "something"
__author_email__ = "something"
__license__ = "something"
__url__ = "something"
# Run as main (since setup.py is a script)
__name__ = "__main__"
# Exec the setup.py
exec(content) in globals(), locals()
if os.path.isfile(setup_cfg_path):
try:
config = configparser.ConfigParser()
config.read(setup_cfg_path)
for req_type in [
"setup_requires",
"install_requires",
"tests_require",
]:
requires = config.get(
'options',
req_type, fallback='').splitlines()
requires = [req for req in requires if req.strip()]
parse_requirements(requires, req_type, setup_cfg)
if config.has_section('options.extras_require'):
extras_require = config._sections['options.extras_require']
for key, value in extras_require.items():
requires = value.splitlines()
requires = [req for req in requires if req.strip()]
parse_requirements(
requires,
f"extras_require:{key}",
setup_cfg
)
except Exception as e:
print(json.dumps({"error": repr(e)}))
exit(1)
return json.dumps({"result": setup_packages})
|