|
14 | 14 | @nox.session(name="dependencies-check", python=False) |
15 | 15 | def dependency_check(session: Session) -> None: |
16 | 16 | content = Path(PROJECT_CONFIG.root, "pyproject.toml").read_text() |
17 | | - dependencies = Dependencies(content).parse() |
| 17 | + dependencies = dependencies = Dependencies.parse(content) |
18 | 18 | console = rich.console.Console() |
19 | 19 | if illegal := dependencies.illegal: |
20 | 20 | report_illegal(illegal, console) |
21 | 21 | sys.exit(1) |
22 | 22 |
|
23 | 23 |
|
24 | 24 | class Dependencies: |
25 | | - ILLEGAL_DEPENDENCIES = ['url', 'git', 'path'] |
26 | | - |
27 | | - def __init__(self, pyproject_toml: str): |
28 | | - self.illegal_dict: Dict[str, List[str]] = {} |
29 | | - self.content = pyproject_toml |
30 | | - |
31 | | - def parse(self) -> "Dependencies": |
32 | | - def source_filter(version) -> bool: |
33 | | - for f in self.ILLEGAL_DEPENDENCIES: |
34 | | - if f in version: |
35 | | - return True |
36 | | - return False |
37 | | - |
38 | | - def extract_dependencies(section) -> List[str]: |
| 25 | + def __init__(self, illegal: Dict[str, List[str]] | None): |
| 26 | + self._illegal = illegal or {} |
| 27 | + @staticmethod |
| 28 | + def parse(pyproject_toml: str) -> "Dependencies": |
| 29 | + def _source_filter(version) -> bool: |
| 30 | + ILLEGAL_SPECIFIERS = ['url', 'git', 'path'] |
| 31 | + return any( |
| 32 | + specifier in version |
| 33 | + for specifier in ILLEGAL_SPECIFIERS |
| 34 | + ) |
| 35 | + def _extract_dependencies(section) -> List[str]: |
39 | 36 | dependencies = [] |
40 | 37 | for name, version in section.items(): |
41 | | - if source_filter(version): |
| 38 | + if _source_filter(version): |
42 | 39 | dependencies.append(f"{name} = {version}") |
43 | 40 | return dependencies |
44 | | - |
45 | 41 | illegal: Dict[str, List[str]] = {} |
46 | | - toml = tomlkit.loads(self.content) |
| 42 | + toml = tomlkit.loads(pyproject_toml) |
47 | 43 | poetry = toml.get("tool", {}).get("poetry", {}) |
48 | | - |
49 | 44 | part = poetry.get("dependencies", {}) |
50 | | - dependencies_list = extract_dependencies(part) |
| 45 | + dependencies_list = _extract_dependencies(part) |
51 | 46 | if dependencies_list: |
52 | 47 | illegal["tool.poetry.dependencies"] = dependencies_list |
53 | | - |
54 | 48 | part = poetry.get("dev", {}).get("dependencies", {}) |
55 | | - dependencies_list = extract_dependencies(part) |
| 49 | + dependencies_list = _extract_dependencies(part) |
56 | 50 | if dependencies_list: |
57 | 51 | illegal["tool.poetry.dev.dependencies"] = dependencies_list |
58 | | - |
59 | 52 | part = poetry.get("group", {}) |
60 | 53 | for group, content in part.items(): |
61 | | - dependencies_list = extract_dependencies(content.get("dependencies", {})) |
| 54 | + dependencies_list = _extract_dependencies(content.get("dependencies", {})) |
62 | 55 | if dependencies_list: |
63 | 56 | illegal[f"tool.poetry.group.{group}.dependencies"] = dependencies_list |
64 | | - |
65 | | - self.illegal_dict = illegal |
66 | | - return self |
67 | | - |
| 57 | + return Dependencies(illegal) |
68 | 58 | @property |
69 | 59 | def illegal(self) -> Dict[str, List[str]]: |
70 | | - return self.illegal_dict |
| 60 | + return self._illegal |
71 | 61 |
|
72 | 62 |
|
73 | 63 | def report_illegal(illegal: Dict[str, List[str]], console: rich.console.Console): |
|
0 commit comments