codehood-cli 0.5.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.
- codehood/__init__.py +0 -0
- codehood/__main__.py +4 -0
- codehood/actions/__init__.py +0 -0
- codehood/actions/base.py +119 -0
- codehood/actions/resources.py +30 -0
- codehood/api/__init__.py +0 -0
- codehood/api/base.py +256 -0
- codehood/api/generate.py +1370 -0
- codehood/api/generated.py +3818 -0
- codehood/api/openapi.py +134 -0
- codehood/api/pages.py +53 -0
- codehood/cli/__init__.py +64 -0
- codehood/cli/api.py +422 -0
- codehood/cli/attendance.py +136 -0
- codehood/cli/auth.py +106 -0
- codehood/cli/base.py +42 -0
- codehood/cli/chill.py +52 -0
- codehood/cli/exam.py +107 -0
- codehood/cli/grades.py +185 -0
- codehood/cli/init.py +161 -0
- codehood/cli/push.py +609 -0
- codehood/cli/show.py +77 -0
- codehood/cli/uitils.py +26 -0
- codehood/functional.py +40 -0
- codehood/globs.py +71 -0
- codehood/grades.py +74 -0
- codehood/hypothesis.py +274 -0
- codehood/models/__init__.py +0 -0
- codehood/models/base.py +15 -0
- codehood/models/repo.py +187 -0
- codehood/models/validators.py +177 -0
- codehood/push/__init__.py +225 -0
- codehood/push/base.py +216 -0
- codehood/push/calendar.py +664 -0
- codehood/push/errors.py +136 -0
- codehood/push/exam.py +636 -0
- codehood/push/exclude.py +60 -0
- codehood/push/grades.py +455 -0
- codehood/push/hook.py +48 -0
- codehood/push/links.py +614 -0
- codehood/push/ours.py +78 -0
- codehood/push/plan.py +1981 -0
- codehood/push/project.py +272 -0
- codehood/push/question.py +338 -0
- codehood/push/report.py +244 -0
- codehood/push/repository.py +326 -0
- codehood/push/resource.py +576 -0
- codehood/push/run.py +1275 -0
- codehood/push/unconsumed.py +208 -0
- codehood/push/wire.py +222 -0
- codehood/py.typed +0 -0
- codehood/repo/__init__.py +62 -0
- codehood/repo/_config.py +28 -0
- codehood/repo/_layout.py +132 -0
- codehood/repo/_scaffold.py +244 -0
- codehood/repo/diff.py +71 -0
- codehood/repo/file_tracker.py +124 -0
- codehood/theme.py +101 -0
- codehood/widgets/__init__.py +0 -0
- codehood/widgets/mascot.py +76 -0
- codehood/widgets/questions/__init__.py +65 -0
- codehood/widgets/questions/base.py +119 -0
- codehood/widgets/questions/controls.py +194 -0
- codehood/widgets/questions/fill_in.py +81 -0
- codehood/widgets/questions/responses.py +137 -0
- codehood/widgets/questions/simple.py +117 -0
- codehood_cli-0.5.0.dist-info/METADATA +294 -0
- codehood_cli-0.5.0.dist-info/RECORD +71 -0
- codehood_cli-0.5.0.dist-info/WHEEL +4 -0
- codehood_cli-0.5.0.dist-info/entry_points.txt +4 -0
- codehood_cli-0.5.0.dist-info/licenses/LICENSE +21 -0
codehood/push/report.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The text report of the decisions a three-way plan adds to a push: `Pull`,
|
|
3
|
+
`Conflict`, `OtherCli` and the `ServerDrifted` notice, plus the exit code
|
|
4
|
+
they imply.
|
|
5
|
+
|
|
6
|
+
Everything here is a pure function of a `Plan`. The report of a `Put`,
|
|
7
|
+
`Prune`, `Keep` and `Skip` stays with the command.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import assert_never
|
|
13
|
+
|
|
14
|
+
from .plan import (
|
|
15
|
+
Conflict,
|
|
16
|
+
ConflictReason,
|
|
17
|
+
Decision,
|
|
18
|
+
EntityKind,
|
|
19
|
+
IgnoredExamPolicy,
|
|
20
|
+
Keep,
|
|
21
|
+
KeptCancelled,
|
|
22
|
+
KeptDraftPublished,
|
|
23
|
+
KeptExamLive,
|
|
24
|
+
KeptForRollCall,
|
|
25
|
+
KeptPinnedSlot,
|
|
26
|
+
KeptRepository,
|
|
27
|
+
Notice,
|
|
28
|
+
OtherCli,
|
|
29
|
+
Plan,
|
|
30
|
+
Prune,
|
|
31
|
+
Pull,
|
|
32
|
+
Put,
|
|
33
|
+
Rejected,
|
|
34
|
+
ServerDrifted,
|
|
35
|
+
Skip,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"describe_conflict",
|
|
40
|
+
"describe_decision",
|
|
41
|
+
"describe_notice_drift",
|
|
42
|
+
"describe_other_cli",
|
|
43
|
+
"describe_pull",
|
|
44
|
+
"describe_server_drifted",
|
|
45
|
+
"sync_exit_code",
|
|
46
|
+
"sync_lines",
|
|
47
|
+
"sync_summary",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
#: How each kind of entity reads in a line.
|
|
51
|
+
KIND_LABELS: dict[EntityKind, str] = {
|
|
52
|
+
"course": "course",
|
|
53
|
+
"resource": "resource",
|
|
54
|
+
"question": "question",
|
|
55
|
+
"exam": "exam",
|
|
56
|
+
"time_slot": "time slot",
|
|
57
|
+
"calendar_event": "calendar event",
|
|
58
|
+
"grade_column": "grade column",
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#: Why each kind of conflict is one, as a clause after "conflict".
|
|
62
|
+
REASON_TEXTS: dict[ConflictReason, str] = {
|
|
63
|
+
"both-changed": "changed both here and on the server",
|
|
64
|
+
"local-edited-remote-deleted": "edited here, removed on the server",
|
|
65
|
+
"local-deleted-remote-edited": "removed here, edited on the server",
|
|
66
|
+
"no-base": "differs from the server and has no recorded base",
|
|
67
|
+
"rename": "renamed here, changed on the server",
|
|
68
|
+
"link": "its lesson link changed",
|
|
69
|
+
"referenced": "removed on the server but still referenced here",
|
|
70
|
+
"event-linked": "removed here, but it stays on the server",
|
|
71
|
+
"slot-in-use": "removed here, but it stays on the server",
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#: What follows the entity list of the conflicts that name what holds an entity.
|
|
75
|
+
HELD_TEXTS: dict[ConflictReason, str] = {
|
|
76
|
+
"event-linked": (
|
|
77
|
+
"; resources still link to it ({held}): unlink them on the server, "
|
|
78
|
+
"or push with --ours resource/<slug> to send the local version of each"
|
|
79
|
+
),
|
|
80
|
+
"slot-in-use": (
|
|
81
|
+
"; events still use it ({held}): settle those events first, "
|
|
82
|
+
"for example with --ours calendar_event/<key>"
|
|
83
|
+
),
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#: What a `Pull` did on the server, as a clause.
|
|
87
|
+
CHANGE_TEXTS = {
|
|
88
|
+
"create": "created on the server",
|
|
89
|
+
"update": "changed on the server",
|
|
90
|
+
"delete": "removed from the server",
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def describe_pull(pull: Pull) -> str:
|
|
95
|
+
"""
|
|
96
|
+
Render a `Pull`: a write that did not come from a CLI, left unapplied
|
|
97
|
+
here and unoverwritten on the server.
|
|
98
|
+
"""
|
|
99
|
+
text = (
|
|
100
|
+
f"pull {_entity(pull.kind, pull.key)} {CHANGE_TEXTS[pull.change]}; "
|
|
101
|
+
"not applied here, and the server's version was not overwritten"
|
|
102
|
+
)
|
|
103
|
+
return text + _fields(pull.fields)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def describe_conflict(conflict: Conflict) -> str:
|
|
107
|
+
"""
|
|
108
|
+
Render a `Conflict`, with the `--ours` hint when the repository's side
|
|
109
|
+
can settle it.
|
|
110
|
+
"""
|
|
111
|
+
name = _entity(conflict.kind, conflict.key)
|
|
112
|
+
if conflict.renamed_to is not None:
|
|
113
|
+
name += f" -> {conflict.renamed_to}"
|
|
114
|
+
text = f"conflict {name}: {REASON_TEXTS[conflict.reason]}"
|
|
115
|
+
if conflict.reason in HELD_TEXTS:
|
|
116
|
+
return text + HELD_TEXTS[conflict.reason].format(
|
|
117
|
+
held=", ".join(conflict.fields)
|
|
118
|
+
)
|
|
119
|
+
text += _fields(conflict.fields)
|
|
120
|
+
if conflict.ours:
|
|
121
|
+
text += (
|
|
122
|
+
"; to keep the local version, push with "
|
|
123
|
+
f"--ours {conflict.kind}/{conflict.key}"
|
|
124
|
+
)
|
|
125
|
+
return text
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def describe_other_cli(other: OtherCli) -> str:
|
|
129
|
+
"""
|
|
130
|
+
Render an `OtherCli`: a change made through another CLI, with the
|
|
131
|
+
`--ours` way out for a source that is no longer in git.
|
|
132
|
+
"""
|
|
133
|
+
text = (
|
|
134
|
+
f"other cli {_entity(other.kind, other.key)} changed on the server "
|
|
135
|
+
f"through another CLI; run `git pull` before pushing{_fields(other.fields)}"
|
|
136
|
+
)
|
|
137
|
+
if other.ours:
|
|
138
|
+
text += (
|
|
139
|
+
"; if the file no longer exists anywhere, push with "
|
|
140
|
+
f"--ours {other.kind}/{other.key}"
|
|
141
|
+
)
|
|
142
|
+
return text
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def describe_server_drifted(notice: ServerDrifted) -> str:
|
|
146
|
+
"""
|
|
147
|
+
Render a `ServerDrifted` notice: the fields of the server's copy that the
|
|
148
|
+
push overwrites.
|
|
149
|
+
"""
|
|
150
|
+
return (
|
|
151
|
+
f"warning {_entity(notice.kind, notice.key)} was changed on the server; "
|
|
152
|
+
f"it will be overwritten by this push{_fields(notice.fields)}"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def describe_notice_drift(notice: Notice) -> str | None:
|
|
157
|
+
"""
|
|
158
|
+
Render a notice that belongs to this report, `None` for the others.
|
|
159
|
+
"""
|
|
160
|
+
match notice:
|
|
161
|
+
case ServerDrifted():
|
|
162
|
+
return describe_server_drifted(notice)
|
|
163
|
+
case (
|
|
164
|
+
Rejected()
|
|
165
|
+
| KeptDraftPublished()
|
|
166
|
+
| KeptExamLive()
|
|
167
|
+
| IgnoredExamPolicy()
|
|
168
|
+
| KeptPinnedSlot()
|
|
169
|
+
| KeptForRollCall()
|
|
170
|
+
| KeptRepository()
|
|
171
|
+
| KeptCancelled()
|
|
172
|
+
):
|
|
173
|
+
return None
|
|
174
|
+
case _:
|
|
175
|
+
assert_never(notice)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def describe_decision(decision: Decision) -> str | None:
|
|
179
|
+
"""
|
|
180
|
+
Render a decision that belongs to this report, `None` for the writes and
|
|
181
|
+
the quiet ones.
|
|
182
|
+
"""
|
|
183
|
+
match decision:
|
|
184
|
+
case Pull():
|
|
185
|
+
return describe_pull(decision)
|
|
186
|
+
case Conflict():
|
|
187
|
+
return describe_conflict(decision)
|
|
188
|
+
case OtherCli():
|
|
189
|
+
return describe_other_cli(decision)
|
|
190
|
+
case Skip() | Put() | Prune() | Keep():
|
|
191
|
+
return None
|
|
192
|
+
case _:
|
|
193
|
+
assert_never(decision)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def sync_lines(plan: Plan) -> list[str]:
|
|
197
|
+
"""
|
|
198
|
+
The lines for every drift notice, `Pull`, `Conflict` and `OtherCli` of
|
|
199
|
+
the plan, in that order. Empty when the plan has none.
|
|
200
|
+
"""
|
|
201
|
+
lines = [
|
|
202
|
+
line for n in plan.notices if (line := describe_notice_drift(n)) is not None
|
|
203
|
+
]
|
|
204
|
+
for decisions in (plan.pulls, plan.conflicts, plan.other_cli):
|
|
205
|
+
lines.extend(
|
|
206
|
+
line for d in decisions if (line := describe_decision(d)) is not None
|
|
207
|
+
)
|
|
208
|
+
return lines
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def sync_summary(plan: Plan) -> str:
|
|
212
|
+
"""
|
|
213
|
+
The summary line with the count of each new decision, empty when there
|
|
214
|
+
are none.
|
|
215
|
+
"""
|
|
216
|
+
if not (plan.pulls or plan.conflicts or plan.other_cli):
|
|
217
|
+
return ""
|
|
218
|
+
return (
|
|
219
|
+
f"sync: {len(plan.pulls)} server change(s) not applied here, "
|
|
220
|
+
f"{len(plan.conflicts)} conflict(s), "
|
|
221
|
+
f"{len(plan.other_cli)} from another CLI"
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def sync_exit_code(plan: Plan) -> int:
|
|
226
|
+
"""
|
|
227
|
+
The exit code the decisions imply: 1 while a `Conflict` remains, else 0.
|
|
228
|
+
|
|
229
|
+
A `Pull` and an `OtherCli` leave the code to the rest of the push.
|
|
230
|
+
"""
|
|
231
|
+
return int(bool(plan.conflicts))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
#
|
|
235
|
+
# Utilities
|
|
236
|
+
#
|
|
237
|
+
def _entity(kind: EntityKind, key: str) -> str:
|
|
238
|
+
if kind == "course":
|
|
239
|
+
return KIND_LABELS[kind]
|
|
240
|
+
return f"{KIND_LABELS[kind]} {key}"
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _fields(fields: tuple[str, ...]) -> str:
|
|
244
|
+
return f" (fields: {', '.join(fields)})" if fields else ""
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The core that turns the folders under `repositories/` into git trees.
|
|
3
|
+
|
|
4
|
+
Each direct subfolder of `repositories/` is one repository, its name the
|
|
5
|
+
slug. A scan reads the files, hashes each one twice (sha-256 for the
|
|
6
|
+
server's blob store, sha1 for git) and computes the sha1 of the root tree
|
|
7
|
+
the server would build from them, so `plan.py` can compare it to the
|
|
8
|
+
server's `tree` without uploading anything. No network here.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
import os
|
|
15
|
+
import re
|
|
16
|
+
import stat
|
|
17
|
+
import sys
|
|
18
|
+
from collections.abc import Iterator, Sequence
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Literal
|
|
22
|
+
|
|
23
|
+
import pathspec
|
|
24
|
+
|
|
25
|
+
from ..globs import is_excluded
|
|
26
|
+
from ..repo import REPOSITORIES_DIR
|
|
27
|
+
from .errors import RepositoryError
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"MAX_FILE_BYTES",
|
|
31
|
+
"REPOSITORIES_DIR",
|
|
32
|
+
"FileMode",
|
|
33
|
+
"RepositoryEntry",
|
|
34
|
+
"RepositoryTree",
|
|
35
|
+
"batch_entries",
|
|
36
|
+
"blob_sha",
|
|
37
|
+
"file_mode",
|
|
38
|
+
"scan_repositories",
|
|
39
|
+
"tree_sha",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
#: The largest file the server accepts, and the largest upload batch.
|
|
43
|
+
MAX_FILE_BYTES = 16 * 1024 * 1024
|
|
44
|
+
|
|
45
|
+
#: The git mode of a file: regular or executable.
|
|
46
|
+
type FileMode = Literal["100644", "100755"]
|
|
47
|
+
|
|
48
|
+
#: What the server accepts as a repository slug.
|
|
49
|
+
SLUG_RE = re.compile(r"^[a-z0-9][a-z0-9._\-]*$")
|
|
50
|
+
|
|
51
|
+
#: The longest slug the server accepts.
|
|
52
|
+
MAX_SLUG_LENGTH = 200
|
|
53
|
+
|
|
54
|
+
#: A directory in a git tree.
|
|
55
|
+
TREE_MODE = "40000"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class RepositoryEntry:
|
|
60
|
+
"""
|
|
61
|
+
One file of a repository, as the server and git name it.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
#: Relative to the repository folder, `/`-separated.
|
|
65
|
+
path: str
|
|
66
|
+
mode: FileMode
|
|
67
|
+
#: Lowercase-hex sha-256 of the bytes: the server's blob hash.
|
|
68
|
+
sha256: str
|
|
69
|
+
#: Lowercase-hex sha1 of `b"blob <size>\0" + bytes`, as `git hash-object`.
|
|
70
|
+
git_sha: str
|
|
71
|
+
size: int
|
|
72
|
+
#: The file on disk, read again only to upload it.
|
|
73
|
+
source: Path
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True)
|
|
77
|
+
class RepositoryTree:
|
|
78
|
+
"""
|
|
79
|
+
One folder under `repositories/`, scanned.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
slug: str
|
|
83
|
+
#: Sorted by `path`, byte order.
|
|
84
|
+
entries: tuple[RepositoryEntry, ...]
|
|
85
|
+
#: Sha1 of the root git tree built from `entries`.
|
|
86
|
+
tree_sha: str
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def size(self) -> int:
|
|
90
|
+
"""
|
|
91
|
+
Total bytes of the files.
|
|
92
|
+
"""
|
|
93
|
+
return sum(entry.size for entry in self.entries)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def scan_repositories(
|
|
97
|
+
root: Path, exclude: Sequence[str] = ()
|
|
98
|
+
) -> Iterator[RepositoryTree]:
|
|
99
|
+
"""
|
|
100
|
+
Yield every repository under `root / "repositories"`, sorted by slug.
|
|
101
|
+
|
|
102
|
+
Inside each folder: `.git` directories are skipped, empty directories
|
|
103
|
+
vanish, and the `.gitignore` files of the folder apply at every level
|
|
104
|
+
(gitwildmatch, `!` included, via `pathspec`); the `.gitignore` files
|
|
105
|
+
themselves are published, and the one at the course root does not
|
|
106
|
+
apply. `exclude` holds the `[push] exclude` patterns, matched against
|
|
107
|
+
paths relative to `root` (`repositories/<slug>/...`). The mode comes from
|
|
108
|
+
the owner's executable bit (`100644` everywhere on Windows). A missing
|
|
109
|
+
`repositories/` yields nothing.
|
|
110
|
+
|
|
111
|
+
Raises:
|
|
112
|
+
RepositoryError: A file sits directly in `repositories/`, a folder
|
|
113
|
+
name is not a valid slug, a symlink is found, a file exceeds
|
|
114
|
+
`MAX_FILE_BYTES`, or a folder has no file left to publish.
|
|
115
|
+
"""
|
|
116
|
+
base = root / REPOSITORIES_DIR
|
|
117
|
+
if not base.is_dir():
|
|
118
|
+
return
|
|
119
|
+
children = sorted(os.scandir(base), key=lambda child: child.name)
|
|
120
|
+
for child in children:
|
|
121
|
+
path = Path(child.path)
|
|
122
|
+
if child.is_symlink():
|
|
123
|
+
raise RepositoryError(f"{path} is a symlink")
|
|
124
|
+
if not child.is_dir():
|
|
125
|
+
raise RepositoryError(
|
|
126
|
+
f"{path} is a file: {REPOSITORIES_DIR}/ holds only repository folders"
|
|
127
|
+
)
|
|
128
|
+
if not SLUG_RE.match(child.name) or len(child.name) > MAX_SLUG_LENGTH:
|
|
129
|
+
raise RepositoryError(
|
|
130
|
+
f"{path}: {child.name!r} is not a valid repository slug "
|
|
131
|
+
"(lowercase letters, digits, '.', '_' and '-', starting with "
|
|
132
|
+
"a letter or digit)"
|
|
133
|
+
)
|
|
134
|
+
for child in children:
|
|
135
|
+
entries = tuple(
|
|
136
|
+
sorted(
|
|
137
|
+
_walk(root, Path(child.path), Path(child.path), exclude, []),
|
|
138
|
+
key=lambda entry: entry.path.encode("utf-8"),
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
if not entries:
|
|
142
|
+
raise RepositoryError(f"{child.path} has no file to publish")
|
|
143
|
+
yield RepositoryTree(child.name, entries, tree_sha(entries))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def blob_sha(data: bytes) -> str:
|
|
147
|
+
"""
|
|
148
|
+
The sha1 git gives a file with these bytes.
|
|
149
|
+
|
|
150
|
+
@example
|
|
151
|
+
>>> blob_sha(b"")
|
|
152
|
+
'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
|
|
153
|
+
"""
|
|
154
|
+
return hashlib.sha1(b"blob %d\0" % len(data) + data).hexdigest()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def tree_sha(entries: Sequence[RepositoryEntry]) -> str:
|
|
158
|
+
"""
|
|
159
|
+
The sha1 of the root tree git builds for these files.
|
|
160
|
+
|
|
161
|
+
Subdirectories become nested trees, entries are sorted in git order
|
|
162
|
+
(a directory compares as `name/`), and the result does not depend on
|
|
163
|
+
the order of `entries`. Matches `git write-tree` on the same checkout.
|
|
164
|
+
|
|
165
|
+
Raises:
|
|
166
|
+
ValueError: `entries` is empty, or a path is both a file and a
|
|
167
|
+
directory.
|
|
168
|
+
"""
|
|
169
|
+
if not entries:
|
|
170
|
+
raise ValueError("a tree needs at least one file")
|
|
171
|
+
root: _Node = {}
|
|
172
|
+
for entry in entries:
|
|
173
|
+
*directories, name = entry.path.split("/")
|
|
174
|
+
node = root
|
|
175
|
+
for directory in directories:
|
|
176
|
+
child = node.setdefault(directory, {})
|
|
177
|
+
if not isinstance(child, dict):
|
|
178
|
+
raise ValueError(f"{entry.path}: {directory!r} is also a file")
|
|
179
|
+
node = child
|
|
180
|
+
if name in node:
|
|
181
|
+
raise ValueError(f"{entry.path} is both a file and a directory")
|
|
182
|
+
node[name] = (entry.mode, entry.git_sha)
|
|
183
|
+
return _node_sha(root)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def file_mode(path: Path) -> FileMode:
|
|
187
|
+
"""
|
|
188
|
+
`100755` when the owner may execute `path`, `100644` otherwise or on
|
|
189
|
+
Windows.
|
|
190
|
+
"""
|
|
191
|
+
if sys.platform == "win32":
|
|
192
|
+
return "100644"
|
|
193
|
+
return "100755" if path.stat().st_mode & stat.S_IXUSR else "100644"
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def batch_entries(
|
|
197
|
+
entries: Sequence[RepositoryEntry], max_bytes: int = MAX_FILE_BYTES
|
|
198
|
+
) -> Iterator[tuple[RepositoryEntry, ...]]:
|
|
199
|
+
"""
|
|
200
|
+
Split files into upload batches of at most `max_bytes` each, in order.
|
|
201
|
+
|
|
202
|
+
Every batch holds at least one file; a file of exactly `max_bytes`
|
|
203
|
+
travels alone.
|
|
204
|
+
|
|
205
|
+
Raises:
|
|
206
|
+
ValueError: A file is larger than `max_bytes`.
|
|
207
|
+
"""
|
|
208
|
+
batch: list[RepositoryEntry] = []
|
|
209
|
+
total = 0
|
|
210
|
+
for entry in entries:
|
|
211
|
+
if entry.size > max_bytes:
|
|
212
|
+
raise ValueError(f"{entry.path} is larger than {max_bytes} bytes")
|
|
213
|
+
if batch and total + entry.size > max_bytes:
|
|
214
|
+
yield tuple(batch)
|
|
215
|
+
batch, total = [], 0
|
|
216
|
+
batch.append(entry)
|
|
217
|
+
total += entry.size
|
|
218
|
+
if total == max_bytes:
|
|
219
|
+
yield tuple(batch)
|
|
220
|
+
batch, total = [], 0
|
|
221
|
+
if batch:
|
|
222
|
+
yield tuple(batch)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
#
|
|
226
|
+
# Utilities
|
|
227
|
+
#
|
|
228
|
+
|
|
229
|
+
#: A directory is a mapping; a file is its `(mode, git sha)`.
|
|
230
|
+
type _Node = dict[str, _Node | tuple[str, str]]
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _node_sha(node: _Node) -> str:
|
|
234
|
+
"""
|
|
235
|
+
The sha1 of the git tree object for a nested directory mapping.
|
|
236
|
+
"""
|
|
237
|
+
items: list[tuple[bytes, bytes]] = []
|
|
238
|
+
for name, value in node.items():
|
|
239
|
+
raw = name.encode("utf-8")
|
|
240
|
+
if isinstance(value, dict):
|
|
241
|
+
mode, sha, key = TREE_MODE, _node_sha(value), raw + b"/"
|
|
242
|
+
else:
|
|
243
|
+
(mode, sha), key = value, raw
|
|
244
|
+
items.append((key, mode.encode() + b" " + raw + b"\0" + bytes.fromhex(sha)))
|
|
245
|
+
body = b"".join(content for _, content in sorted(items))
|
|
246
|
+
return hashlib.sha1(b"tree %d\0" % len(body) + body).hexdigest()
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _walk(
|
|
250
|
+
root: Path,
|
|
251
|
+
repository: Path,
|
|
252
|
+
directory: Path,
|
|
253
|
+
exclude: Sequence[str],
|
|
254
|
+
specs: list[tuple[str, pathspec.PathSpec]],
|
|
255
|
+
) -> Iterator[RepositoryEntry]:
|
|
256
|
+
"""
|
|
257
|
+
Yield the publishable files under `directory`, depth first.
|
|
258
|
+
|
|
259
|
+
`repository` is the folder of the repository being scanned. `specs` pairs each `.gitignore` found on the way down with the
|
|
260
|
+
directory it lives in, relative to the repository folder, outermost
|
|
261
|
+
first.
|
|
262
|
+
"""
|
|
263
|
+
prefix = directory.relative_to(repository).as_posix()
|
|
264
|
+
gitignore = directory / ".gitignore"
|
|
265
|
+
if gitignore.is_file() and not gitignore.is_symlink():
|
|
266
|
+
lines = gitignore.read_text(encoding="utf-8", errors="replace").splitlines()
|
|
267
|
+
specs = [
|
|
268
|
+
*specs,
|
|
269
|
+
(
|
|
270
|
+
"" if prefix == "." else prefix + "/",
|
|
271
|
+
pathspec.PathSpec.from_lines("gitignore", lines),
|
|
272
|
+
),
|
|
273
|
+
]
|
|
274
|
+
for child in sorted(os.scandir(directory), key=lambda item: item.name):
|
|
275
|
+
path = Path(child.path)
|
|
276
|
+
if child.name == ".git":
|
|
277
|
+
continue
|
|
278
|
+
relative = path.relative_to(repository).as_posix()
|
|
279
|
+
if is_excluded(path.relative_to(root).as_posix(), exclude):
|
|
280
|
+
continue
|
|
281
|
+
is_dir = child.is_dir(follow_symlinks=False)
|
|
282
|
+
if _ignored(relative + "/" if is_dir else relative, specs):
|
|
283
|
+
continue
|
|
284
|
+
if child.is_symlink():
|
|
285
|
+
raise RepositoryError(f"{path} is a symlink")
|
|
286
|
+
if is_dir:
|
|
287
|
+
yield from _walk(root, repository, path, exclude, specs)
|
|
288
|
+
elif child.is_file(follow_symlinks=False):
|
|
289
|
+
yield _read_entry(path, relative)
|
|
290
|
+
else:
|
|
291
|
+
raise RepositoryError(f"{path} is not a regular file")
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _ignored(relative: str, specs: list[tuple[str, pathspec.PathSpec]]) -> bool:
|
|
295
|
+
"""
|
|
296
|
+
Whether the nested `.gitignore` files ignore a path: the innermost rule
|
|
297
|
+
that matches decides, and within one file the last one.
|
|
298
|
+
"""
|
|
299
|
+
ignored = False
|
|
300
|
+
for base, spec in specs:
|
|
301
|
+
if not relative.startswith(base):
|
|
302
|
+
continue
|
|
303
|
+
result = spec.check_file(relative[len(base) :])
|
|
304
|
+
if result.include is not None:
|
|
305
|
+
ignored = result.include
|
|
306
|
+
return ignored
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _read_entry(path: Path, relative: str) -> RepositoryEntry:
|
|
310
|
+
"""
|
|
311
|
+
Hash one file into a `RepositoryEntry`.
|
|
312
|
+
"""
|
|
313
|
+
size = path.stat().st_size
|
|
314
|
+
if size > MAX_FILE_BYTES:
|
|
315
|
+
raise RepositoryError(
|
|
316
|
+
f"{path} is {size} bytes, over the {MAX_FILE_BYTES} byte limit"
|
|
317
|
+
)
|
|
318
|
+
data = path.read_bytes()
|
|
319
|
+
return RepositoryEntry(
|
|
320
|
+
path=relative,
|
|
321
|
+
mode=file_mode(path),
|
|
322
|
+
sha256=hashlib.sha256(data).hexdigest(),
|
|
323
|
+
git_sha=blob_sha(data),
|
|
324
|
+
size=len(data),
|
|
325
|
+
source=path,
|
|
326
|
+
)
|