You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.6 KiB
51 lines
1.6 KiB
import re |
|
import subprocess |
|
import sys |
|
import toml |
|
|
|
|
|
REGEX_PKG = re.compile("^([\w\.\[\],_-]+)") |
|
REGEX_SINGLE = re.compile("([\w\._-]+)") |
|
|
|
|
|
def get_pip_freeze_list(): |
|
freeze_text = subprocess.check_output( |
|
[sys.executable, '-m', 'pip', 'list', '-l', '--format=freeze'] |
|
) |
|
freeze_text = freeze_text.decode("utf-8") |
|
return freeze_text.strip().splitlines() |
|
|
|
def get_raw_packages_from_pyproject_file(filename="pyproject.toml"): |
|
pyproject = toml.load(filename) |
|
_flit = pyproject["tool"]["flit"]["metadata"] |
|
pyproject_pkgs = _flit["requires"] |
|
pyproject_pkgs += _flit["requires-extra"]["test"] |
|
pyproject_pkgs += _flit["requires-extra"]["dev"] |
|
return pyproject_pkgs |
|
|
|
|
|
def _parse_package_names(toml_pkgs): |
|
for pkg in toml_pkgs: |
|
group = REGEX_PKG.match(pkg)[0] |
|
yield from REGEX_SINGLE.finditer(group) |
|
|
|
def get_package_names(filename="pyproject.toml"): |
|
toml_pkgs = get_raw_packages_from_pyproject_file(filename) |
|
as_string = (m.group() for m in _parse_package_names(toml_pkgs)) |
|
lowercased = (item.lower() for item in as_string) |
|
return {item.replace("_", "-") for item in lowercased} |
|
|
|
def filter_toml_pkgs_from_freeze_list(freeze_list, toml_pkgs): |
|
for line in freeze_list: |
|
freeze_pkg, _ = line.split("=", 1) |
|
if freeze_pkg.lower() in toml_pkgs: |
|
yield line |
|
|
|
def main(toml_file="pyproject.toml"): |
|
freeze_list = get_pip_freeze_list() |
|
toml_pkgs = get_package_names(toml_file) |
|
return filter_toml_pkgs_from_freeze_list(freeze_list, toml_pkgs) |
|
|
|
|
|
if __name__ == "__main__": |
|
print("\n".join(sorted(main(), key=lambda x: x.lower())))
|
|
|