-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsetupcfg2rpm.py
executable file
·68 lines (49 loc) · 1.84 KB
/
setupcfg2rpm.py
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
#!/usr/bin/python3
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
"""
Script setupcfg2rpm.py parses setup.cfg, extracts python packages from options->install_requires
section and wraps them into python3dist(package) so output can by used with dnf package installer.
Example:
$ dnf install $(setupcfg2rpm.py path_to_setupcfg)
python3dist(some_package)
python3dist(other_package)
$
"""
import os
import re
import pathlib
import argparse
import configparser
from typing import Optional
from packaging.requirements import Requirement
def normalize_name(name: str) -> str:
"""https://www.python.org/dev/peps/pep-0503/#normalized-names"""
return re.sub(r"[-_.]+", "-", name).lower()
def evaluate_marker(req: str) -> Optional[str]:
"""Evaluate the marker in a requirement string
Args:
req: Requirement string to be evaluated.
Returns:
The name of the requirement if there is no marker or the marker
evaluates to True, None otherwise.
"""
req: Requirement = Requirement(req)
return req.name if not req.marker or req.marker.evaluate() else None
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Parse rpm requirements from setup.cfg from section "
"[options], key install_requires"
)
parser.add_argument("path", type=str, help="Path to setup.cfg")
args = parser.parse_args()
setupcfg_path = pathlib.Path(args.path)
config = configparser.ConfigParser()
with open(setupcfg_path) as f:
config.read_file(f)
requirements = config["options"]["install_requires"].strip().splitlines()
python_packages = filter(None, map(evaluate_marker, requirements))
result = [
f"python3dist({normalize_name(pkg_name)})" for pkg_name in python_packages
]
print(os.linesep.join(result))