Rename scripts/ to tools/

This commit is contained in:
jo 2021-09-07 22:56:35 +02:00
parent 2513f08d07
commit 7cb3501540
8 changed files with 4 additions and 4 deletions

18
tools/Makefile Normal file
View file

@ -0,0 +1,18 @@
.PHONY: lint test
.ONESHELL:
SHELL := bash
CPU_CORES := $(shell nproc)
all: lint test
venv:
python3 -m venv venv
source venv/bin/active
pip install -r requirements-dev.txt
lint: venv
pylint tools
test: venv
pytest -n ${CPU_CORES} --color=yes -v .

3
tools/README.md Normal file
View file

@ -0,0 +1,3 @@
# Tools
This folder contains scripts/tools to manage the project.

0
tools/__init__.py Normal file
View file

93
tools/packages.py Executable file
View file

@ -0,0 +1,93 @@
#!/usr/bin/env python3
import json
import sys
from argparse import ArgumentParser
from configparser import ConfigParser
from os import PathLike
from pathlib import Path
from typing import Iterator, Set
DEFAULT_PACKAGES_FILENAME = "packages.ini"
FORMATS = ("list", "line")
SYSTEMS = ("buster", "bionic")
SETTINGS_SECTION = "=settings"
DEVELOPMENT_SECTION = "=development"
def load_packages(raw: str, distribution: str, development: bool = False) -> Set[str]:
manager = ConfigParser(default_section=SETTINGS_SECTION)
manager.read_string(raw)
packages = set()
for section, entries in manager.items():
if not development and section == DEVELOPMENT_SECTION:
continue
for package, distributions in entries.items():
if distribution in distributions.split(", "):
packages.add(package)
return packages
def list_packages_files(paths: str) -> Iterator[Path]:
for path in paths:
path = Path(path)
if path.is_dir():
path = path / DEFAULT_PACKAGES_FILENAME
if not path.is_file():
raise Exception(f"{path} is not a file!")
yield path
def list_packages(paths: str, distribution: str, development: bool = False) -> Set[str]:
packages = set()
for package_file in list_packages_files(paths):
raw = package_file.read_text()
packages.update(load_packages(raw, distribution, development))
return set(sorted(packages))
def run():
parser = ArgumentParser()
parser.add_argument(
"-f",
"--format",
choices=FORMATS,
help="print packages list in a specific format.",
default="list",
),
parser.add_argument(
"-d",
"--dev",
help="include development packages.",
action="store_true",
)
parser.add_argument(
"distribution",
choices=SYSTEMS,
help="list packages for the given distribution.",
)
parser.add_argument(
"path",
nargs="+",
help="list packages from given files or directories.",
)
args = parser.parse_args()
packages = list_packages(args.path, args.distribution, args.dev)
if args.format == "list":
print("\n".join(packages))
else:
print(" ".join(packages))
if __name__ == "__main__":
run()

32
tools/packages_test.py Normal file
View file

@ -0,0 +1,32 @@
from pathlib import Path
from .packages import list_packages, load_packages
package_ini = """
[common]
postgresql = buster
# Some comment
curl = buster, bionic
[legacy]
apache2 = bionic
[=development]
ffmpeg = buster, bionic
"""
result1 = {"curl", "postgresql"}
result2 = {"apache2", "curl", "ffmpeg"}
def test_load_packages():
assert load_packages(package_ini, "buster", False) == result1
assert load_packages(package_ini, "bionic", True) == result2
def test_list_packages(tmp_path: Path):
package_file = tmp_path / "packages.ini"
package_file.write_text(package_ini)
assert list_packages([tmp_path, package_file], "buster", False) == result1
assert list_packages([tmp_path, package_file], "bionic", True) == result2

View file

@ -0,0 +1,3 @@
pylint
pytest
pytest-xdist