Parse a requirements formatted file. Traverse a string until a delimiter is detected, then split at said delimiter, get module name by element index, create a dict consisting of module:version, and add dict to list of parsed modules. Args: file_: File to parse. Raises:
(file_)
| 306 | |
| 307 | |
| 308 | def parse_requirements(file_): |
| 309 | """Parse a requirements formatted file. |
| 310 | |
| 311 | Traverse a string until a delimiter is detected, then split at said |
| 312 | delimiter, get module name by element index, create a dict consisting of |
| 313 | module:version, and add dict to list of parsed modules. |
| 314 | |
| 315 | Args: |
| 316 | file_: File to parse. |
| 317 | |
| 318 | Raises: |
| 319 | OSerror: If there's any issues accessing the file. |
| 320 | |
| 321 | Returns: |
| 322 | tuple: The contents of the file, excluding comments. |
| 323 | """ |
| 324 | modules = [] |
| 325 | # For the dependency identifier specification, see |
| 326 | # https://www.python.org/dev/peps/pep-0508/#complete-grammar |
| 327 | delim = ["<", ">", "=", "!", "~"] |
| 328 | |
| 329 | try: |
| 330 | f = open(file_, "r") |
| 331 | except OSError: |
| 332 | logging.error("Failed on file: {}".format(file_)) |
| 333 | raise |
| 334 | else: |
| 335 | try: |
| 336 | data = [x.strip() for x in f.readlines() if x != "\n"] |
| 337 | finally: |
| 338 | f.close() |
| 339 | |
| 340 | data = [x for x in data if x[0].isalpha()] |
| 341 | |
| 342 | for x in data: |
| 343 | # Check for modules w/o a specifier. |
| 344 | if not any([y in x for y in delim]): |
| 345 | modules.append({"name": x, "version": None}) |
| 346 | for y in x: |
| 347 | if y in delim: |
| 348 | module = x.split(y) |
| 349 | module_name = module[0] |
| 350 | module_version = module[-1].replace("=", "") |
| 351 | module = {"name": module_name, "version": module_version} |
| 352 | |
| 353 | if module not in modules: |
| 354 | modules.append(module) |
| 355 | |
| 356 | break |
| 357 | |
| 358 | return modules |
| 359 | |
| 360 | |
| 361 | def compare_modules(file_, imports): |