auto-optimize 1.0.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.
- auto_optimize/__init__.py +3 -0
- auto_optimize/__main__.py +5 -0
- auto_optimize/cli.py +651 -0
- auto_optimize/skills/auto-optimize-code/LICENSE +21 -0
- auto_optimize/skills/auto-optimize-code/SKILL.md +251 -0
- auto_optimize/skills/auto-optimize-code/VERSION +1 -0
- auto_optimize/skills/auto-optimize-code/references/algorithm.md +182 -0
- auto_optimize/skills/auto-optimize-code/references/changes.md +146 -0
- auto_optimize/skills/auto-optimize-code/references/cli.md +206 -0
- auto_optimize/skills/auto-optimize-code/references/dashboard.md +203 -0
- auto_optimize/skills/auto-optimize-code/references/documents.md +55 -0
- auto_optimize/skills/auto-optimize-code/references/extending.md +87 -0
- auto_optimize/skills/auto-optimize-code/references/external-validation.md +98 -0
- auto_optimize/skills/auto-optimize-code/references/glossary.md +134 -0
- auto_optimize/skills/auto-optimize-code/references/idea-miner.md +108 -0
- auto_optimize/skills/auto-optimize-code/references/issue-bugs.md +182 -0
- auto_optimize/skills/auto-optimize-code/references/layout.md +300 -0
- auto_optimize/skills/auto-optimize-code/references/main-session.md +45 -0
- auto_optimize/skills/auto-optimize-code/references/measuring.md +208 -0
- auto_optimize/skills/auto-optimize-code/references/pr-optimization.md +127 -0
- auto_optimize/skills/auto-optimize-code/references/pr-tests.md +254 -0
- auto_optimize/skills/auto-optimize-code/references/promotion.md +133 -0
- auto_optimize/skills/auto-optimize-code/references/reading-a-search.md +65 -0
- auto_optimize/skills/auto-optimize-code/references/repo-mode.md +224 -0
- auto_optimize/skills/auto-optimize-code/references/resume.md +81 -0
- auto_optimize/skills/auto-optimize-code/references/shared-knowledge.md +114 -0
- auto_optimize/skills/auto-optimize-code/references/step0-interview.md +298 -0
- auto_optimize/skills/auto-optimize-code/references/step1-unit-tests.md +136 -0
- auto_optimize/skills/auto-optimize-code/references/step2-baseline.md +708 -0
- auto_optimize/skills/auto-optimize-code/references/step3-rounds.md +350 -0
- auto_optimize/skills/auto-optimize-code/references/step4-final.md +121 -0
- auto_optimize/skills/auto-optimize-code/references/step5-ship.md +220 -0
- auto_optimize/skills/auto-optimize-code/references/step6-finish.md +67 -0
- auto_optimize/skills/auto-optimize-code/references/subagent.md +467 -0
- auto_optimize/skills/auto-optimize-code/references/symptoms.md +67 -0
- auto_optimize/skills/auto-optimize-code/references/writing-briefs.md +117 -0
- auto_optimize/skills/auto-optimize-code/scripts/attribution.py +156 -0
- auto_optimize/skills/auto-optimize-code/scripts/autoopt.py +12863 -0
- auto_optimize/skills/auto-optimize-code/scripts/check_seeding.py +334 -0
- auto_optimize/skills/auto-optimize-code/scripts/cold.py +90 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/README.md +165 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/autoopt_monitor.py +850 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/gpu_scan.py +954 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/monitor_writer.py +684 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/scan.py +1030 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/dashboard.css +775 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/dashboard.js +3652 -0
- auto_optimize/skills/auto-optimize-code/scripts/dashboard/static/index.html +191 -0
- auto_optimize/skills/auto-optimize-code/scripts/issue_body.py +2118 -0
- auto_optimize/skills/auto-optimize-code/scripts/lint_text.py +448 -0
- auto_optimize/skills/auto-optimize-code/scripts/opt_pr_body.py +894 -0
- auto_optimize/skills/auto-optimize-code/scripts/opt_pr_tables.py +367 -0
- auto_optimize/skills/auto-optimize-code/scripts/paired.py +188 -0
- auto_optimize/skills/auto-optimize-code/scripts/preflight.py +280 -0
- auto_optimize/skills/auto-optimize-code/scripts/probe_run.py +72 -0
- auto_optimize/skills/auto-optimize-code/scripts/publish.py +385 -0
- auto_optimize/skills/auto-optimize-code/scripts/run_iteration.py +1382 -0
- auto_optimize/skills/auto-optimize-code/scripts/ship.py +1193 -0
- auto_optimize/skills/auto-optimize-code/scripts/stages.py +651 -0
- auto_optimize/skills/auto-optimize-code/scripts/tests_pr_body.py +1267 -0
- auto_optimize/skills/auto-optimize-code/scripts/tests_pr_push.py +426 -0
- auto_optimize/skills/auto-optimize-code/scripts/tests_pr_tables.py +779 -0
- auto_optimize-1.0.0.dist-info/METADATA +610 -0
- auto_optimize-1.0.0.dist-info/RECORD +67 -0
- auto_optimize-1.0.0.dist-info/WHEEL +4 -0
- auto_optimize-1.0.0.dist-info/entry_points.txt +2 -0
- auto_optimize-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""One vocabulary check for text a reader outside the search will see.
|
|
3
|
+
|
|
4
|
+
python3 lint_text.py body FILE [--labels A0,A1,...]
|
|
5
|
+
python3 lint_text.py title STRING [--labels ...]
|
|
6
|
+
python3 lint_text.py commits REPO REF BASE [--labels ...]
|
|
7
|
+
python3 lint_text.py diff-comments REPO REF BASE [--labels ...]
|
|
8
|
+
|
|
9
|
+
`body` checks a rendered body, `title` a title, `commits` the messages of
|
|
10
|
+
`BASE..REF`, `diff-comments` the comments on the lines `BASE..REF` adds. Exit
|
|
11
|
+
1 lists every hit with its line; exit 0 prints `no hits`.
|
|
12
|
+
|
|
13
|
+
Published text never names the search: its words (winner, winning iteration,
|
|
14
|
+
baseline, gate, "the search", "the base" for the original code, a round,
|
|
15
|
+
"stock"), the labels of its iterations (A175, when `--labels` or a
|
|
16
|
+
`branch.json` beside the text names them), or a local path (`~/`, `/home/`,
|
|
17
|
+
`/tmp/`, `$WORK`). Write `main`, or the target's own name, for the original.
|
|
18
|
+
|
|
19
|
+
Words that are also real code words pass: "binary search", "iteration over
|
|
20
|
+
the records" and "the base pair" are not hits. The exceptions are narrow and
|
|
21
|
+
fixed here:
|
|
22
|
+
|
|
23
|
+
* a search word inside an inline code span with no space in it (`--search`,
|
|
24
|
+
`search.c`, `binary_search`) is a file, option or function name; a path or
|
|
25
|
+
a label is a hit wherever it stands, and a fenced block is read as written;
|
|
26
|
+
* URLs and link targets are not read;
|
|
27
|
+
* a table cell shorter than 60 characters with no sentence in it is a label,
|
|
28
|
+
and is read strictly: there `search`, `iteration` and `base` are hits unless
|
|
29
|
+
`search` follows a qualifier such as binary or linear (`strict`), because a
|
|
30
|
+
label has no sentence around the word to say which sense it has.
|
|
31
|
+
|
|
32
|
+
`autoopt.py readme` and any script that writes such text call
|
|
33
|
+
`problems`; nothing here rewrites text.
|
|
34
|
+
|
|
35
|
+
Standard library only, Python 3.9 or later.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
import os
|
|
39
|
+
import re
|
|
40
|
+
import sys
|
|
41
|
+
import subprocess
|
|
42
|
+
import argparse
|
|
43
|
+
|
|
44
|
+
QUALIFIERS = (r'binary|linear|interpolation|bisect|bisection|string|substring|'
|
|
45
|
+
r'regex|regular expression|text|full-text|tree|hash|depth-first|'
|
|
46
|
+
r'breadth-first|grid|beam|exhaustive|prefix|suffix|pattern|name|path|'
|
|
47
|
+
r'file|index|database|lookup|keyword|fuzzy|sequence|motif|seed|k-mer')
|
|
48
|
+
|
|
49
|
+
# (rule, pattern, hint) read in prose.
|
|
50
|
+
WORDS = (
|
|
51
|
+
('winner', r'\bwinn(?:er|ers|ing)\b', 'say what the change is, not that it '
|
|
52
|
+
'won'),
|
|
53
|
+
('iteration', r'\b(?:winning|kept|promoted|best) iterations?\b|'
|
|
54
|
+
r'\biterations? (?:on|of) the (?:winner|path)\b', 'say "change" or '
|
|
55
|
+
'"commit"'),
|
|
56
|
+
('baseline', r'\bbaselines?\b', 'name the target branch (`main`)'),
|
|
57
|
+
('round', r'\brounds? \d+\b|\b(?:each|every|next|last|final|previous) '
|
|
58
|
+
r'rounds?\b(?! (?:to|of the (?:value|number|result)))|\brounds? of '
|
|
59
|
+
r'(?:the )?(?:search|iterations)\b', 'a round is a unit of the search'),
|
|
60
|
+
('gate', r'\bgat(?:e|es|ed|ing)\b', 'say what is checked'),
|
|
61
|
+
('search', r'\b(?:the|this|our|that|whole|final|last) search\b|'
|
|
62
|
+
r"\bsearch(?:es)?'s\b|\bsearch (?:ended|stopped|found|kept|promoted|ran"
|
|
63
|
+
r'|directory|run)\b', 'the search is the tool\'s own process'),
|
|
64
|
+
('base', r"\bthe base(?:'s)?\b(?! (?:branch|class|classes|image|layer|url|"
|
|
65
|
+
r"address|pair|pairs|64|of|type|case|conversion|quality|qualities|call|"
|
|
66
|
+
r"calls|rate|position|positions|\d))|\bbase (?:build|binary|time|"
|
|
67
|
+
r'median|iteration)\b', 'name the target branch (`main`)'),
|
|
68
|
+
('stock', r'\bstock\b', 'name the target branch or its release'),
|
|
69
|
+
('tool', r'\bauto-optimize\b(?!/)|\bProgrammable Genomics\b', 'a '
|
|
70
|
+
'posted document does not name the tool or the lab'),
|
|
71
|
+
('notice', r'(?i)\bgenerated (?:with|by) \[?claude\b|\bwritten (?:by|with) '
|
|
72
|
+
r'(?:claude|an? (?:ai|llm|model))\b', 'a posted document says '
|
|
73
|
+
'nothing of how it was written'),
|
|
74
|
+
)
|
|
75
|
+
# Read in a label: a table cell with no sentence.
|
|
76
|
+
STRICT = (
|
|
77
|
+
('search', r'\b((?:' + QUALIFIERS + r')\s+)?search(?:es)?\b', 'a label '
|
|
78
|
+
'names the input or the command, not the search'),
|
|
79
|
+
('iteration', r'\biterations?\b', 'a label names the input or the command'),
|
|
80
|
+
('base', r'\bbase\b', 'a label names the input or the command'),
|
|
81
|
+
)
|
|
82
|
+
PATHS = (
|
|
83
|
+
('local path', r'(?<![\w/.\-])(?:~/|/home/|/tmp/|/scratch/|/mnt/|/Users/|'
|
|
84
|
+
r'/dev/shm/|/var/tmp/)\S*|(?<![\w/.\-])/tmp\b|\$WORK\b|'
|
|
85
|
+
r'\bautooptimize/', 'a path on this machine means '
|
|
86
|
+
'nothing to a reader'),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
URL = re.compile(r'\]\([^)]*\)|https?://\S+')
|
|
90
|
+
SPAN = re.compile(r'`([^`]*)`')
|
|
91
|
+
FENCE = re.compile(r'^\s*(```|~~~)')
|
|
92
|
+
LABEL_CELL = 60
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Hit(object):
|
|
96
|
+
def __init__(self, line, rule, word, hint, text):
|
|
97
|
+
self.line, self.rule, self.word, self.hint, self.text = (line, rule,
|
|
98
|
+
word, hint, text)
|
|
99
|
+
|
|
100
|
+
def __str__(self):
|
|
101
|
+
return '{}: {}: "{}" ({}) in: {}'.format(self.line, self.rule, self.word,
|
|
102
|
+
self.hint, self.text.strip()[:110])
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def blank(match):
|
|
106
|
+
return ' ' * len(match.group(0))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def code_masks(text, inside):
|
|
110
|
+
"""`(text with every identifier-like code span blanked, whether the line
|
|
111
|
+
holds a span)`: a span of one word is a name."""
|
|
112
|
+
|
|
113
|
+
def one(match):
|
|
114
|
+
return blank(match) if not re.search(r'\s', match.group(1)) else \
|
|
115
|
+
match.group(0)
|
|
116
|
+
|
|
117
|
+
return SPAN.sub(one, text)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def label_regex(labels):
|
|
121
|
+
labels = sorted({label for label in labels or () if label}, key=len,
|
|
122
|
+
reverse=True)
|
|
123
|
+
|
|
124
|
+
if not labels:
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
return re.compile(r'(?<![A-Za-z0-9_.\-])(?:' + '|'.join(re.escape(label)
|
|
128
|
+
for label in labels) + r')(?![A-Za-z0-9_])')
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def is_label_cell(cell):
|
|
132
|
+
cell = cell.strip()
|
|
133
|
+
return bool(cell) and len(cell) < LABEL_CELL and not re.search(r'[.!?]\s|'
|
|
134
|
+
r'[.!?]$', cell) and not re.fullmatch(r'[-: ]+', cell)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def scan(line, number, words, labels, hits, fenced=False):
|
|
138
|
+
"""Add the hits of one line to `hits`: `words` are read in what is left
|
|
139
|
+
after the names in code spans and the URLs are blanked."""
|
|
140
|
+
|
|
141
|
+
plain = URL.sub(blank, line)
|
|
142
|
+
named = plain if fenced else code_masks(plain, False)
|
|
143
|
+
|
|
144
|
+
for rule, pattern, hint in words:
|
|
145
|
+
for match in re.finditer(pattern, named, re.I):
|
|
146
|
+
if match.re.groups and match.group(1):
|
|
147
|
+
continue
|
|
148
|
+
|
|
149
|
+
hits.append(Hit(number, rule, match.group(0), hint, line))
|
|
150
|
+
|
|
151
|
+
for rule, pattern, hint in PATHS:
|
|
152
|
+
for match in re.finditer(pattern, plain):
|
|
153
|
+
hits.append(Hit(number, rule, match.group(0), hint, line))
|
|
154
|
+
|
|
155
|
+
if labels:
|
|
156
|
+
for match in labels.finditer(plain):
|
|
157
|
+
hits.append(Hit(number, 'label', match.group(0), 'describe the '
|
|
158
|
+
'change by what it does', line))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def check(text, labels=None, strict=False):
|
|
162
|
+
"""The hits of `text`, a list of `Hit`, in line order. `labels` are the
|
|
163
|
+
iteration labels (a list). `strict` reads every line as a label."""
|
|
164
|
+
|
|
165
|
+
found = label_regex(labels)
|
|
166
|
+
hits, fenced = [], False
|
|
167
|
+
|
|
168
|
+
for number, line in enumerate(text.splitlines(), 1):
|
|
169
|
+
if FENCE.match(line):
|
|
170
|
+
fenced = not fenced
|
|
171
|
+
continue
|
|
172
|
+
|
|
173
|
+
if line.lstrip().startswith('|') and not fenced:
|
|
174
|
+
cells = line.strip().strip('|').split('|')
|
|
175
|
+
|
|
176
|
+
for cell in cells:
|
|
177
|
+
scan(cell, number, WORDS + (STRICT if is_label_cell(cell) else
|
|
178
|
+
()), found, hits)
|
|
179
|
+
|
|
180
|
+
continue
|
|
181
|
+
|
|
182
|
+
scan(line, number, WORDS + (STRICT if strict else ()), found, hits,
|
|
183
|
+
fenced)
|
|
184
|
+
|
|
185
|
+
return hits
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def problems(text, where, labels=None, strict=False):
|
|
189
|
+
"""`check` as strings for a refusal list: `where:LINE: rule: ...`."""
|
|
190
|
+
|
|
191
|
+
return ['{}:{}'.format(where, hit) for hit in check(text, labels, strict)]
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def cell_problems(value, where, advice, labels=None):
|
|
195
|
+
"""A label in a table (`value`, one cell), read strictly. `where` says
|
|
196
|
+
which row, `advice` which field of the data to change."""
|
|
197
|
+
|
|
198
|
+
return ['{}: {} ({})'.format(where, str(hit).split(': ', 1)[1], advice)
|
|
199
|
+
for hit in check(value, labels, strict=True)]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
# Comments of a diff ----------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
C_LIKE = ('.c', '.h', '.cc', '.cpp', '.cxx', '.hpp', '.hh', '.cu', '.cuh', '.java',
|
|
205
|
+
'.js', '.ts', '.go', '.rs', '.cs', '.swift', '.kt', '.scala', '.cl', '.metal')
|
|
206
|
+
HASH_LIKE = ('.py', '.pyx', '.pxd', '.sh', '.bash', '.rb', '.pl', '.pm', '.mk',
|
|
207
|
+
'.yml', '.yaml', '.toml', '.cfg', '.conf', '.cmake', '.r', '.jl', '.nim')
|
|
208
|
+
HASH_NAMES = ('Makefile', 'makefile', 'GNUmakefile', 'CMakeLists.txt',
|
|
209
|
+
'Dockerfile')
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def after_code(line, marker, shell):
|
|
213
|
+
"""The text after the first `marker` that is outside a string, or None."""
|
|
214
|
+
|
|
215
|
+
quote, index = None, 0
|
|
216
|
+
|
|
217
|
+
while index < len(line):
|
|
218
|
+
char = line[index]
|
|
219
|
+
|
|
220
|
+
if quote:
|
|
221
|
+
if char == '\\':
|
|
222
|
+
index += 1
|
|
223
|
+
elif char == quote:
|
|
224
|
+
quote = None
|
|
225
|
+
elif char in '"\'':
|
|
226
|
+
quote = char
|
|
227
|
+
elif line.startswith(marker, index):
|
|
228
|
+
before = line[index - 1] if index else ' '
|
|
229
|
+
|
|
230
|
+
if marker == '#' and before not in ' \t' and index:
|
|
231
|
+
pass
|
|
232
|
+
elif marker == '//' and before == ':':
|
|
233
|
+
pass
|
|
234
|
+
else:
|
|
235
|
+
return line[index + len(marker):]
|
|
236
|
+
|
|
237
|
+
index += 1
|
|
238
|
+
|
|
239
|
+
return None
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def comments_of(path, lines):
|
|
243
|
+
"""`[(number, comment text)]` for the added `lines` (`[(number, text)]`,
|
|
244
|
+
each hunk's run of lines in order) of the file `path`."""
|
|
245
|
+
|
|
246
|
+
name = os.path.basename(path)
|
|
247
|
+
ext = os.path.splitext(name)[1].lower()
|
|
248
|
+
found = []
|
|
249
|
+
|
|
250
|
+
if ext in C_LIKE:
|
|
251
|
+
block = False
|
|
252
|
+
|
|
253
|
+
for number, line in lines:
|
|
254
|
+
text = None
|
|
255
|
+
stripped = line.strip()
|
|
256
|
+
|
|
257
|
+
if block:
|
|
258
|
+
text = line
|
|
259
|
+
block = '*/' not in line
|
|
260
|
+
elif re.match(r'^\*(?:\s|/|$)', stripped):
|
|
261
|
+
text = line
|
|
262
|
+
elif '/*' in line:
|
|
263
|
+
text = line[line.index('/*'):]
|
|
264
|
+
block = '*/' not in text
|
|
265
|
+
else:
|
|
266
|
+
text = after_code(line, '//', False)
|
|
267
|
+
|
|
268
|
+
if text:
|
|
269
|
+
found.append((number, text))
|
|
270
|
+
elif ext in HASH_LIKE or name in HASH_NAMES:
|
|
271
|
+
python = ext in ('.py', '.pyx', '.pxd')
|
|
272
|
+
opened = None
|
|
273
|
+
|
|
274
|
+
for number, line in lines:
|
|
275
|
+
text = None
|
|
276
|
+
|
|
277
|
+
if python:
|
|
278
|
+
for quote in ('"""', "'''"):
|
|
279
|
+
count = line.count(quote)
|
|
280
|
+
|
|
281
|
+
if opened == quote:
|
|
282
|
+
text = line
|
|
283
|
+
opened = None if count % 2 else opened
|
|
284
|
+
elif opened is None and count:
|
|
285
|
+
text = line[line.index(quote):]
|
|
286
|
+
opened = quote if count % 2 else None
|
|
287
|
+
|
|
288
|
+
if text is not None:
|
|
289
|
+
break
|
|
290
|
+
|
|
291
|
+
if text is None and opened is None:
|
|
292
|
+
text = after_code(line, '#', not python)
|
|
293
|
+
|
|
294
|
+
if text and not text.startswith('!'):
|
|
295
|
+
found.append((number, text))
|
|
296
|
+
|
|
297
|
+
return found
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def added_lines(repo, ref, base):
|
|
301
|
+
"""`{path: [(number, text)]}` of the lines `BASE..REF` adds."""
|
|
302
|
+
|
|
303
|
+
done = subprocess.run(['git', '-C', repo, 'diff', '-U0', '--no-color',
|
|
304
|
+
'--no-renames', base, ref], capture_output=True, text=True,
|
|
305
|
+
errors='replace')
|
|
306
|
+
|
|
307
|
+
if done.returncode != 0:
|
|
308
|
+
raise SystemExit('lint_text: git diff {} {} failed: {}'.format(base,
|
|
309
|
+
ref, done.stderr.strip()))
|
|
310
|
+
|
|
311
|
+
found, path, number = {}, None, 0
|
|
312
|
+
|
|
313
|
+
for line in done.stdout.splitlines():
|
|
314
|
+
if line.startswith('+++ '):
|
|
315
|
+
path = line[6:] if line.startswith('+++ b/') else None
|
|
316
|
+
elif line.startswith('@@'):
|
|
317
|
+
match = re.search(r'\+(\d+)', line)
|
|
318
|
+
number = int(match.group(1)) if match else 0
|
|
319
|
+
elif line.startswith('+') and path:
|
|
320
|
+
found.setdefault(path, []).append((number, line[1:]))
|
|
321
|
+
number += 1
|
|
322
|
+
|
|
323
|
+
return found
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def diff_problems(repo, ref, base, labels=None):
|
|
327
|
+
"""The hits in the comments the branch adds, as `path:line: ...`."""
|
|
328
|
+
|
|
329
|
+
out = []
|
|
330
|
+
|
|
331
|
+
for path, lines in sorted(added_lines(repo, ref, base).items()):
|
|
332
|
+
for number, text in comments_of(path, lines):
|
|
333
|
+
for hit in check(text, labels):
|
|
334
|
+
hit.line = number
|
|
335
|
+
out.append('{}:{}'.format(path, hit))
|
|
336
|
+
|
|
337
|
+
return out
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def commit_problems(repo, ref, base, labels=None):
|
|
341
|
+
"""The hits in the messages of `BASE..REF`, as `<short sha>:line: ...`."""
|
|
342
|
+
|
|
343
|
+
done = subprocess.run(['git', '-C', repo, 'log', '--format=%h%x00%B%x01',
|
|
344
|
+
'{}..{}'.format(base, ref)], capture_output=True, text=True,
|
|
345
|
+
errors='replace')
|
|
346
|
+
|
|
347
|
+
if done.returncode != 0:
|
|
348
|
+
raise SystemExit('lint_text: git log {}..{} failed: {}'.format(base,
|
|
349
|
+
ref, done.stderr.strip()))
|
|
350
|
+
|
|
351
|
+
out = []
|
|
352
|
+
|
|
353
|
+
for entry in done.stdout.split('\x01'):
|
|
354
|
+
if '\x00' not in entry:
|
|
355
|
+
continue
|
|
356
|
+
|
|
357
|
+
sha, message = entry.strip('\n').split('\x00', 1)
|
|
358
|
+
out += problems(message, 'commit ' + sha.strip(), labels)
|
|
359
|
+
|
|
360
|
+
return out
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def message_problems(directory, labels=None):
|
|
364
|
+
"""The hits in every `<id>.txt` commit message file of `directory`."""
|
|
365
|
+
|
|
366
|
+
out = []
|
|
367
|
+
|
|
368
|
+
if os.path.isdir(directory):
|
|
369
|
+
for name in sorted(os.listdir(directory)):
|
|
370
|
+
if name.endswith('.txt'):
|
|
371
|
+
with open(os.path.join(directory, name), encoding='utf-8') as handle:
|
|
372
|
+
out += problems(handle.read(), os.path.join(os.path.basename(
|
|
373
|
+
directory), name), labels)
|
|
374
|
+
|
|
375
|
+
return out
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def branch_problems(bare, ref, base, messages_dir, labels=None):
|
|
379
|
+
"""The hits in the commit messages written for a branch and in the comments
|
|
380
|
+
its branch adds to `base`: what is refused before anything is pushed or
|
|
381
|
+
built."""
|
|
382
|
+
|
|
383
|
+
return message_problems(messages_dir, labels) + diff_problems(bare, ref,
|
|
384
|
+
base, labels)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def branch_labels(directory):
|
|
388
|
+
"""The labels in `directory/branch.json`, or []."""
|
|
389
|
+
|
|
390
|
+
import json
|
|
391
|
+
|
|
392
|
+
try:
|
|
393
|
+
with open(os.path.join(directory, 'branch.json'), encoding='utf-8') as handle:
|
|
394
|
+
return list(json.load(handle).get('labels') or [])
|
|
395
|
+
except (OSError, ValueError):
|
|
396
|
+
return []
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def report(found, what):
|
|
400
|
+
if found:
|
|
401
|
+
sys.stderr.write('lint_text: {} hit(s) in {}:\n'.format(len(found), what)
|
|
402
|
+
+ ''.join(' {}\n'.format(item) for item in found))
|
|
403
|
+
return 1
|
|
404
|
+
|
|
405
|
+
print('lint_text: no hits in {}'.format(what))
|
|
406
|
+
return 0
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def main(argv=None):
|
|
410
|
+
parser = argparse.ArgumentParser(description=__doc__,
|
|
411
|
+
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
412
|
+
commands = parser.add_subparsers(dest='command', required=True)
|
|
413
|
+
|
|
414
|
+
for name, arguments in (('body', ['file']), ('title', ['string']),
|
|
415
|
+
('commits', ['repo', 'ref', 'base']), ('diff-comments', ['repo',
|
|
416
|
+
'ref', 'base'])):
|
|
417
|
+
sub = commands.add_parser(name)
|
|
418
|
+
|
|
419
|
+
for argument in arguments:
|
|
420
|
+
sub.add_argument(argument)
|
|
421
|
+
|
|
422
|
+
sub.add_argument('--labels', default='', help='comma-separated '
|
|
423
|
+
'iteration labels (body: default the labels of branch.json beside '
|
|
424
|
+
'the file)')
|
|
425
|
+
|
|
426
|
+
args = parser.parse_args(argv)
|
|
427
|
+
labels = [one for one in args.labels.split(',') if one]
|
|
428
|
+
|
|
429
|
+
if args.command == 'body':
|
|
430
|
+
if not labels:
|
|
431
|
+
labels = branch_labels(os.path.dirname(os.path.abspath(args.file)))
|
|
432
|
+
|
|
433
|
+
with open(args.file, encoding='utf-8') as handle:
|
|
434
|
+
return report(problems(handle.read(), args.file, labels), args.file)
|
|
435
|
+
|
|
436
|
+
if args.command == 'title':
|
|
437
|
+
return report(problems(args.string, 'title', labels), 'the title')
|
|
438
|
+
|
|
439
|
+
if args.command == 'commits':
|
|
440
|
+
return report(commit_problems(args.repo, args.ref, args.base, labels),
|
|
441
|
+
'the commit messages')
|
|
442
|
+
|
|
443
|
+
return report(diff_problems(args.repo, args.ref, args.base, labels),
|
|
444
|
+
'the added comments')
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
if __name__ == '__main__':
|
|
448
|
+
sys.exit(main())
|