forked from atareao/daily-wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
executable file
·335 lines (297 loc) · 10.9 KB
/
tools.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# <one line to give the program's name and a brief idea of what it does.>
#
# Copyright (C) 2010 Lorenzo Carbonell
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
#
import os
import glob
import shlex
import subprocess
import shutil
def ejecuta(comando):
print('Ejecutando... %s' % comando)
args = shlex.split(comando)
p = subprocess.Popen(args, bufsize=10000, stdout=subprocess.PIPE)
valor = p.communicate()[0]
return valor
def list_src(main_dir, src_dir):
file_txt = os.path.join(main_dir, 'files.txt')
f = open(file_txt, 'w')
for file in glob.glob(os.path.join(src_dir, '**', '*.py'), recursive=True):
print(file)
f.write('%s\n' % file)
f.close()
return file_txt
def list_languages(languages_dir):
lans = []
file_txt = os.path.join(languages_dir, 'languages.txt')
if os.path.exists(file_txt) is True:
f = open(file_txt, 'r')
for linea in f.readlines():
lan = linea[:-1]
print(lan)
lans.append(lan)
f.close()
for file in glob.glob(os.path.join(languages_dir, '*.po')):
lan = os.path.splitext(os.path.basename(file))[0]
if lan not in lans:
lans.append(lan)
f = open(file_txt, 'w')
for lan in lans:
f.write('%s\n' % lan)
f.close()
return file_txt
def update_translations(languages_dir, template, app, version):
file_txt = os.path.join(languages_dir, 'languages.txt')
f = open(file_txt, 'r')
for file in f.readlines():
lan = file[:-1]
file = os.path.join(languages_dir, lan + '.po')
print('############################################################')
print(lan)
print('############################################################')
if os.path.exists(file):
command = 'msgmerge -U %s %s' % (file, template)
else:
command = 'msginit --output-file=%s --input=%s --locale=%s' % (
file, template, lan)
print(ejecuta(command))
edit_language_file(file, app, version)
f.close()
def edit_language_file(file, app, version):
fr = open(file, 'r')
file_out = file + '.new'
fs = open(file_out, 'w')
for line in fr.readlines():
if line.find('Project-Id-Version:') != -1:
line = '"Project-Id-Version: %s %s\\n"\n' % (app, version)
elif line.find('Content-Type:') != -1:
line = '"Content-Type: text/plain; charset=UTF-8\\n"\n'
fs.write(line)
fs.close()
fr.close()
shutil.move(file_out, file)
def remove_security_copies(languages_dir):
for file in glob.glob(os.path.join(languages_dir, '*.po~')):
os.remove(file)
def get_files_in_folder(folder):
files = []
for file in glob.glob(os.path.join(folder, '*')):
if file is not None and os.path.exists(file):
if os.path.isdir(file):
files.extend(get_files_in_folder(file))
else:
files.append(file)
return files
def remove_files(dir, ext):
for file in get_files_in_folder(dir):
if os.path.splitext(file)[1] == ext:
os.remove(file)
def remove_compiled_files(dir):
remove_files(dir, '.pyc')
def remove_languages_saved_files(dir):
remove_files(dir, '.po~')
def create_temporal_file(dir, main_dir):
temp_file = os.path.join(main_dir, 'temp_files.txt')
f = open(temp_file, 'w')
for file in get_files_in_folder(dir):
f.write('%s\n' % file)
f.close()
return temp_file
def create_rules(file, languages_dir, app):
if os.path.exists(file):
os.remove(file)
f = open(file, 'w')
f.write('#!/usr/bin/make -f\n')
f.write('# Sample debian/rules that uses debhelper.\n')
f.write('# This file is public domain software, originally written by\
Joey Hess.\n')
f.write('#\n')
f.write('# This version is for packages that are architecture\
independent.\n')
f.write('\n')
f.write('# Uncomment this to turn on verbose mode.\n')
f.write('#export DH_VERBOSE=1\n')
f.write('\n')
f.write('build: build-stamp\n')
f.write('build-stamp:\n')
f.write('\tdh_testdir\n')
f.write('\n')
f.write('\t# Add here commands to compile the package.\n')
f.write('\t#$(MAKE)\n')
f.write('\n')
f.write('\ttouch build-stamp\n')
f.write('\n')
f.write('clean:\n')
f.write('\tdh_testdir\n')
f.write('\tdh_testroot\n')
f.write('\trm -f build-stamp\n')
f.write('\n')
f.write('\t# Add here commands to clean up after the build process.\n')
f.write('\t#$(MAKE) clean\n')
f.write('\t#$(MAKE) distclean\n')
f.write('\n')
f.write('\tdh_clean\n')
f.write('\n')
f.write('install: build\n')
f.write('\tdh_testdir\n')
f.write('\tdh_testroot\n')
f.write('\tdh_prep\n')
f.write('\tdh_installdirs\n')
f.write('\tdh_install\n')
f.write('\t# Create languages directories\n')
file_txt = os.path.join(languages_dir, 'languages.txt')
fl = open(file_txt, 'r')
for lan in fl.readlines():
lan = lan[:-1]
f.write('\tmkdir -p ${CURDIR}/debian/%s/usr/share/\
locale-langpack/%s/LC_MESSAGES\n' % (app, lan))
fl.close()
f.write('\t# End create languages directories\n')
f.write('\t# Compile languages\n')
file_txt = os.path.join(languages_dir, 'languages.txt')
fl = open(file_txt, 'r')
for lan in fl.readlines():
lan = lan[:-1]
f.write('\tmsgfmt {0}/{1}.po -o {2}/debian/{3}/usr/share/\
locale-langpack/{1}/LC_MESSAGES/{3}.mo\n'.format(
os.path.basename(languages_dir), lan, '${CURDIR}', app))
fl.close()
f.write('\t# End comile languages\n')
####################################################################
f.write('\n')
f.write('\t# Add here commands to install the package into debian/\
<packagename>.\n')
f.write('\t#$(MAKE) prefix=`pwd`/debian/`dh_listpackages`/usr install\n')
f.write('\n')
f.write('# Build architecture-independent files here.\n')
f.write('binary-indep: build install\n')
f.write('\tdh_testdir\n')
f.write('\tdh_testroot\n')
f.write('\tdh_installchangelogs\n')
f.write('\tdh_installdocs\n')
f.write('\tdh_installexamples\n')
f.write('\t# added gconf and icons\n')
f.write('\tdh_gconf\n')
f.write('\tdh_icons\n')
f.write('#\tdh_installmenu\n')
f.write('#\tdh_installdebconf\n')
f.write('#\tdh_installlogrotate\n')
f.write('#\tdh_installemacsen\n')
f.write('#\tdh_installcatalogs\n')
f.write('#\tdh_installpam\n')
f.write('#\tdh_installmime\n')
f.write('#\tdh_installinit\n')
f.write('#\tdh_installcron\n')
f.write('#\tdh_installinfo\n')
f.write('#\tdh_installwm\n')
f.write('#\tdh_installudev\n')
f.write('#\tdh_lintian\n')
f.write('#\tdh_bugfiles\n')
f.write('#\tdh_undocumented\n')
f.write('\tdh_installman\n')
f.write('\tdh_link\n')
f.write('\tdh_compress\n')
f.write('\tdh_fixperms\n')
f.write('#\tdh_perl\n')
f.write('#\tdh_pysupport\n')
f.write('\tdh_installdeb\n')
f.write('\tdh_gencontrol\n')
f.write('\tdh_md5sums\n')
f.write('\tdh_builddeb\n')
f.write('\n')
f.write('# Build architecture-dependent files here.\n')
f.write('binary-arch: build install\n')
f.write('# We have nothing to do by default.\n')
f.write('\n')
f.write('binary: binary-indep binary-arch\n')
f.write('.PHONY: build clean binary-indep binary-arch binary install\n')
f.close()
os.chmod(file, 777)
def delete_it(file):
if os.path.exists(file):
if os.path.isdir(file):
shutil.rmtree(file)
else:
os.remove(file)
def babilon(main_dir, languages_dir, src_dir, author_email, template, app,
version):
print('############################################################')
print('Parent dir -> %s' % main_dir)
print('Languages dir -> %s' % languages_dir)
print('Source dir -> %s' % src_dir)
print('############################################################')
print('Updating template')
print('############################################################')
files_file = list_src(main_dir, src_dir)
print(files_file)
command = 'xgettext --msgid-bugs-address=%s --language=Python --keyword=\
_ --keyword=N_ --output=%s --files-from=%s' % (author_email,
template,
files_file)
print(ejecuta(command))
delete_it(files_file)
print('############################################################')
print('List languages')
print('############################################################')
#
list_languages(languages_dir)
#
print('############################################################')
print('Updating translations')
print('############################################################')
update_translations(languages_dir, template, app, version)
print('############################################################')
print('Removing security copies')
print('############################################################')
remove_security_copies(languages_dir)
if __name__ == '__main__':
lang_folder = 'po'
debian_folder = 'debian'
src_folder = 'src'
template_file = 'po.pot'
changelog_file = 'changelog'
author = 'Lorenzo Carbonell'
author_email = '[email protected]'
main_dir = os.getcwd()
debian_dir = os.path.join(main_dir, debian_folder)
languages_dir = os.path.join(main_dir, lang_folder)
src_dir = os.path.join(main_dir, src_folder)
template = os.path.join(languages_dir, template_file)
changelog = os.path.join(debian_dir, changelog_file)
if os.path.exists(changelog):
f = open(changelog, 'r')
line = f.readline()
print(line)
f.close()
pos = line.find('(')
posf = line.find('-', pos)
app = line[:pos].strip()
version = line[pos + 1: posf].strip()
appname = app.title()
babilon(main_dir, languages_dir, src_dir, author_email, template, app,
version)
rules_file = os.path.join(debian_dir, 'rules')
if os.path.exists(rules_file):
delete_it(rules_file)
create_rules(rules_file, languages_dir, app)
print(rules_file)
exit(0)