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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
# -*- coding: ascii -*-
#
# Copyright 2018 - 2025
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Requirement file container
~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import logging as _logging
import os as _os
import re as _re
from .. import _parse
logger = _logging.getLogger("deps.inspect.req_file")
REQUIREMENT = "requirement"
CONSTRAINT = "constraint"
EDITABLE = "editable"
INDEX = "index"
def tokens(filenames, base=None):
"""
Walk over requirement tokens from a list of req files
Parameters:
filenames (iterable):
List of filenames to parse
base (str):
Base path. If omitted or ``None``, the CWD is used.
Returns:
iterable: List of token objects found
"""
base = _os.path.normpath(_os.path.abspath(base or _os.getcwd()))
req_match = _re.compile(r"^(?:-r|--requirement)\s+(\S.*)").match
con_match = _re.compile(r"^(?:-c|--constraint)\s+(\S+)").match
edit_match = _re.compile(r"^(?:-e|--editable)\s+(\S.*)").match
idx_match = _re.compile(r"^(?:-i|--index)\s+(\S+)").match
name_match = _re.compile(r"^(?:%s)" % (_parse.regex.idre)).match
stack = FileIterator(base, filenames)
for line in stack:
content = line.content.strip()
if not content or content.startswith("#"):
continue
match = req_match(content)
if match:
req = match.group(1)
logger.debug("Found requirement reference %r", req)
if req.lower().startswith(("http://", "https://")):
logger.debug("Skipping remote requirement file")
continue
if req.lower().startswith("file://"):
req = req[len("file://") :]
req = _os.path.normpath(
_os.path.join(_os.path.dirname(line.file), req)
)
stack.push_file(req)
continue
match = con_match(content)
if match:
con = match.group(1)
if con.lower().startswith("file://"):
con = con[len("file://") :]
if not con.startswith(("http://", "https://")):
con = _os.path.normpath(
_os.path.join(_os.path.dirname(line.file), con)
)
logger.debug("Returning constraint file %r", con)
yield Token.constraint(con.rstrip(), line)
continue
match = edit_match(content)
if match:
value = match.group(1).rstrip()
logger.debug("Returning editable install %r", value)
yield Token.editable(value, line)
continue
match = idx_match(content)
if match:
value = match.group(1).rstrip()
logger.debug("Returning index %r", value)
yield Token.index(value, line)
continue
if name_match(content):
logger.debug("Returning requirement %r", content)
yield Token.requirement(_parse.requirement(content), line)
continue
logger.debug(
"Ignoring line %s in %s: %r", line.lineno, line.file, content
)
class Token(object):
"""
Container for parsed requirement file tokens
Attributes:
value (any):
The token. Type depends on the token type. Typically a string, but
it's an requirement object for type == REQUIREMENT
line (Line):
The line object
type (str):
The token type
"""
# pylint: disable = redefined-builtin
def __init__(self, value, line, type):
"""
Initialization
Parameters:
value (any):
The token value
line (Line):
The line
type (str):
The token type:
- CONSTRAINT
- REQUIREMENT
- EDITABLE
- INDEX
"""
self.value = value
self.line = line
self.type = type
def __repr__(self):
"""
Create string representation
Returns:
str: String representation
"""
return "%s(%r, line=%r, type=%s)" % (
self.__class__.__name__,
self.value,
self.line,
self.type.upper(),
)
@classmethod
def constraint(cls, value, line):
"""
Create CONSTRAINT token
Parameters:
value (str):
The token value (constraint file reference)
line (Line):
The line
Returns:
Token: New Token instance
"""
return cls(value, line, CONSTRAINT)
@classmethod
def requirement(cls, value, line):
"""
Create REQUIREMENT token
Parameters:
value (parsed requirement):
The token value (the parsed requirement object)
line (Line):
The line
Returns:
Token: New Token instance
"""
return cls(value, line, REQUIREMENT)
@classmethod
def editable(cls, value, line):
"""
Create EDITABLE token
Parameters:
value (str):
The token value (editable reference, a folder or a URL)
line (Line):
The line
Returns:
Token: New Token instance
"""
return cls(value, line, EDITABLE)
@classmethod
def index(cls, value, line):
"""
Create INDEX token
Parameters:
value (str):
The token value (the index URL)
line (Line):
The line
Returns:
Token: New Token instance
"""
return cls(value, line, INDEX)
class FileIterator(object):
"""
Iterator over file lines
The file list can be extended while iterating over the lines. The file
added latest will be processed first.
"""
def __init__(self, base, filenames=None):
"""
Initialization
Parameters:
base (str):
The base path if relative file names are given
filenames (iterable):
The initial list of file names to iterate over
"""
self._base = base
self._stack = []
self._seen = set()
if filenames is not None:
for filename in reversed(list(filenames)):
self.push_file(filename)
def push_file(self, filename):
"""
Add a new file to iterate over
The file will not be added if it has been seen already.
Parameters:
filename (str):
The file name
"""
filename = _os.path.normpath(_os.path.join(self._base, filename))
key = _os.path.relpath(filename, self._base)
if key in self._seen:
logger.debug("Skipping file %r (already seen)", key)
return
self._seen.add(key)
if not _os.path.isfile(filename):
logger.debug("Skipping non-file %r", key)
return
logger.debug("Adding %r to the stack", key)
lines = iter(File(filename))
self._stack.append(getattr(lines, "__next__", None) or lines.next)
def __iter__(self):
"""
Iterate over lines
Returns:
iterable: All lines in LIFO (files) / FIFO (lines) order
"""
while self._stack:
try:
yield self._stack[-1]()
except StopIteration:
self._stack.pop()
class File(object):
"""
Requirement or constraints file
Attributes:
name (str):
File name
"""
def __init__(self, name):
"""
Initialization
Parameters:
name (str):
File name
"""
self.name = name
with open(self.name, "rb") as fp:
self._lines = [
Line(name, lineno, line.decode("latin-1"))
for lineno, line in enumerate(fp, start=1)
]
def __repr__(self):
"""
Create string representation
Returns:
str: String representation
"""
return "%s(%r)" % (
self.__class__.__name__,
self.name,
)
def __iter__(self):
"""
Create line iterator
Returns:
iterable: Line iterator
"""
return iter(self._lines)
class Line(object):
"""
Line in a file
Attributes:
file (str):
File name
lineno (int):
Line number
content (str):
The line
"""
def __init__(self, file, lineno, content):
"""
Initialization
Parameters:
file (str):
File name
lineno (int):
Line number
content (str):
The line
"""
self.file = file
self.lineno = lineno
self.content = content
def __repr__(self):
"""
Create string representation
Returns:
str: String representation
"""
return "%s(%r, %r, %r)" % (
self.__class__.__name__,
self.file,
self.lineno,
self.content,
)
|