-
Notifications
You must be signed in to change notification settings - Fork 1
/
integrate-package
executable file
·164 lines (140 loc) · 5.91 KB
/
integrate-package
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
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import copy
import json
import pathlib
import subprocess
import tempfile
import urllib.request
import os
import ruamel.yaml
BRANCH_NAME_PATTERN = "chore-update-{recipe}-{version}"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--repo", required=True)
parser.add_argument("--package", required=True)
parser.add_argument("--recipe", required=True)
parser.add_argument("--commit", default="Update {recipe} to {version}")
parser.add_argument("--token", default=os.environ.get("GITHUB_TOKEN"))
parser.add_argument("--downstream", default="pyodide/pyodide")
parser.add_argument("--upstream", default="pyodide/pyodide")
group = parser.add_mutually_exclusive_group()
group.add_argument("--ready-for-review", action="store_true", default=False)
group.add_argument("--not-ready-for-review", action="store_true")
group = parser.add_mutually_exclusive_group()
group.add_argument("--dry-run", action="store_true", default=False)
group.add_argument("--not-dry-run", action="store_true")
parser.add_argument("--title", default="Upgrade {recipe} to {version}")
parser.add_argument("--body", default="This PR was created by a bot. Pinging {maintainers} to check this")
parser.add_argument("--maintainers", type=lambda x: x.split(","), required=True)
args = parser.parse_args()
assert args.token is not None
# Query PyPI
with urllib.request.urlopen(f"https://pypi.org/pypi/{args.package}/json") as f:
metadata = json.load(f)
assert f.status == 200
pypi_package_version = metadata['info']['version']
pypi_package_sdist = next(p for p in metadata['urls'] if p['packagetype'] == 'sdist')
with tempfile.TemporaryDirectory() as _tmp_dir:
tmp_dir = pathlib.Path(_tmp_dir)
# Clone pyodide
subprocess.run(
["git", "clone", f"https://github.com/{args.upstream}.git", "--depth=1", "upstream-repo"],
cwd=tmp_dir,
check=True,
)
# Add Git remote
subprocess.run(
[
"git",
"remote",
"add",
"upstream",
f"https://{args.token}@github.com/{args.downstream}.git",
],
check=True,
cwd=tmp_dir / "upstream-repo",
)
subprocess.run(["git", "remote", "update"], cwd=tmp_dir / "upstream-repo")
# Check if we've already made this PR, exit if so!
maintainer_users = ", ".join(f"@{n}" for n in args.maintainers if n.strip())
fork_branch_name = BRANCH_NAME_PATTERN.format(
repo=args.repo,
maintainers=maintainer_users, # FIXME
version=pypi_package_version,
recipe=args.recipe
)
ls_remote_result = subprocess.run(
[
"git",
"ls-remote",
"--exit-code",
"--heads",
"upstream",
fork_branch_name,
], cwd=tmp_dir / "upstream-repo"
)
if ls_remote_result.returncode == 0:
raise SystemExit(0)
# Switch to fork branch
subprocess.run(["git", "switch", "-c", fork_branch_name, "origin/main"], check=True, cwd=tmp_dir / "upstream-repo")
# Read the Conda-Forge meta.yaml
meta_path = tmp_dir / "upstream-repo" / "packages" / args.recipe / "meta.yaml"
yaml = ruamel.yaml.YAML(typ="rt")
new_meta = yaml.load(meta_path)
meta = copy.deepcopy(new_meta)
# Does pyodide already know about the latest version? Exit if so!
if str(meta['package']['version']) == pypi_package_version:
raise SystemExit(0)
# Update meta.yaml
new_meta['package']['version'] = str(pypi_package_version)
new_meta['source']['url'] = pypi_package_sdist['url']
new_meta['source']['sha256'] = pypi_package_sdist['digests']['sha256']
yaml.dump(new_meta, meta_path)
# Add and commit changes to meta
subprocess.run(["git", "add", meta_path], check=True, cwd=tmp_dir / "upstream-repo")
subprocess.run(["git", "commit", "-m", args.commit.format(
maintainers=maintainer_users, # FIXME
version=pypi_package_version,
recipe=args.recipe
)], check=True, cwd=tmp_dir / "upstream-repo")
# Push to remote
if not args.dry_run:
subprocess.run(["git", "push", "--set-upstream", "upstream"], check=True, cwd=tmp_dir / "upstream-repo")
downstream_org, downstream_repo = args.downstream.split("/", 1)
payload = {
"title": args.title.format(
repo=args.repo,
maintainers=maintainer_users, # FIXME
version=pypi_package_version,
recipe=args.recipe),
"body": args.body.format(
repo=args.repo,
maintainers=maintainer_users, # FIXME
version=pypi_package_version,
recipe=args.recipe),
"head": f"{downstream_org}:{fork_branch_name}",
"base": "main",
"draft": not args.ready_for_review,
}
headers = {
"Content-Type": "application/json",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Authorization": f"Bearer {args.token}"
}
if args.dry_run:
print(
json.dumps(payload, indent=4, sort_keys=True)
)
else:
request = urllib.request.Request(
f"https://api.github.com/repos/{args.upstream}/pulls",
method="POST",
headers=headers,
data=json.dumps(payload).encode("utf-8"),
)
with urllib.request.urlopen(request) as f:
assert f.status == 200
print(json.load(f))