freshdags 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- freshdags/__init__.py +5 -0
- freshdags/__main__.py +3 -0
- freshdags/api.py +115 -0
- freshdags/cli.py +210 -0
- freshdags/core.py +416 -0
- freshdags/errors.py +60 -0
- freshdags/git.py +45 -0
- freshdags/models.py +210 -0
- freshdags/py.typed +0 -0
- freshdags/render.py +84 -0
- freshdags-0.1.0.dist-info/METADATA +222 -0
- freshdags-0.1.0.dist-info/RECORD +15 -0
- freshdags-0.1.0.dist-info/WHEEL +4 -0
- freshdags-0.1.0.dist-info/entry_points.txt +3 -0
- freshdags-0.1.0.dist-info/licenses/LICENSE +21 -0
freshdags/__init__.py
ADDED
freshdags/__main__.py
ADDED
freshdags/api.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""The freshdags interface: every CLI command as a method.
|
|
2
|
+
|
|
3
|
+
The CLI only parses arguments, calls one of these methods and renders the result. Tests
|
|
4
|
+
exercise the methods directly.
|
|
5
|
+
|
|
6
|
+
Node names are file paths relative to the repository root, or external names starting with
|
|
7
|
+
`ext:`. Paths given by the caller are normalised: `./docs\\api.md` becomes `docs/api.md`.
|
|
8
|
+
|
|
9
|
+
Every method except `add` and `configure` raises GraphNotFoundError when freshdags.json does
|
|
10
|
+
not exist.
|
|
11
|
+
All errors derive from freshdags.errors.FreshdagsError.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from abc import ABC, abstractmethod
|
|
15
|
+
|
|
16
|
+
from freshdags.models import CheckReport, Graph, Mode, NodeView, Settings
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class FreshdagsApi(ABC):
|
|
20
|
+
"""Operations on the dependency graph of one git repository."""
|
|
21
|
+
|
|
22
|
+
# --- editing the graph ------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def add(self, doc: str, *sources: str) -> None:
|
|
26
|
+
"""Record that `doc` depends on each of `sources`.
|
|
27
|
+
|
|
28
|
+
Creates freshdags.json with default settings if it does not exist. Adding an edge
|
|
29
|
+
that already exists changes nothing.
|
|
30
|
+
|
|
31
|
+
Raises:
|
|
32
|
+
UnknownFileError: a non-external node is not an existing file.
|
|
33
|
+
UntrackedFileError: a non-external node is not tracked by git. Only tracked
|
|
34
|
+
files can be checked; everything else must be an `ext:` node.
|
|
35
|
+
SelfDependencyError: `doc` is among `sources`.
|
|
36
|
+
CycleError: an edge would close a cycle. The graph is left unchanged.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def rm(self, doc: str, *sources: str) -> None:
|
|
41
|
+
"""Remove the edges from `doc` to each of `sources`.
|
|
42
|
+
|
|
43
|
+
Nodes left without any edge are removed.
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
UnknownEdgeError: one of the edges does not exist. The graph is left unchanged.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def configure(
|
|
51
|
+
self, *, auto_flag: Mode | None = None, auto_resolve: Mode | None = None
|
|
52
|
+
) -> Settings:
|
|
53
|
+
"""Change the given settings and return all settings. With no arguments, only reads.
|
|
54
|
+
|
|
55
|
+
Creates freshdags.json with default settings if it does not exist.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
# --- reading the graph ------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
@abstractmethod
|
|
61
|
+
def graph(self) -> Graph:
|
|
62
|
+
"""The whole graph as stored in freshdags.json."""
|
|
63
|
+
|
|
64
|
+
@abstractmethod
|
|
65
|
+
def neighbours(self, node: str) -> NodeView:
|
|
66
|
+
"""`node` with its direct sources and dependents.
|
|
67
|
+
|
|
68
|
+
Raises:
|
|
69
|
+
UnknownNodeError: `node` is not in the graph.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
# --- drift ------------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
@abstractmethod
|
|
75
|
+
def check(self, rev: str | None = None, *, uncommitted: bool = False) -> CheckReport:
|
|
76
|
+
"""Find every doc that is older than one of its sources.
|
|
77
|
+
|
|
78
|
+
Without `rev`, uses the current freshdags.json and, in `on-change` mode, the working
|
|
79
|
+
tree. With `rev`, uses freshdags.json and the history as of that commit only.
|
|
80
|
+
|
|
81
|
+
`uncommitted` treats all uncommitted changes as one commit made now, also in
|
|
82
|
+
`on-commit` mode. That is what a pre-commit hook needs: it runs before the commit
|
|
83
|
+
exists. `off` stays off.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
FreshdagsError: `uncommitted` is combined with `rev`.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
# Flags and resolves are scoped to nodes: a file goes stale, and a review covers all of
|
|
90
|
+
# its changed sources at once.
|
|
91
|
+
|
|
92
|
+
@abstractmethod
|
|
93
|
+
def resolve(self, *docs: str) -> list[str]:
|
|
94
|
+
"""Record that each doc was reviewed and is up to date. Returns the docs resolved.
|
|
95
|
+
|
|
96
|
+
Does not flag the docs that depend on them.
|
|
97
|
+
|
|
98
|
+
Raises:
|
|
99
|
+
UnknownNodeError: a doc is not in the graph.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
@abstractmethod
|
|
103
|
+
def resolve_all(self) -> list[str]:
|
|
104
|
+
"""Resolve every drifted doc, when the change needed no doc update.
|
|
105
|
+
|
|
106
|
+
Returns the docs resolved.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
@abstractmethod
|
|
110
|
+
def flag(self, node: str, note: str | None = None) -> None:
|
|
111
|
+
"""Record that `node` changed in a way git cannot see, flagging its dependents.
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
UnknownNodeError: `node` is not in the graph.
|
|
115
|
+
"""
|
freshdags/cli.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""Command-line entry point: parses arguments, calls FreshdagsApi, renders the result.
|
|
2
|
+
|
|
3
|
+
Exit codes: 0 success / no drift, 1 drift found, 2 usage or graph error.
|
|
4
|
+
|
|
5
|
+
Output: results (the check report, `show`) go to stdout; confirmations and diagnostics are
|
|
6
|
+
log messages on stderr, hidden unless `-v` is given. `-q` hides results too, leaving only
|
|
7
|
+
errors and the exit code.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import functools
|
|
11
|
+
import io
|
|
12
|
+
import logging
|
|
13
|
+
import sys
|
|
14
|
+
from collections.abc import Callable
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Annotated
|
|
17
|
+
|
|
18
|
+
import typer
|
|
19
|
+
|
|
20
|
+
from freshdags import __version__
|
|
21
|
+
from freshdags.api import FreshdagsApi
|
|
22
|
+
from freshdags.core import Freshdags
|
|
23
|
+
from freshdags.errors import FreshdagsError
|
|
24
|
+
from freshdags.models import Mode
|
|
25
|
+
from freshdags.render import render_check, render_graph, render_node, render_settings
|
|
26
|
+
|
|
27
|
+
app = typer.Typer(
|
|
28
|
+
name="freshdags",
|
|
29
|
+
help="Track doc/code dependencies and flag documentation drift.",
|
|
30
|
+
no_args_is_help=True,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
log = logging.getLogger("freshdags")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Output:
|
|
38
|
+
"""Where command results go. Quiet mode drops them; errors are never dropped."""
|
|
39
|
+
|
|
40
|
+
quiet = False
|
|
41
|
+
|
|
42
|
+
def result(self, text: str) -> None:
|
|
43
|
+
if not self.quiet:
|
|
44
|
+
typer.echo(text)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
output = Output()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def api() -> FreshdagsApi:
|
|
51
|
+
return Freshdags.discover(Path.cwd())
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def configure_logging(verbose: int) -> None:
|
|
55
|
+
"""No `-v`: warnings only. `-v`: confirmations. `-vv`: diagnostics, e.g. git calls."""
|
|
56
|
+
level = {0: logging.WARNING, 1: logging.INFO}.get(verbose, logging.DEBUG)
|
|
57
|
+
logging.basicConfig(level=level, format="%(message)s", stream=sys.stderr, force=True)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def tolerate_unprintable_output() -> None:
|
|
61
|
+
"""Print `?` for characters the output encoding cannot show, instead of crashing.
|
|
62
|
+
|
|
63
|
+
Matters on Windows, where piped or redirected output uses the ANSI code page.
|
|
64
|
+
"""
|
|
65
|
+
for stream in (sys.stdout, sys.stderr):
|
|
66
|
+
if isinstance(stream, io.TextIOWrapper):
|
|
67
|
+
stream.reconfigure(errors="replace")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def handle_errors[**P, R](command: Callable[P, R]) -> Callable[P, R]:
|
|
71
|
+
"""Report FreshdagsError as a message on stderr with exit code 2."""
|
|
72
|
+
|
|
73
|
+
@functools.wraps(command)
|
|
74
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
|
75
|
+
try:
|
|
76
|
+
return command(*args, **kwargs)
|
|
77
|
+
except FreshdagsError as error:
|
|
78
|
+
typer.echo(f"Error: {error}", err=True)
|
|
79
|
+
raise typer.Exit(2) from error
|
|
80
|
+
|
|
81
|
+
return wrapper
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _version_callback(value: bool) -> None:
|
|
85
|
+
if value:
|
|
86
|
+
typer.echo(f"freshdags {__version__}")
|
|
87
|
+
raise typer.Exit()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@app.callback()
|
|
91
|
+
def main(
|
|
92
|
+
version: Annotated[
|
|
93
|
+
bool | None,
|
|
94
|
+
typer.Option(
|
|
95
|
+
"--version",
|
|
96
|
+
callback=_version_callback,
|
|
97
|
+
is_eager=True,
|
|
98
|
+
help="Show the version and exit.",
|
|
99
|
+
),
|
|
100
|
+
] = None,
|
|
101
|
+
verbose: Annotated[
|
|
102
|
+
int,
|
|
103
|
+
typer.Option(
|
|
104
|
+
"--verbose", "-v", count=True, help="Show confirmations; -vv adds diagnostics."
|
|
105
|
+
),
|
|
106
|
+
] = 0,
|
|
107
|
+
quiet: Annotated[
|
|
108
|
+
bool, typer.Option("--quiet", "-q", help="Print nothing but errors; use the exit code.")
|
|
109
|
+
] = False,
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Track doc/code dependencies and flag documentation drift."""
|
|
112
|
+
if verbose and quiet:
|
|
113
|
+
raise typer.BadParameter("--verbose and --quiet cannot be combined.")
|
|
114
|
+
tolerate_unprintable_output()
|
|
115
|
+
configure_logging(verbose)
|
|
116
|
+
output.quiet = quiet
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@app.command()
|
|
120
|
+
@handle_errors
|
|
121
|
+
def add(
|
|
122
|
+
doc: Annotated[str, typer.Argument(help="The file (or ext:name) that depends on SOURCES.")],
|
|
123
|
+
sources: Annotated[list[str], typer.Argument(help="Files (or ext:names) it depends on.")],
|
|
124
|
+
) -> None:
|
|
125
|
+
"""Record that DOC depends on each SOURCE."""
|
|
126
|
+
api().add(doc, *sources)
|
|
127
|
+
log.info("%s depends on %s", doc, ", ".join(sources))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@app.command()
|
|
131
|
+
@handle_errors
|
|
132
|
+
def rm(
|
|
133
|
+
doc: Annotated[str, typer.Argument(help="The dependent file.")],
|
|
134
|
+
sources: Annotated[list[str], typer.Argument(help="Sources to remove.")],
|
|
135
|
+
) -> None:
|
|
136
|
+
"""Remove the dependencies of DOC on each SOURCE."""
|
|
137
|
+
api().rm(doc, *sources)
|
|
138
|
+
log.info("%s no longer depends on %s", doc, ", ".join(sources))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@app.command()
|
|
142
|
+
@handle_errors
|
|
143
|
+
def configure(
|
|
144
|
+
auto_flag: Annotated[
|
|
145
|
+
Mode | None, typer.Option(help="What marks a source as changed automatically.")
|
|
146
|
+
] = None,
|
|
147
|
+
auto_resolve: Annotated[
|
|
148
|
+
Mode | None, typer.Option(help="What brings a doc up to date automatically.")
|
|
149
|
+
] = None,
|
|
150
|
+
) -> None:
|
|
151
|
+
"""Show the settings, or change them with --auto-flag / --auto-resolve."""
|
|
152
|
+
output.result(render_settings(api().configure(auto_flag=auto_flag, auto_resolve=auto_resolve)))
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@app.command()
|
|
156
|
+
@handle_errors
|
|
157
|
+
def show(
|
|
158
|
+
node: Annotated[str | None, typer.Argument(help="Show only this node.")] = None,
|
|
159
|
+
) -> None:
|
|
160
|
+
"""Print the graph, or one node's sources and dependents."""
|
|
161
|
+
fd = api()
|
|
162
|
+
output.result(render_node(fd.neighbours(node)) if node else render_graph(fd.graph()))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@app.command()
|
|
166
|
+
@handle_errors
|
|
167
|
+
def check(
|
|
168
|
+
rev: Annotated[
|
|
169
|
+
str | None, typer.Argument(help="Check as of this commit. Default: working tree.")
|
|
170
|
+
] = None,
|
|
171
|
+
as_json: Annotated[bool, typer.Option("--json", help="Machine-readable output.")] = False,
|
|
172
|
+
uncommitted: Annotated[
|
|
173
|
+
bool,
|
|
174
|
+
typer.Option(
|
|
175
|
+
"--uncommitted", help="Count uncommitted changes as a commit made now (pre-commit)."
|
|
176
|
+
),
|
|
177
|
+
] = False,
|
|
178
|
+
) -> None:
|
|
179
|
+
"""List every doc that is older than one of its sources. Exits 1 on drift."""
|
|
180
|
+
report = api().check(rev, uncommitted=uncommitted)
|
|
181
|
+
if as_json:
|
|
182
|
+
output.result(report.model_dump_json(indent=2))
|
|
183
|
+
else:
|
|
184
|
+
output.result(render_check(report))
|
|
185
|
+
raise typer.Exit(0 if report.ok else 1)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@app.command()
|
|
189
|
+
@handle_errors
|
|
190
|
+
def resolve(
|
|
191
|
+
docs: Annotated[list[str] | None, typer.Argument(help="Docs you reviewed.")] = None,
|
|
192
|
+
all_docs: Annotated[bool, typer.Option("--all", help="Resolve every drifted doc.")] = False,
|
|
193
|
+
) -> None:
|
|
194
|
+
"""Mark docs as up to date after reviewing them."""
|
|
195
|
+
if all_docs == bool(docs):
|
|
196
|
+
raise typer.BadParameter("Name the docs to resolve, or use --all, but not both.")
|
|
197
|
+
fd = api()
|
|
198
|
+
resolved = fd.resolve_all() if all_docs else fd.resolve(*(docs or []))
|
|
199
|
+
log.info("Resolved: %s", ", ".join(resolved) if resolved else "nothing")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
@app.command()
|
|
203
|
+
@handle_errors
|
|
204
|
+
def flag(
|
|
205
|
+
node: Annotated[str, typer.Argument(help="The file (or ext:name) that changed.")],
|
|
206
|
+
note: Annotated[str | None, typer.Option(help="What changed.")] = None,
|
|
207
|
+
) -> None:
|
|
208
|
+
"""Record that NODE changed in a way git cannot see, flagging its dependents."""
|
|
209
|
+
api().flag(node, note)
|
|
210
|
+
log.info("Flagged %s", node)
|