Repository navigation
Expand file tree
/
Copy pathresync.py
More file actions
executable file
·151 lines (126 loc) · 5.43 KB
/
Copy pathresync.py
File metadata and controls
executable file
·151 lines (126 loc) · 5.43 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Pull the source clones, re-sync ``content/`` against them, then reseed.
python resync.py # pull, update content/, reseed SQLite
python resync.py --no-seed # pull and update content/ only
python resync.py --no-pull # re-sync from the clones as they are
This is the one script responsible for bringing upstream changes into
Opcode. It runs on the host, by hand; nothing in Opcode pulls on a
schedule, and the container never touches a source repository. The
steps, in order:
1. **Pull.** Each registered clone (:data:`ingestion.sources.ALL_SOURCES`)
is fast-forwarded from its ``origin`` by :mod:`ingestion.upstream`.
A clone with local changes, a diverged branch, or an origin that is
not the registered repository is reported and left alone. Skip this
step with ``--no-pull`` when offline or when the clones were updated
by hand.
2. **Sync.** :func:`ingestion.pipeline.sync` rebuilds every entry whose
upstream file changed and leaves everything else untouched. The run
is idempotent — with nothing new upstream it writes no files.
3. **Inventory.** ``TODO.md`` is regenerated with what was deferred.
4. **Seed.** SQLite is upserted from ``content/`` unless ``--no-seed``
is given. ``DATA_PATH`` says where; its default is the container's
volume, ``/data``, which does not exist on a host, so the script
checks it is writable **before** doing any work and stops with the
fix (``DATA_PATH=./data``, which ``make`` sets for you, or
``--no-seed``) rather than failing after the pull and sync.
An entry whose upstream file changed *after* its body was edited locally
is **flagged for manual review** and left untouched; it is listed at the
end of the run and on the About page. Resolve one by editing its file
under ``content/entries/`` and clearing ``flagged_for_review`` (set
``ingest_hash`` to empty to keep protecting it), or delete the file to
let the next sync take upstream's version.
The exit status is 1 if any clone could not be pulled, so a stale
source is never mistaken for a clean run. The sync still happens for
every source, from the checkout as it is on disk.
"""
from __future__ import annotations
import argparse
import logging
import os
import sys
from pathlib import Path
from app.config import DATA_PATH
from app.db import init_db
from app.seed import seed_content
from ingestion import todo, upstream
from ingestion.pipeline import sync
def unwritable_reason(data_path: Path) -> str | None:
"""Explain why SQLite could not be created under ``data_path``, if so.
The directory need not exist yet — :func:`app.db.connect` creates it —
but its nearest existing ancestor must be writable by this process.
Args:
data_path: The directory the database file goes in.
Returns:
A message naming the obstacle, or None when seeding can proceed.
"""
probe = data_path
while not probe.exists():
probe = probe.parent
if probe != data_path and not probe.is_dir():
return f"{probe} is not a directory"
if not os.access(probe, os.W_OK):
return f"{probe} is not writable by this user"
return None
def _require_seed_target() -> None:
"""Stop before any work if the seed step would fail at the end.
Raises:
SystemExit: With the obstacle and both ways around it.
"""
reason = unwritable_reason(DATA_PATH)
if reason is None:
return
sys.exit(
f"error: cannot seed SQLite under DATA_PATH={DATA_PATH}: {reason}.\n"
"The default is the container's volume. On the host, either point\n"
"DATA_PATH at a writable directory or skip the seed step:\n"
" DATA_PATH=./data python resync.py (make resync does this)\n"
" python resync.py --no-seed\n"
"See documentation/resync-troubleshooting.md"
)
def _parse_args() -> argparse.Namespace:
"""Define and parse the command line."""
parser = argparse.ArgumentParser(
description="Pull the source clones and re-sync content/ from them."
)
parser.add_argument("--content", type=Path, default=None, help="Snapshot root.")
parser.add_argument(
"--no-pull",
action="store_true",
help="Do not git-pull the source clones first; re-sync them as they are.",
)
parser.add_argument(
"--no-seed",
action="store_true",
help="Update content/ but do not touch SQLite.",
)
return parser.parse_args()
def _pull() -> int:
"""Pull every clone, print the report, and return how many were blocked."""
report = upstream.pull_all()
print(report.summary())
blocked = report.blocked()
if blocked:
print(
f"\n{len(blocked)} source(s) could not be pulled (see above); "
"their entries are synced from the checkout as it is on disk."
)
print()
return len(blocked)
def main() -> None:
"""Pull, re-sync the snapshot and, unless told otherwise, reseed SQLite."""
args = _parse_args()
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
if not args.no_seed:
_require_seed_target()
blocked = 0 if args.no_pull else _pull()
report = sync(args.content)
print(report.summary())
todo.write(report.deferred, Path(__file__).resolve().parent)
if not args.no_seed:
init_db()
print(seed_content(args.content))
if blocked:
sys.exit(1)
if __name__ == "__main__":
main()