Marcel Telka
2024-04-05 a885899322f8424d6d708e5c3143b6c43488446d
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 #
3b42bf 18 # Usage:
MT 19 #   python-requires PACKAGE [EXTRA]
20 #
21 # Print requirements for PACKAGE.  Evaluated and normalized.
22 # If PACKAGE is - evaluate and normalize stdin.
a76c89 23 # With optional EXTRA argument passed print requirements for such extra only.
095208 24 #
MT 25
26 import sys
27 import re
28
29 try:
1b2cf4 30     from importlib.metadata import requires
095208 31     from packaging.requirements import Requirement
3b42bf 32     import subprocess
095208 33 except:
MT 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
a76c89 40 reqs = requires(sys.argv[1]) if sys.argv[1] != "-" else sys.stdin.readlines()
MT 41
095208 42 try:
a76c89 43     for req in reqs:
915419 44         try:
MT 45             r = Requirement(re.sub(r"#.*", "", req))
46         except:
47             continue
06c6b6 48         m = r.marker
1b2cf4 49         if (not m and not e) or (m and m.evaluate(e) and (not e or not m.evaluate())):
06c6b6 50             print(re.sub(r"[-_.]+", "-", r.name).lower())
3b42bf 51             for extra in r.extras:
MT 52                 subprocess.run([sys.argv[0], r.name, extra])
095208 53 except:
MT 54     pass