devtools/devctl.py
changeset 2458 4d114865098f
parent 2446 440cb4ea7e5c
parent 2396 8bfb99d7bbcc
child 2527 e60db6312aa0
equal deleted inserted replaced
2457:d0f31e119936 2458:4d114865098f
    17 from logilab.common import STD_BLACKLIST
    17 from logilab.common import STD_BLACKLIST
    18 from logilab.common.modutils import get_module_files
    18 from logilab.common.modutils import get_module_files
    19 from logilab.common.textutils import get_csv
    19 from logilab.common.textutils import get_csv
    20 from logilab.common.clcommands import register_commands
    20 from logilab.common.clcommands import register_commands
    21 
    21 
    22 from cubicweb import CW_SOFTWARE_ROOT as BASEDIR, BadCommandUsage
    22 from cubicweb import CW_SOFTWARE_ROOT as BASEDIR, BadCommandUsage, underline_title
    23 from cubicweb.__pkginfo__ import version as cubicwebversion
    23 from cubicweb.__pkginfo__ import version as cubicwebversion
    24 from cubicweb.toolsutils import Command, confirm, copy_skeleton
    24 from cubicweb.toolsutils import Command, confirm, copy_skeleton
    25 from cubicweb.web.webconfig import WebConfiguration
    25 from cubicweb.web.webconfig import WebConfiguration
    26 from cubicweb.server.serverconfig import ServerConfiguration
    26 from cubicweb.server.serverconfig import ServerConfiguration
    27 
    27 
   259         from logilab.common.fileutils import ensure_fs_mode
   259         from logilab.common.fileutils import ensure_fs_mode
   260         from logilab.common.shellutils import globfind, find, rm
   260         from logilab.common.shellutils import globfind, find, rm
   261         from cubicweb.common.i18n import extract_from_tal, execute
   261         from cubicweb.common.i18n import extract_from_tal, execute
   262         tempdir = tempdir.mkdtemp()
   262         tempdir = tempdir.mkdtemp()
   263         potfiles = [join(I18NDIR, 'entities.pot')]
   263         potfiles = [join(I18NDIR, 'entities.pot')]
   264         print '******** extract schema messages'
   264         print '-> extract schema messages.'
   265         schemapot = join(tempdir, 'schema.pot')
   265         schemapot = join(tempdir, 'schema.pot')
   266         potfiles.append(schemapot)
   266         potfiles.append(schemapot)
   267         # explicit close necessary else the file may not be yet flushed when
   267         # explicit close necessary else the file may not be yet flushed when
   268         # we'll using it below
   268         # we'll using it below
   269         schemapotstream = file(schemapot, 'w')
   269         schemapotstream = file(schemapot, 'w')
   270         generate_schema_pot(schemapotstream.write, cubedir=None)
   270         generate_schema_pot(schemapotstream.write, cubedir=None)
   271         schemapotstream.close()
   271         schemapotstream.close()
   272         print '******** extract TAL messages'
   272         print '-> extract TAL messages.'
   273         tali18nfile = join(tempdir, 'tali18n.py')
   273         tali18nfile = join(tempdir, 'tali18n.py')
   274         extract_from_tal(find(join(BASEDIR, 'web'), ('.py', '.pt')), tali18nfile)
   274         extract_from_tal(find(join(BASEDIR, 'web'), ('.py', '.pt')), tali18nfile)
   275         print '******** .pot files generation'
   275         print '-> generate .pot files.'
   276         for id, files, lang in [('pycubicweb', get_module_files(BASEDIR) + list(globfind(join(BASEDIR, 'misc', 'migration'), '*.py')), None),
   276         for id, files, lang in [('pycubicweb', get_module_files(BASEDIR) + list(globfind(join(BASEDIR, 'misc', 'migration'), '*.py')), None),
   277                                 ('schemadescr', globfind(join(BASEDIR, 'schemas'), '*.py'), None),
   277                                 ('schemadescr', globfind(join(BASEDIR, 'schemas'), '*.py'), None),
   278                                 ('yams', get_module_files(yams.__path__[0]), None),
   278                                 ('yams', get_module_files(yams.__path__[0]), None),
   279                                 ('tal', [tali18nfile], None),
   279                                 ('tal', [tali18nfile], None),
   280                                 ('js', globfind(join(BASEDIR, 'web'), 'cub*.js'), 'java'),
   280                                 ('js', globfind(join(BASEDIR, 'web'), 'cub*.js'), 'java'),
   285             potfile = join(tempdir, '%s.pot' % id)
   285             potfile = join(tempdir, '%s.pot' % id)
   286             execute(cmd % (potfile, ' '.join(files)))
   286             execute(cmd % (potfile, ' '.join(files)))
   287             if exists(potfile):
   287             if exists(potfile):
   288                 potfiles.append(potfile)
   288                 potfiles.append(potfile)
   289             else:
   289             else:
   290                 print 'WARNING: %s file not generated' % potfile
   290                 print '-> WARNING: %s file was not generated' % potfile
   291         print '******** merging .pot files'
   291         print '-> merging %i .pot files' % len(potfiles)
   292         cubicwebpot = join(tempdir, 'cubicweb.pot')
   292         cubicwebpot = join(tempdir, 'cubicweb.pot')
   293         execute('msgcat %s > %s' % (' '.join(potfiles), cubicwebpot))
   293         execute('msgcat %s > %s' % (' '.join(potfiles), cubicwebpot))
   294         print '******** merging main pot file with existing translations'
   294         print '-> merging main pot file with existing translations.'
   295         chdir(I18NDIR)
   295         chdir(I18NDIR)
   296         toedit = []
   296         toedit = []
   297         for lang in LANGS:
   297         for lang in LANGS:
   298             target = '%s.po' % lang
   298             target = '%s.po' % lang
   299             execute('msgmerge -N --sort-output  %s %s > %snew' % (target, cubicwebpot, target))
   299             execute('msgmerge -N --sort-output  %s %s > %snew' % (target, cubicwebpot, target))
   301             shutil.move('%snew' % target, target)
   301             shutil.move('%snew' % target, target)
   302             toedit.append(abspath(target))
   302             toedit.append(abspath(target))
   303         # cleanup
   303         # cleanup
   304         rm(tempdir)
   304         rm(tempdir)
   305         # instructions pour la suite
   305         # instructions pour la suite
   306         print '*' * 72
   306         print '-> regenerated CubicWeb\'s .po catalogs.'
   307         print 'you can now edit the following files:'
   307         print '\nYou can now edit the following files:'
   308         print '* ' + '\n* '.join(toedit)
   308         print '* ' + '\n* '.join(toedit)
   309         print
   309         print 'when you are done, run "cubicweb-ctl i18ncube yourcube".'
   310         print "then you'll have to update cubes catalogs using the i18ncube command"
       
   311 
   310 
   312 
   311 
   313 class UpdateTemplateCatalogCommand(Command):
   312 class UpdateTemplateCatalogCommand(Command):
   314     """Update i18n catalogs for cubes. If no cube is specified, update
   313     """Update i18n catalogs for cubes. If no cube is specified, update
   315     catalogs of all registered cubes.
   314     catalogs of all registered cubes.
   329 
   328 
   330 def update_cubes_catalogs(cubes):
   329 def update_cubes_catalogs(cubes):
   331     toedit = []
   330     toedit = []
   332     for cubedir in cubes:
   331     for cubedir in cubes:
   333         if not isdir(cubedir):
   332         if not isdir(cubedir):
   334             print 'not a directory', cubedir
   333             print '-> ignoring %s that is not a directory.' % cubedir
   335             continue
   334             continue
   336         try:
   335         try:
   337             toedit += update_cube_catalogs(cubedir)
   336             toedit += update_cube_catalogs(cubedir)
   338         except Exception:
   337         except Exception:
   339             import traceback
   338             import traceback
   340             traceback.print_exc()
   339             traceback.print_exc()
   341             print 'error while updating catalogs for', cubedir
   340             print '-> Error while updating catalogs for cube', cubedir
   342     # instructions pour la suite
   341     # instructions pour la suite
   343     print '*' * 72
   342     print '-> regenerated this cube\'s .po catalogs.'
   344     print 'you can now edit the following files:'
   343     print '\nYou can now edit the following files:'
   345     print '* ' + '\n* '.join(toedit)
   344     print '* ' + '\n* '.join(toedit)
   346 
   345     print 'when you are done, run "cubicweb-ctl i18ninstance yourinstance".'
   347 
   346 
   348 def update_cube_catalogs(cubedir):
   347 def update_cube_catalogs(cubedir):
   349     import shutil
   348     import shutil
   350     import tempfile
   349     import tempfile
   351     from logilab.common.fileutils import ensure_fs_mode
   350     from logilab.common.fileutils import ensure_fs_mode
   352     from logilab.common.shellutils import find, rm
   351     from logilab.common.shellutils import find, rm
   353     from cubicweb.common.i18n import extract_from_tal, execute
   352     from cubicweb.common.i18n import extract_from_tal, execute
   354     toedit = []
   353     toedit = []
   355     cube = basename(normpath(cubedir))
   354     cube = basename(normpath(cubedir))
   356     tempdir = tempfile.mkdtemp()
   355     tempdir = tempfile.mkdtemp()
   357     print '*' * 72
   356     print underline_title('Updating i18n catalogs for cube %s' % cube)
   358     print 'updating %s cube...' % cube
       
   359     chdir(cubedir)
   357     chdir(cubedir)
   360     potfiles = [join('i18n', scfile) for scfile in ('entities.pot',)
   358     potfiles = [join('i18n', scfile) for scfile in ('entities.pot',)
   361                 if exists(join('i18n', scfile))]
   359                 if exists(join('i18n', scfile))]
   362     print '******** extract schema messages'
   360     print '-> extract schema messages'
   363     schemapot = join(tempdir, 'schema.pot')
   361     schemapot = join(tempdir, 'schema.pot')
   364     potfiles.append(schemapot)
   362     potfiles.append(schemapot)
   365     # explicit close necessary else the file may not be yet flushed when
   363     # explicit close necessary else the file may not be yet flushed when
   366     # we'll using it below
   364     # we'll using it below
   367     schemapotstream = file(schemapot, 'w')
   365     schemapotstream = file(schemapot, 'w')
   368     generate_schema_pot(schemapotstream.write, cubedir)
   366     generate_schema_pot(schemapotstream.write, cubedir)
   369     schemapotstream.close()
   367     schemapotstream.close()
   370     print '******** extract TAL messages'
   368     print '-> extract TAL messages'
   371     tali18nfile = join(tempdir, 'tali18n.py')
   369     tali18nfile = join(tempdir, 'tali18n.py')
   372     extract_from_tal(find('.', ('.py', '.pt'), blacklist=STD_BLACKLIST+('test',)), tali18nfile)
   370     extract_from_tal(find('.', ('.py', '.pt'), blacklist=STD_BLACKLIST+('test',)), tali18nfile)
   373     print '******** extract Javascript messages'
   371     print '-> extract Javascript messages'
   374     jsfiles =  [jsfile for jsfile in find('.', '.js') if basename(jsfile).startswith('cub')]
   372     jsfiles =  [jsfile for jsfile in find('.', '.js') if basename(jsfile).startswith('cub')]
   375     if jsfiles:
   373     if jsfiles:
   376         tmppotfile = join(tempdir, 'js.pot')
   374         tmppotfile = join(tempdir, 'js.pot')
   377         execute('xgettext --no-location --omit-header -k_ -L java --from-code=utf-8 -o %s %s'
   375         execute('xgettext --no-location --omit-header -k_ -L java --from-code=utf-8 -o %s %s'
   378                 % (tmppotfile, ' '.join(jsfiles)))
   376                 % (tmppotfile, ' '.join(jsfiles)))
   379         # no pot file created if there are no string to translate
   377         # no pot file created if there are no string to translate
   380         if exists(tmppotfile):
   378         if exists(tmppotfile):
   381             potfiles.append(tmppotfile)
   379             potfiles.append(tmppotfile)
   382     print '******** create cube specific catalog'
   380     print '-> create cube-specific catalog'
   383     tmppotfile = join(tempdir, 'generated.pot')
   381     tmppotfile = join(tempdir, 'generated.pot')
   384     cubefiles = find('.', '.py', blacklist=STD_BLACKLIST+('test',))
   382     cubefiles = find('.', '.py', blacklist=STD_BLACKLIST+('test',))
   385     cubefiles.append(tali18nfile)
   383     cubefiles.append(tali18nfile)
   386     execute('xgettext --no-location --omit-header -k_ -o %s %s'
   384     execute('xgettext --no-location --omit-header -k_ -o %s %s'
   387             % (tmppotfile, ' '.join(cubefiles)))
   385             % (tmppotfile, ' '.join(cubefiles)))
   388     if exists(tmppotfile): # doesn't exists of no translation string found
   386     if exists(tmppotfile): # doesn't exists of no translation string found
   389         potfiles.append(tmppotfile)
   387         potfiles.append(tmppotfile)
   390     potfile = join(tempdir, 'cube.pot')
   388     potfile = join(tempdir, 'cube.pot')
   391     print '******** merging .pot files'
   389     print '-> merging %i .pot files:' % len(potfiles)
   392     execute('msgcat %s > %s' % (' '.join(potfiles), potfile))
   390     execute('msgcat %s > %s' % (' '.join(potfiles), potfile))
   393     print '******** merging main pot file with existing translations'
   391     print '-> merging main pot file with existing translations:'
   394     chdir('i18n')
   392     chdir('i18n')
   395     for lang in LANGS:
   393     for lang in LANGS:
   396         print '****', lang
   394         print '-> language', lang
   397         cubepo = '%s.po' % lang
   395         cubepo = '%s.po' % lang
   398         if not exists(cubepo):
   396         if not exists(cubepo):
   399             shutil.copy(potfile, cubepo)
   397             shutil.copy(potfile, cubepo)
   400         else:
   398         else:
   401             execute('msgmerge -N -s %s %s > %snew' % (cubepo, potfile, cubepo))
   399             execute('msgmerge -N -s %s %s > %snew' % (cubepo, potfile, cubepo))