-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint
executable file
·159 lines (130 loc) · 5.47 KB
/
entrypoint
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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Pier Luigi Fiorini <pierluigi.fiorini@liri.io>
#
# SPDX-License-Identifier: GPL-3.0-or-later
import glob
import os
import json
import re
import subprocess
import shlex
import sys
import tempfile
def run(cmd, **kwargs):
out = []
for c in cmd:
out.append(shlex.quote(c))
print('+ ' + repr(' '.join(out))[1:-1])
return subprocess.run(cmd, **kwargs)
def merge_translations():
sources_path = os.environ.get('INPUT_SOURCES_PATH') or os.path.join(github_workspace, '.tx', 'sources.json')
with open(sources_path, 'r') as f:
sources = json.load(f)
for entry in sources:
process_entry(entry)
def process_entry(entry):
for key in ('type', 'output_path'):
if key not in entry:
print('Entry doesn\'t have "%s": skipping' % key, file=sys.stderr)
return
if entry['type'] in ('desktop', 'metainfo'):
for key in ('source_file', 'file_filter'):
if key not in entry:
print('Entry doesn\'t have "%s": skipping' % key, file=sys.stderr)
return
else:
# This is an entry that we don't care about
return
if entry['type'] in ('desktop', 'metainfo'):
source_file = entry['source_file']
output_path = entry['output_path']
dest_file = entry.get('dest_file')
file_filter = entry['file_filter'].replace('<lang>', '*')
globs = glob.glob(file_filter)
if entry['type'] == 'desktop':
run(['desktop-merge', os.path.dirname(entry['file_filter']), source_file, dest_file], check=True)
run(['git', 'add', dest_file])
elif entry['type'] == 'metainfo':
# Generate .mo for each translation
modir = tempfile.mkdtemp()
for filename in globs:
rx = entry['file_filter'].replace('<lang>', r'(.+)')
m = re.match(rx, filename)
if m:
lang = m.group(1)
run(['msgfmt', filename, '-o', os.path.join(modir, lang + '.mo')], check=True)
# Merge translations
globs = glob.glob(modir + '/*.mo')
run(['itstool', '-i', '/usr/share/translation-action/as-metainfo.its', '-j', source_file, '-o', dest_file] + globs, check=True)
run(['git', 'add', dest_file])
def main():
# Parameters
github_repository = os.environ['GITHUB_REPOSITORY']
ssh_key = os.environ.get('INPUT_SSH_KEY')
translations_folder = os.environ.get('INPUT_TRANSLATIONS_FOLDER')
committer_email = os.environ.get('INPUT_COMMITTER_EMAIL', 'git-action@transifex.com')
committer_name = os.environ.get('INPUT_COMMITTER_NAME', 'Transifex Github action')
try:
minimum_perc = int(os.environ.get('INPUT_MINIMUM_PERC', '0'))
except ValueError:
minimum_perc = 0
# tx additional arguments
args = []
if minimum_perc > 0:
args.append(f'--minimum-perc={minimum_perc}')
# Enter the workspace
github_workspace = os.environ['GITHUB_WORKSPACE']
print(f'Entering "{github_workspace}"...')
os.chdir(github_workspace)
# Add workspace as a safe directory
run(['git', 'config', '--global', '--add', 'safe.directory', github_workspace], check=True)
# Save ssh key
ssh_tempfile = ''
if ssh_key:
fd, ssh_tempfile = tempfile.mkstemp()
with open(ssh_tempfile, 'w') as f:
f.write(ssh_key)
# Print hash for reference
run(['git', 'log', '-1', '--format="%H"'], check=True)
# Configure email and name
run(['git', 'config', '--local', 'user.email', committer_email], check=True)
run(['git', 'config', '--local', 'user.name', committer_name], check=True)
# Default push policy
run(['git', 'config', 'push.default', 'simple'], check=True)
# Git checkout action does a shallow clone. That prevents us to
# access common history of branches
run(['git', 'fetch', '--unshallow'], check=True)
# Regenerate sources
run(['regenerate-sources'], check=True)
# Show staged changes (check=False otherwise it will raise an exception
# when there are no changes)
run(['git', 'diff', '--staged'])
# Push sources to Transifex only if something has changed
if run(['git', 'diff', '--staged', '--quiet']).returncode != 0:
run(['tx', 'push', '--source'], check=True)
# Pull rebased translations from Transifex
pull_args = ['tx', 'pull', '--force', '--all']
run(pull_args + args, check=True)
# Merge translations
merge_translations()
# Add files
paths = glob.glob(translations_folder, recursive=True)
for path in paths:
run(['git', 'add', path], check=True)
# Show staged changes (check=False otherwise it will raise an exception
# when there are no changes)
run(['git', 'diff', '--staged'])
# Commit (but stash non needed changes, for example sometimes .tx/config)
if run(['git', 'diff', '--staged', '--quiet']).returncode != 0:
if ssh_tempfile:
os.environ['GIT_SSH_COMMAND'] = f'ssh -i {ssh_tempfile} -o StrictHostKeyChecking=no'
run(['git', 'remote', 'set-url', '--push', 'origin', f'git@github.com:{github_repository}'], check=True)
msg1 = 'Automatic merge of Transifex translations'
msg2 = '[ci skip]'
run(['git', 'commit', '-m', msg1, '-m', msg2], check=True)
run(['git', 'stash'])
run(['git', 'push'], check=True)
else:
print('Nothing to commit')
if __name__ == '__main__':
main()