-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-sync.py
executable file
·361 lines (328 loc) · 15.6 KB
/
git-sync.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
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
#!/usr/bin/env python3
import os
import os.path
import re
import signal
import sys
import time
from datetime import datetime as dt
from datetime import timedelta as td
from pathlib import Path
import git
import pywikibot as pwb
import yaml
class SignalHandler:
def __init__(self):
self._is_sleeping = False
self._exit_requested = False
signal.signal(signal.SIGINT, self._request_exit)
signal.signal(signal.SIGTERM, self._request_exit)
def _request_exit(self, signal, frame):
if self._is_sleeping:
self._exit_now()
else:
self._exit_requested = True
def _exit_now(self):
print('SIGINT or SIGTERM received, exiting...')
sys.exit(0)
def sleep(self, seconds):
if self._exit_requested:
self._exit_now()
else:
self._is_sleeping = True
time.sleep(seconds)
self._is_sleeping = False
class GitSync:
def __init__(self):
self._base_path = Path(__file__).parent
self._config_file = self._base_path / 'config.yml'
self.config = {}
self.repos = []
self.usermap_email_list = []
def init_repos(self):
for repo in self.config['repos']:
file_regex = re.compile(
repo['file_regex'],
re.I if repo['regex_nocase'] else 0)
repo_path = os.path.join(self.config['repositories_root'], repo['name'])
git_repo = git.Repo(repo_path)
site = pwb.Site(
code=repo['project']['code'],
fam=repo['project']['family'],
user=self.config['mediawiki_username'])
self.repos.append(GitRepo(repo['name'], git_repo, site,
repo['namespace'], file_regex, repo['force_extension'],
repo['ignore_list'] + self.config['global_ignore_list'],
self.config['usermap'], self.usermap_email_list))
def read_config(self):
self.config = yaml.load(self._config_file.read_text(), Loader=yaml.FullLoader)
if not self.config:
print('Error: Configuration file not found or empty.', file=sys.stderr)
sys.exit(1)
else:
self.usermap_email_list = [_['email'] for _ in self.config['usermap'].values()]
class GitRepo:
def __init__(self, name, repo, site, namespace, title_regex, force_ext, ignores, usermap, usermap_email_list):
self.name = name
self.repo = repo
self.site = site
self.namespace = namespace
self.title_regex = title_regex
self.force_ext = force_ext
if force_ext:
self.re_force_ext = re.compile(r'\.' + force_ext + '$')
self.ignores = ignores
self._need_resync = False
self._pending_commits = {}
self._usermap = usermap
self._usermap_email_list = usermap_email_list
def _create_summary(self, author_name, author_email, repo_name, commit_sha, message):
base_url = 'https://github.com/wikimedia-bg'
message = message.replace('\n', ' ')
# Try finding the commit author email in the usermap dictionary. For this we have two lists:
# * the top-level keys, i.e. the Wikimedia usernames (list(self._usermap.keys()));
# * the email subkeys in the _same_ order (created in GitSync.read_config()).
# Because the two lists have the same order, the username in index N in the first list corresponds to the email
# in index N in the second list. Thus, if we find the commit author email in the second list at index N, we know
# that the corresponding Wikimedia username will be at index N in the first list.
try:
wiki_user = list(self._usermap.keys())[self._usermap_email_list.index(author_email)]
# If there's no match, just mention the commit author's name in the edit summary, but don't create a user page
# link.
except ValueError:
wiki_user_mention = author_name
# If there is a match, create a user page link, since this must be a valid Wikimedia user.
else:
wiki_user_mention = '[[User:{user}|{user}]]'.format(user=wiki_user)
return '{user_mention} | {base_url}/{repo}/commit/{sha} | {message}'.format(
user_mention=wiki_user_mention,
base_url=base_url,
repo=repo_name,
sha=commit_sha,
message=message[:400] + (message[400:] and '..'))
def _pagelist(self):
return [_ for _ in self.site.allpages(namespace=self.namespace)
if self.title_regex.search(_.title(with_ns=False))]
def _last_changed(self):
return dt.utcfromtimestamp(self.repo.commit('master').committed_date) + td(seconds=1)
def _pending_revs(self):
pending_revs = []
for page in self._pagelist():
try:
revs_since_last_sync = page.revisions(endtime=self._last_changed(), content=True)
candidate_revs = [(page.title(with_ns=False), _, 'edit') for _ in revs_since_last_sync]
if self._need_resync:
# Full re-sync requested, so get the latest revision of _all_ pages in the repo.
last_rev = page.latest_revision
candidate_revs.append(
(
page.title(with_ns=False),
{
'user': 'syncbot',
'comment': 'forced resync from wiki',
'text': last_rev['text'],
'timestamp': dt.utcnow(),
},
'resync',
)
)
except pwb.exceptions.NoPage:
# Apparently, the page got deleted on-wiki during our processing so scrap the candiate_revs.
pass
else:
pending_revs += candidate_revs
# If a resync has been requested, it's done.
self._need_resync = False
# We need to also check for deleted pages that we keep track of.
repo_files = [_.path for _ in self.repo.tree().traverse() if _.type != 'tree']
page_files = set(repo_files) - set(self.ignores)
if self.force_ext:
repo_pages = [self.re_force_ext.sub('', _.replace('.d/', '/')) for _ in page_files]
else:
repo_pages = [_.replace('.d/', '/') for _ in page_files]
existing_pages = [_.title(with_ns=False) for _ in self._pagelist()]
deleted_pages = set(repo_pages) - set(existing_pages)
for page_name in deleted_pages:
for event in self.site.logevents(page=self.namespace + ':' + page_name):
if event.type() in ['delete', 'move']:
pending_revs.append(
(
page_name,
{
'user': event.user(),
'comment': event.comment(),
'timestamp': event.timestamp(),
},
event.type(),
)
)
# We need only the first (chronologically last) delete or move event.
break
pending_revs.sort(key=lambda rev: rev[1]['timestamp'])
return pending_revs
def _pull(self):
old_master = self.repo.commit('master')
self.repo.git.pull()
new_master = self.repo.commit('master')
if new_master == old_master:
return
pull_commits_newest_first = self.repo.iter_commits(
old_master.hexsha + '...' + self.repo.commit('master').hexsha)
pull_commits = reversed([_ for _ in pull_commits_newest_first])
# This requires Python 3.7+ to keep the insertion order of the dictionary.
for commit in pull_commits:
self._pending_commits[commit] = self.repo.git.diff_tree(
'--no-commit-id', '--name-only', '-r', '-z',
commit.parents[0], commit
).split('\0')[:-1]
def _wiki2git(self):
revs = self._pending_revs()
synced_files = []
for rev in revs:
#
# Summary/commit message parsing.
#
git_commit_message = rev[1]['comment'] or '*** празно резюме ***'
#
# User parsing.
#
wiki_user = rev[1]['user']
# Ignore our own sync edits in the wiki.
if wiki_user == self.site.username():
continue
try:
author = self._usermap[wiki_user]['author']
email = self._usermap[wiki_user]['email']
except KeyError:
author = wiki_user
email = ''
git_author = git.Actor(author, email)
git_committer = git.Actor(author, email)
#
# Page/file parsing.
#
# We cannot have both a file and a directory with the same name, so where we have
# 'Page' and 'Page/doc', the latter gets converted to 'Page.d/doc'.
file_name = rev[0].replace('/', '.d/')
# If we've configured a file extension for syntax highlighting, add it, but only for
# files in the root of the namespace/repository (the rest will likely be 'Page/doc').
if self.force_ext and '.d/' not in file_name:
file_name = file_name + '.' + self.force_ext
file_path = os.path.join(self.repo.working_dir, file_name)
#
# Committing.
#
# To avoid conflicts as much as possible, perform git pull right before we apply the
# change and commit it.
self._pull()
if rev[2] in ['edit', 'resync']:
os.makedirs(os.path.dirname(file_path), exist_ok=True)
if rev[2] == 'resync' and os.path.exists(file_path):
with open(file_path, 'r') as f:
if rev[1]['text'] == f.read().rstrip('\n'):
# The on-wiki and Git versions are the same. No need to resync.
continue
with open(file_path, 'w') as f:
f.write(rev[1]['text'] + '\n')
self.repo.index.add([file_path])
elif rev[2] in ['delete', 'move']:
self.repo.index.remove([file_path], working_tree=True)
else:
print('Error: Unknown revision type: "{}"'.format(rev[2]))
continue
print('Syncing to Git: {}'.format(file_name))
self.repo.index.commit(
git_commit_message,
author=git_author,
committer=git_committer,
author_date=dt.isoformat(rev[1]['timestamp'], timespec='seconds'),
commit_date=dt.isoformat(rev[1]['timestamp'], timespec='seconds'))
# Push after each commit. It's inefficient, but should minimize possible conflicts.
self.repo.git.push()
synced_files.append(file_name)
return synced_files
def _git2wiki(self, synced_from_wiki):
# Iterate over a list of the keys, instead of directly on the dictionary. This allows to
# delete the pending commits from the latter once they are processed.
commit_list = list(self._pending_commits)
for commit in commit_list:
if re.search(r'\bDO\s+NOT\s+(MERGE|SYNC)\b', commit.message):
print('Ignoring commit {} because of DO NOT MERGE/SYNC.'.format(commit.hexsha))
del self._pending_commits[commit]
continue
for file_name in self._pending_commits[commit]:
# We cannot have both a file and a directory with the same name, so where we have
# 'Page' and 'Page/doc', the latter was converted to 'Page.d/doc'.
page_name = self.namespace + ':' + file_name.replace('.d/', '/')
# If we've configured a file extension for syntax highlighting, remove it, but only
# for files in the root of the namespace/repo (the rest will likely be 'Page/doc').
if self.force_ext and '/' not in page_name:
page_name = self.re_force_ext.sub('', page_name)
page = pwb.Page(self.site, page_name)
summary = self._create_summary(
commit.author.name,
commit.author.email,
self.name,
commit.hexsha,
commit.message)
if file_name in synced_from_wiki:
# This page has been updated on the wiki in this sync run. To be on the safe
# side, we'll discard the possibly conflicting changes from the Git repo.
print('Ignoring possibly conflicting changes in {}'.format(file_name))
# Sometimes this might lead to out-of-sync situations, so schedule a resync.
self._need_resync = True
continue
file_removed = False
try:
file_git_blob = self.repo.commit(commit).tree.join(file_name)
except KeyError as e:
if str(e).endswith('\'{file}\' not found"'.format(file=file_name)):
file_removed = True
else:
raise
if not file_removed:
file_contents_at_commit = b''.join(file_git_blob.data_stream[3].readlines())
page.text = file_contents_at_commit.decode('utf-8').rstrip('\n')
print('Saving {}'.format(page.title()))
try:
page.save(summary=summary, botflag=True, quiet=True)
except pwb.data.api.APIError as e:
print('APIError exception: {}'.format(str(e)), file=sys.stderr)
else:
print('Deleting {}'.format(page.title()))
try:
page.delete(reason=summary, prompt=False)
except pwb.data.api.APIError as e:
print('APIError exception: {}'.format(str(e)), file=sys.stderr)
# When all files in a commit have been processed, remove it from the pending list.
del self._pending_commits[commit]
def sync(self, resync=False):
if resync:
self._need_resync = True
w2g_synced_files = self._wiki2git()
self._pull()
if self._pending_commits:
self._git2wiki(w2g_synced_files)
def main(argv):
sig_handler = SignalHandler()
git_sync = GitSync()
git_sync.read_config()
git_sync.init_repos()
'''
Disabled temporarily.
if argv:
if argv.pop() in ['resync', 'force']:
for repo in repos:
print('Resyncing repo "{}"...'.format(repo.repo.git_dir))
repo.sync(resync=True)
'''
while True:
for repo in git_sync.repos:
print('Syncing repo "{}"...'.format(repo.repo.git_dir))
repo.sync()
# Sleep for a second between repos to catch requests to shutdown faster.
sig_handler.sleep(1)
print('Sleeping...')
sig_handler.sleep(git_sync.config['daemon_sleep_seconds'])
if __name__ == '__main__':
main(sys.argv[1:])