fritzkink
2023-12-15 9329acaef0fff79c242edf766ac84544d6422be8
commit | author | age
095208 1 #! /usr/bin/python
MT 2 #
3 # This file and its contents are supplied under the terms of the
4 # Common Development and Distribution License ("CDDL"), version 1.0.
5 # You may only use this file in accordance with the terms of version
6 # 1.0 of the CDDL.
7 #
8 # A full copy of the text of the CDDL should have accompanied this
9 # source.  A copy of the CDDL is also available via the Internet at
10 # http://www.illumos.org/license/CDDL.
11 #
12
13 #
14 # Copyright 2022 Marcel Telka
15 #
16
17 #
a76c89 18 # Print requirements for a package (first argument).
095208 19 # Evaluated and normalized.
a76c89 20 # If the package specified is - evaluate and normalize stdin.
MT 21 # With optional EXTRA argument passed print requirements for such extra only.
095208 22 #
MT 23
24 import sys
25 import re
26
27 try:
28     try:
29         from importlib.metadata import requires
30     except ImportError:
31         from importlib_metadata import requires
32     from packaging.requirements import Requirement
33 except:
34     exit()
35
36 if len(sys.argv) < 2:
37     exit()
38
06c6b6 39 e = {'extra': sys.argv[2]} if len(sys.argv) > 2 else None
MT 40 # packaging up to 21.3 raises UndefinedEnvironmentName when extra is not
41 # defined in environment, but marker contains it.  This should change in new
42 # packaging once released.  See https://github.com/pypa/packaging/pull/550.  To
43 # workaround this (so we do not need to handle exceptions) we pass empty extra
44 # in environment when we do not want any extra.
45 noe = {'extra': ''}
46
a76c89 47 reqs = requires(sys.argv[1]) if sys.argv[1] != "-" else sys.stdin.readlines()
MT 48
095208 49 try:
a76c89 50     for req in reqs:
915419 51         try:
MT 52             r = Requirement(re.sub(r"#.*", "", req))
53         except:
54             continue
06c6b6 55         m = r.marker
MT 56         if (not m and not e) or m and ((not e and m.evaluate(noe)) or (e and not m.evaluate(noe) and m.evaluate(e))):
57             print(re.sub(r"[-_.]+", "-", r.name).lower())
095208 58 except:
MT 59     pass