-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure
safety
checks the installed dependencies
- Loading branch information
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
# Retrieve dependencies | ||
Retrieve dependencies from a `requirements.txt`-style file. | ||
""" | ||
import argparse | ||
from pathlib import Path | ||
import re | ||
from typing import Set | ||
|
||
|
||
def main(argv: list = None) -> Set[str]: | ||
"""Retrieve dependencies""" | ||
parser = argparse.ArgumentParser( | ||
description=main.__doc__, | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
parser.add_argument( | ||
"requirements_files", | ||
metavar="FILE", | ||
type=Path, | ||
nargs="+", | ||
help="The path(s) to one or several requirements.txt-style file(s).", | ||
) | ||
args = parser.parse_args(argv) | ||
|
||
requirements_regex = re.compile(r"^(?P<name>[A-Za-z0-9_-]+)(~=|>=|==).*\n$") | ||
dependencies = set() | ||
for file in args.requirements_files: | ||
if not file.exists(): | ||
raise FileNotFoundError(f"Could not find {file} !") | ||
with open(file.resolve(), "r") as handle: | ||
for line in handle.readlines(): | ||
match = requirements_regex.fullmatch(line) | ||
if match is None: | ||
raise ValueError( | ||
f"Couldn't determine package name from line:\n\n {line}" | ||
) | ||
dependencies.add(match.group("name")) | ||
return dependencies | ||
|
||
|
||
if __name__ == "__main__": | ||
from sys import argv | ||
|
||
dependencies = main(argv[1:]) | ||
grep_extended_regex = f"({'|'.join(dependencies)})" | ||
print(grep_extended_regex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters