forked from OpenTTD/eints
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheintsgit.py
More file actions
executable file
·424 lines (325 loc) · 10.3 KB
/
eintsgit.py
File metadata and controls
executable file
·424 lines (325 loc) · 10.3 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
import subprocess
import os
import os.path
import sys
import fcntl
import getopt
import datetime
# Eints authentification
eints_login_file = "user.cfg"
# Commit credentials
commit_user = "translators <translators@openttd.org>"
commit_message = "Update: Translations from eints\n\n"
# Source structure
git_remote = "origin"
git_branch = "master"
git_remote_branch = git_remote + "/" + git_branch
# External tools
lang_sync_command = "./lang_sync"
git_command = "git"
# Temporary files
lock_file = "/tmp/eints.lock"
msg_file = "/tmp/eints.msg"
class Settings:
def __init__(self):
self.base_url = None
self.project = None
self.lang_dir = None
self.unstable_lang_dir = None
self.lang_file_ext = None
self.working_copy = None
def get_lang_sync_params(self):
result = [
"--user-password-file",
eints_login_file,
"--base-url",
self.base_url,
"--project",
self.project,
"--lang-dir",
self.lang_dir,
"--lang-file-ext",
self.lang_file_ext,
]
if self.unstable_lang_dir:
result.extend(["--unstable-lang-dir", self.unstable_lang_dir])
return result
def get_working_dirs(self):
result = [self.lang_dir]
if self.unstable_lang_dir:
result.append(self.unstable_lang_dir)
return result
class FileLock:
"""
Inter-process lock mechanism via exclusive file locking.
"""
def __init__(self, name):
self.name = name
self.file = None
def __enter__(self):
assert self.file is None
self.file = open(self.name, "a")
try:
fcntl.flock(self.file, fcntl.LOCK_EX | fcntl.LOCK_NB)
except Exception:
self.file.close()
self.file = None
raise
self.file.truncate()
self.file.write("pid:{} date:{:%Y-%m-%d %H:%M:%S}\n".format(os.getpid(), datetime.datetime.now()))
self.file.flush()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
assert self.file is not None
os.remove(self.name)
fcntl.flock(self.file, fcntl.LOCK_UN)
self.file.close()
self.file = None
return False
def print_info(msg):
"""
Print info message.
@param msg: Message
@type msg: C{str}
"""
print("[{:%Y-%m-%d %H:%M:%S}] {}".format(datetime.datetime.now(), msg))
def git_status(settings):
"""
Check whether working copy is in a valid status,
and whether there are modifies.
A valid state means:
- No untracked files.
- No added files.
- No removed files.
All these alterations are not allowed to be performed by eintsgit.
@return: Whether files are modified.
@rtype: C{bool}
"""
msg = subprocess.check_output(
[git_command, "status", "-s", *settings.get_working_dirs()],
universal_newlines=True,
)
return msg.strip() != ""
def git_pull(settings):
"""
Update working copy, and revert all modifications.
@return: Whether files were updated.
@rtype: C{bool}
"""
subprocess.check_call([git_command, "fetch", git_remote])
subprocess.check_call([git_command, "clean", "-f", os.environ.get("GIT_WORK_TREE")])
changes = subprocess.check_output(
[git_command, "diff", "--name-only", "HEAD.." + git_remote_branch],
universal_newlines=True,
)
subprocess.check_call([git_command, "reset", "--hard", git_remote_branch])
for line in changes.splitlines():
if line.startswith(settings.lang_dir) >= 0:
return True
if settings.lang_dir and line.startswith(settings.lang_dir) >= 0:
return True
return False
def git_push(settings, msg_file, dry_run):
"""
Commit and push working copy.
@param msg_file: Path to file with commit message.
@type msg_file: C{str}
@param dry_run: Do not push
@type dry_run: C{bool}
"""
subprocess.check_call([git_command, "add", *settings.get_working_dirs()])
subprocess.check_call([git_command, "commit", "--author", commit_user, "-F", msg_file])
if not dry_run:
subprocess.check_call([git_command, "push"])
def eints_upload(settings):
"""
Update base language and translations to Eints.
"""
subprocess.check_call(
[
lang_sync_command,
*settings.get_lang_sync_params(),
"upload-base",
"upload-translations",
]
)
def eints_download(settings, credits_file):
"""
Download translations from Eints.
@param credits_file: File for translator credits.
@type credits_file: C{str}
"""
subprocess.check_call(
[
lang_sync_command,
*settings.get_lang_sync_params(),
"--credits",
credits_file,
"download-translations",
]
)
def update_eints_from_git(settings, force, pull):
"""
Perform the complete operation from syncing Eints from the repository.
@param force: Upload even if no changes.
@type force: C{bool}
@param pull: Pull from remote, or use working copy as-it.
@type pull: C{bool}
"""
with FileLock(lock_file):
has_changes = False
if pull:
print_info("Check updates from git")
has_changes = git_pull(settings)
if has_changes or force:
print_info("Upload translations")
eints_upload(settings)
print_info("Done")
def commit_eints_to_git(settings, dry_run, pull):
"""
Perform the complete operation from commit Eints changes to the repository.
@param dry_run: Do not commit, leave as modified.
@type dry_run: C{bool}
@param pull: Pull from remote, or use working copy as-it.
@type pull: C{bool}
"""
with FileLock(lock_file):
# Upload first in any case.
if pull:
print_info("Update from git")
git_pull(settings)
print_info("Upload/Merge translations")
eints_upload(settings)
print_info("Download translations")
eints_download(settings, msg_file)
if git_status(settings):
print_info("Commit changes")
# Assemble commit messge
with open(msg_file, "r+", encoding="utf-8") as f:
cred = f.read()
f.seek(0)
f.truncate(0)
f.write(commit_message)
f.write(cred)
git_push(settings, msg_file, dry_run)
print_info("Done")
def run():
"""
Run the program (it was started from the command line).
"""
try:
opts, args = getopt.getopt(
sys.argv[1:],
"h",
[
"help",
"force",
"pull",
"dry-run",
"base-url=",
"project=",
"lang-dir=",
"unstable-lang-dir=",
"lang-file-ext=",
"working-copy=",
],
)
except getopt.GetoptError as err:
print("eintsgit: " + str(err) + ' (try "eintsgit --help")')
sys.exit(2)
# Parse options
force = False
dry_run = False
pull = False
settings = Settings()
for opt, val in opts:
if opt in ("--help", "-h"):
print("""\
eintsgit -- Synchronize language files between git and Eints.
eintsgit <options> <operations>
with <options>:
--help
-h
Get this help text.
--force
See individual operations below
--pull
Update working copy from remote
--dry-run
See individual operations below
--project
Eints project identifier
--working-copy
Path to git working copy
--lang-dir=LANG_DIR
Path of the directory containing the language files at the local disc.
--unstable-lang-dir=UNSTABLE_LANG_DIR
Path of the directory containing the unstable language files at the local disc.
--lang-file-ext=LANG_EXT
Filename suffix used by the language files.
--base-url
URL where eints is at
and <operations>:
update-from-git
Update working copy and upload modifications.
With --force upload even if git reported no modifications.
commit-to-git
Update working copy, merge and download translations from Eints, commit and push.
With --dry-run stop before pushing and leave commits local.
""")
sys.exit(0)
if opt == "--force":
force = True
continue
if opt == "--pull":
pull = True
continue
if opt == "--dry-run":
dry_run = True
continue
key = opt[2:].replace("-", "_")
if hasattr(settings, key):
if getattr(settings, key):
print("Duplicate {} option".format(opt))
sys.exit(2)
setattr(settings, key, val)
continue
raise ValueError("Unknown option {} encountered.".format(opt))
# Parse operations
do_update = False
do_commit = False
for arg in args:
if arg == "update-from-git":
do_update = True
continue
if arg == "commit-to-git":
do_commit = True
continue
print("Unknown operation: {}".format(arg))
sys.exit(2)
# Check options
if do_update or do_commit:
lsp = settings.get_lang_sync_params()
for k, v in zip(lsp[0::2], lsp[1::2]):
if v is None:
print("No {} specified".format(k))
sys.exit(2)
if settings.working_copy:
settings.working_copy = os.path.abspath(settings.working_copy)
settings.lang_dir = os.path.join(settings.working_copy, settings.lang_dir)
if settings.unstable_lang_dir:
settings.unstable_lang_dir = os.path.join(settings.working_copy, settings.unstable_lang_dir)
os.environ["GIT_WORK_TREE"] = settings.working_copy
os.environ["GIT_DIR"] = os.path.join(settings.working_copy, ".git")
else:
print("No --working-copy specified")
sys.exit(2)
# Execute operations
if do_update:
update_eints_from_git(settings, force, pull)
if do_commit:
commit_eints_to_git(settings, dry_run, pull)
sys.exit(0)
if __name__ == "__main__":
run()