forked from gramps-project/addons-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
1051 lines (834 loc) · 32.9 KB
/
setup.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2007-2009 Douglas S. Blank
# Copyright (C) 2012 Jerome Rapinat
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
setup.py for Gramps addons.
Examples:
python setup.py -i or --init AddonDirectory
Creates the initial directories for the addon.
python setup.py -i or --init AddonDirectory fr
Creates the initial empty AddonDirectory/po/fr-local.po file
for the addon.
python setup.py -u or --update AddonDirectory fr
Updates AddonDirectory/po/fr-local.po with the latest
translations.
python setup.py -b or --build AddonDirectory
Build ../download/AddonDirectory.addon.tgz
python setup.py -b or --build ALL
Build ../download/*.addon.tgz
python setup.py -c or --compile AddonDirectory
python setup.py -c or --compile ALL
Compiles AddonDirectory/po/*-local.po and puts the resulting
.mo file in AddonDirectory/locale/*/LC_MESSAGES/addon.mo
python3 setup.py -l or --listing AddonDirectory
python3 setup.py -l or --listing all
python setup.py -c or --clean AddonDirectory
python setup.py -c or --clean ALL
"""
import shutil
import glob
import os
import sys
from argparse import ArgumentParser
ADDONS = sorted([name for name in os.listdir('.')
if os.path.isdir(name) and not name.startswith('.')])
ALL_ADDONS = ADDONS.append('ALL')
LINGUAS = ( # translation template
'en',
'bg',
'ca',
'cs',
'da',
'de',
'es',
'en_GB',
'fi',
'fr',
'he',
'hr',
'hu',
'it',
'ja',
'lt',
'mk',
'nb',
'nl',
'nn',
'pl',
'pt_BR',
'pt_PT',
'ru',
'sk',
'sl',
'sq',
'sv',
'uk',
'vi',
'zh_CN',
)
ALL_LINGUAS = ADDONS.append('all')
if sys.platform == 'win32':
# GetText Win 32 obtained from http://gnuwin32.sourceforge.net/packages/gettext.htm
# ....\gettext\bin\msgmerge.exe needs to be on the path
msginitCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msginit.exe')
msgmergeCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msgmerge.exe')
msgfmtCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msgfmt.exe')
msgcatCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msgcat.exe')
msggrepCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msggrep.exe')
msgcmpCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msgcmp.exe')
msgattribCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'msgattrib.exe')
xgettextCmd = os.path.join('C:', 'Program Files(x86)', 'gettext',
'bin', 'xgettext.exe')
pythonCmd = os.path.join(sys.prefix, 'bin', 'python.exe')
# GNU tools
# see http://gnuwin32.sourceforge.net/packages.html
sedCmd = os.path.join('C:', 'Program Files(x86)', 'sed.exe') # sed
mkdirCmd = os.path.join('C:', 'Program Files(x86)', 'mkdir.exe') # CoreUtils
rmCmd = os.path.join('C:', 'Program Files(x86)', 'rm.exe') # CoreUtils
tarCmd = os.path.join('C:', 'Program Files(x86)', 'tar.exe') # tar
elif sys.platform in ['linux2', 'darwin', 'cygwin']:
msginitCmd = 'msginit'
msgmergeCmd = 'msgmerge'
msgfmtCmd = 'msgfmt'
msgcatCmd = 'msgcat'
msggrepCmd = 'msggrep'
msgcmpCmd = 'msgcmp'
msgattribCmd = 'msgattrib'
xgettextCmd = 'xgettext'
pythonCmd = os.path.join(sys.prefix, 'bin', 'python')
sedCmd = 'sed'
mkdirCmd = 'mkdir'
rmCmd = 'rm'
tarCmd = 'tar'
else:
print("ERROR: unknown system, don't know commands")
sys.exit(0)
GNU = [sedCmd, mkdirCmd, rmCmd, tarCmd]
def tests():
"""
Testing installed programs.
We made tests (-t flag) by displaying versions of tools if properly
installed. Cannot run all commands without 'gettext' and 'python'.
"""
try:
print("""
====='msginit'=(create your translation)===============
""")
os.system('%(program)s -V' % {'program': msginitCmd})
except:
raise ValueError('Please, install %(program)s for creating your translation'
% {'program': msginitCmd})
try:
print("""
====='msgmerge'=(merge your translation)===============
""")
os.system('%(program)s -V' % {'program': msgmergeCmd})
except:
raise ValueError('Please, install %(program)s for updating your translation'
% {'program': msgmergeCmd})
try:
print("""
=='msgfmt'=(format your translation for installation)==
""")
os.system('%(program)s -V' % {'program': msgfmtCmd})
except:
raise ValueError('Please, install %(program)s for checking your translation'
% {'program': msgfmtCmd})
try:
print("""
==='msgcat'=(concate translations)=====================
""")
os.system('%(program)s -V' % {'program': msgcatCmd})
except:
raise ValueError('Please, install %(program)s for concating translations'
% {'program': msgcatCmd})
try:
print("""
===='msggrep'==(extract messages from catalog)=========
""")
os.system('%(program)s -V' % {'program': msggrepCmd})
except:
raise ValueError('Please, install %(program)s for extracting messages'
% {'program': msggrepCmd})
try:
print("""
===='msgcmp'==(compare two gettext files)===============
""")
os.system('%(program)s -V' % {'program': msgcmpCmd})
except:
raise ValueError('Please, install %(program)s for comparing gettext files'
% {'program': msgcmpCmd})
try:
print("""
===='msgattrib'==(list groups of messages)=============
""")
os.system('%(program)s -V' % {'program': msgattribCmd})
except:
raise ValueError('Please, install %(program)s for listing groups of messages'
% {'program': msgattribCmd})
try:
print("""
===='xgettext' =(generate a new template)==============
""")
os.system('%(program)s -V' % {'program': xgettextCmd})
except:
raise ValueError('Please, install %(program)s for generating a new template'
% {'program': xgettextCmd})
try:
print("""
=================='python'=============================
""")
os.system('%(program)s -V' % {'program': pythonCmd})
except:
raise ValueError('Please, install python')
for program in GNU:
try:
print("""
=================='%s'=============================
"""
% program)
os.system('%s --version' % program)
except:
raise ValueError('Please, install or set path for GNU tool: %s'
% program)
def main():
"""
The utility for handling addon.
"""
parser = \
ArgumentParser(description='This specific script build addon',
add_help=True, version='0.1.4')
#parser.add_argument('addon', choices=ADDONS)
parser.add_argument('lang', nargs='?', const=LINGUAS,
default='en')
# parser.add_argument("-t", "--test",
# action="store_true", dest="test", default=True,
# help="test if programs are properly installed")
translating = parser.add_argument_group('Translations Options',
'Everything around translations for addon.')
building = parser.add_argument_group('Build Options',
'Everything around package.')
translating.add_argument(
'-i',
'--init',
choices=ALL_ADDONS,
dest='init',
default=False,
help='create the environment',
)
translating.add_argument(
'-u',
'--update',
choices=ALL_ADDONS,
dest='update',
default=False,
help='update the translation',
)
building.add_argument(
'-c',
'--compile',
choices=ALL_ADDONS,
dest='compilation',
default=False,
help='compile translation files for generating package',
)
building.add_argument(
'-b',
'--build',
choices=ALL_ADDONS,
dest='build',
default=False,
help='build package',
)
building.add_argument(
'-l',
'--listing',
choices=ALL_LINGUAS,
dest='listing',
default=False,
help='list packages',
)
building.add_argument(
'-r',
'--clean',
choices=ALL_ADDONS,
dest='clean',
default=False,
help='remove files generated by building process',
)
if len(sys.argv) == 2:
m = "Run 'setup.py --help en' or 'setup.py -h en'\n"
parser.exit(message = m)
if not 2 < len(sys.argv) < 5:
m1 = 'Wrong number of arguments: %s \n' % len(sys.argv)
parser.exit(message = m1)
else:
try:
args = parser.parse_args()
except AssertionError:
m = 'Wrong argument: %s \n' % sys.argv
l = ' lang available: %s \n' % str(ALL_LINGUAS)
a = ' addon available: %s \n' % ALL_ADDONS
parser.exit(message = m + l + a)
# if args.test:
# tests()
if args.init:
print(parser.parse_args())
if args.init not in ADDONS:
m1 = 'Wrong argument: %s \nTry "setup.py -i {addon_name} {lang}"!\n' % sys.argv
parser.exit(message = m1)
else:
if args.lang == 'en':
pass
else:
init(args.init, args.lang)
if args.update:
print(parser.parse_args())
if args.update not in ADDONS:
m1 = 'Wrong argument: %s \nTry "setup.py -u {addon_name} {lang}"!\n' % sys.argv
parser.exit(message = m1)
else:
update(args.update, args.lang)
if args.compilation:
print(parser.parse_args())
if args.compilation == "ALL":
compilation_all(args.compilation)
elif args.compilation == "all":
m1 = 'Wrong argument: %s \nTry "setup.py -c ALL"!\n' % sys.argv
parser.exit(message = m1)
else:
compilation(args.compilation)
if args.build:
print(parser.parse_args())
if args.build == "ALL":
build_all(args.build)
elif args.build == "all":
m1 = 'Wrong argument: %s \nTry "setup.py -b ALL"!\n' % sys.argv
parser.exit(message = m1)
else:
build(args.build)
if args.listing:
print(parser.parse_args())
if args.listing != False:
if args.listing == "all":
listing_all(args.lang)
elif args.listing == "ALL":
m1 = 'Wrong argument: %s \nTry "setup.py -l all"!\n' % sys.argv
parser.exit(message = m1)
else:
sys.path.insert(3, args.listing)
is_listing(sys.argv[2])
else:
m2 = 'Wrong argument: %s \n' % sys.argv
parser.exit(message = m2)
if args.clean:
print(parser.parse_args())
if args.clean == "ALL":
clean_all(args.clean)
elif args.clean == "all":
m1 = 'Wrong argument: %s \nTry "setup.py -r ALL"!\n' % sys.argv
parser.exit(message = m1)
else:
clean(args.clean)
def versioning(addon):
"""
Update gpr.py version
"""
gprs = glob.glob('''%(addon)s/*gpr.py''' % {'addon': addon})
if len(gprs) > 0:
for gpr in gprs:
f = open(gpr, 'r')
lines = [file.strip() for file in f]
f.close()
upf = open(gpr, 'w')
for line in lines:
if line.lstrip().startswith('version') and '=' in line:
print('orig %s' % line.rstrip())
(line, stuff) = line.rsplit(',', 1)
line = line.rstrip()
pos = line.index('version')
indent = line[0:pos]
(var, gtv) = line[pos:].split('=', 1)
lyst = version(gtv.strip()[1:-1])
lyst[2] += 1
newv = '.'.join(map(str, lyst))
newline = "%sversion = '%s'," % (indent, newv)
print('new %s' % newline.rstrip())
upf.write('%s\n' % newline)
else:
upf.write('%s\n' % line)
upf.close()
def myint(s):
"""
Protected version of int()
"""
try:
v = int(s)
except:
v = s
return v
def version(sversion):
"""
Return the tuple version of a string version.
"""
return [myint(x or '0') for x in (sversion + '..').split('.')][0:3]
def init(ADDON, LANG):
"""
Creates the initial empty po/x-local.po file and generates the
template.pot for the addon.
"""
template(ADDON)
os.system('%(mkdir)s -pv "%(addon)s/po"' % {'mkdir': mkdirCmd,
'addon': ADDON})
if os.path.isfile('%(addon)s/po/%(lang)s-local.po'
% {'addon': ADDON, 'lang': LANG}):
print('"%(addon)s/po/%(lang)s-local.po" already exists!'
% {'addon': ADDON, 'lang': LANG})
else:
os.system('%(msginit)s --locale=%(lang)s --input="%(addon)s/po/template.pot" --output="%(addon)s/po/%(lang)s-local.po"'
% {'msginit': msginitCmd, 'addon': ADDON,
'lang': LANG})
print('You can now edit "%(addon)s/po/%(lang)s-local.po"!'
% {'addon': ADDON, 'lang': LANG})
def template(ADDON):
"""
Generates the template.pot for the addon.
"""
os.system('%(xgettext)s --language=Python --keyword=_ --keyword=N_ --from-code=UTF-8 -o "%(addon)s/po/template.pot" %(addon)s/*.py'
% {'xgettext': xgettextCmd, 'addon': ADDON})
if os.path.isfile('%(addon)s/placecompletion.glade'
% {'addon': ADDON}):
os.system('%(xgettext)s --add-comments -j -L Glade --from-code=UTF-8 -o "%(addon)s/po/template.pot" %(addon)s/*.glade'
% {'xgettext': xgettextCmd, 'addon': ADDON})
if os.path.isfile('%s/census.xml' % ADDON):
xml(ADDON)
os.system('%(xgettext)s --keyword=N_ --add-comments -j --from-code=UTF-8 -o "%(addon)s/po/template.pot" %(addon)s/*.xml.h'
% {'xgettext': xgettextCmd, 'addon': ADDON})
os.system('%(sed)s -i "s/charset=CHARSET/charset=UTF-8/" "%(addon)s/po/template.pot"'
% {'sed': sedCmd, 'addon': ADDON})
def xml(ADDON):
"""
Experimental alternative to 'intltool-extract' for 'census.xml'.
"""
from xml.etree import ElementTree
tree = ElementTree.parse('%s/census.xml' % ADDON)
root = tree.getroot()
catalog = open('%(addon)s/%(addon)s.xml.h' % {'addon': ADDON}, 'w')
for key in root.iter('_attribute'):
catalog.write('char *s = N_("%s");\n' % key.text)
catalog.close()
root.clear()
def update(ADDON, LANG):
"""
Updates po/x-local.po with the latest translations.
"""
template(ADDON)
os.system('%(mkdir)s -pv "%(addon)s/po"' % {'mkdir': mkdirCmd,
'addon': ADDON})
# create a temp header file (time log)
temp(ADDON, LANG)
# create the locale-local.po file
init(ADDON, LANG)
# create a temp header file (time log)
temp(ADDON, LANG)
# merge data from previous translation to the temp one
print('Merge "%(addon)s/po/%(lang)s.po" with "%(addon)s/po/%(lang)s-local.po":'
% {'addon': ADDON, 'lang': LANG})
os.system('%(msgmerge)s %(addon)s/po/%(lang)s-local.po %(addon)s/po/%(lang)s.po -o %(addon)s/po/%(lang)s.po --no-location -v'
% {'msgmerge': msgmergeCmd, 'addon': ADDON,
'lang': LANG})
memory(ADDON, LANG)
# like template (msgid) with last message strings (msgstr)
print('Merge "%(addon)s/po/%(lang)s.po" with "po/template.pot":'
% {'addon': ADDON, 'lang': LANG})
os.system('%(msgmerge)s -U %(addon)s/po/%(lang)s.po %(addon)s/po/template.pot -v'
% {'msgmerge': msgmergeCmd, 'addon': ADDON,
'lang': LANG})
# only used messages (need) and merge back
print('Move content to "po/%s-local.po".' % LANG)
os.system('%(msgattrib)s --no-obsolete %(addon)s/po/%(lang)s.po -o %(addon)s/po/%(lang)s-local.po'
% {'msgattrib': msgattribCmd, 'addon': ADDON,
'lang': LANG})
# remove temp locale.po file
os.system('%(rm)s -rf -v %(addon)s/po/%(lang)s.po' % {'rm': rmCmd,
'addon': ADDON, 'lang': LANG})
print('You can now edit "%(addon)s/po/%(lang)s-local.po"!'
% {'addon': ADDON, 'lang': LANG})
def temp(addon, lang):
"""
Generate a temp file for header (time log) and Translation Memory
"""
os.system('%(msginit)s --locale=%(lang)s --input="%(addon)s/po/template.pot" --output="%(addon)s/po/%(lang)s.po" --no-translator'
% {'msginit': msginitCmd, 'addon': addon, 'lang': lang})
def memory(addon, lang):
"""
Translation memory for Gramps (own dictionary: msgid/msgstr)
"""
if 'GRAMPSPATH' in os.environ:
GRAMPSPATH = os.environ['GRAMPSPATH']
else:
GRAMPSPATH = '../../../..'
if not os.path.isdir(GRAMPSPATH + '/po'):
raise ValueError("Where is GRAMPSPATH/po: '%s/po'? Use 'GRAMPSPATH=path python setup.py ...'"
% GRAMPSPATH)
# Get all of the addon strings out of the catalog
os.system('%(msggrep)s --location=*/* %(addon)s/po/template.pot --output-file=%(addon)s/po/%(lang)s-temp.po'
% {'msggrep': msggrepCmd, 'addon': addon, 'lang': lang})
# start with Gramps main PO file
locale_po_files = '%(GRAMPSPATH)s/po/%(lang)s.po' \
% {'GRAMPSPATH': GRAMPSPATH, 'addon': addon, 'lang': lang}
# concat global dict as temp file
if os.path.isfile(locale_po_files):
print('Concat temp data: "%(addon)s/po/%(lang)s.po" with "%(global)s".'
% {'global': locale_po_files, 'addon': addon,
'lang': lang})
os.system('%(msgcat)s --use-first %(addon)s/po/%(lang)s.po %(global)s -o %(addon)s/po/%(lang)s.po --no-location'
% {
'msgcat': msgcatCmd,
'global': locale_po_files,
'addon': addon,
'lang': lang,
})
os.system('%(msgcmp)s -m --use-fuzzy --use-untranslated %(addon)s/po/%(lang)s.po %(global)s'
% {
'msgcmp': msgcmpCmd,
'global': locale_po_files,
'addon': addon,
'lang': lang,
})
if os.path.isfile('%(addon)s/po/%(lang)s-temp.po'
% {'addon': addon, 'lang': lang}):
print('Concat temp data: "%(addon)s/po/%(lang)s.po" with "%(addon)s/po/%(lang)s-temp.po".'
% {'addon': addon, 'lang': lang})
os.system('%(msgcat)s --use-first %(addon)s/po/%(lang)s.po %(addon)s/po/%(lang)s-temp.po -o %(addon)s/po/%(lang)s.po --no-location'
% {'msgcat': msgcatCmd, 'addon': addon,
'lang': lang})
print('Remove temp "%(addon)s/po/%(lang)s-temp.po".'
% {'addon': addon, 'lang': lang})
os.system('%(rm)s -rf -v %(addon)s/po/%(lang)s-temp.po'
% {'rm': rmCmd, 'addon': addon, 'lang': lang})
def compilation(addon):
"""
Compile translations
"""
non_empty = glob.glob(os.path.join(addon, 'po', '*-local.po'))
if len(non_empty) > 0:
os.system('%(mkdir)s -pv "%(addon)s/locale"' % {'mkdir': mkdirCmd,
'addon': addon})
for po in non_empty:
f = os.path.basename(po[:-3])
mo = os.path.join(addon, 'locale', f[:-6], 'LC_MESSAGES',
'addon.mo')
directory = os.path.dirname(mo)
if not os.path.exists(directory):
os.makedirs(directory)
os.system('%(msgfmtCmd)s %(addon)s/po/%(lang)s.po -o %(build)s'
% {
'msgfmtCmd': msgfmtCmd,
'addon': addon,
'lang': f,
'build': mo,
})
def compilation_all(ADDON):
"""
Compile all translations
"""
for addon in ADDONS:
if addon == 'ALL':
continue
compilation(addon)
def build(addon):
"""
Build ../download/{ADDON}.addon.tgz
"""
compilation(addon)
versioning(addon)
files = []
files += glob.glob('''%s/*.py''' % addon)
files += glob.glob('''%s/locale/*/LC_MESSAGES/*.mo''' % addon)
files += glob.glob('''%s/*.glade''' % addon)
files += glob.glob('''%s/*.xml''' % addon)
files_str = ' '.join(files)
os.system('%(mkdir)s -pv ../download/%(addon)s '
% {'mkdir': mkdirCmd, 'addon': addon})
os.system('%(tar)s cfzv "../download/%(addon)s.addon.tgz" %(files_list)s'
% {'tar': tarCmd, 'files_list': files_str,
'addon': addon})
os.system('rmdir ../download/%(addon)s '
% {'addon': addon})
def build_all(ADDON):
"""
Build all ../download/*.addon.tgz
"""
for addon in ADDONS:
if addon == 'ALL':
continue
build(addon)
def is_listing(LANG):
"""
Listing files ../listing/{lang}.fr
"""
if 'GRAMPSPATH' in os.environ:
GRAMPSPATH = os.environ['GRAMPSPATH']
else:
GRAMPSPATH = '../../../..'
try:
sys.path.insert(0, GRAMPSPATH)
os.environ['GRAMPS_RESOURCES'] = os.path.abspath(GRAMPSPATH)
from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gen.plug import make_environment, PTYPE_STR
except ImportError:
raise ValueError("Where is 'GRAMPSPATH' or 'GRAMPS_RESOURCES'?")
def register(ptype, **kwargs):
global plugins
kwargs['ptype'] = PTYPE_STR[ptype] # related to gramps translations
plugins.append(kwargs)
cmd_arg = LANG
# Make the locale for for any local languages for Addon:
for addon in ADDONS:
for po in glob.glob('%(addon)s/po/*-local.po' % {'addon': addon}):
# Compile
locale = os.path.basename(po[:-9])
os.system('mkdir -p "%(addon)s/locale/%(locale)s/LC_MESSAGES/"'
% {'addon': addon, 'locale': locale})
os.system('msgfmt %(po)s -o "%(addon)s/locale/%(locale)s/LC_MESSAGES/addon.mo"'
% {'po': po, 'addon': addon, 'locale': locale})
# Get all languages from all addons:
languages = set(['en'])
for addon in [file for file in glob.glob('*')
if os.path.isdir(file)]:
for po in glob.glob('%(addon)s/po/*-local.po' % {'addon': addon}):
length = len(po)
locale = po[length - 11:length - 9]
(locale_path, locale) = po.rsplit('/', 1)
languages.add(locale[:-9])
# next, create/edit a file for all languages listing plugins
for lang in languages:
print("----Building listing for '%s'..." % lang)
listings = []
for addon in ADDONS:
for gpr in glob.glob('%(addon)s/*.gpr.py' % {'addon': addon}):
print(gpr)
# Make fallback language English (rather than current LANG)
local_gettext = glocale.get_addon_translator(gpr,
languages=[lang, 'en.UTF-8']).gettext
plugins = []
with open(gpr.encode('utf-8', errors='backslashreplace'
)) as f:
code = compile(f.read(), gpr.encode('utf-8',
errors='backslashreplace'), 'exec')
#exec(code, make_environment(_=local_gettext),
#{"register": register})
for p in plugins:
tgz_file = '%s.addon.tgz' % gpr.split('/', 1)[0]
tgz_exists = os.path.isfile('../download/'
+ tgz_file)
if p.get('include_in_listing', True) and tgz_exists:
plugin = {
'n': repr(p['name']),
'i': repr(p['id']),
't': repr(p['ptype']),
'd': repr(p['description']),
'v': repr(p['version']),
'g': repr(p['gramps_target_version']),
'z': repr(tgz_file),
}
listings.append(plugin)
else:
print(" ignoring '%s'" % p['name'])
def listing(LANG):
"""
Listing files ../listing/{lang}.fr
"""
if 'GRAMPSPATH' in os.environ:
GRAMPSPATH = os.environ['GRAMPSPATH']
else:
GRAMPSPATH = '../../../..'
try:
sys.path.insert(0, GRAMPSPATH)
os.environ['GRAMPS_RESOURCES'] = os.path.abspath(GRAMPSPATH)
from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gen.plug import make_environment, PTYPE_STR
except ImportError:
raise ValueError("Where is 'GRAMPSPATH' or 'GRAMPS_RESOURCES'?")
LOCALE = glocale.get_language_list()
compilation_all('ALL')
listings = []
need = False
# change the method
fp = open('../listings/addons-%s.txt' % LANG, 'w')
for addon in sorted(ADDONS):
tgz_file = '%s.addon.tgz' % addon
tgz_exists = os.path.isfile('../download/' + tgz_file)
gprs = glob.glob('%(addon)s/*gpr.py' % {'addon': addon})
for gpr in gprs:
gpr_file = gpr
print(gpr_file, gprs)
gpr_exists = os.path.isfile(gpr_file)
mo_file = "%s/locale/%s/LC_MESSAGES/addon.mo" % (addon, LANG)
mo_exists = os.path.isfile(mo_file)
if tgz_exists and gpr_exists:
gpr = open(gpr_file.encode('utf-8',
errors='backslashreplace'))
plug = dict([file.strip(), None] for file in gpr
if file.strip())
name = ident = ptype = description = version = target = ''
if mo_exists:
LANGUAGE = LANG +".UTF-8"
else:
LANGUAGE = os.environ['LANGUAGE']
# print(plug)
for p in plug:
# print(repr(p))
if repr(p).startswith("'register("):
ptype = p.replace("register(", "")
ptype = ptype.replace(",", "")
# incomplete dirty hack!
print(glocale._get_translation(), LANG+".UTF-8")
if LANG != LOCALE[0]:
# mixup between LOCALE[0] and 'en' (avoid corruption)
# need 'en.UTF-8' !
local_gettext = glocale.get_addon_translator(gpr_file, languages=[LANGUAGE]).ugettext
#return
else:
local_gettext = glocale.get_addon_translator(gpr_file, languages=[LANG, "en"]).ugettext
ptype = make_environment(_ = local_gettext)[ptype]
# need to match translations build by Gramps program
try:
ptype = PTYPE_STR[ptype]
except:
# fallback and corruption with LOCALE[0]
print(' wrong PTYPE: %s' % ptype)
print(local_gettext('Tool')) # always corrupted by the locale
print("LANGUAGE='%(language)s', LANG='%(lang)s'" % {'language': os.environ['LANGUAGE'], 'lang': os.environ['LANG']})
return
if not (repr(p).startswith("'include_in_listing = False,"
) or repr(p).startswith("'status = UNSTABLE,")):
need = True
else:
print("Ignoring: '%s'" % addon)
if repr(p).startswith("'id") or repr(p).startswith('"id'
):
ident = p.replace('id', '')
ident = ident.replace('=', '')
ident = ident.replace(',', '')
ident = ident.strip()
#ident = repr(ident)
if repr(p).startswith("'name") \
or repr(p).startswith('"name'):
name = p.replace('name', '')
name = name.replace('=', '')
name = name.replace(',', '')
name = name.strip()
name = name.replace('_(', '')
name = name.replace(')', '')
name = name.replace('"', '')
name = glocale._get_translation().ugettext(name)
try:
if name == local_gettext(name):
print(addon, name, local_gettext(name))
name = repr(local_gettext(name))
except:
print('Cannot use local_gettext on', repr(p))
# ugly workaround for name_accell (Export GEDCOM Extensions)
name = name.replace('_accell ', '')
name = name.replace('(GED2', '(GED2)')
if repr(p).startswith("'description"):
description = p.replace('description', '')
description = description.replace('=', '')
description = description.replace(',', '')
description = description.strip()
description = description.replace('_(', '')
description = description.replace(')', '')
description = description.replace('"', '')
description = glocale._get_translation().ugettext(description)
try:
if description == local_gettext(description):
print(addon, description, local_gettext(description))
description = repr(local_gettext(description))
except:
print('Cannot use local_gettext on', repr(p))
if repr(p).startswith('"version'):
version = p.replace('version', '')
version = version.replace('=', '')
version = version.replace(',', '')
version = version.replace("'", "")
version = version.replace('"', '')
version = version.strip()
version = repr(version)
# workaround #7395~c38994
if description == '':
description = "''"
print(description, addon)
if need:
plugin = {
'n': name,
'i': ident,
't': repr(ptype),
'd': description,
'v': version,
'g': "'4.2'",
'z': repr(tgz_file),
}
#if name or ident or version or target == "":
#print(plugin)
fp.write('{"t":%(t)s,"i":%(i)s,"n":%(n)s,"v":%(v)s,"g":%(g)s,"d":%(d)s,"z":%(z)s}\n'