-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_repos.xsh
More file actions
394 lines (345 loc) · 11.7 KB
/
Copy pathfind_repos.xsh
File metadata and controls
394 lines (345 loc) · 11.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import argparse
from typing import Dict, Optional
from collections import defaultdict
from pathlib import Path
import sys
from dataclasses import dataclass, asdict
import os
import subprocess
import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
import yaml
parser = argparse.ArgumentParser(description='Find source repos.')
parser.add_argument("path", help='Top path to start searching for repos in.', type=Path)
parser.add_argument("--update-used", help="If the used repo yaml should be updated.", action='store_true')
args = parser.parse_args()
path = args.path
def find_git_repos(path):
for candidate in $(find @(path) -type d -name '.git').split():
candidate = candidate.strip()
if '.tox' in str(candidate):
continue
if 'crabby-rathbun' in str(candidate):
continue
yield Path(candidate).resolve().parent
def find_hg_repos(path):
for candidate in !(find @(path) -type d -name '.hg'):
candidate = candidate.strip()
yield Path(candidate).resolve().parent
def _git(repo, *args):
"""Run a git command in a repo using stdlib subprocess (thread-safe)."""
return subprocess.run(
["git", "-C", str(repo), *args],
capture_output=True, text=True, check=True,
).stdout
def fix_git_protcol_to_https(repo):
for ln in _git(repo, "remote", "-v").splitlines():
if not ln.strip():
continue
try:
name, url, _ = ln.split()
except Exception:
print(repo, ln)
continue
if url.startswith('git://') and 'github.com' in url:
new_url = url.replace('git://', 'https://')
print(url, '->', new_url)
subprocess.run(
["git", "-C", str(repo), "remote", "set-url", name, new_url],
check=True,
)
def get_git_remotes(repo):
"""Given a path to a repository, return its remotes."""
remotes = defaultdict(dict)
for remote in _git(repo, "remote", "-v").splitlines():
if not remote.strip():
continue
name, _, rest = remote.strip().partition('\t')
url, _, direction = rest.partition(' (')
direction = direction[:-1]
remotes[name][direction] = url
return dict(remotes)
def get_work_trees(repo):
a = _git(repo, "worktree", "list", "--porcelain")
primary, *rest = [
{
k: v
for k, v in [
___.split(" ") if " " in ___ else ("branch", None) for ___ in __
]
}
for __ in [_.split("\n") for _ in a.split("\n\n") if len(_)]
]
return primary, rest
def get_hg_remotes(repo):
"""Given a path to a repository, return its remotes."""
remotes = {}
ret = subprocess.run(
["hg", "-R", str(repo), "paths"],
capture_output=True, text=True,
)
if ret.returncode not in (0, 1):
print(f"warning: hg paths in {repo} exited {ret.returncode}: {ret.stderr.strip()}")
out = ret.stdout
for remote in out.splitlines():
if not remote.strip():
continue
name, _, url = [_.strip() for _ in remote.strip().partition('=')]
remotes[name] = url
return dict(remotes)
@dataclass
class Remote:
url: str
protocol: str
host: str
user: Optional[str]
repo_name: Optional[str]
ssh_user: Optional[str] = None
vc: str = "git"
@dataclass
class Project:
name: str
primary_remote: Remote
remotes: Dict[str, Remote]
local_checkout: str
def strip_dotgit(repo_name):
return repo_name[:-4] if repo_name.endswith(".git") else repo_name
def parse_git_name(git_url):
if git_url.startswith("git@"):
_, _, rest = git_url.partition("@")
host, _, rest = rest.partition(":")
parts = rest.split("/")
if len(parts) == 1:
user = None
(repo_name,) = parts
elif len(parts) == 2:
user, repo_name = parts
else:
user, *rest = parts
repo_name = "/".join(rest)
return Remote(
url=git_url,
host=host,
user=user,
repo_name=strip_dotgit(repo_name),
ssh_user="git",
protocol="ssh",
)
elif git_url.startswith("git://"):
rest = git_url[len("git://") :]
parts = rest.split("/")
if len(parts) == 1:
raise ValueError(f"this should not happen {git_url} {parts}")
elif len(parts) == 2:
host, repo_name = parts
user = None
elif len(parts) == 3:
host, user, repo_name = parts
else:
host, user, *rest = parts
repo_name = "/".join(rest)
return Remote(
url=f'git@{host}:{user}/{repo_name}',
host=host,
user=user,
repo_name=strip_dotgit(repo_name),
ssh_user='git',
protocol="ssh",
)
elif git_url.startswith("ssh://") or git_url.startswith("ssh+git://"):
_, _, rest = git_url.partition(":")
rest = rest[2:]
if "@" in rest:
ssh_user, _, rest = git_url.partition("@")
else:
ssh_user = ${'USER'}
host, _, repo_name = rest.partition("/")
return Remote(
url=git_url,
host=host,
user=None,
repo_name=strip_dotgit(repo_name),
ssh_user=ssh_user,
protocol="ssh",
)
elif git_url.startswith("https://"):
rest = git_url[len("https://") :]
parts = rest.split("/")
if len(parts) >= 3:
host, user, *rest = parts
repo_name = "/".join(rest)
elif len(parts) == 2:
host, repo_name = parts
user = None
elif len(parts) < 2:
raise ValueError(f"do not think this can happen {git_url} {parts}")
return Remote(
url=git_url,
host=host,
user=user,
repo_name=strip_dotgit(repo_name),
protocol="https",
)
return "http"
elif "@" in git_url:
ssh_user, _, rest = git_url.partition("@")
host, _, rest = rest.partition(":")
user, _, repo_name = rest.partition("/")
return Remote(
url=git_url,
host=host,
user=user,
repo_name=strip_dotgit(repo_name),
ssh_user=ssh_user,
protocol="ssh",
)
elif git_url.startswith("/") or git_url.startswith("."):
return Remote(
url=git_url, host="localhost", user=None, repo_name=None, protocol="file"
)
elif ":" in git_url:
host, _, repo_name = git_url.partition(":")
ssh_user = ${'USER'}
return Remote(
url=git_url,
host=host,
user=None,
repo_name=strip_dotgit(repo_name),
ssh_user=ssh_user,
protocol="ssh",
)
else:
raise ValueError(f"unknown scheme: {git_url}")
def parse_hg_name(hg_url):
if hg_url.startswith("http"):
proto, _, rest = hg_url.partition("://")
host, *parts = [_ for _ in rest.split("/") if len(_)]
if parts[0] == "hg":
parts = parts[1:]
if len(parts) == 1:
(repo_name,) = parts
user = None
elif len(parts) == 2:
user, repo_name = parts
else:
user, *rest = parts
repo_name = "/".join(parts)
return Remote(
url=hg_url,
host=host,
user=user,
repo_name=repo_name,
ssh_user=None,
protocol=proto,
vc="hg",
)
elif hg_url.startswith("ssh"):
proto, _, rest = hg_url.partition("://")
if "@" in rest:
ssh_user, _, rest = rest.partition("@")
else:
ssh_user = ${'USER'}
host, *parts = [_ for _ in rest.split("/") if len(_)]
if parts[0] == "hg":
parts = parts[1:]
if len(parts) == 1:
(repo_name,) = parts
user = None
elif len(parts) == 2:
user, repo_name = parts
else:
user, *rest = parts
repo_name = "/".join(parts)
return Remote(
url=hg_url,
host=host,
user=user,
repo_name=repo_name,
ssh_user=ssh_user,
protocol=proto,
vc="hg",
)
def process_git_repo(repo_path):
"""Process a single git repo; returns a Project or None. Safe to call from threads."""
base, worktrees = get_work_trees(repo_path)
if str(repo_path) != base['worktree']:
return None
remotes = {}
fix_git_protcol_to_https(repo_path)
for k, v in get_git_remotes(repo_path).items():
parsed = {direction: parse_git_name(url) for direction, url in v.items()}
assert len(parsed) == 2
remotes[k] = parsed["fetch"]
if not len(remotes):
return None
for k in ["upstream", "origin"]:
if k in remotes:
primary_remote = remotes[k]
break
else:
primary_remote = next(iter(remotes.values()))
if primary_remote is None:
return None
return Project(
name=primary_remote.repo_name,
primary_remote=primary_remote,
remotes=remotes,
local_checkout=str(repo_path),
)
def process_hg_repo(repo_path):
"""Process a single hg repo; returns a Project or None. Safe to call from threads."""
remotes = {}
for k, url in get_hg_remotes(repo_path).items():
remotes[k] = parse_hg_name(url)
if not len(remotes):
return None
for k in ["origin", "default"]:
if k in remotes:
primary_remote = remotes[k]
break
else:
primary_remote = next(iter(remotes.values()))
if primary_remote is None:
return None
return Project(
name=primary_remote.repo_name,
primary_remote=primary_remote,
remotes=remotes,
local_checkout=str(repo_path),
)
print(sys.version_info)
# Use 2x CPU count workers: git subprocess calls are I/O-bound so threads
# can overlap while waiting on disk/network.
_workers = (os.cpu_count() or 1) * 2
projects = []
git_repos = list(find_git_repos(path))
with ThreadPoolExecutor(max_workers=_workers) as executor:
futures = {executor.submit(process_git_repo, repo_path): repo_path for repo_path in git_repos}
for future in tqdm.tqdm(as_completed(futures), total=len(git_repos), desc="git repos"):
result = future.result()
if result is not None:
projects.append(result)
hg_repos = list(find_hg_repos(path))
with ThreadPoolExecutor(max_workers=_workers) as executor:
futures = {executor.submit(process_hg_repo, repo_path): repo_path for repo_path in hg_repos}
for future in tqdm.tqdm(as_completed(futures), total=len(hg_repos), desc="hg repos"):
result = future.result()
if result is not None:
projects.append(result)
with open("all_repos.yaml", "w") as fout:
yaml.dump_all([asdict(_) for _ in projects], fout)
if args.update_used:
local_checkouts = {co.name: co for co in projects}
repos = []
for order in sorted(Path('build_order.d').glob('[!.]*yaml')):
with open(order) as fin:
build_order = list(yaml.safe_load_all(fin))
for step in build_order:
if step['kind'] != 'source_install':
continue
lc = local_checkouts[step['proj_name']]
repos.append(asdict(lc.primary_remote))
repos.append(asdict(local_checkouts['cpython'].primary_remote))
repos = filter(lambda x: x['vc'] == 'git', repos)
with open("used_repos.yaml", "w") as fout:
yaml.dump_all(sorted(repos, key=lambda x: (x['user'], x['repo_name'])), fout)