-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbundle.py
executable file
·294 lines (232 loc) · 9.17 KB
/
bundle.py
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
#!/usr/bin/env python3
import contextlib
import functools
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from urllib import parse
def is_dir_empty(path):
path = Path(path)
if path.exists():
try:
next(path.iterdir())
return False
except StopIteration:
return True
class Repository:
def __init__(self, url):
self.url = url
@property
@functools.cache
def author(self):
return self.fullname.split("/")[0]
@property
@functools.cache
def name(self):
return self.fullname.split("/")[1]
@property
@functools.cache
def fullname(self):
return "/".join(parse.urlparse(self.url).path.rsplit("/", 2)[1:])
class Bundle:
def __init__(self, repo, browse_parent_dir, bundle_parent_dir):
self.repo = repo
self._browse_parent_dir = browse_parent_dir
self._bundle_parent_dir = bundle_parent_dir
@property
def _browse_parent_dir(self):
return self.__browse_parent_dir
@_browse_parent_dir.setter
def _browse_parent_dir(self, path):
self.__browse_parent_dir = Path(path)
@property
def _bundle_parent_dir(self):
return self.__bundle_parent_dir
@_bundle_parent_dir.setter
def _bundle_parent_dir(self, path):
self.__bundle_parent_dir = Path(path)
@property
@functools.cache
def name(self):
return "{}.{}".format(self.repo.author, self.repo.name)
@property
@functools.cache
def browse_path(self):
return self._browse_parent_dir / self.repo.fullname
@property
@functools.cache
def bundle_path(self):
return (self._bundle_parent_dir / (self.name + ".bundle"))
def parse_repo_list(file_path):
with open(file_path) as file:
for line in file.readlines():
line = line.strip()
if not len(line):
continue
yield Repository(line)
def clone_repo(repo_url, clone_path, stdout=sys.stderr, stderr=sys.stderr):
return subprocess.run(
subprocess.list2cmdline(("git", "clone", repo_url, clone_path)),
stdout=stdout,
stderr=stderr,
shell=True
).returncode == 0
def bundle_repo(repo_path, dest_file, stdout=sys.stderr, stderr=sys.stderr):
return subprocess.run(
subprocess.list2cmdline(
("git", "bundle", "create", dest_file, "--all")),
stdout=stdout,
stderr=stderr,
shell=True,
cwd=repo_path
).returncode == 0
def main(repo_list_file, repo_browse_dir, repo_bundle_dir):
repo_list_file = Path(repo_list_file)
repo_browse_dir = Path(repo_browse_dir)
repo_bundle_dir = Path(repo_bundle_dir)
print("INFO: Parsing repository list file...")
repo_list = set(parse_repo_list(repo_list_file))
invalid_dirs = set()
invalid_files = set()
if repo_browse_dir.is_file():
invalid_dirs.add(repo_browse_dir)
else:
repo_browse_dir.mkdir(parents=True, exist_ok=True)
if repo_bundle_dir.is_file():
invalid_dirs.add(repo_bundle_dir)
else:
repo_bundle_dir.mkdir(parents=True, exist_ok=True)
repo_browse_dir = repo_browse_dir.resolve()
repo_bundle_dir = repo_bundle_dir.resolve()
bundle_list = tuple(
Bundle(repo, repo_browse_dir, repo_bundle_dir) for repo in repo_list)
browse_paths_exist = set()
browse_paths_no_exist = set()
browse_paths_are_empty = set()
bundle_paths_exist = set()
bundle_paths_no_exist = set()
browse_parent_dirs = set()
for bundle in bundle_list:
if bundle.browse_path.is_file():
invalid_files.add(bundle.browse_path)
elif bundle.browse_path.is_dir():
if is_dir_empty(bundle.browse_path):
browse_paths_are_empty.add(bundle)
else:
browse_paths_exist.add(bundle)
else:
browse_paths_no_exist.add(bundle)
if bundle.bundle_path.is_dir():
invalid_dirs.add(bundle.bundle_path)
elif bundle.bundle_path.is_file():
bundle_paths_exist.add(bundle)
else:
bundle_paths_no_exist.add(bundle)
browse_parent_dirs.add(bundle.browse_path.parent)
for path in browse_parent_dirs:
if path.is_file():
invalid_files.add(path)
if invalid_dirs:
print(
"FATAL: The following directories exist but should not:\n" +
" " + "\n ".join(os.path.relpath(path)
for path in invalid_dirs)
)
sys.exit(1)
if invalid_files:
print(
"FATAL: The following files exist but should be empty directories " +
"or not exist at all:\n" +
" " + "\n ".join(os.path.relpath(path)
for path in invalid_files)
)
sys.exit(1)
if browse_but_no_bundle := browse_paths_exist & bundle_paths_no_exist:
print(
"WARNING: The following browse directories exist but have no corresponding " +
"bundle files, they will be left alone:\n" +
" " + "\n ".join(os.path.relpath(bundle.browse_path)
for bundle in browse_but_no_bundle)
)
if bundle_but_no_browse := bundle_paths_exist & browse_paths_no_exist:
print(
"WARNING: The following bundle files exist but have no corresponding " +
"browse directories, they will automatically be unpacked:\n" +
" " + "\n ".join(os.path.relpath(bundle.bundle_path)
for bundle in bundle_but_no_browse)
)
if browse_paths_are_empty:
print(
"WARNING: The following browse directories already exist, " +
"but are empty:\n" +
" " + "\n ".join(os.path.relpath(bundle.browse_path)
for bundle in browse_paths_are_empty)
)
if not bundle_but_no_browse | bundle_paths_no_exist:
print("INFO: There are no new bundles or browse directories to create.")
sys.exit()
bundles_not_cloned = set()
repos_not_cloned = set()
bundles_not_created = set()
with tempfile.TemporaryDirectory() as work_dir:
work_dir = Path(work_dir)
for bundle in bundle_but_no_browse:
print("INFO: Cloning bundle: {}".format(
os.path.relpath(bundle.bundle_path)))
clone_path = work_dir / bundle.name
if not clone_repo(bundle.bundle_path, clone_path):
bundles_not_cloned.add(bundle)
bundles_not_to_copy = bundles_not_cloned | browse_but_no_bundle
for bundle in bundle_paths_no_exist:
print("INFO: Cloning repository: {}".format(bundle.repo.url))
clone_path = work_dir / bundle.name
bundle_path = work_dir / bundle.bundle_path.name
if not clone_repo(bundle.repo.url, clone_path):
repos_not_cloned.add(bundle)
continue
print("INFO: Creating bundle: {}".format(bundle_path))
if not bundle_repo(clone_path, bundle_path):
bundles_not_created.add(bundle)
for bundle in browse_paths_are_empty:
print("INFO: Removing empty browse directory: {}".format(
os.path.relpath(bundle.browse_path)))
bundle.browse_path.rmdir()
for bundle in (bundle_but_no_browse | bundle_paths_no_exist) - bundles_not_to_copy:
print("INFO: Moving repository files to browse directory: {}".format(
os.path.relpath(bundle.browse_path)))
assert not bundle.browse_path.is_dir()
repo_path = work_dir / bundle.name
shutil.rmtree(repo_path / ".git")
shutil.move(repo_path, bundle.browse_path)
for bundle in bundle_paths_no_exist - bundles_not_created:
print("INFO: Moving bundle files to bundles directory: {}".format(
os.path.relpath(bundle.bundle_path)))
assert not bundle.bundle_path.is_file()
bundle_path = work_dir / bundle.bundle_path.name
shutil.move(bundle_path, bundle.bundle_path)
if bundles_not_cloned:
print(
"WARNING: The following bundle files have not been cloned due to errors: \n" +
" " + "\n ".join(os.path.relpath(bundle.bundle_path)
for bundle in bundles_not_cloned)
)
if repos_not_cloned:
print(
"WARNING: The following repositories have not been cloned due to errors: \n" +
" " + "\n ".join(os.path.relpath(bundle.repo.url)
for bundle in repos_not_cloned)
)
if bundles_not_created:
print(
"WARNING: The following bundle files have not been created due to errors: \n" +
" " + "\n ".join(os.path.relpath(bundle.bundle_path)
for bundle in bundles_not_created)
)
if __name__ == "__main__":
REPO_LIST_FILE = "./repo_list.txt"
REPO_BROWSE_DIR = "./browse/"
REPO_BUNDLE_DIR = "./bundle/"
main(REPO_LIST_FILE, REPO_BROWSE_DIR, REPO_BUNDLE_DIR)