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,426 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Push step 1's tests branch and open the unit-test PR.
|
|
3
|
+
|
|
4
|
+
python3 tests_pr_push.py DIR --repo OWNER/REPO # preview: pushes and opens nothing
|
|
5
|
+
python3 tests_pr_push.py DIR --repo OWNER/REPO --post # push the branch, open the PR
|
|
6
|
+
python3 tests_pr_push.py DIR --repo OWNER/REPO --edit # replace the open PR's title and body
|
|
7
|
+
|
|
8
|
+
DIR is `$WORK/ship/pr-tests`, holding `body.md` and `body.title`
|
|
9
|
+
(`tests_pr_body.py --out`), and `files.tsv` and `branch.json`
|
|
10
|
+
(`tests_pr_tables.py files`). The branch's commits are pushed as they are,
|
|
11
|
+
from the clone step 1 wrote them in; nothing here commits. Each default below
|
|
12
|
+
gives way to its flag:
|
|
13
|
+
|
|
14
|
+
- the clone, the branch and the commit it was made from (`--suite`,
|
|
15
|
+
`--branch`, `--base`): `DIR/branch.json`;
|
|
16
|
+
- the branch the PR targets (`--target-branch`): `branch` in
|
|
17
|
+
`$WORK/tests/branch.json`, which `autoopt.py tests-branch` wrote, else the
|
|
18
|
+
original's branch as the search's config.json records it;
|
|
19
|
+
- the remote pushed to (`--remote`): `after.pr_remote` of the search, else
|
|
20
|
+
origin; a remote name the clone does not know is read from the original
|
|
21
|
+
repository (`$WORK/tests/branch.json`'s `path`), else taken as a URL;
|
|
22
|
+
- the name the branch gets there (`--remote-branch`): its own,
|
|
23
|
+
`auto-optimize/<date>-unit-tests`, when the user chose that prefix at step 5
|
|
24
|
+
(`after.branch_prefix` yes), else `unit-tests-<date>`;
|
|
25
|
+
- the PR's head (`--head OWNER:BRANCH`): `after.pr_head_owner:BRANCH` when the
|
|
26
|
+
remote came from `after.pr_remote`, else that name.
|
|
27
|
+
|
|
28
|
+
The preview prints the repository, the target branch, the head, the title,
|
|
29
|
+
the commit's files and what `--post` needs; its one `gh` call reads the
|
|
30
|
+
account `gh` is logged in as. It refuses, listing every problem, when
|
|
31
|
+
attribution.py rejects the title or the body (a `#<bug issue>` still in it,
|
|
32
|
+
say); the body is not what the tables and prose render to; files.tsv is not
|
|
33
|
+
the branch's files; the clone has uncommitted changes; a commit lacks
|
|
34
|
+
after.commit_trailer; or `DIR/pr.json` names another repository.
|
|
35
|
+
|
|
36
|
+
`--post` pushes the branch, runs `gh pr create` and records the PR in
|
|
37
|
+
`DIR/pr.json`; once that record exists, `--post` is refused. `--edit`
|
|
38
|
+
replaces the open PR's title and body (`gh pr edit`) and pushes nothing. When
|
|
39
|
+
OWNER is not the account `gh` is logged in as, both also need
|
|
40
|
+
`--other-owner`, given once the user has confirmed the repository.
|
|
41
|
+
`--allow-source PATH` is the renderer's flag of that name, passed on: give the
|
|
42
|
+
push the flags body.md was rendered with.
|
|
43
|
+
|
|
44
|
+
Standard library only, Python 3.9 or later; needs git, and gh for `--post`
|
|
45
|
+
and `--edit`.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
import os
|
|
49
|
+
import re
|
|
50
|
+
import sys
|
|
51
|
+
import json
|
|
52
|
+
import shutil
|
|
53
|
+
import argparse
|
|
54
|
+
import tempfile
|
|
55
|
+
|
|
56
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
57
|
+
|
|
58
|
+
import attribution
|
|
59
|
+
import autoopt
|
|
60
|
+
import publish
|
|
61
|
+
|
|
62
|
+
RECORD = 'pr.json'
|
|
63
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
64
|
+
|
|
65
|
+
run = publish.run
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def refuse(problems, left='nothing done'):
|
|
69
|
+
return publish.refuse('tests_pr_push', problems, left)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def rendered(directory, title, allow_source=()):
|
|
73
|
+
"""The body the tables and prose render to, or None and the reasons they
|
|
74
|
+
do not."""
|
|
75
|
+
|
|
76
|
+
with tempfile.TemporaryDirectory() as scratch:
|
|
77
|
+
out = os.path.join(scratch, 'body.md')
|
|
78
|
+
code, _, err = run([sys.executable, os.path.join(HERE,
|
|
79
|
+
'tests_pr_body.py'), directory, '--title', title, '--out', out]
|
|
80
|
+
+ [flag for path in allow_source for flag in ('--allow-source', path)])
|
|
81
|
+
|
|
82
|
+
if code != 0:
|
|
83
|
+
return None, err
|
|
84
|
+
|
|
85
|
+
return publish.read(out), ''
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def search_config(work):
|
|
89
|
+
"""The search's config.json, or {} when `work` holds no search."""
|
|
90
|
+
|
|
91
|
+
path = os.path.join(work, 'autooptimize', 'config.json')
|
|
92
|
+
|
|
93
|
+
return autoopt.read_json(path) if os.path.exists(path) else {}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def target_branch(work, config):
|
|
97
|
+
"""(the original's branch, where it was read), or (None, None)."""
|
|
98
|
+
|
|
99
|
+
record = autoopt.work_path(work, 'tests-branch')
|
|
100
|
+
|
|
101
|
+
if os.path.exists(record):
|
|
102
|
+
branch = autoopt.read_json(record).get('branch')
|
|
103
|
+
|
|
104
|
+
if branch:
|
|
105
|
+
return branch, record
|
|
106
|
+
|
|
107
|
+
if config:
|
|
108
|
+
# A search made before `tests-branch` wrote its record.
|
|
109
|
+
try:
|
|
110
|
+
branch = autoopt.original_of(work, config).get('branch')
|
|
111
|
+
except (RuntimeError, OSError, KeyError):
|
|
112
|
+
branch = None
|
|
113
|
+
|
|
114
|
+
if branch:
|
|
115
|
+
return branch, 'the search\'s config.json'
|
|
116
|
+
|
|
117
|
+
return None, None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def git_problems(args, work):
|
|
121
|
+
"""(problems, the branch's changed files as `git diff --name-status`
|
|
122
|
+
prints them, the number of commits over the base)."""
|
|
123
|
+
|
|
124
|
+
problems = []
|
|
125
|
+
span = '{}...{}'.format(args.base, args.branch)
|
|
126
|
+
code, count, err = run(['git', '-C', args.suite, 'rev-list', '--count',
|
|
127
|
+
'{}..{}'.format(args.base, args.branch)])
|
|
128
|
+
|
|
129
|
+
if code != 0:
|
|
130
|
+
return ['git: {}..{}: {}'.format(args.base, args.branch, err)], '', None
|
|
131
|
+
|
|
132
|
+
code, out, err = run(['git', '-C', args.suite, 'status', '--porcelain',
|
|
133
|
+
'--untracked-files=no'])
|
|
134
|
+
|
|
135
|
+
if out:
|
|
136
|
+
problems.append('{} has uncommitted changes:\n {}'.format(args.suite,
|
|
137
|
+
out.replace('\n', '\n ')))
|
|
138
|
+
|
|
139
|
+
trailer = autoopt.commit_trailer(os.path.join(work, 'autooptimize'))
|
|
140
|
+
|
|
141
|
+
if trailer:
|
|
142
|
+
code, out, err = run(['git', '-C', args.suite, 'log', '--format=%H%n%B'
|
|
143
|
+
'%x00', '{}..{}'.format(args.base, args.branch)])
|
|
144
|
+
|
|
145
|
+
for entry in out.split('\0'):
|
|
146
|
+
head, _, message = entry.strip().partition('\n')
|
|
147
|
+
|
|
148
|
+
for problem in autoopt.trailer_problems(message, trailer) if head \
|
|
149
|
+
else []:
|
|
150
|
+
problems.append('commit {}: {}'.format(head[:10], problem))
|
|
151
|
+
|
|
152
|
+
code, changed, err = run(['git', '-C', args.suite, 'diff', '--name-status',
|
|
153
|
+
'--no-renames', span])
|
|
154
|
+
|
|
155
|
+
if code != 0:
|
|
156
|
+
problems.append('git diff {}: {}'.format(span, err))
|
|
157
|
+
|
|
158
|
+
return problems, changed, count
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def listed_files(directory):
|
|
162
|
+
"""files.tsv as sorted (status, path) pairs, or None when it is absent."""
|
|
163
|
+
|
|
164
|
+
path = os.path.join(directory, 'files.tsv')
|
|
165
|
+
|
|
166
|
+
if not os.path.exists(path):
|
|
167
|
+
return None
|
|
168
|
+
|
|
169
|
+
rows = [line.split('\t') for line in publish.read(path).splitlines()[1:]
|
|
170
|
+
if line.strip()]
|
|
171
|
+
|
|
172
|
+
return sorted((row[0][:1], row[1]) for row in rows if len(row) > 1)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def read_record(directory):
|
|
176
|
+
path = os.path.join(directory, RECORD)
|
|
177
|
+
|
|
178
|
+
if not os.path.exists(path):
|
|
179
|
+
return None
|
|
180
|
+
|
|
181
|
+
return json.loads(publish.read(path))
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def main(argv=None):
|
|
185
|
+
parser = argparse.ArgumentParser(description='Preview, then push step 1\'s '
|
|
186
|
+
'tests branch and open the unit-test PR. Pushes and opens nothing '
|
|
187
|
+
'without --post or --edit.')
|
|
188
|
+
parser.add_argument('dir', help='$WORK/ship/pr-tests: body.md, body.title, '
|
|
189
|
+
'files.tsv and branch.json')
|
|
190
|
+
parser.add_argument('--repo', required=True, help='OWNER/REPO the PR '
|
|
191
|
+
'opens on')
|
|
192
|
+
parser.add_argument('--suite', help='the clone holding the tests branch '
|
|
193
|
+
'(default: DIR/branch.json, which `tests_pr_tables.py files` wrote)')
|
|
194
|
+
parser.add_argument('--branch', help='the tests branch (default: '
|
|
195
|
+
'DIR/branch.json)')
|
|
196
|
+
parser.add_argument('--base', help='the commit the tests branch was made '
|
|
197
|
+
'from (default: DIR/branch.json)')
|
|
198
|
+
parser.add_argument('--remote-branch', help='the name the branch gets '
|
|
199
|
+
'on the remote (default: its own, auto-optimize/<date>-unit-tests, '
|
|
200
|
+
'when the search\'s after.branch_prefix is yes, else '
|
|
201
|
+
'unit-tests-<date>)')
|
|
202
|
+
parser.add_argument('--target-branch', help='the branch of the original '
|
|
203
|
+
'the PR targets (default: `branch` in $WORK/tests/branch.json, else the '
|
|
204
|
+
'original\'s branch in the search\'s config.json)')
|
|
205
|
+
parser.add_argument('--remote', help='the remote to push to (default: '
|
|
206
|
+
'after.pr_remote of the search, else origin)')
|
|
207
|
+
parser.add_argument('--head', help='OWNER:BRANCH for a fork (default: '
|
|
208
|
+
'after.pr_head_owner:BRANCH when the remote is after.pr_remote, else the '
|
|
209
|
+
'branch)')
|
|
210
|
+
parser.add_argument('--allow-source', action='append', default=[],
|
|
211
|
+
metavar='PATH', help='a changed file outside the tests that the user '
|
|
212
|
+
'approved by name (repeatable); the same flag tests_pr_body.py took when '
|
|
213
|
+
'it rendered body.md, which is rendered again with it here')
|
|
214
|
+
parser.add_argument('--post', action='store_true', help='push the branch '
|
|
215
|
+
'and open the PR')
|
|
216
|
+
parser.add_argument('--edit', action='store_true', help='replace the open '
|
|
217
|
+
'PR\'s title and body; pushes nothing')
|
|
218
|
+
parser.add_argument('--other-owner', action='store_true', help='confirm a '
|
|
219
|
+
'repository owned by another account')
|
|
220
|
+
args = parser.parse_args(argv)
|
|
221
|
+
|
|
222
|
+
problems = []
|
|
223
|
+
args.dir = os.path.abspath(args.dir)
|
|
224
|
+
work = os.path.dirname(os.path.dirname(args.dir))
|
|
225
|
+
config = search_config(work)
|
|
226
|
+
after = config.get('after') or {}
|
|
227
|
+
remote_from = '--remote'
|
|
228
|
+
|
|
229
|
+
if not args.remote and after.get('pr_remote'):
|
|
230
|
+
args.remote, remote_from = after['pr_remote'], 'after.pr_remote'
|
|
231
|
+
elif not args.remote:
|
|
232
|
+
args.remote, remote_from = 'origin', 'the default'
|
|
233
|
+
recorded = os.path.join(args.dir, 'branch.json')
|
|
234
|
+
|
|
235
|
+
if os.path.exists(recorded):
|
|
236
|
+
known = json.loads(publish.read(recorded))
|
|
237
|
+
args.suite = args.suite or known.get('suite')
|
|
238
|
+
args.base = args.base or known.get('base')
|
|
239
|
+
args.branch = args.branch or known.get('branch')
|
|
240
|
+
|
|
241
|
+
for flag in ('suite', 'branch', 'base'):
|
|
242
|
+
if not getattr(args, flag):
|
|
243
|
+
problems.append('no --{}, and {} does not hold one (`tests_pr_tables.py '
|
|
244
|
+
'files` writes it)'.format(flag, recorded))
|
|
245
|
+
|
|
246
|
+
target_from = 'the flags'
|
|
247
|
+
|
|
248
|
+
if not args.target_branch:
|
|
249
|
+
args.target_branch, target_from = target_branch(work, config)
|
|
250
|
+
|
|
251
|
+
if not args.target_branch:
|
|
252
|
+
problems.append('no --target-branch, and neither {} (`autoopt.py '
|
|
253
|
+
'tests-branch` writes it) nor the search\'s config.json names the '
|
|
254
|
+
'original\'s branch'.format(autoopt.work_path(work, 'tests-branch')))
|
|
255
|
+
|
|
256
|
+
if args.branch and not args.remote_branch:
|
|
257
|
+
args.remote_branch = autoopt.tests_branch_remote(config, args.branch)
|
|
258
|
+
|
|
259
|
+
if not args.head and remote_from == 'after.pr_remote' \
|
|
260
|
+
and after.get('pr_head_owner') and args.branch:
|
|
261
|
+
args.head = '{}:{}'.format(after['pr_head_owner'], args.remote_branch)
|
|
262
|
+
|
|
263
|
+
head = args.head or args.remote_branch
|
|
264
|
+
body_path = os.path.join(args.dir, 'body.md')
|
|
265
|
+
title_path = os.path.join(args.dir, 'body.title')
|
|
266
|
+
|
|
267
|
+
if not re.match(r'^[\w.-]+/[\w.-]+$', args.repo):
|
|
268
|
+
problems.append('--repo must be OWNER/REPO')
|
|
269
|
+
|
|
270
|
+
for path in (body_path, title_path):
|
|
271
|
+
if not os.path.exists(path):
|
|
272
|
+
problems.append('no {}: `tests_pr_body.py {} --title TITLE --out {}` '
|
|
273
|
+
'writes it'.format(path, args.dir, body_path))
|
|
274
|
+
|
|
275
|
+
if problems:
|
|
276
|
+
return refuse(problems)
|
|
277
|
+
|
|
278
|
+
body, title = publish.read(body_path), publish.read(title_path).strip()
|
|
279
|
+
|
|
280
|
+
# A stand-in for the bug report's number is expected in a preview made
|
|
281
|
+
# before the report is filed; posting or editing needs it filled.
|
|
282
|
+
stand_ins = ['body: ' + problem for problem in
|
|
283
|
+
attribution.placeholder_problems(body)]
|
|
284
|
+
problems += [problem for problem in attribution.problems('tests', title,
|
|
285
|
+
body) if args.post or args.edit or problem not in stand_ins]
|
|
286
|
+
fresh, why = rendered(args.dir, title, args.allow_source)
|
|
287
|
+
|
|
288
|
+
if fresh is None:
|
|
289
|
+
problems.append('the tables and prose do not render:\n '
|
|
290
|
+
+ why.replace('\n', '\n '))
|
|
291
|
+
elif fresh != body:
|
|
292
|
+
problems.append('body.md is not what the tables and prose render to; '
|
|
293
|
+
'render it again with tests_pr_body.py (it also fills the bug '
|
|
294
|
+
'report\'s number, once filed)')
|
|
295
|
+
|
|
296
|
+
extra, changed, commits = git_problems(args, work)
|
|
297
|
+
problems += extra
|
|
298
|
+
listed = listed_files(args.dir)
|
|
299
|
+
actual = sorted((line.split('\t')[0][:1], line.split('\t')[-1])
|
|
300
|
+
for line in changed.splitlines() if '\t' in line)
|
|
301
|
+
|
|
302
|
+
if listed is None:
|
|
303
|
+
problems.append('no files.tsv in {}: `tests_pr_tables.py files` writes '
|
|
304
|
+
'it, and the opening\'s claim about changed files is checked '
|
|
305
|
+
'against it'.format(args.dir))
|
|
306
|
+
elif commits is not None and listed != actual:
|
|
307
|
+
problems.append('files.tsv is not the files {} changes over {}: run '
|
|
308
|
+
'`tests_pr_tables.py files` again, then render the body again'.format(
|
|
309
|
+
args.branch, args.base))
|
|
310
|
+
|
|
311
|
+
record = read_record(args.dir)
|
|
312
|
+
|
|
313
|
+
if record and record.get('repo') != args.repo:
|
|
314
|
+
problems.append('{} says this was opened on {}, not {}'.format(RECORD,
|
|
315
|
+
record.get('repo'), args.repo))
|
|
316
|
+
|
|
317
|
+
if (args.post or args.edit) and not shutil.which('gh'):
|
|
318
|
+
problems.append('--post and --edit need gh on the PATH')
|
|
319
|
+
|
|
320
|
+
if args.edit and not record:
|
|
321
|
+
problems.append('--edit: no {} in {}, so no PR is open to edit; --post '
|
|
322
|
+
'opens it'.format(RECORD, args.dir))
|
|
323
|
+
|
|
324
|
+
if args.post and not args.edit and record:
|
|
325
|
+
problems.append('already opened: {}. A PR is opened once; --edit '
|
|
326
|
+
'replaces its title and body'.format(record.get('url')))
|
|
327
|
+
|
|
328
|
+
if problems:
|
|
329
|
+
return refuse(problems)
|
|
330
|
+
|
|
331
|
+
changed_body = record and record.get('digest') != publish.digest(title, body)
|
|
332
|
+
# The URL as the clone names it, before any `insteadOf` rewrite; a remote
|
|
333
|
+
# the clone lacks is read from the original's clone, or taken as a URL,
|
|
334
|
+
# as `autoopt.py pr` reads after.pr_remote.
|
|
335
|
+
url = run(['git', '-C', args.suite, 'config', '--get', 'remote.{}.url'
|
|
336
|
+
.format(args.remote)])[1]
|
|
337
|
+
target = args.remote
|
|
338
|
+
|
|
339
|
+
if not url:
|
|
340
|
+
try:
|
|
341
|
+
original = autoopt.original_of(work, config).get('path') if config \
|
|
342
|
+
else None
|
|
343
|
+
except (RuntimeError, OSError, KeyError):
|
|
344
|
+
original = None
|
|
345
|
+
|
|
346
|
+
url = autoopt.fork_url(None, original, args.remote)
|
|
347
|
+
target = url or args.remote
|
|
348
|
+
|
|
349
|
+
print('repository: {} (target branch {}, from {})'.format(args.repo,
|
|
350
|
+
args.target_branch, target_from))
|
|
351
|
+
print('branch: {} -> {} on remote {} ({}, from {}), head {}'.format(
|
|
352
|
+
args.branch, args.remote_branch, args.remote, url or 'no such remote',
|
|
353
|
+
remote_from, head))
|
|
354
|
+
print('title: {}'.format(title))
|
|
355
|
+
print('body: {} ({:,} characters)'.format(body_path, len(body)))
|
|
356
|
+
print('commits: {} over {}, changing:\n {}'.format(commits, args.base,
|
|
357
|
+
changed.replace('\n', '\n ')))
|
|
358
|
+
|
|
359
|
+
for line in re.findall(r'(?m)^## .*$', body):
|
|
360
|
+
print('section: {}'.format(line[3:]))
|
|
361
|
+
|
|
362
|
+
if record:
|
|
363
|
+
print('already opened: {}{}'.format(record['url'], '; the title or body '
|
|
364
|
+
'has changed since' if changed_body else ''))
|
|
365
|
+
|
|
366
|
+
print('\n'.join(publish.post_needs(args.repo, 'remote {} ({}) -> branch {}; '
|
|
367
|
+
'the PR opens on {} with head {}'.format(args.remote, url or
|
|
368
|
+
'no such remote', args.remote_branch, args.repo, head))))
|
|
369
|
+
|
|
370
|
+
if not args.post and not args.edit:
|
|
371
|
+
print('\npreview only: nothing was pushed or opened. Run again with '
|
|
372
|
+
'--post after the repository, branch, title and sections have '
|
|
373
|
+
'been approved')
|
|
374
|
+
return 0
|
|
375
|
+
|
|
376
|
+
if args.edit:
|
|
377
|
+
if not changed_body:
|
|
378
|
+
print('nothing to edit: {} already has this title and body'.format(
|
|
379
|
+
record['url']))
|
|
380
|
+
return 0
|
|
381
|
+
|
|
382
|
+
problem = publish.edit_pull(os.path.join(args.dir, RECORD), record,
|
|
383
|
+
title, body, body_path, args.other_owner)
|
|
384
|
+
|
|
385
|
+
if problem:
|
|
386
|
+
return refuse([problem], 'nothing changed')
|
|
387
|
+
|
|
388
|
+
print('edited {}'.format(record['url']))
|
|
389
|
+
return 0
|
|
390
|
+
|
|
391
|
+
problem = publish.account_problem(args.repo, args.other_owner)
|
|
392
|
+
|
|
393
|
+
if problem:
|
|
394
|
+
return refuse([problem])
|
|
395
|
+
|
|
396
|
+
code, out, err = run(['git', '-C', args.suite, 'push', target,
|
|
397
|
+
'{}:refs/heads/{}'.format(args.branch, args.remote_branch)])
|
|
398
|
+
|
|
399
|
+
if code != 0:
|
|
400
|
+
sys.stderr.write('tests_pr_push: git push failed ({}); no PR '
|
|
401
|
+
'opened:\n{}\n{}\n'.format(code, out, err))
|
|
402
|
+
return 1
|
|
403
|
+
|
|
404
|
+
print('pushed {} to {} as {}'.format(args.branch, args.remote,
|
|
405
|
+
args.remote_branch))
|
|
406
|
+
code, out, err = publish.run_gh(['pr', 'create', '--repo', args.repo,
|
|
407
|
+
'--base', args.target_branch, '--head', head, '--title', title,
|
|
408
|
+
'--body-file', body_path])
|
|
409
|
+
found = publish.PULL_URL.search(out)
|
|
410
|
+
|
|
411
|
+
if code != 0 or not found:
|
|
412
|
+
sys.stderr.write('tests_pr_push: gh pr create failed ({}); the branch is '
|
|
413
|
+
'pushed, so running this again retries it:\n{}\n{}\n'.format(code,
|
|
414
|
+
out, err))
|
|
415
|
+
return 1
|
|
416
|
+
|
|
417
|
+
record = publish.new_record(args.repo, found.group(0), args.remote_branch,
|
|
418
|
+
title, body)
|
|
419
|
+
publish.write_record(os.path.join(args.dir, RECORD), record)
|
|
420
|
+
print('opened {}'.format(record['url']))
|
|
421
|
+
|
|
422
|
+
return 0
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
if __name__ == '__main__':
|
|
426
|
+
raise SystemExit(main())
|